@akala/core
Version:
135 lines • 4.21 kB
JavaScript
import { PipeEvent, ReplayEvent } from "../events/shared.js";
function noop() { }
/**
* A class representing a replayable asynchronous event.
* @template T - The type of the event arguments.
* @template TReturnType - The return type of the event.
*/
export class ReplayAsyncEvent extends ReplayEvent {
/**
* Creates an instance of ReplayAsyncEvent.
* @param {number} bufferLength - The length of the buffer.
* @param {number} maxListeners - The maximum number of listeners.
* @param {function} combineReturnTypes - A function to combine the return types.
*/
constructor(bufferLength, maxListeners, combineReturnTypes) {
super(bufferLength, maxListeners, (promises) => Promise.all(promises).then((returns) => combineReturnTypes(returns)));
}
}
/**
* A class representing a valued event.
* @template T - The type of the event arguments.
* @template TReturnType - The return type of the event.
* @template TOptions - The options for the event.
*/
export class ValuedEvent {
value;
/**
* Creates an instance of ValuedEvent.
* @param {T} value - The value of the event.
*/
constructor(value) {
this.value = value;
}
hasListeners = false;
maxListeners = Number.POSITIVE_INFINITY;
/**
* Adds a listener to the event.
* @param {function} listener - The listener function.
* @returns {function} A function to remove the listener.
*/
addListener(listener) {
listener(...this.value);
return () => true;
}
/**
* Removes a listener from the event.
* @returns {boolean} Whether the listener was removed.
*/
removeListener() {
return false;
}
/**
* Disposes of the event.
*/
[Symbol.dispose]() {
this.value = null;
}
}
/**
* Creates a valued event.
* @template T - The type of the event arguments.
* @param {...T} args - The event arguments.
* @returns {ValuedEvent} The valued event.
*/
export function of(...args) {
return new ValuedEvent(args);
}
/**
* A class representing a debounce event.
* @template T - The type of the event arguments.
* @template TOptions - The options for the event.
*/
export class DebounceEvent extends PipeEvent {
duration;
/**
* Creates an instance of DebounceEvent.
* @param {IEventSink} source - The source event sink.
* @param {number} duration - The debounce duration.
*/
constructor(source, duration) {
super(source, (...args) => args, noop);
this.duration = duration;
}
/**
* Subscribes to the source event if required.
*/
subscribeToSourceIfRequired() {
let timeout;
if (!this.subscription)
this.subscription = this.source.addListener((...args) => {
if (timeout)
clearTimeout(timeout);
timeout = setTimeout(() => super.emit(...args), this.duration);
});
}
}
/**
* Creates a debounce event.
* @template T - The type of the event arguments.
* @template TOptions - The options for the event.
* @param {IEventSink} source - The source event sink.
* @param {number} duration - The debounce duration.
* @returns {DebounceEvent} The debounce event.
*/
export function debounce(source, duration) {
return new DebounceEvent(source, duration);
}
/**
* Creates a pipe event.
* @template T - The type of the event arguments.
* @template U - The type of the mapped event arguments.
* @template TOptions - The options for the event.
* @param {IEventSink} source - The source event sink.
* @param {function} map - The mapping function.
* @returns {PipeEvent} The pipe event.
*/
export function pipe(source, map) {
return new PipeEvent(source, map, noop);
}
/**
* An abstract class representing a watcher formatter.
* @template T - The type of the formatted value.
*/
export class WatcherFormatter {
watcher;
/**
* Creates an instance of WatcherFormatter.
* @param {Watcher} [watcher] - The watcher.
*/
constructor(watcher) {
this.watcher = watcher;
}
}
export const watcher = Symbol.for("akala/watcher");
//# sourceMappingURL=shared.js.map