Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial work to get async resource working correctly #465

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fibers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ if (process.fiberLib) {
throw new Error('Missing binary. See message above.');
}

setupAsyncHacks(module.exports);
//setupAsyncHacks(module.exports);
}

function setupAsyncHacks(Fiber) {
Expand Down
64 changes: 64 additions & 0 deletions fibers_async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const { AsyncResource } = require('async_hooks');
const { timeStamp } = require('console');
const _Fiber = require('./fibers.js');

const weakMap = new WeakMap();
const Fiber = function Fiber(...args) {
if (!(this instanceof Fiber)) {
return new Fiber(...args);
}

const _private = {
_ar: new AsyncResource('Fiber'),
_fiber: _Fiber(...args)
};
weakMap.set(this, _private);
_private._fiber._f = this;
return this;
};

Fiber.__proto__ = _Fiber;

Object.defineProperty(Fiber, 'current', {
get() {
return _Fiber.current && _Fiber.current._f;
}
})

_Fiber[Symbol.hasInstance] = function(obj) {
// hacky
return obj instanceof Fiber || obj.run;
};

module.exports = Fiber;
Fiber.prototype = {
__proto__: Fiber,
get current() {
return _Fiber.current._f;
},

yield() {
return _Fiber.yield(...args);
},

get _ar() {
return weakMap.get(this)._ar;
},

// because of promise fiber pool, we want this.
set _ar(ar) {
return weakMap.get(this)._ar = ar;
},

get _fiber() {
return weakMap.get(this)._fiber;
},

run(...args) {
return this._ar.runInAsyncScope(() => this._fiber.run(...args));
},

throwInto(...args) {
return this._ar.runInAsyncScope(() => this._fiber.throwInto(...args));
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"homepage": "https://github.com/laverdet/node-fibers",
"author": "Marcel Laverdet <[email protected]> (https://github.com/laverdet/)",
"main": "fibers",
"main": "fibers_async",
"scripts": {
"install": "node build.js || nodejs build.js",
"test": "node test.js || nodejs test.js"
Expand Down