/public/lib/stream/app.js

https://github.com/dcinzona/streamie · JavaScript · 113 lines · 86 code · 8 blank · 19 comment · 16 complexity · 9e22e87d255a83ed1debf30d212df524 MD5 · raw file

  1. /*
  2. * Main entry point for our app
  3. * "start" method gets called by require.js when the initial dependencies are loaded.
  4. * We always have require.js, jQuery and underscore.js everwhere
  5. */
  6. // we really do not want to break if somebody leaves a console.log in the code
  7. if(typeof console == "undefined") {
  8. var console = {
  9. log: function () {},
  10. error: function () {}
  11. }
  12. }
  13. require.def("stream/app",
  14. ["stream/tweetstream", "stream/tweet", "stream/streamplugins", "stream/initplugins", "stream/client", "stream/status", "/ext/underscore.js", "/ext/modernizr-1.5.js", "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"],
  15. function(tweetstream, tweetModule, streamPlugin, initPlugin, client, status) {
  16. // Stream plugins are called in the order defined here for each incoming tweet.
  17. // Important: Stream plugins have to call this() to continue the execution!
  18. // They receive two paramters. The tweet which is an instance of stream/tweet.Tweet
  19. // and the stream which is an instance of stream/tweetstream.Stream.
  20. var streamPlugins = [
  21. streamPlugin.handleRetweet,
  22. streamPlugin.tweetsOnly,
  23. streamPlugin.everSeen,
  24. streamPlugin.avoidDuplicates,
  25. streamPlugin.conversations,
  26. streamPlugin.mentions,
  27. streamPlugin.template,
  28. streamPlugin.htmlEncode,
  29. streamPlugin.formatTweetText,
  30. streamPlugin.renderTemplate,
  31. streamPlugin.prepend,
  32. streamPlugin.keepScrollState,
  33. streamPlugin.age,
  34. streamPlugin.newTweetEvent
  35. ];
  36. // initPlugins are loaded when the page is loaded and the backend web socket connection has been established
  37. // and the stream connection to Twitter was established without authorization problems
  38. var initPlugins = [
  39. initPlugin.prefillTimeline,
  40. initPlugin.hashState,
  41. initPlugin.navigation,
  42. initPlugin.signalNewTweets,
  43. initPlugin.personalizeForCurrentUser,
  44. initPlugin.notifyAfterPause,
  45. initPlugin.keyboardShortCuts,
  46. initPlugin.favicon,
  47. status.observe,
  48. status.replyForm,
  49. status.location,
  50. status.quote,
  51. status.retweet,
  52. status.favorite,
  53. status.conversation,
  54. status.showJSON
  55. ];
  56. var stream = new tweetstream.Stream();
  57. window.stream = stream; // make this globally accessible so we can see what is in it.
  58. var initial = true;
  59. return {
  60. start: function () {
  61. $(function () {
  62. stream.addPlugins(streamPlugins);
  63. location.hash = ""; // start fresh, we dont maintain any important state
  64. // connect to the backend system
  65. var connect = function(data) {
  66. data = JSON.parse(data); // data must always be JSON
  67. if(data.error) {
  68. //console.log("Error: "+data.error)
  69. if(data.error == "no_auth") {
  70. if(confirm("Streamie.org is a Twitter client. We'll send you to Twitter to ask for access to your account now. OK?")) {
  71. location.href = "/access" // redirect to do oauth
  72. } else {
  73. // No where else to go. Patches welcome;
  74. location.href = "http://www.nonblocking.io/2010/08/future-is-here-i-just-forked-running.html";
  75. }
  76. }
  77. }
  78. else if(data.action == "auth_ok") {
  79. // we are now connected and authorization was fine
  80. stream.user = data.info; // store the info of the logged user
  81. if(initial) {
  82. initial = false;
  83. // run initPlugins
  84. initPlugins.forEach(function (plugin) {
  85. plugin.func.call(function () {}, stream, plugin);
  86. })
  87. }
  88. }
  89. else if(data.tweet) {
  90. // We actually received a tweet. Let the stream process it
  91. stream.process(tweetModule.make(JSON.parse(data.tweet)));
  92. }
  93. else {
  94. // dunno what to do here
  95. if(data != "pong") {
  96. console.log(data);
  97. }
  98. }
  99. };
  100. var socket = client.connect(connect);
  101. })
  102. }
  103. }
  104. }
  105. );