@merchise/action-queue
Advanced tools
+43
-22
@@ -72,3 +72,2 @@ class ActionQueue { | ||
| * @param {function} fn The action to perform | ||
| * @param {boolean} force | ||
| * @param {...any} extra Extra arguments to pass to callbacks | ||
@@ -153,2 +152,18 @@ * | ||
| /** | ||
| * Replaces the entire queue with the given action. Without canceling | ||
| * running actions. | ||
| * | ||
| * @param {function} fn The action to perform | ||
| * @param {...any} extra Extra arguments to pass to callbacks | ||
| */ | ||
| replace_pending(fn, ...extra) { | ||
| let pending = this._queue.concat(); | ||
| this._queue.splice(0, this._queue.length); | ||
| while (pending.length > 0) { | ||
| let action = pending.shift(); | ||
| this._cancel_action(action); | ||
| } | ||
| this.append(fn, ...extra); | ||
| } | ||
| /** | ||
| * Clear the entire queue. Cancel pending and running actions. | ||
@@ -227,11 +242,17 @@ */ | ||
| * The result is an object with two properties: 'running' and 'pending'. | ||
| * Each is an array of objects the a single property `args`; which is an | ||
| * array (possibly empty) with the extra arguments passed to `append`, | ||
| * `prepend` or `replace`. | ||
| * Each is an array of objects the properties: | ||
| * | ||
| * - `args`; which is an array (possibly empty) with the extra arguments | ||
| * passed to `append`, `prepend` or `replace`. | ||
| * | ||
| * - `cancel`; a function that allows to cancel this particular action | ||
| * | ||
| * - `promise`; the promise attached to this action (undefined if | ||
| * `createPromises` is false). | ||
| * | ||
| */ | ||
| info() { | ||
| let map = function(d) { | ||
| let { promise, extra } = d; | ||
| return { args: extra, cancel: d.cancel, promise: d.external_promise }; | ||
| let { extra, cancel, external_promise } = d; | ||
| return { args: extra, cancel, promise: external_promise }; | ||
| }; | ||
@@ -268,3 +289,3 @@ const workers = Object.values(this._workers); | ||
| let running = this._queue.shift(); | ||
| let { fn, connectors, extra, cancelled } = running; | ||
| let { fn, connectors, cancelled } = running; | ||
| if (cancelled) { | ||
@@ -288,3 +309,3 @@ this._run(); | ||
| } | ||
| let extra2 = running.extra || []; | ||
| let extra = running.extra || []; | ||
| if (self._rolling !== null) { | ||
@@ -294,3 +315,3 @@ let rolling_resolve = self._rolling.resolve; | ||
| try { | ||
| rolling_resolve.apply(self, result.concat(extra2)); | ||
| rolling_resolve.apply(self, result.concat(extra)); | ||
| } catch (e) { | ||
@@ -304,3 +325,3 @@ console.error(e); | ||
| try { | ||
| fn2.apply(self, result.concat(extra2)); | ||
| fn2.apply(self, result.concat(extra)); | ||
| } catch (e) { | ||
@@ -312,3 +333,3 @@ console.error(e); | ||
| try { | ||
| fn2.apply(self, result.concat(extra2)); | ||
| fn2.apply(self, result.concat(extra)); | ||
| } catch (e) { | ||
@@ -329,3 +350,3 @@ console.error(e); | ||
| } | ||
| let extra2 = running.extra || []; | ||
| let extra = running.extra || []; | ||
| if (self._rolling !== null) { | ||
@@ -335,3 +356,3 @@ let rolling_reject = self._rolling.reject; | ||
| try { | ||
| rolling_reject.apply(self, result.concat(extra2)); | ||
| rolling_reject.apply(self, result.concat(extra)); | ||
| } catch (e) { | ||
@@ -345,3 +366,3 @@ console.error(e); | ||
| try { | ||
| fn2.apply(self, result.concat(extra2)); | ||
| fn2.apply(self, result.concat(extra)); | ||
| } catch (e) { | ||
@@ -353,3 +374,3 @@ console.error(e); | ||
| try { | ||
| fn2.apply(self, result.concat(extra2)); | ||
| fn2.apply(self, result.concat(extra)); | ||
| } catch (e) { | ||
@@ -395,9 +416,2 @@ console.error(e); | ||
| action.cancelled = true; | ||
| if (this._options.rejectCanceled) { | ||
| try { | ||
| action.connectors.reject(new Error("Action was cancelled")); | ||
| } catch (e) { | ||
| console.error(e); | ||
| } | ||
| } | ||
| let extra = action.extra; | ||
@@ -412,2 +426,9 @@ let self = this; | ||
| }); | ||
| if (this._options.rejectCanceled) { | ||
| try { | ||
| action.connectors.reject(new Error("Action was cancelled")); | ||
| } catch (e) { | ||
| console.error(e); | ||
| } | ||
| } | ||
| this._finallys.forEach(function(fn) { | ||
@@ -414,0 +435,0 @@ try { |
+109
-88
@@ -1,2 +0,2 @@ | ||
| class p { | ||
| class u { | ||
| constructor(e) { | ||
@@ -58,3 +58,2 @@ typeof e < "u" ? this._options = { | ||
| * @param {function} fn The action to perform | ||
| * @param {boolean} force | ||
| * @param {...any} extra Extra arguments to pass to callbacks | ||
@@ -71,4 +70,4 @@ * | ||
| prepend(e, ...t) { | ||
| let r = this._build_action(e, t); | ||
| return this._queue.splice(0, 0, r), this._run(), r.external_promise; | ||
| let n = this._build_action(e, t); | ||
| return this._queue.splice(0, 0, n), this._run(), n.external_promise; | ||
| } | ||
@@ -90,24 +89,24 @@ /** | ||
| append(e, ...t) { | ||
| let r = this._build_action(e, t); | ||
| return this._queue.push(r), this._run(), r.external_promise; | ||
| let n = this._build_action(e, t); | ||
| return this._queue.push(n), this._run(), n.external_promise; | ||
| } | ||
| _build_action(e, t) { | ||
| let r = { resolve: () => { | ||
| let n = { resolve: () => { | ||
| }, reject: () => { | ||
| } }, s; | ||
| this._options.createPromises ? s = new Promise(function(h, _) { | ||
| r.resolve = h, r.reject = _; | ||
| }) : s = void 0; | ||
| let o = { | ||
| } }, l; | ||
| this._options.createPromises ? l = new Promise(function(h, r) { | ||
| n.resolve = h, n.reject = r; | ||
| }) : l = void 0; | ||
| let c = { | ||
| fn: e, | ||
| connectors: r, | ||
| connectors: n, | ||
| extra: t, | ||
| external_promise: s, | ||
| external_promise: l, | ||
| inner_promise: void 0, | ||
| cancelled: !1, | ||
| cancel: () => { | ||
| this._cancel_action(o); | ||
| this._cancel_action(c); | ||
| } | ||
| }; | ||
| return o; | ||
| return c; | ||
| } | ||
@@ -130,2 +129,17 @@ /** | ||
| /** | ||
| * Replaces the entire queue with the given action. Without canceling | ||
| * running actions. | ||
| * | ||
| * @param {function} fn The action to perform | ||
| * @param {...any} extra Extra arguments to pass to callbacks | ||
| */ | ||
| replace_pending(e, ...t) { | ||
| let n = this._queue.concat(); | ||
| for (this._queue.splice(0, this._queue.length); n.length > 0; ) { | ||
| let l = n.shift(); | ||
| this._cancel_action(l); | ||
| } | ||
| this.append(e, ...t); | ||
| } | ||
| /** | ||
| * Clear the entire queue. Cancel pending and running actions. | ||
@@ -199,16 +213,22 @@ */ | ||
| * The result is an object with two properties: 'running' and 'pending'. | ||
| * Each is an array of objects the a single property `args`; which is an | ||
| * array (possibly empty) with the extra arguments passed to `append`, | ||
| * `prepend` or `replace`. | ||
| * Each is an array of objects the properties: | ||
| * | ||
| * - `args`; which is an array (possibly empty) with the extra arguments | ||
| * passed to `append`, `prepend` or `replace`. | ||
| * | ||
| * - `cancel`; a function that allows to cancel this particular action | ||
| * | ||
| * - `promise`; the promise attached to this action (undefined if | ||
| * `createPromises` is false). | ||
| * | ||
| */ | ||
| info() { | ||
| let e = function(s) { | ||
| let { promise: o, extra: h } = s; | ||
| return { args: h, cancel: s.cancel, promise: s.external_promise }; | ||
| let e = function(l) { | ||
| let { extra: c, cancel: h, external_promise: r } = l; | ||
| return { args: c, cancel: h, promise: r }; | ||
| }; | ||
| const t = Object.values(this._workers), r = [].concat(this._queue); | ||
| const t = Object.values(this._workers), n = [].concat(this._queue); | ||
| return { | ||
| running: t.map(e), | ||
| pending: r.map(e) | ||
| pending: n.map(e) | ||
| }; | ||
@@ -222,4 +242,4 @@ } | ||
| reject: null | ||
| }, e._rolling.promise = new Promise(function(t, r) { | ||
| e._rolling.resolve = t, e._rolling.reject = r; | ||
| }, e._rolling.promise = new Promise(function(t, n) { | ||
| e._rolling.resolve = t, e._rolling.reject = n; | ||
| }); | ||
@@ -237,75 +257,75 @@ } | ||
| if (!this.paused() && this._idle.size > 0 && this._queue.length > 0) { | ||
| let e = this._queue.shift(), { fn: t, connectors: r, extra: s, cancelled: o } = e; | ||
| if (o) { | ||
| let e = this._queue.shift(), { fn: t, connectors: n, cancelled: l } = e; | ||
| if (l) { | ||
| this._run(); | ||
| return; | ||
| } | ||
| let h = t(); | ||
| e.inner_promise = h; | ||
| let _ = this._acquire(e), n = this; | ||
| h.then(function(...l) { | ||
| n._release(_); | ||
| let c = t(); | ||
| e.inner_promise = c; | ||
| let h = this._acquire(e), r = this; | ||
| c.then(function(...s) { | ||
| r._release(h); | ||
| try { | ||
| r.resolve(...l); | ||
| n.resolve(...s); | ||
| } catch (i) { | ||
| console.error(i); | ||
| } | ||
| l.length == 1 && typeof l[0] > "u" && (l = []); | ||
| s.length == 1 && typeof s[0] > "u" && (s = []); | ||
| let a = e.extra || []; | ||
| if (n._rolling !== null) { | ||
| let i = n._rolling.resolve; | ||
| if (r._rolling !== null) { | ||
| let i = r._rolling.resolve; | ||
| if (typeof i < "u" && i !== null) | ||
| try { | ||
| i.apply(n, l.concat(a)); | ||
| } catch (c) { | ||
| console.error(c); | ||
| i.apply(r, s.concat(a)); | ||
| } catch (o) { | ||
| console.error(o); | ||
| } | ||
| n._rolling = null; | ||
| r._rolling = null; | ||
| } | ||
| n._thens.forEach(function(i) { | ||
| r._thens.forEach(function(i) { | ||
| try { | ||
| i.apply(n, l.concat(a)); | ||
| } catch (c) { | ||
| console.error(c); | ||
| i.apply(r, s.concat(a)); | ||
| } catch (o) { | ||
| console.error(o); | ||
| } | ||
| }), n._finallys.forEach(function(i) { | ||
| }), r._finallys.forEach(function(i) { | ||
| try { | ||
| i.apply(n, l.concat(a)); | ||
| } catch (c) { | ||
| console.error(c); | ||
| i.apply(r, s.concat(a)); | ||
| } catch (o) { | ||
| console.error(o); | ||
| } | ||
| }), n._run(); | ||
| }).catch(function(...l) { | ||
| n._release(_); | ||
| }), r._run(); | ||
| }).catch(function(...s) { | ||
| r._release(h); | ||
| try { | ||
| r.reject(...l); | ||
| n.reject(...s); | ||
| } catch (i) { | ||
| console.error(i); | ||
| } | ||
| l.length == 1 && typeof l[0] > "u" && (l = []); | ||
| s.length == 1 && typeof s[0] > "u" && (s = []); | ||
| let a = e.extra || []; | ||
| if (n._rolling !== null) { | ||
| let i = n._rolling.reject; | ||
| if (r._rolling !== null) { | ||
| let i = r._rolling.reject; | ||
| if (typeof i < "u" && i !== null) | ||
| try { | ||
| i.apply(n, l.concat(a)); | ||
| } catch (c) { | ||
| console.error(c); | ||
| i.apply(r, s.concat(a)); | ||
| } catch (o) { | ||
| console.error(o); | ||
| } | ||
| n._rolling = null; | ||
| r._rolling = null; | ||
| } | ||
| n._catchs.forEach(function(i) { | ||
| r._catchs.forEach(function(i) { | ||
| try { | ||
| i.apply(n, l.concat(a)); | ||
| } catch (c) { | ||
| console.error(c); | ||
| i.apply(r, s.concat(a)); | ||
| } catch (o) { | ||
| console.error(o); | ||
| } | ||
| }), n._finallys.forEach(function(i) { | ||
| }), r._finallys.forEach(function(i) { | ||
| try { | ||
| i.apply(n, l.concat(a)); | ||
| } catch (c) { | ||
| console.error(c); | ||
| i.apply(r, s.concat(a)); | ||
| } catch (o) { | ||
| console.error(o); | ||
| } | ||
| }), n._run(); | ||
| }), n._run(); | ||
| }), r._run(); | ||
| }), r._run(); | ||
| } | ||
@@ -326,4 +346,4 @@ } | ||
| typeof t.cancel == "function" ? t.cancel() : typeof t.abort == "function" && t.abort(); | ||
| } catch (r) { | ||
| console.error(r); | ||
| } catch (n) { | ||
| console.error(n); | ||
| } | ||
@@ -338,20 +358,21 @@ this._cancel_action(e); | ||
| _cancel_action(e) { | ||
| if (e.cancelled = !0, this._options.rejectCanceled) | ||
| e.cancelled = !0; | ||
| let t = e.extra, n = this; | ||
| if (this._cancels.forEach(function(l) { | ||
| try { | ||
| e.connectors.reject(new Error("Action was cancelled")); | ||
| } catch (s) { | ||
| console.error(s); | ||
| l.apply(n, t); | ||
| } catch (c) { | ||
| console.error(c); | ||
| } | ||
| let t = e.extra, r = this; | ||
| this._cancels.forEach(function(s) { | ||
| }), this._options.rejectCanceled) | ||
| try { | ||
| s.apply(r, t); | ||
| } catch (o) { | ||
| console.error(o); | ||
| e.connectors.reject(new Error("Action was cancelled")); | ||
| } catch (l) { | ||
| console.error(l); | ||
| } | ||
| }), this._finallys.forEach(function(s) { | ||
| this._finallys.forEach(function(l) { | ||
| try { | ||
| s.apply(r, t); | ||
| } catch (o) { | ||
| console.error(o); | ||
| l.apply(n, t); | ||
| } catch (c) { | ||
| console.error(c); | ||
| } | ||
@@ -361,4 +382,4 @@ }); | ||
| _acquire(e) { | ||
| let r = this._idle.values().next().value; | ||
| return this._idle.delete(r), this._workers[r] = e, r; | ||
| let n = this._idle.values().next().value; | ||
| return this._idle.delete(n), this._workers[n] = e, n; | ||
| } | ||
@@ -370,3 +391,3 @@ _release(e) { | ||
| export { | ||
| p as ActionQueue | ||
| u as ActionQueue | ||
| }; |
+43
-22
@@ -76,3 +76,2 @@ (function(global, factory) { | ||
| * @param {function} fn The action to perform | ||
| * @param {boolean} force | ||
| * @param {...any} extra Extra arguments to pass to callbacks | ||
@@ -157,2 +156,18 @@ * | ||
| /** | ||
| * Replaces the entire queue with the given action. Without canceling | ||
| * running actions. | ||
| * | ||
| * @param {function} fn The action to perform | ||
| * @param {...any} extra Extra arguments to pass to callbacks | ||
| */ | ||
| replace_pending(fn, ...extra) { | ||
| let pending = this._queue.concat(); | ||
| this._queue.splice(0, this._queue.length); | ||
| while (pending.length > 0) { | ||
| let action = pending.shift(); | ||
| this._cancel_action(action); | ||
| } | ||
| this.append(fn, ...extra); | ||
| } | ||
| /** | ||
| * Clear the entire queue. Cancel pending and running actions. | ||
@@ -231,11 +246,17 @@ */ | ||
| * The result is an object with two properties: 'running' and 'pending'. | ||
| * Each is an array of objects the a single property `args`; which is an | ||
| * array (possibly empty) with the extra arguments passed to `append`, | ||
| * `prepend` or `replace`. | ||
| * Each is an array of objects the properties: | ||
| * | ||
| * - `args`; which is an array (possibly empty) with the extra arguments | ||
| * passed to `append`, `prepend` or `replace`. | ||
| * | ||
| * - `cancel`; a function that allows to cancel this particular action | ||
| * | ||
| * - `promise`; the promise attached to this action (undefined if | ||
| * `createPromises` is false). | ||
| * | ||
| */ | ||
| info() { | ||
| let map = function(d) { | ||
| let { promise, extra } = d; | ||
| return { args: extra, cancel: d.cancel, promise: d.external_promise }; | ||
| let { extra, cancel, external_promise } = d; | ||
| return { args: extra, cancel, promise: external_promise }; | ||
| }; | ||
@@ -272,3 +293,3 @@ const workers = Object.values(this._workers); | ||
| let running = this._queue.shift(); | ||
| let { fn, connectors, extra, cancelled } = running; | ||
| let { fn, connectors, cancelled } = running; | ||
| if (cancelled) { | ||
@@ -292,3 +313,3 @@ this._run(); | ||
| } | ||
| let extra2 = running.extra || []; | ||
| let extra = running.extra || []; | ||
| if (self2._rolling !== null) { | ||
@@ -298,3 +319,3 @@ let rolling_resolve = self2._rolling.resolve; | ||
| try { | ||
| rolling_resolve.apply(self2, result.concat(extra2)); | ||
| rolling_resolve.apply(self2, result.concat(extra)); | ||
| } catch (e) { | ||
@@ -308,3 +329,3 @@ console.error(e); | ||
| try { | ||
| fn2.apply(self2, result.concat(extra2)); | ||
| fn2.apply(self2, result.concat(extra)); | ||
| } catch (e) { | ||
@@ -316,3 +337,3 @@ console.error(e); | ||
| try { | ||
| fn2.apply(self2, result.concat(extra2)); | ||
| fn2.apply(self2, result.concat(extra)); | ||
| } catch (e) { | ||
@@ -333,3 +354,3 @@ console.error(e); | ||
| } | ||
| let extra2 = running.extra || []; | ||
| let extra = running.extra || []; | ||
| if (self2._rolling !== null) { | ||
@@ -339,3 +360,3 @@ let rolling_reject = self2._rolling.reject; | ||
| try { | ||
| rolling_reject.apply(self2, result.concat(extra2)); | ||
| rolling_reject.apply(self2, result.concat(extra)); | ||
| } catch (e) { | ||
@@ -349,3 +370,3 @@ console.error(e); | ||
| try { | ||
| fn2.apply(self2, result.concat(extra2)); | ||
| fn2.apply(self2, result.concat(extra)); | ||
| } catch (e) { | ||
@@ -357,3 +378,3 @@ console.error(e); | ||
| try { | ||
| fn2.apply(self2, result.concat(extra2)); | ||
| fn2.apply(self2, result.concat(extra)); | ||
| } catch (e) { | ||
@@ -399,9 +420,2 @@ console.error(e); | ||
| action.cancelled = true; | ||
| if (this._options.rejectCanceled) { | ||
| try { | ||
| action.connectors.reject(new Error("Action was cancelled")); | ||
| } catch (e) { | ||
| console.error(e); | ||
| } | ||
| } | ||
| let extra = action.extra; | ||
@@ -416,2 +430,9 @@ let self2 = this; | ||
| }); | ||
| if (this._options.rejectCanceled) { | ||
| try { | ||
| action.connectors.reject(new Error("Action was cancelled")); | ||
| } catch (e) { | ||
| console.error(e); | ||
| } | ||
| } | ||
| this._finallys.forEach(function(fn) { | ||
@@ -418,0 +439,0 @@ try { |
@@ -1,1 +0,1 @@ | ||
| (function(u,_){typeof exports=="object"&&typeof module<"u"?_(exports):typeof define=="function"&&define.amd?define(["exports"],_):(u=typeof globalThis<"u"?globalThis:u||self,_(u["@merchise/action-queue"]={}))})(this,function(u){"use strict";class _{constructor(e){typeof e<"u"?this._options={createPromises:typeof e.createPromises=="boolean"?e.createPromises:!0,rejectCanceled:typeof e.rejectCanceled=="boolean"?e.rejectCanceled:!0,workers:typeof e.workers=="number"&&e.workers>1?e.workers:1}:this._options={createPromises:!0,workers:1,rejectCanceled:!0},this._thens=[],this._catchs=[],this._finallys=[],this._cancels=[],this._paused=!1,this._queue=[],this._rolling=null,this._workers={},this._idle=new Set,[...Array(this._options.workers).keys()].map(t=>this._idle.add(t))}then(e){this._thens.push(e)}catch(e){this._catchs.push(e)}finally(e){this._finallys.push(e)}oncancel(e){this._cancels.push(e)}prepend(e,...t){let n=this._build_action(e,t);return this._queue.splice(0,0,n),this._run(),n.external_promise}append(e,...t){let n=this._build_action(e,t);return this._queue.push(n),this._run(),n.external_promise}_build_action(e,t){let n={resolve:()=>{},reject:()=>{}},l;this._options.createPromises?l=new Promise(function(h,f){n.resolve=h,n.reject=f}):l=void 0;let c={fn:e,connectors:n,extra:t,external_promise:l,inner_promise:void 0,cancelled:!1,cancel:()=>{this._cancel_action(c)}};return c}replace(e,...t){return this.clear(),this.append(e,...t)}clear(){let e=this._queue.concat();for(this._queue.splice(0,this._queue.length),this._cancel_running();e.length>0;){let t=e.shift();this._cancel_action(t)}}length(){return this._queue.length+this.running()}running(){return this._options.workers-this._idle.size}busy(){return this.length()>0}promise(){return this._rolling===null&&this._setup_rolling_promise(),this._rolling.promise}paused(){return this._paused}pause(){this._paused=!0}resume(){this._paused=!1,this._run()}info(){let e=function(l){let{promise:c,extra:h}=l;return{args:h,cancel:l.cancel,promise:l.external_promise}};const t=Object.values(this._workers),n=[].concat(this._queue);return{running:t.map(e),pending:n.map(e)}}_setup_rolling_promise(){let e=this;e._rolling={promise:null,resolve:null,reject:null},e._rolling.promise=new Promise(function(t,n){e._rolling.resolve=t,e._rolling.reject=n})}_run(){if(!this.paused()&&this._idle.size>0&&this._queue.length>0){let e=this._queue.shift(),{fn:t,connectors:n,extra:l,cancelled:c}=e;if(c){this._run();return}let h=t();e.inner_promise=h;let f=this._acquire(e),r=this;h.then(function(...s){r._release(f);try{n.resolve(...s)}catch(i){console.error(i)}s.length==1&&typeof s[0]>"u"&&(s=[]);let a=e.extra||[];if(r._rolling!==null){let i=r._rolling.resolve;if(typeof i<"u"&&i!==null)try{i.apply(r,s.concat(a))}catch(o){console.error(o)}r._rolling=null}r._thens.forEach(function(i){try{i.apply(r,s.concat(a))}catch(o){console.error(o)}}),r._finallys.forEach(function(i){try{i.apply(r,s.concat(a))}catch(o){console.error(o)}}),r._run()}).catch(function(...s){r._release(f);try{n.reject(...s)}catch(i){console.error(i)}s.length==1&&typeof s[0]>"u"&&(s=[]);let a=e.extra||[];if(r._rolling!==null){let i=r._rolling.reject;if(typeof i<"u"&&i!==null)try{i.apply(r,s.concat(a))}catch(o){console.error(o)}r._rolling=null}r._catchs.forEach(function(i){try{i.apply(r,s.concat(a))}catch(o){console.error(o)}}),r._finallys.forEach(function(i){try{i.apply(r,s.concat(a))}catch(o){console.error(o)}}),r._run()}),r._run()}}_cancel_running(){for(const e of Object.values(this._workers)){let t=e.inner_promise;try{typeof t.cancel=="function"?t.cancel():typeof t.abort=="function"&&t.abort()}catch(n){console.error(n)}this._cancel_action(e)}this._workers={},this._idle=new Set,[...Array(this._options.workers).keys()].map(e=>this._idle.add(e))}_cancel_action(e){if(e.cancelled=!0,this._options.rejectCanceled)try{e.connectors.reject(new Error("Action was cancelled"))}catch(l){console.error(l)}let t=e.extra,n=this;this._cancels.forEach(function(l){try{l.apply(n,t)}catch(c){console.error(c)}}),this._finallys.forEach(function(l){try{l.apply(n,t)}catch(c){console.error(c)}})}_acquire(e){let n=this._idle.values().next().value;return this._idle.delete(n),this._workers[n]=e,n}_release(e){delete this._workers[e],this._idle.add(e)}}u.ActionQueue=_,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})}); | ||
| (function(h,_){typeof exports=="object"&&typeof module<"u"?_(exports):typeof define=="function"&&define.amd?define(["exports"],_):(h=typeof globalThis<"u"?globalThis:h||self,_(h["@merchise/action-queue"]={}))})(this,function(h){"use strict";class _{constructor(e){typeof e<"u"?this._options={createPromises:typeof e.createPromises=="boolean"?e.createPromises:!0,rejectCanceled:typeof e.rejectCanceled=="boolean"?e.rejectCanceled:!0,workers:typeof e.workers=="number"&&e.workers>1?e.workers:1}:this._options={createPromises:!0,workers:1,rejectCanceled:!0},this._thens=[],this._catchs=[],this._finallys=[],this._cancels=[],this._paused=!1,this._queue=[],this._rolling=null,this._workers={},this._idle=new Set,[...Array(this._options.workers).keys()].map(t=>this._idle.add(t))}then(e){this._thens.push(e)}catch(e){this._catchs.push(e)}finally(e){this._finallys.push(e)}oncancel(e){this._cancels.push(e)}prepend(e,...t){let n=this._build_action(e,t);return this._queue.splice(0,0,n),this._run(),n.external_promise}append(e,...t){let n=this._build_action(e,t);return this._queue.push(n),this._run(),n.external_promise}_build_action(e,t){let n={resolve:()=>{},reject:()=>{}},s;this._options.createPromises?s=new Promise(function(u,r){n.resolve=u,n.reject=r}):s=void 0;let c={fn:e,connectors:n,extra:t,external_promise:s,inner_promise:void 0,cancelled:!1,cancel:()=>{this._cancel_action(c)}};return c}replace(e,...t){return this.clear(),this.append(e,...t)}replace_pending(e,...t){let n=this._queue.concat();for(this._queue.splice(0,this._queue.length);n.length>0;){let s=n.shift();this._cancel_action(s)}this.append(e,...t)}clear(){let e=this._queue.concat();for(this._queue.splice(0,this._queue.length),this._cancel_running();e.length>0;){let t=e.shift();this._cancel_action(t)}}length(){return this._queue.length+this.running()}running(){return this._options.workers-this._idle.size}busy(){return this.length()>0}promise(){return this._rolling===null&&this._setup_rolling_promise(),this._rolling.promise}paused(){return this._paused}pause(){this._paused=!0}resume(){this._paused=!1,this._run()}info(){let e=function(s){let{extra:c,cancel:u,external_promise:r}=s;return{args:c,cancel:u,promise:r}};const t=Object.values(this._workers),n=[].concat(this._queue);return{running:t.map(e),pending:n.map(e)}}_setup_rolling_promise(){let e=this;e._rolling={promise:null,resolve:null,reject:null},e._rolling.promise=new Promise(function(t,n){e._rolling.resolve=t,e._rolling.reject=n})}_run(){if(!this.paused()&&this._idle.size>0&&this._queue.length>0){let e=this._queue.shift(),{fn:t,connectors:n,cancelled:s}=e;if(s){this._run();return}let c=t();e.inner_promise=c;let u=this._acquire(e),r=this;c.then(function(...l){r._release(u);try{n.resolve(...l)}catch(i){console.error(i)}l.length==1&&typeof l[0]>"u"&&(l=[]);let a=e.extra||[];if(r._rolling!==null){let i=r._rolling.resolve;if(typeof i<"u"&&i!==null)try{i.apply(r,l.concat(a))}catch(o){console.error(o)}r._rolling=null}r._thens.forEach(function(i){try{i.apply(r,l.concat(a))}catch(o){console.error(o)}}),r._finallys.forEach(function(i){try{i.apply(r,l.concat(a))}catch(o){console.error(o)}}),r._run()}).catch(function(...l){r._release(u);try{n.reject(...l)}catch(i){console.error(i)}l.length==1&&typeof l[0]>"u"&&(l=[]);let a=e.extra||[];if(r._rolling!==null){let i=r._rolling.reject;if(typeof i<"u"&&i!==null)try{i.apply(r,l.concat(a))}catch(o){console.error(o)}r._rolling=null}r._catchs.forEach(function(i){try{i.apply(r,l.concat(a))}catch(o){console.error(o)}}),r._finallys.forEach(function(i){try{i.apply(r,l.concat(a))}catch(o){console.error(o)}}),r._run()}),r._run()}}_cancel_running(){for(const e of Object.values(this._workers)){let t=e.inner_promise;try{typeof t.cancel=="function"?t.cancel():typeof t.abort=="function"&&t.abort()}catch(n){console.error(n)}this._cancel_action(e)}this._workers={},this._idle=new Set,[...Array(this._options.workers).keys()].map(e=>this._idle.add(e))}_cancel_action(e){e.cancelled=!0;let t=e.extra,n=this;if(this._cancels.forEach(function(s){try{s.apply(n,t)}catch(c){console.error(c)}}),this._options.rejectCanceled)try{e.connectors.reject(new Error("Action was cancelled"))}catch(s){console.error(s)}this._finallys.forEach(function(s){try{s.apply(n,t)}catch(c){console.error(c)}})}_acquire(e){let n=this._idle.values().next().value;return this._idle.delete(n),this._workers[n]=e,n}_release(e){delete this._workers[e],this._idle.add(e)}}h.ActionQueue=_,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})}); |
+1
-1
| { | ||
| "name": "@merchise/action-queue", | ||
| "version": "4.3.1", | ||
| "version": "4.4.0", | ||
| "description": "A coordinated queue of actions", | ||
@@ -5,0 +5,0 @@ "files": [ |
+43
-1
@@ -63,2 +63,7 @@ # A coordinated queue of actions | ||
| ### `replace_pending(fn, ...args)` | ||
| Replaces the entire queue with the given action without canceling running | ||
| actions. | ||
| ### `clear()` | ||
@@ -68,3 +73,2 @@ | ||
| ### `then(callback)` | ||
@@ -124,1 +128,39 @@ | ||
| different promises. | ||
| ### `info()` | ||
| Return an object with the running and pending jobs in the queue. | ||
| The result is an object with two properties: `running` and `pending`. Each | ||
| contains an array of objects the properties: | ||
| - `args`; which is an array (possibly empty) with the extra arguments passed | ||
| to `append`, `prepend` or `replace`. | ||
| - `cancel`; a function that allows to cancel this particular action | ||
| - `promise`; the promise attached to this action (undefined if | ||
| `createPromises` is false). | ||
| ### `busy()` | ||
| Return True if the queue is busy, either running or has actions waiting. This | ||
| might True even if the queue is paused. | ||
| ### `pause()` | ||
| Pause the queue. This has no effect on the currently running action (if any); | ||
| if we was running it will finish. But no new actions are going to be | ||
| executed. | ||
| If the queue is already paused, do nothing. | ||
| ### `resume()` | ||
| Resume the queue. If the queue is not paused, do nothing. | ||
| ### `paused()` | ||
| Return True if the queue is paused. |
+40
-19
@@ -85,3 +85,2 @@ export class ActionQueue { | ||
| * @param {function} fn The action to perform | ||
| * @param {boolean} force | ||
| * @param {...any} extra Extra arguments to pass to callbacks | ||
@@ -170,2 +169,19 @@ * | ||
| /** | ||
| * Replaces the entire queue with the given action. Without canceling | ||
| * running actions. | ||
| * | ||
| * @param {function} fn The action to perform | ||
| * @param {...any} extra Extra arguments to pass to callbacks | ||
| */ | ||
| replace_pending(fn, ...extra) { | ||
| let pending = this._queue.concat(); | ||
| this._queue.splice(0, this._queue.length); | ||
| while (pending.length > 0) { | ||
| let action = pending.shift(); | ||
| this._cancel_action(action); | ||
| } | ||
| this.append(fn, ...extra); | ||
| } | ||
| /** | ||
| * Clear the entire queue. Cancel pending and running actions. | ||
@@ -254,11 +270,17 @@ */ | ||
| * The result is an object with two properties: 'running' and 'pending'. | ||
| * Each is an array of objects the a single property `args`; which is an | ||
| * array (possibly empty) with the extra arguments passed to `append`, | ||
| * `prepend` or `replace`. | ||
| * Each is an array of objects the properties: | ||
| * | ||
| * - `args`; which is an array (possibly empty) with the extra arguments | ||
| * passed to `append`, `prepend` or `replace`. | ||
| * | ||
| * - `cancel`; a function that allows to cancel this particular action | ||
| * | ||
| * - `promise`; the promise attached to this action (undefined if | ||
| * `createPromises` is false). | ||
| * | ||
| */ | ||
| info() { | ||
| let map = function(d) { | ||
| let {promise, extra} = d; | ||
| return {args: extra, cancel: d.cancel, promise: d.external_promise}; | ||
| let {extra, cancel, external_promise} = d; | ||
| return {args: extra, cancel, promise: external_promise}; | ||
| } | ||
@@ -298,3 +320,3 @@ | ||
| let running = this._queue.shift(); | ||
| let { fn, connectors, extra, cancelled } = running; | ||
| let { fn, connectors, cancelled } = running; | ||
| if (cancelled) { | ||
@@ -402,6 +424,5 @@ this._run(); | ||
| for (const action of Object.values(this._workers)) { | ||
| // Some promises reject when cancelled, we let's avoid | ||
| // rejecting the queue's promise in such cases, because our | ||
| // API states that we won't reject the promise when cancelling | ||
| // an action. | ||
| // Some promises reject when cancelled, we avoid rejecting the | ||
| // queue's promise in such cases, because our API states that we | ||
| // won't reject the promise when cancelling an action. | ||
| let promise = action.inner_promise; | ||
@@ -430,10 +451,2 @@ try { | ||
| action.cancelled = true; | ||
| if (this._options.rejectCanceled) { | ||
| try { | ||
| action.connectors.reject(new Error("Action was cancelled")); | ||
| } | ||
| catch (e) { | ||
| console.error(e); | ||
| } | ||
| } | ||
| let extra = action.extra; | ||
@@ -448,2 +461,10 @@ let self = this; | ||
| }); | ||
| if (this._options.rejectCanceled) { | ||
| try { | ||
| action.connectors.reject(new Error("Action was cancelled")); | ||
| } | ||
| catch (e) { | ||
| console.error(e); | ||
| } | ||
| } | ||
| this._finallys.forEach(function (fn) { | ||
@@ -450,0 +471,0 @@ try { |
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
63561
6.54%1739
5.08%164
34.43%