/
dynamics_browser.js
65 lines (56 loc) · 1.61 KB
/
dynamics_browser.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
// Simple implementation of dynamic scoping, for use in browsers
var nextSlot = 0;
var currentValues = [];
Meteor.EnvironmentVariable = function () {
this.slot = nextSlot++;
};
_.extend(Meteor.EnvironmentVariable.prototype, {
get: function () {
return currentValues[this.slot];
},
getOrNullIfOutsideFiber: function () {
return this.get();
},
withValue: function (value, func) {
var saved = currentValues[this.slot];
try {
currentValues[this.slot] = value;
var ret = func();
} finally {
currentValues[this.slot] = saved;
}
return ret;
}
});
Meteor.bindEnvironment = function (func, onException, _this) {
// needed in order to be able to create closures inside func and
// have the closed variables not change back to their original
// values
var boundValues = _.clone(currentValues);
if (!onException || typeof(onException) === 'string') {
var description = onException || "callback of async function";
onException = function (error) {
Meteor._debug(
"Exception in " + description + ":",
error && error.stack || error
);
};
}
return function (/* arguments */) {
var savedValues = currentValues;
try {
currentValues = boundValues;
var ret = func.apply(_this, _.toArray(arguments));
} catch (e) {
// note: callback-hook currently relies on the fact that if onException
// throws in the browser, the wrapped call throws.
onException(e);
} finally {
currentValues = savedValues;
}
return ret;
};
};
Meteor._nodeCodeMustBeInFiber = function () {
// no-op on browser
};