forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiber-helpers.js
39 lines (36 loc) · 1.3 KB
/
fiber-helpers.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
// node (v8) defaults to only recording 10 lines of stack trace.
// this becomes especially bad when using fibers, since you get deeper
// stack traces that would have been split up between different callbacks
//
// this only affects the `meteor` executable, not server code on
// meteor apps.
Error.stackTraceLimit = Infinity; // http://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
var _ = require("underscore");
var Fiber = require("fibers");
var Future = require("fibers/future");
// runs a function within a fiber. we wrap the entry point into
// meteor.js into a fiber but if you use callbacks that call
// synchronous code you need to wrap those as well.
//
// NOTE: It's probably better to not use callbacks. Instead you can
// use Futures to generate synchronous equivalents.
exports.inFiber = function(func) {
return function(/*arguments*/) {
var self = this;
var args = arguments;
new Fiber(function () {
func.apply(self, args);
}).run();
};
};
exports.parallelEach = function (collection, callback, context) {
var futures = _.map(collection, function () {
var args = _.toArray(arguments);
return function () {
return callback.apply(context, args);
}.future()();
});
Future.wait(futures);
// Throw if any threw.
_.each(futures, function (f) { f.get(); });
};