From 174577cca1256f0b65b78837887d4ea9bac108a5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jordan=20Aug=C3=A9?= Date: Tue, 3 Dec 2013 17:35:47 +0100 Subject: [PATCH] added buffer class to manage batches of asynchronous records --- manifold/static/js/buffer.js | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 manifold/static/js/buffer.js diff --git a/manifold/static/js/buffer.js b/manifold/static/js/buffer.js new file mode 100644 index 00000000..46bac84a --- /dev/null +++ b/manifold/static/js/buffer.js @@ -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; + }, + +}); -- 2.43.0