Last active
March 25, 2019 22:26
-
-
Save vinicius73/450a90f10b3dcceca2e187ca1c957b43 to your computer and use it in GitHub Desktop.
inspired by https://github.com/yosbelms/idley
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { PromisePipeFactory } from './p-pipe' | |
import { isFunction, isArray } from 'lodash' | |
const w = window | |
/** | |
* @type Function | |
* @param {Function} | |
* @returns {void} | |
*/ | |
const subscribe = (() => { | |
if (isFunction(w.requestIdleCallback)) { | |
return w.requestIdleCallback.bind(w) | |
} | |
if (isFunction(w.requestAnimationFrame)) { | |
return w.requestAnimationFrame.bind(w) | |
} | |
return function _setTimeout (fn) { | |
return setTimeout(fn, 1) | |
} | |
})() | |
/** | |
* @param {Function} fn | |
* @returns {Promise<any>} | |
*/ | |
const onIdle = fn => { | |
return new Promise(resolve => { | |
subscribe(() => { | |
resolve(fn()) | |
}) | |
}) | |
} | |
class IdlePromise extends Promise { | |
then (fn) { | |
return super.then((...args) => { | |
return onIdle(() => { | |
return fn(...args) | |
}) | |
}) | |
} | |
} | |
const pipeP = PromisePipeFactory(IdlePromise) | |
/** | |
* @param {Array<Function>} fns | |
* @returns {Promise<Array<any>>} | |
*/ | |
const serialOnIdle = async (...fns) => { | |
if (isArray(fns[0])) { | |
return serialOnIdle(...fns[0]) | |
} | |
return pipeP(...fns)() | |
} | |
export { onIdle, subscribe, serialOnIdle, IdlePromise, pipeP } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/JasonPollman/p-compose | |
const promiseReducer = (current, next) => current.then(next) | |
const hasNonFunction = fnArray => fnArray.some(fn => typeof fn !== 'function') | |
const PromisePipeFactory = Promise => (...fns) => { | |
if (fns.length === 0) return () => Promise.resolve() | |
if (hasNonFunction(fns)) throw new TypeError('Expected a function') | |
const head = fns[0] | |
const rest = fns.slice(1) | |
return (...args) => rest.reduce(promiseReducer, Promise.resolve().then(() => head(...args))) | |
} | |
export { PromisePipeFactory } | |
export default PromisePipeFactory(Promise) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { serialOnIdle } from './utils' | |
const install = Vue => { | |
Object.defineProperty(Vue.prototype, '$onIdle', { | |
value: serialOnIdle | |
}) | |
} | |
export { install } | |
export default { install } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment