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 / get-file-hash.ts
Last active November 17, 2025 09:01
Generates the cryptographic hash (fingerprint) of a local file.
import * as crypto from "crypto";
import * as fs from "fs";
/**
* Generates the cryptographic hash (fingerprint) of a local file.
*
* @param filePath The path to the file.
* @param algorithm The hashing algorithm to use (e.g., 'sha256', 'md5').
*
* @returns A promise that resolves to the hexadecimal hash string.
@mimshins
mimshins / with-context-selectors.ts
Last active November 10, 2025 11:47
A HOC that will help with fine-grained reactivity in Context state managers.
import { createContext, memo, useContext } from "react";
export const withContextSelectors = <
TProps extends object,
TContextValue,
TSelectors extends Record<string, (data: TContextValue) => unknown>,
>(
Component: React.ComponentType<
TProps & { [K in keyof TSelectors]: ReturnType<TSelectors[K]> }
>,
@mimshins
mimshins / can-use-dom.ts
Created November 9, 2025 08:02
A boolean indicates whether we are in the correct DOM execution
const canUseDOM: boolean = !!(
typeof window !== 'undefined' &&
typeof window.document !== 'undefined' &&
typeof window.document.createElement !== 'undefined'
);
@mimshins
mimshins / with-recent-cache.ts
Created November 8, 2025 12:10
Memoizes a function by caching only the most recent call result. Compares arguments with the previous call using shallow equality.
/**
* Memoizes a function by caching only the most recent call result.
* Compares arguments with the previous call using shallow equality.
*
* @template TArgs The function arguments type
* @template TReturn The function return type
* @param fn The function to memoize
* @returns Memoized function that caches only the last result
*
* @example
@mimshins
mimshins / define-query-keys.ts
Last active October 4, 2025 08:05
Creates a type-safe factory for generating consistent, domain-prefixed TanStack Query keys.
// eslint-disable-next-line eslint-comments/disable-enable-pair
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { QueryKey } from "@tanstack/react-query";
type UnknownObject = Record<PropertyKey, unknown>;
type QueryKeySegment =
| string
| number
| boolean
@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>();
});