Skip to content

Commit

Permalink
fix: upgrade async_hook to new API after nodejs/node#4600
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasMadsen committed Feb 21, 2016
1 parent 890e888 commit a8da858
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 22 deletions.
16 changes: 3 additions & 13 deletions async-hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const patchs = {
'timers': require('./patches/timers.js')
};

const uidSymbol = Symbol('async-hook-uid');
const ignoreUIDs = new Set();

function State() {
Expand All @@ -23,28 +22,20 @@ function Hooks() {
const postFns = this.postFns = [];
const destroyFns = this.destroyFns = [];

this.init = function (provider, uid, parentHandle) {
this[uidSymbol] = uid;

this.init = function (uid, provider, parentUid, parentHandle) {
// Ignore TIMERWRAP, since setTimeout etc. is monkey patched
if (provider === TIMERWRAP) {
ignoreUIDs.add(uid);
return;
}

// send the parent uid, not the parent handle. The user map the handle
// objects appropiatly if needed.
let parentUid = null;
if (parentHandle !== null) parentUid = parentHandle[uidSymbol];

// call hooks
for (const hook of initFns) {
hook(uid, this, provider, parentUid, parentHandle);
}
};

this.pre = function () {
const uid = this[uidSymbol];
this.pre = function (uid) {
if (ignoreUIDs.has(uid)) return;

// call hooks
Expand All @@ -53,8 +44,7 @@ function Hooks() {
}
};

this.post = function () {
const uid = this[uidSymbol];
this.post = function (uid) {
if (ignoreUIDs.has(uid)) return;

// call hooks
Expand Down
6 changes: 3 additions & 3 deletions patches/next-tick.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ module.exports = function patch() {
const uid = --state.counter;

// call the init hook
hooks.init.call(handle, 0, uid, null);
hooks.init.call(handle, uid, 0, null, null);

// overwrite callback
args[0] = function () {
// call the pre hook
hooks.pre.call(handle);
hooks.pre.call(handle, uid);

callback.apply(this, arguments);

// call the post hook, followed by the destroy hook
hooks.post.call(handle);
hooks.post.call(handle, uid);
hooks.destroy.call(null, uid);
};

Expand Down
6 changes: 3 additions & 3 deletions patches/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ module.exports = function patchPromise() {
}

return function wrappedHandler() {
hooks.pre.call(handle);
hooks.pre.call(handle, uid);
try {
return fn.apply(this, arguments);
} finally {
hooks.post.call(handle);
hooks.post.call(handle, uid);
hooks.destroy.call(null, uid);
}
};
Expand All @@ -53,7 +53,7 @@ module.exports = function patchPromise() {
const handle = new PromiseWrap();
const uid = --state.counter;

hooks.init.call(handle, 0, uid, null);
hooks.init.call(handle, uid, 0, null, null);

return oldThen.call(
this,
Expand Down
6 changes: 3 additions & 3 deletions patches/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ function patchTimer(hooks, state, setFn, clearFn, Handle, timerMap, singleCall)
let timerId;

// call the init hook
hooks.init.call(handle, 0, uid, null);
hooks.init.call(handle, uid, 0, null, null);

// overwrite callback
args[0] = function () {
// call the pre hook
hooks.pre.call(handle);
hooks.pre.call(handle, uid);

callback.apply(this, arguments);

// call the post hook
hooks.post.call(handle);
hooks.post.call(handle, uid);

// call thie destroy hook if the callback will only be called once
if (singleCall) {
Expand Down

0 comments on commit a8da858

Please sign in to comment.