diff options
Diffstat (limited to 'node_modules/release-zalgo/lib')
-rw-r--r-- | node_modules/release-zalgo/lib/Async.js | 21 | ||||
-rw-r--r-- | node_modules/release-zalgo/lib/Sync.js | 24 | ||||
-rw-r--r-- | node_modules/release-zalgo/lib/Thenable.js | 39 | ||||
-rw-r--r-- | node_modules/release-zalgo/lib/constants.js | 6 | ||||
-rw-r--r-- | node_modules/release-zalgo/lib/unwrapSync.js | 54 |
5 files changed, 144 insertions, 0 deletions
diff --git a/node_modules/release-zalgo/lib/Async.js b/node_modules/release-zalgo/lib/Async.js new file mode 100644 index 000000000..846f7add0 --- /dev/null +++ b/node_modules/release-zalgo/lib/Async.js @@ -0,0 +1,21 @@ +'use strict' + +class Async { + run (executors) { + const args = Array.from(arguments).slice(1) + return new Promise(resolve => resolve(executors.async.apply(null, args))) + } + + all (arr) { + return Promise.all(arr) + } + + returns (value) { + return Promise.resolve(value) + } + + throws (reason) { + return Promise.reject(reason) + } +} +module.exports = Async diff --git a/node_modules/release-zalgo/lib/Sync.js b/node_modules/release-zalgo/lib/Sync.js new file mode 100644 index 000000000..d081d5f59 --- /dev/null +++ b/node_modules/release-zalgo/lib/Sync.js @@ -0,0 +1,24 @@ +'use strict' + +const Thenable = require('./Thenable') +const unwrapSync = require('./unwrapSync') + +class Sync { + run (executors) { + const args = Array.from(arguments).slice(1) + return new Thenable(() => executors.sync.apply(null, args)) + } + + all (arr) { + return new Thenable(() => arr.map(value => unwrapSync(value))) + } + + returns (value) { + return new Thenable(() => value) + } + + throws (reason) { + return new Thenable(() => { throw reason }) + } +} +module.exports = Sync diff --git a/node_modules/release-zalgo/lib/Thenable.js b/node_modules/release-zalgo/lib/Thenable.js new file mode 100644 index 000000000..7a809b266 --- /dev/null +++ b/node_modules/release-zalgo/lib/Thenable.js @@ -0,0 +1,39 @@ +'use strict' + +const constants = require('./constants') +const unwrapSync = require('./unwrapSync') + +// Behaves like a Promise, though the then() and catch() methods are unbound and +// must be called with the instance as their thisArg. +// +// The executor must either return a value or throw a rejection reason. It is +// not provided resolver or rejecter methods. The executor may return another +// thenable. +class Thenable { + constructor (executor) { + try { + this.result = unwrapSync(executor()) + this.state = constants.RESOLVED + } catch (err) { + this.result = err + this.state = constants.REJECTED + } + } + + then (onFulfilled, onRejected) { + if (this.state === constants.RESOLVED && typeof onFulfilled === 'function') { + return new Thenable(() => onFulfilled(this.result)) + } + + if (this.state === constants.REJECTED && typeof onRejected === 'function') { + return new Thenable(() => onRejected(this.result)) + } + + return this + } + + catch (onRejected) { + return this.then(null, onRejected) + } +} +module.exports = Thenable diff --git a/node_modules/release-zalgo/lib/constants.js b/node_modules/release-zalgo/lib/constants.js new file mode 100644 index 000000000..8c417b5fa --- /dev/null +++ b/node_modules/release-zalgo/lib/constants.js @@ -0,0 +1,6 @@ +'use strict' + +exports.PENDING = Symbol('PENDING') +exports.RESOLVED = Symbol('RESOLVED') +exports.REJECTED = Symbol('REJECTED') +exports.ASYNC = Symbol('ASYNC') diff --git a/node_modules/release-zalgo/lib/unwrapSync.js b/node_modules/release-zalgo/lib/unwrapSync.js new file mode 100644 index 000000000..baa130b18 --- /dev/null +++ b/node_modules/release-zalgo/lib/unwrapSync.js @@ -0,0 +1,54 @@ +'use strict' + +const ExtendableError = require('es6-error') + +const constants = require('./constants') + +class UnwrapError extends ExtendableError { + constructor (thenable) { + super('Could not unwrap asynchronous thenable') + this.thenable = thenable + } +} + +// Logic is derived from the Promise Resolution Procedure, as described in the +// Promises/A+ specification: https://promisesaplus.com/#point-45 +// +// Note that there is no cycle detection. +function unwrapSync (x) { + if (!x || typeof x !== 'object' && typeof x !== 'function') { + return x + } + + const then = x.then + if (typeof then !== 'function') return x + + let state = constants.PENDING + let value + const unwrapValue = y => { + if (state === constants.PENDING) { + state = constants.RESOLVED + value = y + } + } + const unwrapReason = r => { + if (state === constants.PENDING) { + state = constants.REJECTED + value = r + } + } + then.call(x, unwrapValue, unwrapReason) + + if (state === constants.PENDING) { + state = constants.ASYNC + throw new UnwrapError(x) + } + + if (state === constants.RESOLVED) { + return unwrapSync(value) + } + + // state === REJECTED + throw value +} +module.exports = unwrapSync |