pull/11/head
Timothée Rebours 3 years ago
parent b57e3a01fb
commit 42a7d45856
  1. 8
      lib/cursor.js
  2. 32
      lib/datastore.js
  3. 19
      lib/executor.js

@ -167,12 +167,12 @@ class Cursor {
callbackify(this._execAsync.bind(this))(_callback) callbackify(this._execAsync.bind(this))(_callback)
} }
exec () { exec (...args) {
this.db.executor.push({ this: this, fn: this._exec, arguments: arguments }) this.db.executor.push({ this: this, fn: this._exec, arguments: args })
} }
execAsync () { execAsync (...args) {
return this.db.executor.pushAsync({ this: this, fn: this._execAsync, arguments: arguments }) return this.db.executor.pushAsync({ this: this, fn: this._execAsync, arguments: args })
} }
then (onFulfilled, onRejected) { then (onFulfilled, onRejected) {

@ -85,15 +85,15 @@ class Datastore extends EventEmitter {
/** /**
* Load the database from the datafile, and trigger the execution of buffered commands if any * Load the database from the datafile, and trigger the execution of buffered commands if any
*/ */
loadDatabase () { loadDatabase (...args) {
this.executor.push({ this: this.persistence, fn: this.persistence.loadDatabase, arguments: arguments }, true) this.executor.push({ this: this.persistence, fn: this.persistence.loadDatabase, arguments: args }, true)
} }
loadDatabaseAsync () { loadDatabaseAsync (...args) {
return this.executor.pushAsync({ return this.executor.pushAsync({
this: this.persistence, this: this.persistence,
fn: this.persistence.loadDatabaseAsync, fn: this.persistence.loadDatabaseAsync,
arguments: arguments arguments: args
}, true) }, true)
} }
@ -404,12 +404,12 @@ class Datastore extends EventEmitter {
} }
} }
insert () { insert (...args) {
this.executor.push({ this: this, fn: this._insert, arguments: arguments }) this.executor.push({ this: this, fn: this._insert, arguments: args })
} }
insertAsync () { insertAsync (...args) {
return this.executor.push({ this: this, fn: this._insertAsync, arguments: arguments, async: true }) return this.executor.push({ this: this, fn: this._insertAsync, arguments: args, async: true })
} }
/** /**
@ -602,12 +602,12 @@ class Datastore extends EventEmitter {
} }
} }
update () { update (...args) {
this.executor.push({ this: this, fn: this._update, arguments: arguments }) this.executor.push({ this: this, fn: this._update, arguments: args })
} }
updateAsync () { updateAsync (...args) {
return this.executor.pushAsync({ this: this, fn: this._updateAsync, arguments: arguments }) return this.executor.pushAsync({ this: this, fn: this._updateAsync, arguments: args })
} }
/** /**
@ -650,12 +650,12 @@ class Datastore extends EventEmitter {
return numRemoved return numRemoved
} }
remove () { remove (...args) {
this.executor.push({ this: this, fn: this._remove, arguments: arguments }) this.executor.push({ this: this, fn: this._remove, arguments: args })
} }
removeAsync () { removeAsync (...args) {
return this.executor.pushAsync({ this: this, fn: this._removeAsync, arguments: arguments }) return this.executor.pushAsync({ this: this, fn: this._removeAsync, arguments: args })
} }
} }

@ -71,35 +71,32 @@ class Executor {
this.ready = false this.ready = false
this.queue = new Queue(async (task, async) => { 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 the task isn't async, let's proceed with the old handler
if (!async) { if (!async) {
const lastArg = newArguments[newArguments.length - 1] const lastArg = task.arguments[task.arguments.length - 1]
await new Promise(resolve => { await new Promise(resolve => {
if (typeof lastArg === 'function') { if (typeof lastArg === 'function') {
// We got a callback // We got a callback
newArguments.pop() // remove original callback task.arguments.pop() // remove original callback
task.fn.apply(task.this, [...newArguments, function () { task.fn.apply(task.this, [...task.arguments, function () {
resolve() // triggers next task after next tick resolve() // triggers next task after next tick // TODO: check if it's at next tick or not
lastArg.apply(null, arguments) // call original callback lastArg.apply(null, arguments) // call original callback
}]) }])
} else if (!lastArg && task.arguments.length !== 0) { } else if (!lastArg && task.arguments.length !== 0) {
// We got a falsy callback // We got a falsy callback
newArguments.pop() // remove original callback task.arguments.pop() // remove original callback
task.fn.apply(task.this, [...newArguments, () => { task.fn.apply(task.this, [...task.arguments, () => {
resolve() resolve()
}]) }])
} else { } else {
// We don't have a callback // We don't have a callback
task.fn.apply(task.this, [...newArguments, () => { task.fn.apply(task.this, [...task.arguments, () => {
resolve() resolve()
}]) }])
} }
}) })
} else { } else {
return task.fn.apply(task.this, newArguments) return task.fn.apply(task.this, task.arguments)
} }
}) })
} }

Loading…
Cancel
Save