-
Notifications
You must be signed in to change notification settings - Fork 11
/
SyncItBuffer.js
73 lines (65 loc) · 1.75 KB
/
SyncItBuffer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
module.exports = (function (SyncItConstant, SyncIt) {
"use strict";
/**
* ## SyncItBuffer
*
* A wrapper that can be used around SyncIt so you can blindly call functions
* that lock SyncIt and they will queue up.
*
* NOTE: This is not unit tested and is perhaps subject to change (may have bugs).
*/
var SyncItBuffer = function(syncIt) {
/* global setInterval */
this._syncIt = syncIt;
this._instructions = [];
this._current = null;
setInterval(function() {
var args, c;
if ((this._current === null) && this._instructions.length) {
this._current = this._instructions.shift();
args = Array.prototype.slice.call(this._current.a);
args.push(function(e) {
if (e !== SyncItConstant.Error.UNABLE_TO_PROCESS_BECAUSE_LOCKED) {
c = this._current.c;
this._current = null;
c.apply(
this._syncIt,
arguments
);
}
}.bind(this));
this._current.f.apply(this._syncIt, args);
}
}.bind(this), 50);
};
var mapNonBuffered = function(k) {
SyncItBuffer.prototype[k] = function() {
this._syncIt[k].apply(
this._syncIt,
Array.prototype.slice.call(arguments)
);
};
};
var mapBuffered = function(k) {
SyncItBuffer.prototype[k] = function() {
this._instructions.push({
f: this._syncIt[k],
a: Array.prototype.slice.call(arguments,0,arguments.length - 1),
c: Array.prototype.slice.call(arguments,arguments.length - 1)[0]
});
};
};
var k,
bufferedFuncs = ['update', 'remove', 'set', 'purge', 'clean', 'feed', 'advance', 'getFirst'];
for (k in SyncIt.prototype) {
if (SyncIt.prototype.hasOwnProperty(k)) {
if (k.match(/^_/)) { continue; }
if (bufferedFuncs.indexOf(k) > -1) {
mapBuffered(k);
} else {
mapNonBuffered(k);
}
}
}
return SyncItBuffer;
}(require('./Constant.js'), require('./SyncIt.js')));