-
Notifications
You must be signed in to change notification settings - Fork 3
/
sync_map.js
52 lines (47 loc) · 1.12 KB
/
sync_map.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
if (Meteor.isServer) {
var path = Npm.require('path');
var Future = Npm.require(path.join('fibers', 'future'));
}
_SynchronousMapEntry = function () {
var self = this;
self._future = undefined;
self._value = undefined;
self._done = false;
};
SynchronousMap = function () {
var self = this;
self._map = {};
};
_.extend(SynchronousMap.prototype, {
get: function (arguments, f) {
var self = this;
var v = self._map[arguments];
if (v === undefined) {
self._map[arguments] = v = new _SynchronousMapEntry;
}
if (v._done) {
return v._value;
}
if (Meteor.isServer) {
if (v._future) {
return v._future.wait();
}
var future = v._future = new Future();
try {
var value = f.apply(null, arguments);
v._future.return(value);
v._value = value;
v._done = true;
} catch (e) {
v._future.throw(e);
// We'll retry this, so we won't set _done
}
v._future = null;
return future.wait();
} else {
v._value = f.apply(null, arguments);
v._done = true;
return v._value;
}
}
});