simplify executor

pull/11/head
Timothée Rebours 3 years ago
parent 42a7d45856
commit 06dc91915c
  1. 2
      lib/cursor.js
  2. 10
      lib/datastore.js
  3. 117
      lib/executor.js

@ -172,7 +172,7 @@ class Cursor {
} }
execAsync (...args) { execAsync (...args) {
return this.db.executor.pushAsync({ this: this, fn: this._execAsync, arguments: args }) return this.db.executor.pushAsync(() => this._execAsync(...args))
} }
then (onFulfilled, onRejected) { then (onFulfilled, onRejected) {

@ -90,11 +90,7 @@ class Datastore extends EventEmitter {
} }
loadDatabaseAsync (...args) { loadDatabaseAsync (...args) {
return this.executor.pushAsync({ return this.executor.pushAsync(() => this.persistence.loadDatabaseAsync(args), true)
this: this.persistence,
fn: this.persistence.loadDatabaseAsync,
arguments: args
}, true)
} }
/** /**
@ -607,7 +603,7 @@ class Datastore extends EventEmitter {
} }
updateAsync (...args) { updateAsync (...args) {
return this.executor.pushAsync({ this: this, fn: this._updateAsync, arguments: args }) return this.executor.pushAsync(() => this._updateAsync(args))
} }
/** /**
@ -655,7 +651,7 @@ class Datastore extends EventEmitter {
} }
removeAsync (...args) { removeAsync (...args) {
return this.executor.pushAsync({ this: this, fn: this._removeAsync, arguments: args }) return this.executor.pushAsync(() => this._removeAsync(args))
} }
} }

@ -1,78 +1,55 @@
/** /**
* Responsible for sequentially executing actions on the database * Responsible for sequentially executing actions on the database
*/ */
class Waterfall {
class Queue { constructor () {
constructor (execute) { this._guardian = Promise.resolve()
this.execute = execute
this.tasks = new Map()
this.buffer = new Map()
this.running = false
this.drainPromise = Promise.resolve()
} }
async executeNextTask (force = false) { get guardian () {
if (!this.tasks.size) { return this._guardian
this.running = false
return
} else if (this.running && !force) return
this.running = true
const [task, { resolve, reject, async }] = this.tasks[Symbol.iterator]().next().value
this.tasks.delete(task)
try {
resolve(await this.execute(task, async))
} catch (err) {
reject(err)
}
this.drainPromise = this.executeNextTask(true)
} }
_push (task, async, map, run = false) { waterfall (func) {
let _resolve, _reject return (...args) => {
const promise = new Promise((resolve, reject) => { this._guardian = this.guardian.then(() => {
_reject = reject return func(...args)
_resolve = resolve .then(result => ({ error: false, result }), result => ({ error: true, result }))
})
return this.guardian.then(({ error, result }) => {
if (error) return Promise.reject(result)
else return Promise.resolve(result)
}) })
map.set(task, { async: async, resolve: _resolve, reject: _reject })
if (run && !this.running) this.drainPromise = this.executeNextTask()
return promise
}
push (task) {
this._push(task, false, this.tasks, true).then(() => {}, () => {}) // to avoid having unhandledRejection
}
pushAsync (task) {
return this._push(task, true, this.tasks, true)
}
addToBuffer (task) {
this._push(task, false, this.buffer, false).then(() => {}, () => {}) // to avoid having unhandledRejection
}
addToBufferAsync (task) {
return this._push(task, true, this.buffer, false)
} }
processBuffer () {
this.tasks = new Map([...this.tasks, ...this.buffer])
this.buffer = new Map()
this.drainPromise = this.executeNextTask()
} }
async drain () { chain (promise) {
return this.drainPromise return this.waterfall(() => promise)()
} }
} }
class Executor { class Executor {
constructor () { constructor () {
this.ready = false this.ready = false
this._mainWaterfallObject = new Waterfall()
this._bufferWaterfallObject = new Waterfall()
this._bufferWaterfallObject.chain(new Promise(resolve => {
this._resolveBuffer = resolve
}))
}
this.queue = new Queue(async (task, async) => { /**
// If the task isn't async, let's proceed with the old handler * If executor is ready, queue task (and process it immediately if executor was idle)
if (!async) { * If not, buffer task for later processing
* @param {Object} task
* task.this - Object to use as this
* task.fn - Function to execute
* task.arguments - Array of arguments, IMPORTANT: only the last argument may be a function (the callback)
* and the last argument cannot be false/undefined/null
* @param {Boolean} forceQueuing Optional (defaults to false) force executor to queue task even if it is not ready
*/
push (task, forceQueuing) {
const func = async () => {
const lastArg = task.arguments[task.arguments.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') {
@ -95,30 +72,13 @@ class Executor {
}]) }])
} }
}) })
} else {
return task.fn.apply(task.this, task.arguments)
}
})
} }
this.pushAsync(func, forceQueuing)
/**
* If executor is ready, queue task (and process it immediately if executor was idle)
* If not, buffer task for later processing
* @param {Object} task
* task.this - Object to use as this
* task.fn - Function to execute
* task.arguments - Array of arguments, IMPORTANT: only the last argument may be a function (the callback)
* and the last argument cannot be false/undefined/null
* @param {Boolean} forceQueuing Optional (defaults to false) force executor to queue task even if it is not ready
*/
push (task, forceQueuing) {
if (this.ready || forceQueuing) this.queue.push(task)
else this.queue.addToBuffer(task)
} }
pushAsync (task, forceQueuing) { pushAsync (task, forceQueuing) {
if (this.ready || forceQueuing) return this.queue.pushAsync(task) if (this.ready || forceQueuing) return this._mainWaterfallObject.waterfall(task)()
else return this.queue.addToBufferAsync(task) else return this._bufferWaterfallObject.waterfall(task)()
} }
/** /**
@ -127,7 +87,8 @@ class Executor {
*/ */
processBuffer () { processBuffer () {
this.ready = true this.ready = true
this.queue.processBuffer() this._resolveBuffer()
this._mainWaterfallObject.waterfall(() => this._bufferWaterfallObject.guardian)
} }
} }

Loading…
Cancel
Save