client/js/tweetsource.js
changeset 10 dd7d86fbee70
parent 3 82b4715202d1
child 13 e42d9f11f6e2
--- a/client/js/tweetsource.js	Mon Feb 20 14:36:59 2012 +0100
+++ b/client/js/tweetsource.js	Mon Feb 20 17:44:20 2012 +0100
@@ -4,6 +4,10 @@
  * 
  * */
 
+function regexpFromText(_text) {
+    return new RegExp(_text.replace(/\W/gim,'\\$1'),'gim');
+}
+
 Btv_TweetArray = function() {
     this.tweets = [];
     this.idIndex = [];
@@ -14,7 +18,11 @@
     this.idIndex.push(_tweet.id_str);
 }
 
-Btv_TweetArray.prototype.addTweet = function(_tweet, _inhibitCallback) {
+Btv_TweetArray.prototype.setOnAdd = function(_callback) {
+    this.onAdd = _callback;
+}
+
+Btv_TweetArray.prototype.addTweet = function(_tweet) {
     if (this.idIndex.indexOf(_tweet.id_str) != -1) {
         return;
     }
@@ -27,6 +35,9 @@
     }
     this.tweets.splice(_pos,0,_tweet);
     this.idIndex.splice(_pos,0,_tweet.id_str);
+    if (typeof this.onAdd == "function") {
+        this.onAdd(_tweet);
+    }
 }
 
 Btv_TweetArray.prototype.addMultipleTweets = function(_multiTweets) {
@@ -40,8 +51,8 @@
     return this.tweets.length;
 }
 
-Btv_TweetArray.prototype.getTweet = function(_i) {
-    return this.tweets(_i);
+Btv_TweetArray.prototype.tweetAtPos = function(_i) {
+    return this.tweets[_i];
 }
 
 Btv_TweetArray.prototype.slice = function(_start, _end) {
@@ -80,7 +91,7 @@
 
 Btv_TweetArray.prototype.search = function(_filter) {
     var _filtered = new Btv_TweetArray(),
-        _reFilter = new RegExp(_filter.replace(/\W/gim,'\\$1'),'gim');
+        _reFilter = regexpFromText(_filter);
     this.each(function(_tweet) {
         var _mention = '@' + _tweet.from_user;
         if (( _tweet.text.search(_reFilter) != -1 ) || ( _mention.search(_reFilter) != -1 )) {
@@ -93,7 +104,7 @@
 Btv_TweetArray.prototype.beforeDate = function(_date) {
     var _filtered = new Btv_TweetArray();
     this.each(function(_tweet) {
-        if (_tweet.date_value < _date) {
+        if (_tweet.date_value <= _date) {
             _filtered.push(_tweet);
         }
     });
@@ -131,7 +142,7 @@
     }
     this.options = _opts;
     var _this = this;
-    this.retrieveInitialTweets()
+    this.retrieveInitialTweets();
     setInterval(function() {
         _this.retrieveNewTweets();
     }, 5000);
@@ -157,18 +168,18 @@
             
             if (_isLast) {
                 _this.loading = false;
-                if (_this.tweetsCallback) {
-                    _this.tweetsCallback();
+                if (typeof _this.onNewTweets == "function") {
+                    _this.onNewTweets();
                 }
             }
         });
     }
     
+    
     if (this.loading) {
         return;
     }
     this.loading = true;
-    
     var _baseurl = "http://search.twitter.com/search.json",
         _currentPage = 0,
         _firstparams = "?q="
@@ -185,7 +196,7 @@
 
 Btv_TweetSource.prototype.retrieveInitialTweets = function() {
     this.retrieveTweets({
-        "pages": 6
+        "pages": 4
     });
 }
 
@@ -196,20 +207,63 @@
     });
 }
 
-Btv_TweetArray.prototype.setTweetsCallback = function(_callback) {
-    this.tweetsCallback = _callback;
+Btv_TweetSource.prototype.setOnNewTweets = function(_callback) {
+    this.onNewTweets = _callback;
+}
+
+Btv_TweetQueueManager = function(_tweetArray, _callback) {
+    this.tweetArray = _tweetArray;
+    this.majorInterval = 10000; // Time slices for calculating the minor Interval setting
+    this.minimumInterval = 1000; // Safe limit to avoid tweets going to the wrong column
+    this.initialBuffer = 20000; // don't start with empty columns, but show the tweets of the last 20000 seconds
+    this.waitIfNoTweets = 2000;
+    this.callback = _callback;
+    this.lastEndTime = new Date().valueOf() - 20000;
+    this.isPaused = false;
+    this.onMajorInterval();
 }
 
-/*
-Btv_TweetSubscriber = function(tweetArray) {
-    this.tweetArray = tweetArray;
-    this.position = 0;
+Btv_TweetQueueManager.prototype.onMinorInterval = function() {
+    if (this.isPaused) {
+        this.waitMinorInterval();
+    } else {
+        var _l = this.currentSlice.count();
+        if (this.position < _l) {
+            this.callback(this.currentSlice.tweetAtPos(this.position));
+            this.position++;
+            this.waitMinorInterval();
+        } else {
+            this.onMajorInterval();
+        }
+    }
+}
+
+Btv_TweetQueueManager.prototype.waitMinorInterval = function() {
+    var _this = this;
+    window.setTimeout(function() {
+        _this.onMinorInterval();
+    }, this.minorInterval);
 }
 
-Btv_TweetSubscriber.getTweets = function() {
-    var _p = this.position,
-        _l = this.tweetArray.count();
-    this.position = _l;
-    return this.tweetArray.slice(_p,_l);
+Btv_TweetQueueManager.prototype.onMajorInterval = function() {
+    this.position = 0;
+    this.currentSlice = this.tweetArray.afterDate(this.lastEndTime);
+    var _l = this.currentSlice.count();
+    if (_l) {
+        this.lastEndTime = this.currentSlice.tweetAtPos(_l - 1).date_value;
+        this.minorInterval = Math.floor(Math.max(this.minimumInterval, this.majorInterval / _l));
+        console.log("Added "+_l+" tweets to the queue, with an interval of "+this.minorInterval+" ms");
+        this.onMinorInterval();
+    } else {
+        var _this = this;
+        console.log("No tweets in the queue, waiting");
+        window.setTimeout(function() {
+            _this.onMajorInterval();
+        }, this.waitIfNoTweets);
+    }
 }
-*/
\ No newline at end of file
+
+Btv_TweetQueueManager.prototype.playPause = function() {
+    this.isPaused = !this.isPaused;
+    return this.isPaused;
+}