Skip to content

Commit

Permalink
fixed #252 - setImmediate not available in browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
bjouhier committed Nov 28, 2014
1 parent f3eed78 commit 1ee6cd7
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 51 deletions.
29 changes: 18 additions & 11 deletions lib/callbacks/flows.js
Expand Up @@ -178,13 +178,13 @@
return {
wait: __rt.streamlinify(function(cb) {
if (callback) { throw new Error("already waiting") };
if (notified) { setImmediate(cb); } else {
if (notified) { exports.setImmediate(cb); } else {
callback = cb; };
notified = false;
}, 0),
notify: function() {
if (!callback) { notified = true; } else {
setImmediate(callback); };
exports.setImmediate(callback); };
callback = null; } }; };


Expand All @@ -210,12 +210,12 @@
var item = q.shift();

if ((q.length === 0)) { q = []; };
setImmediate(function() {
exports.setImmediate(function() {
cb(null, item); });

if ((pendingWrites.length > 0)) {
var wr = pendingWrites.shift();
setImmediate(function() {
exports.setImmediate(function() {
wr[0](null, wr[1]); }); } ; }


Expand All @@ -225,7 +225,7 @@
}, 0),
write: __rt.streamlinify(function(cb, item) {
if (this.put(item)) {
setImmediate(function() {
exports.setImmediate(function() {
cb(); }); }

else {
Expand All @@ -239,7 +239,7 @@
else {
var cb = callback;
callback = null;
setImmediate(function() {
exports.setImmediate(function() {
cb(null, item); }); } ;


Expand Down Expand Up @@ -280,21 +280,28 @@

exports.trampoline = __rt.streamlinify(function(cb, fn, thisObj) {
fn = globals.withContext(fn, globals.context);
setImmediate(function() {
exports.setImmediate(function() {
fn.call(thisObj, cb); });

}, 0);




exports.setImmediate = ((typeof setImmediate === "function") ? setImmediate : function(fn) {
setTimeout(fn, 0); });






var nextTick = (((typeof process === "object") && (typeof process.nextTick === "function")) ? process.nextTick : function(cb) {
cb(); });



exports.nextTick = function exports_nextTick__11(_) { var __frame = { name: "exports_nextTick__11", line: 297 }; return __func(_, this, arguments, exports_nextTick__11, 0, __frame, function __$exports_nextTick__11() {
exports.nextTick = function exports_nextTick__11(_) { var __frame = { name: "exports_nextTick__11", line: 304 }; return __func(_, this, arguments, exports_nextTick__11, 0, __frame, function __$exports_nextTick__11() {
return nextTick(__cb(_, __frame, 1, 2, function __$exports_nextTick__11() { _(); }, true)); }); };


Expand Down Expand Up @@ -326,15 +333,15 @@



exports.sleep = function exports_sleep__12(_, millis) { var __frame = { name: "exports_sleep__12", line: 329 }; return __func(_, this, arguments, exports_sleep__12, 0, __frame, function __$exports_sleep__12() {
exports.sleep = function exports_sleep__12(_, millis) { var __frame = { name: "exports_sleep__12", line: 336 }; return __func(_, this, arguments, exports_sleep__12, 0, __frame, function __$exports_sleep__12() {
return setTimeout(__cb(_, __frame, 1, 9, _, true), millis); }); };


exports.eventHandler = function(fn) {
return function() {
var that = this;
var args = Array.prototype.slice(arguments, 0);
return (function __1(_) { var __frame = { name: "__1", line: 337 }; return __func(_, this, arguments, __1, 0, __frame, function __$__1() {
return (function __1(_) { var __frame = { name: "__1", line: 344 }; return __func(_, this, arguments, __1, 0, __frame, function __$__1() {
return fn.apply_(__cb(_, __frame, 1, 14, _, true), that, args, 0); });
})(function(err) {
if (err) { throw err }; }); }; };
Expand All @@ -343,7 +350,7 @@



exports.apply = function apply(_, fn, thisObj, args, index) { var __frame = { name: "apply", line: 346 }; return __func(_, this, arguments, apply, 0, __frame, function __$apply() {
exports.apply = function apply(_, fn, thisObj, args, index) { var __frame = { name: "apply", line: 353 }; return __func(_, this, arguments, apply, 0, __frame, function __$apply() {
return fn.apply_(__cb(_, __frame, 1, 12, _, true), thisObj, args, index); }); };


Expand Down
21 changes: 14 additions & 7 deletions lib/compiler/flows._js
Expand Up @@ -178,13 +178,13 @@
return {
wait: _(function(cb) {
if (callback) throw new Error("already waiting");
if (notified) setImmediate(cb);
if (notified) exports.setImmediate(cb);
else callback = cb;
notified = false;
}, 0),
notify: function() {
if (!callback) notified = true;
else setImmediate(callback);
else exports.setImmediate(callback);
callback = null;
},
};
Expand All @@ -210,12 +210,12 @@
var item = q.shift();
// recycle queue when empty to avoid maintaining arrays that have grown large and shrunk
if (q.length === 0) q = [];
setImmediate(function() {
exports.setImmediate(function() {
cb(null, item);
});
if (pendingWrites.length > 0) {
var wr = pendingWrites.shift();
setImmediate(function() {
exports.setImmediate(function() {
wr[0](null, wr[1]);
});
}
Expand All @@ -225,7 +225,7 @@
}, 0),
write: _(function(cb, item) {
if (this.put(item)) {
setImmediate(function() {
exports.setImmediate(function() {
cb();
});
} else {
Expand All @@ -239,7 +239,7 @@
} else {
var cb = callback;
callback = null;
setImmediate(function() {
exports.setImmediate(function() {
cb(null, item);
});
}
Expand Down Expand Up @@ -280,11 +280,18 @@
/// before calling `fn`.
exports.trampoline = _(function(cb, fn, thisObj) {
fn = globals.withContext(fn, globals.context);
setImmediate(function() {
exports.setImmediate(function() {
fn.call(thisObj, _ >> cb);
});
}, 0);

///
/// * `flows.setImmediate(fn)`
/// portable `setImmediate` both browser and server.
exports.setImmediate = typeof setImmediate === "function" ? setImmediate : function(fn) {
setTimeout(fn, 0);
};

///
/// * `flows.nextTick(_)`
/// `nextTick` function for both browser and server.
Expand Down
21 changes: 14 additions & 7 deletions lib/fibers-fast/flows.js
Expand Up @@ -178,13 +178,13 @@
return {
wait: fstreamline__.star(function(cb) {
if (callback) throw new Error("already waiting");
if (notified) setImmediate(cb);
if (notified) exports.setImmediate(cb);
else callback = cb;
notified = false;
}, 0),
notify: function() {
if (!callback) notified = true;
else setImmediate(callback);
else exports.setImmediate(callback);
callback = null;
},
};
Expand All @@ -210,12 +210,12 @@
var item = q.shift();
// recycle queue when empty to avoid maintaining arrays that have grown large and shrunk
if (q.length === 0) q = [];
setImmediate(function() {
exports.setImmediate(function() {
cb(null, item);
});
if (pendingWrites.length > 0) {
var wr = pendingWrites.shift();
setImmediate(function() {
exports.setImmediate(function() {
wr[0](null, wr[1]);
});
}
Expand All @@ -225,7 +225,7 @@
}, 0),
write: fstreamline__.star(function(cb, item) {
if (this.put(item)) {
setImmediate(function() {
exports.setImmediate(function() {
cb();
});
} else {
Expand All @@ -239,7 +239,7 @@
} else {
var cb = callback;
callback = null;
setImmediate(function() {
exports.setImmediate(function() {
cb(null, item);
});
}
Expand Down Expand Up @@ -280,11 +280,18 @@
/// before calling `fn`.
exports.trampoline = fstreamline__.star(function(cb, fn, thisObj) {
fn = globals.withContext(fn, globals.context);
setImmediate(function() {
exports.setImmediate(function() {
fstreamline__.createBound(fn,'call',1)(thisObj, cb);
});
}, 0);

///
/// * `flows.setImmediate(fn)`
/// portable `setImmediate` both browser and server.
exports.setImmediate = typeof setImmediate === "function" ? setImmediate : function(fn) {
setTimeout(fn, 0);
};

///
/// * `flows.nextTick(_)`
/// `nextTick` function for both browser and server.
Expand Down
29 changes: 18 additions & 11 deletions lib/fibers/flows.js
Expand Up @@ -178,13 +178,13 @@
return {
wait: fstreamline__.streamlinify(function(cb) {
if (callback) throw new Error("already waiting");
if (notified) setImmediate(cb);
if (notified) exports.setImmediate(cb);
else callback = cb;
notified = false;
}, 0),
notify: function() {
if (!callback) notified = true;
else setImmediate(callback);
else exports.setImmediate(callback);
callback = null;
},
};
Expand All @@ -210,12 +210,12 @@
var item = q.shift();
// recycle queue when empty to avoid maintaining arrays that have grown large and shrunk
if (q.length === 0) q = [];
setImmediate(function() {
exports.setImmediate(function() {
cb(null, item);
});
if (pendingWrites.length > 0) {
var wr = pendingWrites.shift();
setImmediate(function() {
exports.setImmediate(function() {
wr[0](null, wr[1]);
});
}
Expand All @@ -225,7 +225,7 @@
}, 0),
write: fstreamline__.streamlinify(function(cb, item) {
if (this.put(item)) {
setImmediate(function() {
exports.setImmediate(function() {
cb();
});
} else {
Expand All @@ -239,7 +239,7 @@
} else {
var cb = callback;
callback = null;
setImmediate(function() {
exports.setImmediate(function() {
cb(null, item);
});
}
Expand Down Expand Up @@ -280,11 +280,18 @@
/// before calling `fn`.
exports.trampoline = fstreamline__.streamlinify(function(cb, fn, thisObj) {
fn = globals.withContext(fn, globals.context);
setImmediate(function() {
exports.setImmediate(function() {
fn.call(thisObj, cb);
});
}, 0);

///
/// * `flows.setImmediate(fn)`
/// portable `setImmediate` both browser and server.
exports.setImmediate = typeof setImmediate === "function" ? setImmediate : function(fn) {
setTimeout(fn, 0);
};

///
/// * `flows.nextTick(_)`
/// `nextTick` function for both browser and server.
Expand All @@ -296,7 +303,7 @@
// document later
exports.nextTick = fstreamline__.create(function(_) {
fstreamline__.invoke(null, nextTick, [_], 0);
}, 0,__filename,297);
}, 0,__filename,304);

// document later
// should probably cap millis instead of trying to be too smart
Expand Down Expand Up @@ -328,15 +335,15 @@
/// Sleeps `millis` ms.
exports.sleep = fstreamline__.create(function(_, millis) {
return fstreamline__.invoke(null, setTimeout, [_, millis], 0);
}, 0,__filename,329);
}, 0,__filename,336);

exports.eventHandler = function(fn) {
return function() {
var that = this;
var args = Array.prototype.slice(arguments, 0);
return fstreamline__.create((function(_) {
return fstreamline__.invoke(fn, "apply_", [_, that, args, 0], 0);
}), 0,__filename,337)( function(err) {
}), 0,__filename,344)( function(err) {
if (err) throw err;
});
};
Expand All @@ -345,7 +352,7 @@
// Obsolete. Use `fn.apply_` instead.
exports.apply = fstreamline__.create(function apply(_, fn, thisObj, args, index) {
return fstreamline__.invoke(fn, "apply_", [_, thisObj, args, index], 0);
}, 0,__filename,346);
}, 0,__filename,353);

///
/// * `flows.callWithTimeout(_, fn, millis)`
Expand Down

0 comments on commit 1ee6cd7

Please sign in to comment.