This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type WithRetryOptions = { | |
/** | |
* The maximum number of retry attempts. | |
* Defaults to 5. | |
* | |
* @default 5 | |
*/ | |
maxRetries?: number; | |
/** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A Mutex (Mutual Exclusion) class to ensure a task runs exclusively. | |
* It prevents race conditions in asynchronous code by ensuring only one task | |
* can access a shared resource at a time. | |
*/ | |
export class Mutex { | |
private _isLocked: boolean = false; | |
private _taskQueue: Array<() => void> = []; | |
/** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Waits for a specific value to be present in local storage. | |
* | |
* @param key The key to look for in local storage. | |
* @param [options] The options to configure the waiting behavior. | |
* @returns A promise that resolves when the condition is met, or rejects if the timeout is reached. | |
*/ | |
export const waitForStorage = ( | |
key: string, | |
options?: { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { flushSync } from "react-dom"; | |
/** | |
* Flush custom event dispatch. | |
* | |
* React batches *all* event handlers since version 18, this introduces certain considerations when using custom event types. | |
* | |
* Internally, React prioritises events in the following order: | |
* - discrete | |
* - continuous |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { describe, it, expect, beforeEach } from "vitest"; | |
import { DoubleEndedList, Positions } from "./DoubleEndedList.ts"; | |
describe("DoubleEndedList", () => { | |
let list: DoubleEndedList<number>; | |
beforeEach(() => { | |
list = new DoubleEndedList<number>(); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class LruCache<K, V> { | |
private readonly _maxSize: number; | |
private readonly _cache: Map<K, V>; | |
constructor(maxSize: number) { | |
this._maxSize = maxSize; | |
this._cache = new Map(); | |
} | |
public get size(): number { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const createPromiseResolvers = <T = void>() => { | |
const noop = () => void 0; | |
let _resolve: (value: T | PromiseLike<T>) => void = noop; | |
let _reject: (reason?: unknown) => void = noop; | |
const createPromise = () => | |
new Promise<T>((res, rej) => { | |
_resolve = res; | |
_reject = rej; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Runs a callback as soon as possible after next repaint. | |
* | |
* This can be helpful for operations that need to wait until the | |
* next repaint is complete before performing a follow-up task. | |
* When you need a small delay after the next repaint, perhaps for | |
* batching DOM updates or deferring non-critical tasks right after a | |
* frame is rendered. | |
*/ | |
export const runAfterRepaint = (fn: () => void): void => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ObjectPool<T> { | |
private _pool: T[] = []; | |
private readonly _createObject: () => T; | |
private readonly _resetObject: (obj: T) => void; | |
private readonly _maxPoolSize: number; | |
constructor(creator: () => T, reset: (obj: T) => void, maxPoolSize: number = 10) { | |
this._createObject = creator; | |
this._resetObject = reset; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ArrayPool<T> { | |
private _pool: T[][] = []; | |
private readonly _maxPoolSize: number; | |
constructor(maxPoolSize: number = 10) { | |
this._maxPoolSize = maxPoolSize; | |
} | |
// Get an array from the pool, or create a new one if the pool is empty |
NewerOlder