From 42a7d4585679a099f614786f2b6da55dff36a4bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Rebours?= Date: Fri, 22 Oct 2021 12:04:02 +0200 Subject: [PATCH] minor --- lib/cursor.js | 8 ++++---- lib/datastore.js | 32 ++++++++++++++++---------------- lib/executor.js | 19 ++++++++----------- 3 files changed, 28 insertions(+), 31 deletions(-) diff --git a/lib/cursor.js b/lib/cursor.js index ad6c9bf..b0eb402 100755 --- a/lib/cursor.js +++ b/lib/cursor.js @@ -167,12 +167,12 @@ class Cursor { callbackify(this._execAsync.bind(this))(_callback) } - exec () { - this.db.executor.push({ this: this, fn: this._exec, arguments: arguments }) + exec (...args) { + this.db.executor.push({ this: this, fn: this._exec, arguments: args }) } - execAsync () { - return this.db.executor.pushAsync({ this: this, fn: this._execAsync, arguments: arguments }) + execAsync (...args) { + return this.db.executor.pushAsync({ this: this, fn: this._execAsync, arguments: args }) } then (onFulfilled, onRejected) { diff --git a/lib/datastore.js b/lib/datastore.js index 94a707d..14e125c 100755 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -85,15 +85,15 @@ class Datastore extends EventEmitter { /** * Load the database from the datafile, and trigger the execution of buffered commands if any */ - loadDatabase () { - this.executor.push({ this: this.persistence, fn: this.persistence.loadDatabase, arguments: arguments }, true) + loadDatabase (...args) { + this.executor.push({ this: this.persistence, fn: this.persistence.loadDatabase, arguments: args }, true) } - loadDatabaseAsync () { + loadDatabaseAsync (...args) { return this.executor.pushAsync({ this: this.persistence, fn: this.persistence.loadDatabaseAsync, - arguments: arguments + arguments: args }, true) } @@ -404,12 +404,12 @@ class Datastore extends EventEmitter { } } - insert () { - this.executor.push({ this: this, fn: this._insert, arguments: arguments }) + insert (...args) { + this.executor.push({ this: this, fn: this._insert, arguments: args }) } - insertAsync () { - return this.executor.push({ this: this, fn: this._insertAsync, arguments: arguments, async: true }) + insertAsync (...args) { + return this.executor.push({ this: this, fn: this._insertAsync, arguments: args, async: true }) } /** @@ -602,12 +602,12 @@ class Datastore extends EventEmitter { } } - update () { - this.executor.push({ this: this, fn: this._update, arguments: arguments }) + update (...args) { + this.executor.push({ this: this, fn: this._update, arguments: args }) } - updateAsync () { - return this.executor.pushAsync({ this: this, fn: this._updateAsync, arguments: arguments }) + updateAsync (...args) { + return this.executor.pushAsync({ this: this, fn: this._updateAsync, arguments: args }) } /** @@ -650,12 +650,12 @@ class Datastore extends EventEmitter { return numRemoved } - remove () { - this.executor.push({ this: this, fn: this._remove, arguments: arguments }) + remove (...args) { + this.executor.push({ this: this, fn: this._remove, arguments: args }) } - removeAsync () { - return this.executor.pushAsync({ this: this, fn: this._removeAsync, arguments: arguments }) + removeAsync (...args) { + return this.executor.pushAsync({ this: this, fn: this._removeAsync, arguments: args }) } } diff --git a/lib/executor.js b/lib/executor.js index 8a76078..1d7f7f6 100755 --- a/lib/executor.js +++ b/lib/executor.js @@ -71,35 +71,32 @@ class Executor { this.ready = false this.queue = new Queue(async (task, async) => { - // task.arguments is an array-like object on which adding a new field doesn't work, so we transform it into a real array - const newArguments = Array.from(task.arguments) - // If the task isn't async, let's proceed with the old handler if (!async) { - const lastArg = newArguments[newArguments.length - 1] + const lastArg = task.arguments[task.arguments.length - 1] await new Promise(resolve => { if (typeof lastArg === 'function') { // We got a callback - newArguments.pop() // remove original callback - task.fn.apply(task.this, [...newArguments, function () { - resolve() // triggers next task after next tick + task.arguments.pop() // remove original callback + task.fn.apply(task.this, [...task.arguments, function () { + resolve() // triggers next task after next tick // TODO: check if it's at next tick or not lastArg.apply(null, arguments) // call original callback }]) } else if (!lastArg && task.arguments.length !== 0) { // We got a falsy callback - newArguments.pop() // remove original callback - task.fn.apply(task.this, [...newArguments, () => { + task.arguments.pop() // remove original callback + task.fn.apply(task.this, [...task.arguments, () => { resolve() }]) } else { // We don't have a callback - task.fn.apply(task.this, [...newArguments, () => { + task.fn.apply(task.this, [...task.arguments, () => { resolve() }]) } }) } else { - return task.fn.apply(task.this, newArguments) + return task.fn.apply(task.this, task.arguments) } }) }