added buffer class to manage batches of asynchronous records
authorJordan Augé <jordan.auge@lip6.fr>
Tue, 3 Dec 2013 16:35:47 +0000 (17:35 +0100)
committerJordan Augé <jordan.auge@lip6.fr>
Tue, 3 Dec 2013 16:35:47 +0000 (17:35 +0100)
manifold/static/js/buffer.js [new file with mode: 0644]

diff --git a/manifold/static/js/buffer.js b/manifold/static/js/buffer.js
new file mode 100644 (file)
index 0000000..46bac84
--- /dev/null
@@ -0,0 +1,40 @@
+/* Buffered DOM updates */
+var Buffer = Class.extend({
+
+    init: function(callback, callback_this) {
+        this._callback = callback;
+        this._callback_this = callback_this;
+        this._timerid  = null;
+        this._num_elements = 0;
+        this._elements = Array();
+
+        this._interval = 1000;
+
+        return this;
+    },
+
+    add: function(element)
+    {
+        this._elements.push(element);
+        if (this._num_elements == 0) {
+            this._timerid = setInterval(
+                (function(self) {         //Self-executing func which takes 'this' as self
+                    return function() {   //Return a function in the context of 'self'
+                        console.log("running callback");
+                        clearInterval(self._timerid);
+                        self._callback.apply(self._callback_this);
+                    }
+                })(this),
+                this._interval);
+        }
+        this._num_elements++;
+    },
+
+    get: function() {
+        var elements = this._elements;
+        this._elements = Array();
+        this._num_elements = 0;
+        return elements;
+    },
+
+});