@lit-labs/task
Advanced tools
Comparing version 2.1.1 to 2.1.2
@@ -81,2 +81,6 @@ import { ReactiveControllerHost } from '@lit/reactive-element/reactive-controller.js'; | ||
/** | ||
* Controls if they task will run when its arguments change. Defaults to true. | ||
*/ | ||
autoRun: boolean; | ||
/** | ||
* A Promise that resolve when the current task run is complete. | ||
@@ -87,9 +91,6 @@ * | ||
*/ | ||
taskComplete: Promise<R>; | ||
/** | ||
* Controls if they task will run when its arguments change. Defaults to true. | ||
*/ | ||
autoRun: boolean; | ||
private _resolveTaskComplete; | ||
private _rejectTaskComplete; | ||
get taskComplete(): Promise<R>; | ||
private _resolveTaskComplete?; | ||
private _rejectTaskComplete?; | ||
private _taskComplete?; | ||
constructor(host: ReactiveControllerHost, task: TaskFunction<T, R>, args?: ArgsFunction<T>); | ||
@@ -96,0 +97,0 @@ constructor(host: ReactiveControllerHost, task: TaskConfig<T, R>); |
@@ -90,7 +90,35 @@ /** | ||
} | ||
this.taskComplete = new Promise((res, rej) => { | ||
this._resolveTaskComplete = res; | ||
this._rejectTaskComplete = rej; | ||
}); | ||
} | ||
/** | ||
* A Promise that resolve when the current task run is complete. | ||
* | ||
* If a new task run is started while a previous run is pending, the Promise | ||
* is kept and only resolved when the new run is completed. | ||
*/ | ||
get taskComplete() { | ||
// If a task run exists, return the cached promise. This is true in the case | ||
// where the user has called taskComplete in pending or completed state | ||
// before and has not started a new task run since. | ||
if (this._taskComplete) { | ||
return this._taskComplete; | ||
} | ||
// Generate an in-progress promise if the the status is pending and has been | ||
// cleared by .run(). | ||
if (this.status === TaskStatus.PENDING) { | ||
this._taskComplete = new Promise((res, rej) => { | ||
this._resolveTaskComplete = res; | ||
this._rejectTaskComplete = rej; | ||
}); | ||
// If the status is error, return a rejected promise. | ||
} | ||
else if (this.status === TaskStatus.ERROR) { | ||
this._taskComplete = Promise.reject(this._error); | ||
// Otherwise we are at a task run's completion or this is the first | ||
// request and we are not in the middle of a task (i.e. INITIAL). | ||
} | ||
else { | ||
this._taskComplete = Promise.resolve(this._value); | ||
} | ||
return this._taskComplete; | ||
} | ||
hostUpdated() { | ||
@@ -125,10 +153,11 @@ this.performTask(); | ||
async run(args) { | ||
var _a, _b, _c; | ||
var _a, _b, _c, _d, _e; | ||
args !== null && args !== void 0 ? args : (args = (_a = this._getArgs) === null || _a === void 0 ? void 0 : _a.call(this)); | ||
if (this.status === TaskStatus.COMPLETE || | ||
this.status === TaskStatus.ERROR) { | ||
this.taskComplete = new Promise((res, rej) => { | ||
this._resolveTaskComplete = res; | ||
this._rejectTaskComplete = rej; | ||
}); | ||
// Clear the last complete task run in INITIAL because it may be a resolved | ||
// promise. Also clear if COMPLETE or ERROR because the value returned by | ||
// awaiting taskComplete may have changed since last run. | ||
if (this.status !== TaskStatus.PENDING) { | ||
this._taskComplete = undefined; | ||
this._resolveTaskComplete = undefined; | ||
this._rejectTaskComplete = undefined; | ||
} | ||
@@ -162,7 +191,7 @@ this.status = TaskStatus.PENDING; | ||
this.status = TaskStatus.COMPLETE; | ||
this._resolveTaskComplete(result); | ||
(_c = this._resolveTaskComplete) === null || _c === void 0 ? void 0 : _c.call(this, result); | ||
} | ||
else { | ||
try { | ||
(_c = this._onError) === null || _c === void 0 ? void 0 : _c.call(this, error); | ||
(_d = this._onError) === null || _d === void 0 ? void 0 : _d.call(this, error); | ||
} | ||
@@ -173,3 +202,3 @@ catch { | ||
this.status = TaskStatus.ERROR; | ||
this._rejectTaskComplete(error); | ||
(_e = this._rejectTaskComplete) === null || _e === void 0 ? void 0 : _e.call(this, error); | ||
} | ||
@@ -176,0 +205,0 @@ this._value = result; |
{ | ||
"name": "@lit-labs/task", | ||
"version": "2.1.1", | ||
"version": "2.1.2", | ||
"description": "A controller for Lit that renders asynchronous tasks.", | ||
@@ -5,0 +5,0 @@ "license": "BSD-3-Clause", |
@@ -81,2 +81,6 @@ import { ReactiveControllerHost } from '@lit/reactive-element/reactive-controller.js'; | ||
/** | ||
* Controls if they task will run when its arguments change. Defaults to true. | ||
*/ | ||
autoRun: boolean; | ||
/** | ||
* A Promise that resolve when the current task run is complete. | ||
@@ -87,9 +91,6 @@ * | ||
*/ | ||
taskComplete: Promise<R>; | ||
/** | ||
* Controls if they task will run when its arguments change. Defaults to true. | ||
*/ | ||
autoRun: boolean; | ||
private _resolveTaskComplete; | ||
private _rejectTaskComplete; | ||
get taskComplete(): Promise<R>; | ||
private _resolveTaskComplete?; | ||
private _rejectTaskComplete?; | ||
private _taskComplete?; | ||
constructor(host: ReactiveControllerHost, task: TaskFunction<T, R>, args?: ArgsFunction<T>); | ||
@@ -96,0 +97,0 @@ constructor(host: ReactiveControllerHost, task: TaskConfig<T, R>); |
@@ -6,3 +6,3 @@ import{notEqual as t}from"@lit/reactive-element"; | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/const i={INITIAL:0,PENDING:1,COMPLETE:2,ERROR:3},s=Symbol();class h{constructor(t,i,s){this.i=0,this.status=0,this.autoRun=!0,this.o=t,this.o.addController(this);const h="object"==typeof i?i:{task:i,args:s};this.t=h.task,this.h=h.args,this.l=h.onComplete,this.u=h.onError,void 0!==h.autoRun&&(this.autoRun=h.autoRun),this.taskComplete=new Promise(((t,i)=>{this.v=t,this._=i}))}hostUpdated(){this.performTask()}async performTask(){var t;const i=null===(t=this.h)||void 0===t?void 0:t.call(this);this.shouldRun(i)&&await this.run(i)}shouldRun(t){return this.autoRun&&this.k(t)}async run(t){var i,h,r;let e,o;null!=t||(t=null===(i=this.h)||void 0===i?void 0:i.call(this)),2!==this.status&&3!==this.status||(this.taskComplete=new Promise(((t,i)=>{this.v=t,this._=i}))),this.status=1,queueMicrotask((()=>this.o.requestUpdate()));const n=++this.i;try{e=await this.t(t)}catch(t){o=t}if(this.i===n){if(e===s)this.status=0;else{if(void 0===o){try{null===(h=this.l)||void 0===h||h.call(this,e)}catch{}this.status=2,this.v(e)}else{try{null===(r=this.u)||void 0===r||r.call(this,o)}catch{}this.status=3,this._(o)}this.m=e,this.T=o}this.o.requestUpdate()}}get value(){return this.m}get error(){return this.T}render(t){var i,s,h,r;switch(this.status){case 0:return null===(i=t.initial)||void 0===i?void 0:i.call(t);case 1:return null===(s=t.pending)||void 0===s?void 0:s.call(t);case 2:return null===(h=t.complete)||void 0===h?void 0:h.call(t,this.value);case 3:return null===(r=t.error)||void 0===r?void 0:r.call(t,this.error);default:this.status}}k(i){const s=this.p;return this.p=i,Array.isArray(i)&&Array.isArray(s)?i.length===s.length&&i.some(((i,h)=>t(i,s[h]))):i!==s}}export{h as Task,i as TaskStatus,s as initialState}; | ||
*/const i={INITIAL:0,PENDING:1,COMPLETE:2,ERROR:3},s=Symbol();class h{constructor(t,i,s){this.i=0,this.status=0,this.autoRun=!0,this.t=t,this.t.addController(this);const h="object"==typeof i?i:{task:i,args:s};this.o=h.task,this.h=h.args,this.l=h.onComplete,this.u=h.onError,void 0!==h.autoRun&&(this.autoRun=h.autoRun)}get taskComplete(){return this.v||(1===this.status?this.v=new Promise(((t,i)=>{this.m=t,this._=i})):3===this.status?this.v=Promise.reject(this.k):this.v=Promise.resolve(this.p)),this.v}hostUpdated(){this.performTask()}async performTask(){var t;const i=null===(t=this.h)||void 0===t?void 0:t.call(this);this.shouldRun(i)&&await this.run(i)}shouldRun(t){return this.autoRun&&this.T(t)}async run(t){var i,h,r,e,o;let l,n;null!=t||(t=null===(i=this.h)||void 0===i?void 0:i.call(this)),1!==this.status&&(this.v=void 0,this.m=void 0,this._=void 0),this.status=1,queueMicrotask((()=>this.t.requestUpdate()));const a=++this.i;try{l=await this.o(t)}catch(t){n=t}if(this.i===a){if(l===s)this.status=0;else{if(void 0===n){try{null===(h=this.l)||void 0===h||h.call(this,l)}catch{}this.status=2,null===(r=this.m)||void 0===r||r.call(this,l)}else{try{null===(e=this.u)||void 0===e||e.call(this,n)}catch{}this.status=3,null===(o=this._)||void 0===o||o.call(this,n)}this.p=l,this.k=n}this.t.requestUpdate()}}get value(){return this.p}get error(){return this.k}render(t){var i,s,h,r;switch(this.status){case 0:return null===(i=t.initial)||void 0===i?void 0:i.call(t);case 1:return null===(s=t.pending)||void 0===s?void 0:s.call(t);case 2:return null===(h=t.complete)||void 0===h?void 0:h.call(t,this.value);case 3:return null===(r=t.error)||void 0===r?void 0:r.call(t,this.error);default:this.status}}T(i){const s=this.g;return this.g=i,Array.isArray(i)&&Array.isArray(s)?i.length===s.length&&i.some(((i,h)=>t(i,s[h]))):i!==s}}export{h as Task,i as TaskStatus,s as initialState}; | ||
//# sourceMappingURL=task.js.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
62495
509