UNPKG

shelving

Version:

Toolkit for using data in JavaScript.

28 lines (27 loc) 1.05 kB
import { awaitDispose } from "../util/dispose.js"; import { BooleanStore } from "./BooleanStore.js"; import { Store } from "./Store.js"; /** * Store that tracks its busy status via a separate `this.busy` store. * - "busy" means the store is awaiting a new value. */ export class BusyStore extends Store { busy = new BooleanStore(false); // Overload to set `this.busy` to `true` when we start awaiting a value. // Gets set back to `false` when `abort()` is called (this also happens whenever a value or reason is set). await(pending) { this.busy.value = true; return super.await(pending); } // Override to set busy to false on abort. // This also happens whenever a value or reaosn is set. abort() { this.busy.value = false; super.abort(); } // Implement `AsyncDisposable`. async [Symbol.asyncDispose]() { await awaitDispose(() => this.abort(), this.busy, // Send `done: true` to any iterators of the busy store. super[Symbol.asyncDispose]()); } }