From a71cc2585096206f52644dae21b9b9eaf86e44b9 Mon Sep 17 00:00:00 2001 From: toyobayashi Date: Sat, 21 Jan 2023 22:22:56 +0800 Subject: [PATCH] use MessagePort instead of setInterval --- packages/runtime/src/Context.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/runtime/src/Context.ts b/packages/runtime/src/Context.ts index 5becbce5..b1ea0917 100644 --- a/packages/runtime/src/Context.ts +++ b/packages/runtime/src/Context.ts @@ -78,12 +78,18 @@ class CleanupQueue { } class NodejsWaitingRequestCounter { - private timer: any = undefined - private count: number = 0 + private readonly refHandle: { ref: () => void; unref: () => void } + private count: number + + constructor () { + this.refHandle = new MessageChannel().port1 as unknown as import('worker_threads').MessagePort + this.refHandle.unref() + this.count = 0 + } public increase (): void { if (this.count === 0) { - this.timer = setInterval(() => {}, 1e8) + this.refHandle.ref() } this.count++ } @@ -91,8 +97,7 @@ class NodejsWaitingRequestCounter { public decrease (): void { if (this.count === 0) return if (this.count === 1) { - clearInterval(this.timer) - this.timer = undefined + this.refHandle.unref() } this.count-- }