From: Jordan Augé Date: Tue, 3 Dec 2013 16:35:47 +0000 (+0100) Subject: added buffer class to manage batches of asynchronous records X-Git-Tag: myslice-0.3-0~96^2 X-Git-Url: http://git.onelab.eu/?p=myslice.git;a=commitdiff_plain;h=174577cca1256f0b65b78837887d4ea9bac108a5 added buffer class to manage batches of asynchronous records --- 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; + }, + +});