Skip to content

Commit

Permalink
issue #239 - documented flows.trampoline helper
Browse files Browse the repository at this point in the history
  • Loading branch information
bjouhier committed Oct 11, 2014
1 parent d19eae1 commit 2dfa94b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/compiler/flows._js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,12 @@
return globals.context;
}

///
/// * result = flows.trampoline(_, fn, thisObj)`
/// Executes `fn(_)` through a trampoline.
/// Waits for `fn`'s result and returns it.
/// This is equivalent to calling `fn.call(thisObj, _)` but the current stack is unwound
/// before calling `fn`.
exports.trampoline = _(function(cb, fn, thisObj) {
setImmediate(function() {
fn.call(thisObj, _ >> cb);
Expand All @@ -286,10 +292,13 @@
cb();
};

// document later
exports.nextTick = function(_) {
nextTick(~_);
};

// document later
// should probably cap millis instead of trying to be too smart
exports.setTimeout = function(fn, millis) {
// node's setTimeout notifies immediately if millis > max!!
// So be safe and work around it.
Expand All @@ -306,12 +315,16 @@
}
}

// document later
exports.setInterval = function(fn, millis) {
return setInterval(function() {
fn(!_);
}, millis)
}

///
/// * `flows.sleep(_, millis)`
/// Sleeps `millis` ms.
exports.sleep = function(_, millis) {
return setTimeout(~_, millis)
}
Expand All @@ -333,6 +346,10 @@
return fn.apply_(_, thisObj, args, index);
}

///
/// * `flows.callWithTimeout(_, fn, millis)`
/// Calls `fn(_)` with a timeout guard.
/// Throws a timeout exception if `fn` takes more than `millis` ms to complete.
exports.callWithTimeout = _(function(cb, fn, millis) {
var tid = setTimeout(function() {
if (cb) {
Expand Down
13 changes: 13 additions & 0 deletions lib/util/flows.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ won't be called, and no other operation will enter the funnel.
* `results = flows.collect(_, futures)`
collects the results of an array of futures

* result = flows.trampoline(_, fn, thisObj)`
Executes `fn(_)` through a trampoline.
Waits for `fn`'s result and returns it.
This is equivalent to calling `fn.call(thisObj, _)` but the current stack is unwound
before calling `fn`.

* `flows.nextTick(_)`
`nextTick` function for both browser and server.
Aliased to `process.nextTick` on the server side.

* `flows.sleep(_, millis)`
Sleeps `millis` ms.

* `flows.callWithTimeout(_, fn, millis)`
Calls `fn(_)` with a timeout guard.
Throws a timeout exception if `fn` takes more than `millis` ms to complete.

0 comments on commit 2dfa94b

Please sign in to comment.