Browse Source

disable async hook when possible

master
Conduitry 1 year ago
parent
commit
8893733364
2 changed files with 18 additions and 3 deletions
  1. +6
    -1
      src/Defiler.ts
  2. +12
    -2
      src/context.ts

+ 6
- 1
src/Defiler.ts View File

@ -86,6 +86,7 @@ export default class Defiler {
// execute everything, and return a promise that resolves when the first wave of processing is complete
async exec(): Promise<void> {
context.ref();
if (this._status !== Status.Before) {
throw new Error('defiler.exec: cannot call more than once');
}
@ -127,7 +128,11 @@ export default class Defiler {
await done;
this._status = Status.After;
this._is_processing = false;
this._enqueue();
if (this._watchers.some(watcher => watcher.watch)) {
this._enqueue();
} else {
context.unref();
}
}
// wait for a file to be available and retrieve it, marking dependencies as appropriate

+ 12
- 2
src/context.ts View File

@ -2,10 +2,20 @@ import { createHook, executionAsyncId } from 'async_hooks';
const contexts = new Map<Number, any>();
createHook({
const hook = createHook({
init: (id, _, trigger) => contexts.set(id, contexts.get(trigger)),
destroy: id => contexts.delete(id),
}).enable();
});
let refs = 0;
export const ref = (): void => {
refs++ || hook.enable();
};
export const unref = (): void => {
--refs || hook.disable();
};
export const create = (data: any): void => {
contexts.set(executionAsyncId(), data);

Loading…
Cancel
Save