@cleanweb/utils
Version:
Simple, tiny, straight-forward utils for everyday Typescript needs.
74 lines (73 loc) • 2.67 kB
TypeScript
/**
* Holds methods for sending updates to all subscribers.
*/
export interface IPublisher<DataType> {
/**
* Calls the `next` callback of all current subscribers immediately,
* passing the new data to them.
*/
publish: (data: DataType) => void;
/**
* Calls the `done` callback of all current subscribers immediately,
* indicating that no new data is expected to be published to this instance.
*/
done: VoidFunction;
}
/**
* A callback to signal the owner when the first subscriber is added.
*/
type StartCallback<DataType> = (
/** An object with methods for sending updates to all subscribers. */
publisher: IPublisher<DataType>) => void;
type ConstructorParams<DataType> = [
/**
* Called when the first subscriber is added.
* Can be used as a signal to know when to start
* generating data.
*/
start: StartCallback<DataType>,
/**
* Called when there are no subscribers left on the instance.
* Can be used as a signal to pause generation of data.
*
* the `start` callback will be called again if a new subscriber is added.
*/
stop: VoidFunction,
/**
* An initial value for the "lastPublished" data.
* "lastPublished" is published to new subscribers immediately, even if
* `publish` has not been called yet.
*
* Subscribers can opt out of receiving this using the `freshOnly` argument.
* If so, they will only receive values published after they subscribed.
*/
initialData: DataType
];
type TSubscribe<DataType> = (
/** A callback to be called with the latest data each time there is an update. */
onPublish: (data: DataType) => void,
/** A callback to be called when the publisher indicates that no further updates will be published. */
onComplete: VoidFunction,
/**
* Set to true to opt out of receiving the data that was last published at the time of subscription.
* If false or omitted, your `onPublish` callback will be called with the
* last published data immediately you subscribe.
* If true, your callback will only receive new data that was published after you subscribed.
*/
freshOnly: boolean) => VoidFunction;
export default class Subscribable<DataType> {
private _start;
private _pause;
private _lastPublishedData;
private _subscribers;
private _allTimeSubscribersCount;
/** @inheritdoc {@link IPublisher} */
private _publisher;
private _publish;
private _close;
constructor(...params: ConstructorParams<DataType>);
subscribe: TSubscribe<DataType>;
/** Returns the data that was last published. */
get snapshot(): DataType | undefined;
}
export {};