UNPKG

@fine-js/channels

Version:

Bits of Clojure's `core.async` ported to JS

36 lines (29 loc) 982 B
'use strict' const valueError = (val) => new TypeError(`Can not put \`${val}\` onto channel`) const badval = (val) => val === null || val === undefined const none = Symbol('none') const once = (fn, multithrow = false) => { let cache = none return (...args) => { if (cache === none) cache = fn(...args) else if (multithrow === true) throw new Error(`${fn.name} called twice`) return cache } } // In node, we'd like to use setImmediate, but since browsers do not support it, // we have to go with `setTimeout` of 0 milliseconds. We access `setImmediate` // like we do, so that Browserify does not include its `timers` module. const schedule = ((browser) => { return browser ? (fn, ...args) => void setTimeout(fn, 0, ...args) : global['set' + 'Immediate'] // eslint-disable-line no-useless-concat })(typeof window === 'object') module.exports = { privates: Symbol('channels/private-apis'), badval, valueError, once, schedule, }