From 9d0892abdd4f6d518fb761dff104464516a65b8e Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Fri, 31 Jan 2020 14:56:23 +0100 Subject: [PATCH] more timeout logic --- index.js | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index fca3cf9..e600c70 100644 --- a/index.js +++ b/index.js @@ -203,7 +203,6 @@ class Fuse extends Nanoresource { _makeHandlerArray () { const self = this const handlers = new Array(OpcodesAndDefaults.size) - const to = this.timeout for (const [name, { op, defaults }] of OpcodesAndDefaults) { const nativeSignal = binding[`fuse_native_signal_${name}`] @@ -215,6 +214,13 @@ class Fuse extends Nanoresource { return handlers function makeHandler (name, op, defaults, nativeSignal) { + let to = self.timeout + if (typeof to === 'object' && to) { + const defaultTimeout = to.default || DEFAULT_TIMEOUT + to = to[name] + if (!to && to !== false) to = defaultTimeout + } + return function (nativeHandler, opCode, ...args) { const sig = signal.bind(null, nativeHandler) const boundSignal = to ? autoTimeout(sig) : sig @@ -231,18 +237,18 @@ class Fuse extends Nanoresource { } return process.nextTick(nativeSignal, ...arr) } - } - function autoTimeout (cb) { - let called = false - const timeout = setTimeout(timeoutWrap, to, TIMEOUT_ERRNO) - return timeoutWrap + function autoTimeout (cb) { + let called = false + const timeout = setTimeout(timeoutWrap, to, TIMEOUT_ERRNO) + return timeoutWrap - function timeoutWrap (...args) { - if (called) return - called = true - clearTimeout(timeout) - cb(...args) + function timeoutWrap (...args) { + if (called) return + called = true + clearTimeout(timeout) + cb(...args) + } } } }