Skip to content

Commit

Permalink
perf: tune up queue callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Jul 1, 2020
1 parent b363217 commit b68abe3
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/main/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,28 @@ export const isPromiseLike = (target: any): boolean =>
&& typeof target.then === 'function'
&& typeof target.catch === 'function'

export const invoke = (fn: IAsyncFn, task: ITask, next: any): void => {
export const compose = (cb: Function, next: Function) => <V>(v: V): void => {
cb(v)
next()
}

export const invoke = (fn: IAsyncFn, task: ITask, next: Function): void => {
const {iop, args} = task
const resolve = compose(iop.resolve, next)
const reject = compose(iop.reject, next)

try {
const res = fn(...args)

if (isPromiseLike(res)) {
res
.then(v => {
iop.resolve(v)
next()

})
.catch(v => {
iop.reject(v)
next()
})
res.then(resolve, reject)
}
else {
iop.resolve(res)
next()
resolve(res)
}

}
catch (e) {
iop.reject(e)
next()
reject(e)
}
}

Expand Down

0 comments on commit b68abe3

Please sign in to comment.