This repository has been archived by the owner on Jul 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmeteor.js
70 lines (63 loc) · 2.31 KB
/
meteor.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
var withoutInvocation = function (f) {
if (Package.ddp) {
var _CurrentInvocation = Package.ddp.DDP._CurrentInvocation;
if (_CurrentInvocation.get() && _CurrentInvocation.get().isSimulation)
throw new Error("Can't set timers inside simulations");
return function () { _CurrentInvocation.withValue(null, f); };
}
else
return f;
};
var bindAndCatch = function (context, f) {
return Meteor.bindEnvironment(withoutInvocation(f), context);
};
_.extend(Meteor, {
// Meteor.setTimeout and Meteor.setInterval callbacks scheduled
// inside a server method are not part of the method invocation and
// should clear out the CurrentInvocation environment variable.
/**
* @memberOf Meteor
* @summary Call a function in the future after waiting for a specified delay.
* @locus Anywhere
* @param {Function} func The function to run
* @param {Number} delay Number of milliseconds to wait before calling function
*/
setTimeout: function (f, duration) {
return Kernel.setTimeout(bindAndCatch("setTimeout callback", f), duration);
},
/**
* @memberOf Meteor
* @summary Call a function repeatedly, with a time delay between calls.
* @locus Anywhere
* @param {Function} func The function to run
* @param {Number} delay Number of milliseconds to wait between each function call.
*/
setInterval: function (f, duration) {
return Kernel.setInterval(bindAndCatch("setInterval callback", f), duration);
},
/**
* @memberOf Meteor
* @summary Cancel a repeating function call scheduled by `Meteor.setInterval`.
* @locus Anywhere
* @param {Number} id The handle returned by `Meteor.setInterval`
*/
clearInterval: function(x) {
return Kernel.clearInterval(x);
},
/**
* @memberOf Meteor
* @summary Cancel a function call scheduled by `Meteor.setTimeout`.
* @locus Anywhere
* @param {Number} id The handle returned by `Meteor.setTimeout`
*/
clearTimeout: function(x) {
return Kernel.clearTimeout(x);
},
// XXX consider making this guarantee ordering of defer'd callbacks, like
// Tracker.afterFlush or Node's nextTick (in practice). Then tests can do:
// callSomethingThatDefersSomeWork();
// Meteor.defer(expect(somethingThatValidatesThatTheWorkHappened));
defer: function (f) {
Kernel.defer(bindAndCatch("defer callback", f));
}
});