thorish
Version:
This is a library of useful JS concepts and data structures for Node and the browser. It it, unashamedly, a dumping ground for code needed by [@samthor](https://twitter.com/samthor)'s projects.
49 lines (48 loc) • 1.43 kB
TypeScript
import type { AbortSignalArgs } from './types.ts';
/**
* The options passed to {@link Condition} when adding a listener.
*/
export type ConditionOptions = AbortSignalArgs & {
/**
* Whether to call the listener for both state begin and state end, as opposed to just begin.
*
* @default false
*/
both?: boolean;
};
/**
* A listener added to {@link Condition}.
*/
export type ConditionListener<T> = (state: T) => any;
/**
* Controls a value which fires begin/end listeners on its state change.
*/
export declare class Condition<T> {
private listeners;
private signal;
private _state;
constructor(defaultValue: T, options?: AbortSignalArgs);
get state(): T;
set state(v: T);
/**
* Does this {@link Condition} currently have any listeners.
*/
observed(): boolean;
/**
* For subclasses to override. The actions to take when the first listener is added.
*/
protected setup(): void;
/**
* For subclasses to override. The actions to take when the last listener is removed, or this
* {@link Condition} is aborted.
*/
protected teardown(): void;
/**
* Adds a listener to this {@link Condition}.
*/
addListener(fn: ConditionListener<T>, options?: ConditionOptions): boolean;
/**
* Removes a listener from this {@link Condition}.
*/
removeListener(fn: ConditionListener<T>): boolean;
}