UNPKG

shelving

Version:

Toolkit for using data in JavaScript.

31 lines (30 loc) 872 B
import { call } from "./callback.js"; /** * Wrapper class to handle state on start/stop callback process. * - If process has already started, `starter.start()` won't be called twice (including if `start()` didn't return a `stop()` callback). */ export class Starter { _start; _stop = false; constructor(start) { this._start = start; } start(...values) { if (this._stop === false) this._stop = this._start(...values) || true; } stop() { if (this._stop !== false) { if (typeof this._stop === "function") call(this._stop); this._stop = false; } } [Symbol.dispose]() { this.stop(); } } /** Get a `Starter` from a `PossibleStarter` */ export function getStarter(start) { return typeof start === "function" ? new Starter(start) : start; }