This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cat adapter.js | |
require('./node-fibers'); | |
var print = require('util').print; | |
// This function runs an asynchronous function from within a fiber as if it | |
// were synchronous. | |
function asyncAsSync(fn /* ... */) { | |
var args = [].slice.call(arguments, 1); | |
var fiber = Fiber.current; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/coroutine.cc b/src/coroutine.cc | |
index e1d8908..265faf4 100644 | |
--- a/src/coroutine.cc | |
+++ b/src/coroutine.cc | |
@@ -42,7 +42,10 @@ static pthread_key_create_t& dyn_pthread_key_create(); | |
* the original implementation but that doesn't work because dlsym() tries to get a lock which ends | |
* up calling pthread_getspecific and pthread_setspecific. So have to implement our own versions of | |
* these functions assuming one thread only and then as soon as we can, put all that saved data into | |
- * real TLS. | |
+ * a better structure. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('fibers'); | |
var fs = require('fs'); | |
function resumer() { | |
var fiber = Fiber.current; | |
return function(err, val) { | |
if (err) { | |
fiber.throwInto(err); | |
} else { | |
fiber.run(val); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String.prototype.method = function() {} | |
function func(str) {} | |
var stringObject = new String("hello world"); | |
var stringPrimitive = "hello world"; | |
exports.compare = { | |
"stringObject.method()": function() { | |
stringObject.method(); | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Future() {} | |
Future.mixin({ | |
didComplete: function(value) { | |
if (value === undefined) { | |
throw new TypeError('May not return undefined from future'); | |
} | |
this._value = value; | |
var fibers = this._fibers; | |
if (fibers) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
require('fibers'); | |
this.Future = Future; | |
this.FiberFuture = FiberFuture; | |
this.wait = wait; | |
/** | |
* Return a function which automatically creates a fiber for each invocation. | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setTimeout(function() { | |
console.log('timeout'); | |
}, 0); | |
// simulate extreme cpu load | |
var d = +new Date; | |
console.log(new Date); | |
while (d + 2000 > new Date); | |
console.log(new Date); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Bad third-party code | |
var fs = require('fs'); | |
require('fibers'); | |
function fetchSomething() { // don't do this! | |
var fiber = Fiber.current; | |
fs.readFile(__filename, 'utf8', function(err, val) { | |
fiber.run(val); | |
}); | |
return Fiber.yield(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
this.MysqlBinlogTailer = MysqlBinlogTailer; | |
var EventEmitter = require('events').EventEmitter; | |
var fs = require('fs'); | |
var path = require('path'); | |
/** | |
* Tails a Mysql binlog and emits an event for every query executed. | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('fibers'); | |
// Generator function. Returns a function which returns incrementing | |
// Fibonacci numbers with each call. | |
function Fibonacci() { | |
// Create a new fiber which yields sequential Fibonacci numbers | |
var fiber = Fiber(function() { | |
Fiber.yield(0); // F(0) -> 0 | |
var prev = 0, curr = 1; | |
while (true) { |
OlderNewer