getStarter()function
Get a Starter from a PossibleStarter.
getStarter(start: StartCallback<T> | Starter<T>): Starter<T>
Manage processes that have a start callback and an optional stop callback. Starter prevents double-starts and wraps errors, making it easy to build safe, disposable lifecycle objects.
import { Starter } from "shelving/util";
const starter = new Starter(() => {
const id = setInterval(tick, 1000);
return () => clearInterval(id); // returned function is the stop callback
});
starter.start(); // begins the interval
starter.start(); // no-op — already started
starter.stop(); // calls the stop callbackStarter implements Disposable, so it works with using:
{
using starter = new Starter(myStartCallback);
starter.start();
// starter.stop() called automatically when block exits
}import { getStarter, type PossibleStarter } from "shelving/util";
function setup(s: PossibleStarter<[]>) {
const starter = getStarter(s); // wraps a function; passes a Starter through unchanged
starter.start();
}STOPHOLE is a start callback that always returns a no-op stop callback. Useful as a safe default.
import { STOPHOLE } from "shelving/util";
const noop = new Starter(STOPHOLE);
noop.start(); // fine — stop callback does nothingGet a Starter from a PossibleStarter.
getStarter(start: StartCallback<T> | Starter<T>): Starter<T>
Wrapper class to handle state on start/stop callback process.
new Starter<T>(start: StartCallback<T>)
Callback function that starts something with multiple values and returns an optional stop callback.
(...values: T) => StopCallback | void
Callback function that stops something.
() => void
Something that can be made into a Starter.
StartCallback<T> | Starter<T>
Callback function that does nothing and returns a blackhole stop callback.
STOPHOLE: (...args: Arguments) => StopCallback