Skip to content

Instantly share code, notes, and snippets.

View mimshins's full-sized avatar
👾
Invading Codebases

Mostafa Shamsitabar mimshins

👾
Invading Codebases
View GitHub Profile
@mimshins
mimshins / with-retry.ts
Created September 6, 2025 10:04
Executes an asynchronous action and retries it with an exponential backoff and jitter strategy if it fails.
export type WithRetryOptions = {
/**
* The maximum number of retry attempts.
* Defaults to 5.
*
* @default 5
*/
maxRetries?: number;
/**
@mimshins
mimshins / Mutex.ts
Created August 24, 2025 22:17
A Mutex (Mutual Exclusion) class to ensure a task runs exclusively.
/**
* 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> = [];
/**
@mimshins
mimshins / wait-for-storage.ts
Created August 5, 2025 12:57
This function repeatedly checks local storage for a key-value pair until it either finds a match, or a specified timeout is reached.
/**
* 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?: {
@mimshins
mimshins / flush-dispatch-custom-event.ts
Created July 28, 2025 09:22
Flush custom event dispatch
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
@mimshins
mimshins / DoubleEndedList.test.ts
Last active July 2, 2025 16:18
A TypeScript implementation of Redis' List data structure
import { describe, it, expect, beforeEach } from "vitest";
import { DoubleEndedList, Positions } from "./DoubleEndedList.ts";
describe("DoubleEndedList", () => {
let list: DoubleEndedList<number>;
beforeEach(() => {
list = new DoubleEndedList<number>();
});
@mimshins
mimshins / lru-cache.ts
Last active August 5, 2025 12:59
basic lru cache impl
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 {
@mimshins
mimshins / create-promise-resolvers.ts
Created February 15, 2025 07:54
An utility function to create a promise and its resolvers.
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;
@mimshins
mimshins / execution-flow-helpers.ts
Last active June 2, 2025 13:51
DOM Execution Flow Helpers
/**
* 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 => {
@mimshins
mimshins / ObjectPool.ts
Last active October 30, 2024 18:35
Simple Object Pool Implementation
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;
@mimshins
mimshins / ArrayPool.ts
Last active October 30, 2024 18:36
Simple Array Pool Implementation
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