UNPKG

seventh

Version:

A lean Promises and Async lib for ES6/ES7

2,058 lines (1,477 loc) 88.8 kB
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Promise7th = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ /* Seventh Copyright (c) 2017 - 2020 Cédric Ronvel The MIT License (MIT) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ "use strict" ; const Promise = require( './seventh.js' ) ; function Queue( jobRunner , concurrency = 4 ) { this.jobRunner = jobRunner ; this.jobs = new Map() ; // all jobs this.pendingJobs = new Map() ; // only pending jobs (not run) this.runningJobs = new Map() ; // only running jobs (not done) this.errorJobs = new Map() ; // jobs that have failed this.jobsDone = new Map() ; // jobs that finished successfully this.concurrency = + concurrency || 1 ; // Internal this.isQueueRunning = true ; this.isLoopRunning = false ; this.canLoopAgain = false ; this.ready = Promise.resolved ; // Misc this.startTime = null ; // timestamp at the first time the loop is run this.endTime = null ; // timestamp at the last time the loop exited // External API, resolved when there is no jobs anymore in the queue, a new Promise is created when new element are injected this.drained = Promise.resolved ; // External API, resolved when the Queue has nothing to do: either it's drained or the pending jobs have dependencies that cannot be solved this.idle = Promise.resolved ; } Promise.Queue = Queue ; function Job( id , dependencies = null , data = undefined ) { this.id = id ; this.dependencies = dependencies === null ? null : [ ... dependencies ] ; this.data = data === undefined ? id : data ; this.error = null ; this.startTime = null ; this.endTime = null ; } Queue.Job = Job ; Queue.prototype.setConcurrency = function( concurrency ) { this.concurrency = + concurrency || 1 ; } ; Queue.prototype.stop = Queue.prototype.pause = function() { this.isQueueRunning = false ; } ; Queue.prototype.has = function( id ) { return this.jobs.has( id ) ; } ; Queue.prototype.add = Queue.prototype.addJob = function( id , data , dependencies = null ) { // Don't add it twice! if ( this.jobs.has( id ) ) { return false ; } var job = new Job( id , dependencies , data ) ; this.jobs.set( id , job ) ; this.pendingJobs.set( id , job ) ; this.canLoopAgain = true ; if ( this.isQueueRunning && ! this.isLoopRunning ) { this.run() ; } if ( this.drained.isSettled() ) { this.drained = new Promise() ; } return job ; } ; // Add a batch of jobs, with only id (data=id) and no dependencies Queue.prototype.addBatch = Queue.prototype.addJobBatch = function( ids ) { var id , job ; for ( id of ids ) { // Don't add it twice! if ( this.jobs.has( id ) ) { return false ; } job = new Job( id ) ; this.jobs.set( id , job ) ; this.pendingJobs.set( id , job ) ; } this.canLoopAgain = true ; if ( this.isQueueRunning && ! this.isLoopRunning ) { this.run() ; } if ( this.drained.isSettled() ) { this.drained = new Promise() ; } } ; Queue.prototype.run = Queue.prototype.resume = async function() { var job ; this.isQueueRunning = true ; if ( this.isLoopRunning ) { return ; } this.isLoopRunning = true ; if ( ! this.startTime ) { this.startTime = Date.now() ; } do { this.canLoopAgain = false ; for ( job of this.pendingJobs.values() ) { if ( job.dependencies && job.dependencies.some( dependencyId => ! this.jobsDone.has( dependencyId ) ) ) { continue ; } // This should be done synchronously: if ( this.idle.isSettled() ) { this.idle = new Promise() ; } this.canLoopAgain = true ; await this.ready ; // Something has stopped the queue while we were awaiting. // This check MUST be done only after "await", before is potentially synchronous, and things only change concurrently during an "await" if ( ! this.isQueueRunning ) { this.finishRun() ; return ; } this.runJob( job ) ; } } while ( this.canLoopAgain ) ; this.finishRun() ; } ; // Finish current run Queue.prototype.finishRun = function() { this.isLoopRunning = false ; if ( ! this.pendingJobs.size ) { this.drained.resolve() ; } if ( ! this.runningJobs.size ) { this.endTime = Date.now() ; this.idle.resolve() ; } } ; Queue.prototype.runJob = async function( job ) { // Immediately remove it synchronously from the pending queue and add it to the running one this.pendingJobs.delete( job.id ) ; this.runningJobs.set( job.id , job ) ; if ( this.runningJobs.size >= this.concurrency ) { this.ready = new Promise() ; } // Async part try { job.startTime = Date.now() ; await this.jobRunner( job.data ) ; job.endTime = Date.now() ; this.jobsDone.set( job.id , job ) ; this.canLoopAgain = true ; } catch ( error ) { job.endTime = Date.now() ; job.error = error ; this.errorJobs.set( job.id , job ) ; } this.runningJobs.delete( job.id ) ; if ( this.runningJobs.size < this.concurrency ) { this.ready.resolve() ; } // This MUST come last, because it retry the loop: dependencies may have been unlocked! if ( ! this.isLoopRunning ) { if ( this.isQueueRunning && this.pendingJobs.size ) { this.run() ; } else { this.finishRun() ; } } } ; Queue.prototype.getJobTimes = function() { var job , stats = {} ; for ( job of this.jobsDone.values() ) { stats[ job.id ] = job.endTime - job.startTime ; } return stats ; } ; Queue.prototype.getStats = function() { var job , sum = 0 , stats = { pending: this.pendingJobs.size , running: this.runningJobs.size , failed: this.errorJobs.size , done: this.jobsDone.size , averageJobTime: null , queueTime: null } ; if ( this.jobsDone.size ) { for ( job of this.jobsDone.values() ) { sum += job.endTime - job.startTime ; } stats.averageJobTime = sum / this.jobsDone.size ; } if ( this.endTime ) { stats.queueTime = this.endTime - this.startTime ; } return stats ; } ; },{"./seventh.js":8}],2:[function(require,module,exports){ /* Seventh Copyright (c) 2017 - 2020 Cédric Ronvel The MIT License (MIT) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ "use strict" ; const Promise = require( './seventh.js' ) ; Promise.promisifyNodeApi = ( api , suffix , multiSuffix , filter , anything ) => { var keys ; suffix = suffix || 'Async' ; multiSuffix = multiSuffix || 'AsyncAll' ; filter = filter || ( key => key[ 0 ] !== '_' && ! key.endsWith( 'Sync' ) ) ; if ( anything ) { keys = [] ; for ( let key in api ) { if ( typeof api[ key ] === 'function' ) { keys.push( key ) ; } } } else { keys = Object.keys( api ) ; } keys.filter( key => { if ( typeof api[ key ] !== 'function' ) { return false ; } // If it has any enumerable properties on its prototype, it's a constructor for ( let trash in api[ key ].prototype ) { return false ; } return filter( key , api ) ; } ) .forEach( key => { const targetKey = key + suffix ; const multiTargetKey = key + multiSuffix ; // Do nothing if it already exists if ( ! api[ targetKey ] ) { api[ targetKey ] = Promise.promisify( api[ key ] , api ) ; } if ( ! api[ multiTargetKey ] ) { api[ multiTargetKey ] = Promise.promisifyAll( api[ key ] , api ) ; } } ) ; } ; Promise.promisifyAnyNodeApi = ( api , suffix , multiSuffix , filter ) => { Promise.promisifyNodeApi( api , suffix , multiSuffix , filter , true ) ; } ; },{"./seventh.js":8}],3:[function(require,module,exports){ /* Seventh Copyright (c) 2017 - 2020 Cédric Ronvel The MIT License (MIT) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ "use strict" ; const Promise = require( './seventh.js' ) ; // This object is used as a special unique value for array hole (see Promise.filter()) const HOLE = {} ; function noop() {} Promise.all = ( iterable ) => { var index = - 1 , settled = false , count = 0 , length = Infinity , value , values = [] , allPromise = new Promise() ; for ( value of iterable ) { if ( settled ) { break ; } const promiseIndex = ++ index ; Promise.resolve( value ) .then( value_ => { if ( settled ) { return ; } values[ promiseIndex ] = value_ ; count ++ ; if ( count >= length ) { settled = true ; allPromise._resolveValue( values ) ; } } , error => { if ( settled ) { return ; } settled = true ; allPromise.reject( error ) ; } ) ; } length = index + 1 ; if ( ! length ) { allPromise._resolveValue( values ) ; } return allPromise ; } ; // Maybe faster, but can't find any reasonable grounds for that ATM //Promise.all = Promise._allArray = ( iterable ) => { var length = iterable.length ; if ( ! length ) { Promise._resolveValue( [] ) ; } var index , runtime = { settled: false , count: 0 , length: length , values: [] , allPromise: new Promise() } ; for ( index = 0 ; ! runtime.settled && index < length ; index ++ ) { Promise._allArrayOne( iterable[ index ] , index , runtime ) ; } return runtime.allPromise ; } ; // internal for allArray Promise._allArrayOne = ( value , index , runtime ) => { Promise._bareThen( value , value_ => { if ( runtime.settled ) { return ; } runtime.values[ index ] = value_ ; runtime.count ++ ; if ( runtime.count >= runtime.length ) { runtime.settled = true ; runtime.allPromise._resolveValue( runtime.values ) ; } } , error => { if ( runtime.settled ) { return ; } runtime.settled = true ; runtime.allPromise.reject( error ) ; } ) ; } ; Promise.allSettled = ( iterable ) => { var index = - 1 , settled = false , count = 0 , length = Infinity , value , values = [] , allPromise = new Promise() ; for ( value of iterable ) { if ( settled ) { break ; } const promiseIndex = ++ index ; Promise.resolve( value ) .then( value_ => { if ( settled ) { return ; } values[ promiseIndex ] = { status: 'fulfilled' , value: value_ } ; count ++ ; if ( count >= length ) { settled = true ; allPromise._resolveValue( values ) ; } } , error => { if ( settled ) { return ; } values[ promiseIndex ] = { status: 'rejected' , reason: error } ; count ++ ; if ( count >= length ) { settled = true ; allPromise._resolveValue( values ) ; } } ) ; } length = index + 1 ; if ( ! length ) { allPromise._resolveValue( values ) ; } return allPromise ; } ; // Promise.all() with an iterator Promise.every = Promise.map = ( iterable , iterator ) => { var index = - 1 , settled = false , count = 0 , length = Infinity , value , values = [] , allPromise = new Promise() ; for ( value of iterable ) { if ( settled ) { break ; } const promiseIndex = ++ index ; Promise.resolve( value ) .then( value_ => { if ( settled ) { return ; } return iterator( value_ , promiseIndex ) ; } ) .then( value_ => { if ( settled ) { return ; } values[ promiseIndex ] = value_ ; count ++ ; if ( count >= length ) { settled = true ; allPromise._resolveValue( values ) ; } } , error => { if ( settled ) { return ; } settled = true ; allPromise.reject( error ) ; } ) ; } length = index + 1 ; if ( ! length ) { allPromise._resolveValue( values ) ; } return allPromise ; } ; /* It works symmetrically with Promise.all(), the resolve and reject logic are switched. Therefore, it resolves to the first resolving promise OR reject if all promises are rejected with, as a reason an AggregateError of all promise rejection reasons. */ Promise.any = ( iterable ) => { var index = - 1 , settled = false , count = 0 , length = Infinity , value , errors = [] , anyPromise = new Promise() ; for ( value of iterable ) { if ( settled ) { break ; } const promiseIndex = ++ index ; Promise.resolve( value ) .then( value_ => { if ( settled ) { return ; } settled = true ; anyPromise._resolveValue( value_ ) ; } , error => { if ( settled ) { return ; } errors[ promiseIndex ] = error ; count ++ ; if ( count >= length ) { settled = true ; anyPromise.reject( new AggregateError( errors ) , 'Promise.any(): All promises have rejected' ) ; } } ) ; } length = index + 1 ; if ( ! length ) { anyPromise.reject( new RangeError( 'Promise.any(): empty array' ) ) ; } return anyPromise ; } ; // Like Promise.any() but with an iterator Promise.some = ( iterable , iterator ) => { var index = - 1 , settled = false , count = 0 , length = Infinity , value , errors = [] , anyPromise = new Promise() ; for ( value of iterable ) { if ( settled ) { break ; } const promiseIndex = ++ index ; Promise.resolve( value ) .then( value_ => { if ( settled ) { return ; } return iterator( value_ , promiseIndex ) ; } ) .then( value_ => { if ( settled ) { return ; } settled = true ; anyPromise._resolveValue( value_ ) ; } , error => { if ( settled ) { return ; } errors[ promiseIndex ] = error ; count ++ ; if ( count >= length ) { settled = true ; anyPromise.reject( new AggregateError( errors , 'Promise.some(): All promises have rejected' ) ) ; } } ) ; } length = index + 1 ; if ( ! length ) { anyPromise.reject( new RangeError( 'Promise.some(): empty array' ) ) ; } return anyPromise ; } ; /* More close to Array#filter(). The iterator should return truthy if the array element should be kept, or falsy if the element should be filtered out. Any rejection reject the whole promise. */ Promise.filter = ( iterable , iterator ) => { var index = - 1 , settled = false , count = 0 , length = Infinity , value , values = [] , filterPromise = new Promise() ; for ( value of iterable ) { if ( settled ) { break ; } const promiseIndex = ++ index ; Promise.resolve( value ) .then( value_ => { if ( settled ) { return ; } values[ promiseIndex ] = value_ ; return iterator( value_ , promiseIndex ) ; } ) .then( iteratorValue => { if ( settled ) { return ; } count ++ ; if ( ! iteratorValue ) { values[ promiseIndex ] = HOLE ; } if ( count >= length ) { settled = true ; values = values.filter( e => e !== HOLE ) ; filterPromise._resolveValue( values ) ; } } , error => { if ( settled ) { return ; } settled = true ; filterPromise.reject( error ) ; } ) ; } length = index + 1 ; if ( ! length ) { filterPromise._resolveValue( values ) ; } else if ( count >= length ) { settled = true ; values = values.filter( e => e !== HOLE ) ; filterPromise._resolveValue( values ) ; } return filterPromise ; } ; // forEach performs reduce as well, if a third argument is supplied // Force a function statement because we are using arguments.length, so we can support accumulator equals to undefined Promise.foreach = Promise.forEach = function( iterable , iterator , accumulator ) { var index = - 1 , isReduce = arguments.length >= 3 , it = iterable[Symbol.iterator]() , forEachPromise = new Promise() , lastPromise = Promise.resolve( accumulator ) ; // The array-like may contains promises that could be rejected before being handled if ( Promise.warnUnhandledRejection ) { Promise._handleAll( iterable ) ; } var nextElement = () => { lastPromise.then( accumulator_ => { let { value , done } = it.next() ; index ++ ; if ( done ) { forEachPromise.resolve( accumulator_ ) ; } else { lastPromise = Promise.resolve( value ).then( isReduce ? value_ => iterator( accumulator_ , value_ , index ) : value_ => iterator( value_ , index ) ) ; nextElement() ; } } , error => { forEachPromise.reject( error ) ; // We have to eat all remaining promise errors for ( ;; ) { let { value , done } = it.next() ; if ( done ) { break ; } //if ( ( value instanceof Promise ) || ( value instanceof NativePromise ) ) if ( Promise.isThenable( value ) ) { value.then( noop , noop ) ; } } } ) ; } ; nextElement() ; return forEachPromise ; } ; Promise.reduce = ( iterable , iterator , accumulator ) => { // Force 3 arguments return Promise.forEach( iterable , iterator , accumulator ) ; } ; /* Same than map, but iterate over an object and produce an object. Think of it as a kind of Object#map() (which of course does not exist). */ Promise.mapObject = ( inputObject , iterator ) => { var settled = false , count = 0 , keys = Object.keys( inputObject ) , length = keys.length , outputObject = {} , mapPromise = new Promise() ; for ( let i = 0 ; ! settled && i < length ; i ++ ) { const key = keys[ i ] ; const value = inputObject[ key ] ; Promise.resolve( value ) .then( value_ => { if ( settled ) { return ; } return iterator( value_ , key ) ; } ) .then( value_ => { if ( settled ) { return ; } outputObject[ key ] = value_ ; count ++ ; if ( count >= length ) { settled = true ; mapPromise._resolveValue( outputObject ) ; } } , error => { if ( settled ) { return ; } settled = true ; mapPromise.reject( error ) ; } ) ; } if ( ! length ) { mapPromise._resolveValue( outputObject ) ; } return mapPromise ; } ; // Like map, but with a concurrency limit Promise.concurrent = ( limit , iterable , iterator ) => { var index = - 1 , settled = false , running = 0 , count = 0 , length = Infinity , value , done = false , values = [] , it = iterable[Symbol.iterator]() , concurrentPromise = new Promise() ; // The array-like may contains promises that could be rejected before being handled if ( Promise.warnUnhandledRejection ) { Promise._handleAll( iterable ) ; } limit = + limit || 1 ; const runBatch = () => { while ( ! done && running < limit ) { //console.log( "Pre" , index ) ; ( { value , done } = it.next() ) ; if ( done ) { length = index + 1 ; if ( count >= length ) { settled = true ; concurrentPromise._resolveValue( values ) ; return ; } break ; } if ( settled ) { break ; } const promiseIndex = ++ index ; running ++ ; //console.log( "Launch" , promiseIndex ) ; Promise.resolve( value ) .then( value_ => { if ( settled ) { return ; } return iterator( value_ , promiseIndex ) ; } ) .then( value_ => { //console.log( "Done" , promiseIndex , value_ ) ; if ( settled ) { return ; } values[ promiseIndex ] = value_ ; count ++ ; running -- ; //console.log( "count/length" , count , length ) ; if ( count >= length ) { settled = true ; concurrentPromise._resolveValue( values ) ; return ; } if ( running < limit ) { runBatch() ; return ; } } , error => { if ( settled ) { return ; } settled = true ; concurrentPromise.reject( error ) ; } ) ; } } ; runBatch() ; if ( index < 0 ) { concurrentPromise._resolveValue( values ) ; } return concurrentPromise ; } ; /* Like native Promise.race(), it is hanging forever if the array is empty. It resolves or rejects to the first resolved/rejected promise. */ Promise.race = ( iterable ) => { var settled = false , value , racePromise = new Promise() ; for ( value of iterable ) { if ( settled ) { break ; } Promise.resolve( value ) .then( value_ => { if ( settled ) { return ; } settled = true ; racePromise._resolveValue( value_ ) ; } , error => { if ( settled ) { return ; } settled = true ; racePromise.reject( error ) ; } ) ; } return racePromise ; } ; },{"./seventh.js":8}],4:[function(require,module,exports){ (function (process,global,setImmediate){(function (){ /* Seventh Copyright (c) 2017 - 2020 Cédric Ronvel The MIT License (MIT) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ "use strict" ; /* Prerequisite. */ const NativePromise = global.Promise ; // Cross-platform next tick function var nextTick ; if ( ! process.browser ) { nextTick = process.nextTick ; } else { // Browsers suck, they don't have setImmediate() except IE/Edge. // A module is needed to emulate it. require( 'setimmediate' ) ; nextTick = setImmediate ; } /* Constructor. */ function Promise( fn ) { this.fn = fn ; this._then = Promise._dormantThen ; this.value = null ; this.thenHandlers = null ; this.handledRejection = null ; if ( this.fn ) { this._exec() ; } } module.exports = Promise ; Promise.Native = NativePromise ; Promise.warnUnhandledRejection = true ; Promise.nextTick = nextTick ; Promise.prototype._exec = function() { this._then = Promise._pendingThen ; try { this.fn( // Don't return anything, it would create nasty bugs! E.g.: // bad: error_ => this.reject( error_ ) // good: error_ => { this.reject( error_ ) ; } result_ => { this.resolve( result_ ) ; } , error_ => { this.reject( error_ ) ; } ) ; } catch ( error ) { this.reject( error ) ; } } ; /* Resolve/reject and then-handlers management. */ Promise.prototype.resolve = Promise.prototype.fulfill = function( value ) { // Throw an error? if ( this._then.settled ) { return this ; } if ( Promise.isThenable( value ) ) { this._execThenPromise( value ) ; return this ; } return this._resolveValue( value ) ; } ; Promise.prototype._resolveValue = function( value ) { this._then = Promise._fulfilledThen ; this.value = value ; if ( this.thenHandlers && this.thenHandlers.length ) { this._execFulfillHandlers() ; } return this ; } ; // Faster on node v8.x Promise.prototype._execThenPromise = function( thenPromise ) { try { thenPromise.then( result_ => { this.resolve( result_ ) ; } , error_ => { this.reject( error_ ) ; } ) ; } catch ( error ) { this.reject( error ) ; } } ; Promise.prototype.reject = function( error ) { // Throw an error? if ( this._then.settled ) { return this ; } this._then = Promise._rejectedThen ; this.value = error ; if ( this.thenHandlers && this.thenHandlers.length ) { this._execRejectionHandlers() ; } else if ( Promise.warnUnhandledRejection && ! this.handledRejection ) { this._unhandledRejection() ; } return this ; } ; Promise.prototype._execFulfillHandlers = function() { var i , length = this.thenHandlers.length ; // Do cache the length, if a handler is synchronously added, it will be called on next tick for ( i = 0 ; i < length ; i += 3 ) { if ( this.thenHandlers[ i + 1 ] ) { this._execOneFulfillHandler( this.thenHandlers[ i ] , this.thenHandlers[ i + 1 ] ) ; } else { this.thenHandlers[ i ].resolve( this.value ) ; } } } ; // Faster on node v8.x? //* Promise.prototype._execOneFulfillHandler = function( promise , onFulfill ) { try { promise.resolve( onFulfill( this.value ) ) ; } catch ( error_ ) { promise.reject( error_ ) ; } } ; //*/ Promise.prototype._execRejectionHandlers = function() { var i , length = this.thenHandlers.length ; // Do cache the length, if a handler is synchronously added, it will be called on next tick for ( i = 0 ; i < length ; i += 3 ) { if ( this.thenHandlers[ i + 2 ] ) { this._execOneRejectHandler( this.thenHandlers[ i ] , this.thenHandlers[ i + 2 ] ) ; } else { this.thenHandlers[ i ].reject( this.value ) ; } } } ; // Faster on node v8.x? //* Promise.prototype._execOneRejectHandler = function( promise , onReject ) { try { promise.resolve( onReject( this.value ) ) ; } catch ( error_ ) { promise.reject( error_ ) ; } } ; //*/ Promise.prototype.resolveTimeout = Promise.prototype.fulfillTimeout = function( time , result ) { setTimeout( () => this.resolve( result ) , time ) ; } ; Promise.prototype.rejectTimeout = function( time , error ) { setTimeout( () => this.reject( error ) , time ) ; } ; Promise.prototype.resolveNextTick = Promise.prototype.fulfillNextTick = function( result ) { nextTick( () => this.resolve( result ) ) ; } ; Promise.prototype.rejectNextTick = function( error ) { nextTick( () => this.reject( error ) ) ; } ; /* .then() variants depending on the state */ // .then() variant when the promise is dormant Promise._dormantThen = function( onFulfill , onReject ) { if ( this.fn ) { // If this is a dormant promise, wake it up now! this._exec() ; // Return now, some sync stuff can change the status return this._then( onFulfill , onReject ) ; } var promise = new Promise() ; if ( ! this.thenHandlers ) { this.thenHandlers = [ promise , onFulfill , onReject ] ; } else { //this.thenHandlers.push( onFulfill ) ; this.thenHandlers[ this.thenHandlers.length ] = promise ; this.thenHandlers[ this.thenHandlers.length ] = onFulfill ; this.thenHandlers[ this.thenHandlers.length ] = onReject ; } return promise ; } ; Promise._dormantThen.settled = false ; // .then() variant when the promise is pending Promise._pendingThen = function( onFulfill , onReject ) { var promise = new Promise() ; if ( ! this.thenHandlers ) { this.thenHandlers = [ promise , onFulfill , onReject ] ; } else { //this.thenHandlers.push( onFulfill ) ; this.thenHandlers[ this.thenHandlers.length ] = promise ; this.thenHandlers[ this.thenHandlers.length ] = onFulfill ; this.thenHandlers[ this.thenHandlers.length ] = onReject ; } return promise ; } ; Promise._pendingThen.settled = false ; // .then() variant when the promise is fulfilled Promise._fulfilledThen = function( onFulfill ) { if ( ! onFulfill ) { return this ; } var promise = new Promise() ; // This handler should not fire in this code sync flow nextTick( () => { try { promise.resolve( onFulfill( this.value ) ) ; } catch ( error ) { promise.reject( error ) ; } } ) ; return promise ; } ; Promise._fulfilledThen.settled = true ; // .then() variant when the promise is rejected Promise._rejectedThen = function( onFulfill , onReject ) { if ( ! onReject ) { return this ; } this.handledRejection = true ; var promise = new Promise() ; // This handler should not fire in this code sync flow nextTick( () => { try { promise.resolve( onReject( this.value ) ) ; } catch ( error ) { promise.reject( error ) ; } } ) ; return promise ; } ; Promise._rejectedThen.settled = true ; /* .then() and short-hands. */ Promise.prototype.then = function( onFulfill , onReject ) { return this._then( onFulfill , onReject ) ; } ; Promise.prototype.catch = function( onReject = () => undefined ) { return this._then( undefined , onReject ) ; } ; Promise.prototype.finally = function( onSettled ) { return this._then( onSettled , onSettled ) ; } ; Promise.prototype.tap = Promise.prototype.tapThen = function( onFulfill ) { this._then( onFulfill , undefined ) ; return this ; } ; Promise.prototype.tapCatch = function( onReject ) { this._then( undefined , onReject ) ; return this ; } ; Promise.prototype.tapFinally = function( onSettled ) { this._then( onSettled , onSettled ) ; return this ; } ; // Any unhandled error throw ASAP Promise.prototype.fatal = function() { this._then( undefined , error => { // Throw async, otherwise it would be catched by .then() nextTick( () => { throw error ; } ) ; } ) ; } ; Promise.prototype.done = function( onFulfill , onReject ) { this._then( onFulfill , onReject ).fatal() ; return this ; } ; Promise.prototype.callback = function( cb ) { this._then( value => { cb( undefined , value ) ; } , error => { cb( error ) ; } ).fatal() ; return this ; } ; Promise.prototype.callbackAll = function( cb ) { this._then( values => { if ( Array.isArray( values ) ) { cb( undefined , ... values ) ; } else { cb( undefined , values ) ; } } , error => { cb( error ) ; } ).fatal() ; return this ; } ; /* The reverse of .callback(), it calls the function with a callback argument and return a promise that resolve or reject depending on that callback invocation. Usage: await Promise.callback( callback => myFunctionRelyingOnCallback( [arg1] , [arg2] , [...] , callback ) ; */ Promise.callback = function( fn ) { return new Promise( ( resolve , reject ) => { fn( ( error , arg ) => { if ( error ) { reject( error ) ; } else { resolve( arg ) ; } } ) ; } ) ; } ; Promise.callbackAll = function( fn ) { return new Promise( ( resolve , reject ) => { fn( ( error , ... args ) => { if ( error ) { reject( error ) ; } else { resolve( args ) ; } } ) ; } ) ; } ; Promise.prototype.toPromise = // <-- DEPRECATED, use .propagate Promise.prototype.propagate = function( promise ) { this._then( value => { promise.resolve( value ) ; } , error => { promise.reject( error ) ; } ) ; return this ; } ; /* Foreign promises facilities */ Promise.propagate = function( foreignPromise , promise ) { foreignPromise.then( value => { promise.resolve( value ) ; } , error => { promise.reject( error ) ; } ) ; return foreignPromise ; } ; Promise.finally = function( foreignPromise , onSettled ) { return foreignPromise.then( onSettled , onSettled ) ; } ; /* Static factories. */ Promise.resolve = Promise.fulfill = function( value ) { if ( Promise.isThenable( value ) ) { return Promise.fromThenable( value ) ; } return Promise._resolveValue( value ) ; } ; Promise._resolveValue = function( value ) { var promise = new Promise() ; promise._then = Promise._fulfilledThen ; promise.value = value ; return promise ; } ; Promise.reject = function( error ) { //return new Promise().reject( error ) ; var promise = new Promise() ; promise._then = Promise._rejectedThen ; promise.value = error ; return promise ; } ; Promise.prototype.resolveTimeout = Promise.prototype.fulfillTimeout = function( timeout , value ) { setTimeout( () => this.resolve( value ) , timeout ) ; } ; Promise.resolveTimeout = Promise.fulfillTimeout = function( timeout , value ) { return new Promise( resolve => setTimeout( () => resolve( value ) , timeout ) ) ; } ; Promise.prototype.rejectTimeout = function( timeout , error ) { setTimeout( () => this.reject( error ) , timeout ) ; } ; Promise.rejectTimeout = function( timeout , error ) { return new Promise( ( resolve , reject ) => setTimeout( () => reject( error ) , timeout ) ) ; } ; Promise.resolveNextTick = Promise.fulfillNextTick = function( value ) { return new Promise( resolve => nextTick( () => resolve( value ) ) ) ; } ; Promise.rejectNextTick = function( error ) { return new Promise( ( resolve , reject ) => nextTick( () => reject( error ) ) ) ; } ; // A dormant promise is activated the first time a then handler is assigned Promise.dormant = function( fn ) { var promise = new Promise() ; promise.fn = fn ; return promise ; } ; // Try-catched Promise.resolve( fn() ) Promise.try = function( fn , ... args ) { try { return Promise.resolve( fn( ... args ) ) ; } catch ( error ) { return Promise.reject( error ) ; } } ; /* Thenables. */ Promise.isThenable = function( value ) { return value && typeof value === 'object' && typeof value.then === 'function' ; } ; // We assume a thenable object here Promise.fromThenable = function( thenable ) { if ( thenable instanceof Promise ) { return thenable ; } return new Promise( ( resolve , reject ) => { thenable.then( value => { resolve( value ) ; } , error => { reject( error ) ; } ) ; } ) ; } ; // When you just want a fast then() function out of anything, without any desync and unchainable Promise._bareThen = function( value , onFulfill , onReject ) { //if ( Promise.isThenable( value ) ) if( value && typeof value === 'object' ) { if ( value instanceof Promise ) { if ( value._then === Promise._fulfilledThen ) { onFulfill( value.value ) ; } else if ( value._then === Promise._rejectedThen ) { onReject( value.value ) ; } else { value._then( onFulfill , onReject ) ; } } else if ( typeof value.then === 'function' ) { value.then( onFulfill , onReject ) ; } else { onFulfill( value ) ; } } else { onFulfill( value ) ; } } ; /* Misc. */ // Internal usage, mark all promises as handled ahead of time, useful for series, // because a warning would be displayed for unhandled rejection for promises that are not yet processed. Promise._handleAll = function( iterable ) { var value ; for ( value of iterable ) { //if ( ( value instanceof Promise ) || ( value instanceof NativePromise ) ) if ( Promise.isThenable( value ) ) { value.handledRejection = true ; } } } ; Promise.prototype._unhandledRejection = function() { // This promise is currently unhandled // If still unhandled at the end of the synchronous block of code, // output an error message. this.handledRejection = false ; // Don't know what is the correct way to inform node.js about that. // There is no doc about that, and emitting unhandledRejection, // does not produce what is expected. //process.emit( 'unhandledRejection' , this.value , this ) ; /* nextTick( () => { if ( this.handledRejection === false ) { process.emit( 'unhandledRejection' , this.value , this ) ; } } ) ; */ // It looks like 'await' inside a 'try-catch' does not handle the promise soon enough -_-' //const nextTick_ = nextTick ; const nextTick_ = cb => setTimeout( cb , 0 ) ; //* if ( this.value instanceof Error ) { nextTick_( () => { if ( this.handledRejection === false ) { this.value.message = 'Unhandled promise rejection: ' + this.value.message ; console.error( this.value ) ; } } ) ; } else { // Avoid starting the stack trace in the nextTick()... let error_ = new Error( 'Unhandled promise rejection' ) ; nextTick_( () => { if ( this.handledRejection === false ) { console.error( error_ ) ; console.error( 'Rejection reason:' , this.value ) ; } } ) ; } //*/ } ; Promise.prototype.isSettled = function() { return this._then.settled ; } ; Promise.prototype.getStatus = function() { switch ( this._then ) { case Promise._dormantThen : return 'dormant' ; case Promise._pendingThen : return 'pending' ; case Promise._fulfilledThen : return 'fulfilled' ; case Promise._rejectedThen : return 'rejected' ; } } ; Promise.prototype.inspect = function() { switch ( this._then ) { case Promise._dormantThen : return 'Promise { <DORMANT> }' ; case Promise._pendingThen : return 'Promise { <PENDING> }' ; case Promise._fulfilledThen : return 'Promise { <FULFILLED> ' + this.value + ' }' ; case Promise._rejectedThen : return 'Promise { <REJECTED> ' + this.value + ' }' ; } } ; // A shared dummy promise, when you just want to return an immediately thenable Promise.resolved = Promise.dummy = Promise.resolve() ; /* Vanilla Promise compatibility. */ // This method merely achieves what this lib is doing right from the begining: // allowing one to pass a promise and process it elsewhere. Promise.withResolvers = () => { var promise = new Promise() ; return { promise , resolve: promise.resolve.bind( promise ) , reject: promise.reject.bind( promise ) } ; } ; /* Browser specific. */ if ( process.browser ) { Promise.prototype.resolveAtAnimationFrame = function( value ) { window.requestAnimationFrame( () => this.resolve( value ) ) ; } ; Promise.prototype.rejectAtAnimationFrame = function( error ) { window.requestAnimationFrame( () => this.reject( error ) ) ; } ; Promise.resolveAtAnimationFrame = function( value ) { return new Promise( resolve => window.requestAnimationFrame( () => resolve( value ) ) ) ; } ; Promise.rejectAtAnimationFrame = function( error ) { return new Promise( ( resolve , reject ) => window.requestAnimationFrame( () => reject( error ) ) ) ; } ; } }).call(this)}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {},require("timers").setImmediate) },{"_process":11,"setimmediate":10,"timers":12}],5:[function(require,module,exports){ /* Seventh Copyright (c) 2017 - 2020 Cédric Ronvel The MIT License (MIT) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ "use strict" ; const Promise = require( './seventh.js' ) ; const noop = () => undefined ; Promise.promisifyAll = ( nodeAsyncFn , thisBinding ) => { // Little optimization here to have a promisified function as fast as possible if ( thisBinding ) { return ( ... args ) => { return new Promise( ( resolve , reject ) => { nodeAsyncFn.call( thisBinding , ... args , ( error , ... cbArgs ) => { if ( error ) { if ( cbArgs.length && error instanceof Error ) { error.args = cbArgs ; } reject( error ) ; } else { resolve( cbArgs ) ; } } ) ; } ) ; } ; } return function( ... args ) { return new Promise( ( resolve , reject ) => { nodeAsyncFn.call( this , ... args , ( error , ... cbArgs ) => { if ( error ) { if ( cbArgs.length && error instanceof Error ) { error.args = cbArgs ; } reject( error ) ; } else { resolve( cbArgs ) ; } } ) ; } ) ; } ; } ; // Same than .promisifyAll() but only return the callback args #1 instead of an array of args from #1 to #n Promise.promisify = ( nodeAsyncFn , thisBinding ) => { // Little optimization here to have a promisified function as fast as possible if ( thisBinding ) { return ( ... args ) => { return new Promise( ( resolve , reject ) => { nodeAsyncFn.call( thisBinding , ... args , ( error , cbArg ) => { if ( error ) { if ( cbArg !== undefined && error instanceof Error ) { error.arg = cbArg ; } reject( error ) ; } else { resolve( cbArg ) ; } } ) ; } ) ; } ; } return function( ... args ) { return new Promise( ( resolve , reject ) => { nodeAsyncFn.call( this , ... args , ( error , cbArg ) => { if ( error ) { if ( cbArg !== undefined && error instanceof Error ) { error.arg = cbArg ; } reject( error ) ; } else { resolve( cbArg ) ; } } ) ; } ) ; } ; } ; /* Intercept each decoratee resolve/reject. */ Promise.interceptor = ( asyncFn , interceptor , errorInterceptor , thisBinding ) => { if ( typeof errorInterceptor !== 'function' ) { thisBinding = errorInterceptor ; errorInterceptor = noop ; } return function( ... args ) { var localThis = thisBinding || this , maybePromise = asyncFn.call( localThis , ... args ) ; Promise.resolve( maybePromise ).then( value => interceptor.call( localThis , value ) , error => errorInterceptor.call( localThis , error ) ) ; return maybePromise ; } ; } ; /* Run only once, return always the same promise. */ Promise.once = ( asyncFn , thisBinding ) => { var functionInstance = null , // instance when not called as an object's method but a regular function ('this' is undefined) instanceMap = new WeakMap() ; const getInstance = ( localThis ) => { var instance = localThis ? instanceMap.get( localThis ) : functionInstance ; if ( instance ) { return instance ; } instance = { triggered: false , result: undefined } ; if ( localThis ) { instanceMap.set( localThis , instance ) ; } else { functionInstance = instance ; } return instance ; } ; return function( ... args ) { var localThis = thisBinding || this , instance = getInstance( localThis ) ; if ( ! instance.triggered ) { instance.triggered = true ; instance.result = asyncFn.call( localThis , ... args ) ; } return instance.result ; } ; } ; /* The decoratee execution does not overlap, multiple calls are serialized. */ Promise.serialize = ( asyncFn , thisBinding ) => { var functionInstance = null , // instance when not called as an object's method but a regular function ('this' is undefined) instanceMap = new WeakMap() ; const getInstance = ( localThis ) => { var instance = localThis ? instanceMap.get( localThis ) : functionInstance ; if ( instance ) { return instance ; } instance = { lastPromise: Promise.resolve() } ; if ( localThis ) { instanceMap.set( localThis , instance ) ; } else { functionInstance = instance ; } return instance ; } ; return function( ... args ) { var localThis = thisBinding || this , instance = getInstance( localThis ) , promise = new Promise() ; instance.lastPromise.finally( () => { Promise.propagate( asyncFn.call( localThis , ... args ) , promise ) ; } ) ; instance.lastPromise = promise ; return promise ; } ; } ; /* It does nothing if the decoratee is still in progress, but it returns the promise of the action in progress. */ Promise.debounce = ( asyncFn , thisBinding ) => { var functionInstance = null , // instance when not called as an object's method but a regular function ('this' is undefined) instanceMap = new WeakMap() ; const getInstance = ( localThis ) => { var instance = localThis ? instanceMap.get( localThis ) : functionInstance ; if ( instance ) { return instance ; } instance = { inProgress: null } ; if ( localThis ) { instanceMap.set( localThis , instance ) ; } else { functionInstance = instance ; } return instance ; } ; return function( ... args ) { var localThis = thisBinding || this , instance = getInstance( localThis ) ; if ( instance.inProgress ) { return instance.inProgress ; } let inProgress = instance.inProgress = asyncFn.call( localThis , ... args ) ; Promise.finally( inProgress , () => instance.inProgress = null ) ; return inProgress ; } ; } ; /* Like .debounce(), but subsequent call continue to return the last promise for some extra time after it resolved. */ Promise.debounceDelay = ( delay , asyncFn , thisBinding ) => { var functionInstance = null , // instance when not called as an object's method but a regular function ('this' is undefined) instanceMap = new WeakMap() ; const getInstance = ( localThis ) => { var instance = localThis ? instanceMap.get( localThis ) : functionInstance ; if ( instance ) { return instance ; } instance = { inProgress: null } ; if ( localThis ) { instanceMap.set( localThis , instance ) ; } else { functionInstance = instance ; } return instance ; } ; return function( ... args ) { var localThis = thisBinding || this , instance = getInstance( localThis ) ; if ( instance.inProgress ) { return instance.inProgress ; } let inProgress = instance.inProgress = asyncFn.call( localThis , ... args ) ; Promise.finally( inProgress , () => setTimeout( () => instance.inProgress = null , delay ) ) ; return inProgress ; } ; } ; /* debounceNextTick( [asyncFn|syncFn] , thisBinding ) => { It does nothing until the next tick. The decoratee is called only once with the arguments of the last decorator call. The function argument can be sync or async. The use case is can be some niche case of .update()/.refresh()/.redraw() functions. */ Promise.debounceNextTick = ( asyncFn , thisBinding ) => { var inWrapper = null , outWrapper = null , waitFn = null , functionInstance = null , // instance when not called as an object's method but a regular function ('this' is undefined) instanceMap = new WeakMap() ; const getInstance = ( localThis ) => { var instance = localThis ? instanceMap.get( localThis ) : functionInstance ; if ( instance ) { return instance ; } instance = { inProgress: null , waitingNextTick: false , currentUpdateWith: null , currentUpdatePromise: null , nextUpdateWith: null , nextUpdatePromise: null } ; if ( localThis ) { instanceMap.set( localThis , instance ) ; } else { functionInstance = instance ; } return instance ; } ; const nextUpdate = function() { var instance = getInstance( this ) ; instance.inProgress = instance.currentUpdatePromise = null ; if ( instance.nextUpdateWith ) { let args = instance.nextUpdateWith ; instance.nextUpdateWith = null ; let sharedPromise = instance.nextUpdatePromise ; instance.nextUpdatePromise = null ; instance.inProgress = inWrapper.call( this , args ) ; // Forward the result to the pending promise Promise.propagate( instance.inProgress , sharedPromise ) ; } } ; inWrapper = function( args )