Events in JS/TS with easy typing/autocompletion.
Example:
class MusicPlayer {
onPlay = new Callbacks<() => void>();
// ...
play() {
// ...
// Call/invoke the callbacks:
this.onPlay.invoke();
}
}
const player = new MusicPlayer();
// ...
player.onPlay.add(() => {
console.info("player play");
});Ref with onChanged callbacks.
Example:
class MusicPlayer {
refState = new Ref<"stopped" | "playing" | "paused">("stopped");
// ...
play() {
// ...
this.refState.value = "playing";
}
}
const player = new MusicPlayer();
// ...
player.refState.onChanged.add(() => {
console.info("player current state:", player.refState.value);
});Example:
// Limit the concurrency of downloads
const downloadLimit = new Semaphore({ maxCount: 5 });
async function download() {
// Will block/await here if >= 5 downloads currently running,
await downloadLimit.enter();
try {
await actuallyDownload();
} finally {
downloadLimit.exit();
}
}Or shorter version with run():
const downloadLimit = new Semaphore({ maxCount: 5 });
function download() {
return downloadLimit.run(async () => {
await actuallyDownload();
});
}(TBD)
function strPadLeft(str: string, len: number, ch?: string): string;function formatDuration(sec: number | any): string;function formatFileSize(size: number | any): string;function formatDateTime(date: Date): string;function numLimit(num: number, min: number, max: number): number;function createName(
nameFunc: (num: number) => string,
existsFunc: (str: string) => boolean
): string;function base64EncodeUtf8(str: any): string;function sleepAsync(time: number): Promise<void>;function arrayRemove<T>(array: T[], val: T): void;function arrayInsert<T>(array: T[], val: T, pos?: number): void;function arrayMap<T, TRet>(
arr: Iterable<T>,
func: (item: T, idx: number) => TRet
): TRet[];function arrayForeach<T>(
arr: Iterable<T>,
func: (item: T, idx: number) => void
): void;function foreachFlaten<T>(arr: T[], func: Action<T>): void;function arrayFind<T>(
arr: Iterable<T>,
func: (item: T, idx: number) => any
): T | null;function arraySum<T>(
arr: Iterable<T>,
func: (item: T) => number | null | undefined
): number;function objectApply<T>(
obj: Partial<T>,
kv?: Partial<T>,
keys?: Array<keyof T>
): Partial<T>;function objectInit<T>(obj: T, kv?: ObjectInit<T>, keys?: Array<keyof T>): T;function mod(a: number, b: number): number;function readBlobAsDataUrl(blob: Blob): Promise<string>;