Skip to content
This repository has been archived by the owner on Dec 19, 2017. It is now read-only.

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyWebb committed Dec 8, 2016
1 parent 14c4e7e commit 02d9b3b
Showing 1 changed file with 25 additions and 27 deletions.
52 changes: 25 additions & 27 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@ export function isArray(arr) {
return typeof arr.splice === 'function'
}

export function isFunction(x) {
return typeof x === 'function'
}

export function isGenerator(x) {
return x.constructor.name === 'GeneratorFunction'
}

export function isPlainObject(x) {
return _isPlainObject(x)
}
Expand All @@ -27,12 +19,8 @@ export function isUndefined(x) {
export function runMiddleware(callbacks, ...args) {
const downstream = []

callbacks = callbacks.map((_fn) => {
const fn = isGenerator(_fn)
? _fn
: generatorify(_fn)

const runner = fn(...args)
callbacks = callbacks.map((fn) => {
const runner = generatorify(fn)(...args)
const run = async () => {
let ret = runner.next()
ret = isThenable(ret)
Expand All @@ -58,25 +46,35 @@ export async function sequence(callbacks, ...args) {
}
}

function generatorify(fn) {
return async function * (...args) {
const ret = await promisify(fn)(...args)

if (isPlainObject(ret)) {
yield await promisify(ret.beforeRender)()
yield await promisify(ret.afterRender)()
yield await promisify(ret.beforeDispose)()
yield await promisify(ret.afterDispose)()
} else {
yield ret
}
}
function isFunction(x) {
return typeof x === 'function'
}

function isGenerator(x) {
return x.constructor.name === 'GeneratorFunction'
}

function isThenable(x) {
return !isUndefined(x) && isFunction(x.then)
}

function generatorify(fn) {
return isGenerator(fn)
? fn
: async function * (...args) {
const ret = await promisify(fn)(...args)

if (isPlainObject(ret)) {
yield await promisify(ret.beforeRender)()
yield await promisify(ret.afterRender)()
yield await promisify(ret.beforeDispose)()
yield await promisify(ret.afterDispose)()
} else {
yield ret
}
}
}

function promisify(_fn = () => {}) {
return async (...args) => {
const fn = () =>
Expand Down

0 comments on commit 02d9b3b

Please sign in to comment.