Skip to content

Instantly share code, notes, and snippets.

View jtmthf's full-sized avatar

Jack Moore jtmthf

View GitHub Profile
import { useRef, useCallback, DependencyList } from "react";
export function useCallbackOnce<T extends (...args: any[]) => any>(
callback: T,
deps: DependencyList
): T {
const calledRef = useRef(false);
const handler = useCallback(
((...args) => {
import { useRef, useCallback, DependencyList } from 'react';
function useBlockingCallback<T extends (...args: any[]) => any>(
callback: T,
initiallyBlocked = false,
deps: DependencyList = [],
): [T, (blocked?: boolean) => void] {
const blockRef = useRef(initiallyBlocked);
const handler = useCallback(
import {
createEntityAdapter,
createSlice,
configureStore,
EntitySelectors,
EntityId,
} from '@reduxjs/toolkit';
type Book = { bookId: string; title: string; authorId: string }
export const $ = document.querySelector.bind(document);
export const $$ = document.querySelectorAll.bind(document);
Node.prototype.on = window.on = function (name: string, fn: EventListenerOrEventListenerObject) {
this.addEventListener(name, fn);
};
(NodeList.prototype as any).__proto__ = Array.prototype;
NodeList.prototype.on = NodeList.prototype.addEventListener = function (name, fn) {
class DataRow<T> {
private constructor(public readonly value: T) {}
static of<T>(value: DataRow<T>): DataRow<T>;
static of<T>(value: T): DataRow<T>;
static of<T>(value: T | DataRow<T>): DataRow<T> {
return value instanceof DataRow ? value : new DataRow(value);
}
}
interface LinkedList<T> {
readonly value: T;
readonly next?: LinkedList<T>;
}
function linkedListFromIterable<T>(
source: Iterable<T>,
): LinkedList<T> | undefined {
const [first, iterable] = head(source);
if (first.done) {
function* chunk<T>(
source: Iterable<T>,
size = 1,
): Iterable<IterableIterator<T>> {
const iterator = source[Symbol.iterator]();
let done = false;
function* take() {
let count = 0;
while (count++ < size) {
type NestedMap<K extends any[], V> = Map<K[number], NestedMap<K, V>>;
export class MultiKeyMap<K extends any[], V> implements Map<K, V> {
#data: NestedMap<K, V> = new Map();
#depth = 0;
constructor(iterable?: [K, V][]) {
if (iterable) {
for (const [keys, value] of iterable) {
this.set(keys, value);
/**
* Extension of the Map class that stores multiple values.
*/
export class MultiValueMap<K, V> extends Map<K, V[]> {
/**
* Add the given value to the current list of values for the given key.
*
* @param key - the key
* @param value - the value to be added
*/
import { useEffect, useState, useMemo } from 'react';
export interface UseEventSourceOptions<T = string> {
withCredentials?: boolean;
extractor?: (value: any) => T;
}
export function useEventSource<T = string>(url: string, { withCredentials, extractor = JSON.parse }: UseEventSourceOptions<T> = {}) {
const [data, setData] = useState<T>();
const [readyState, setReadyState] = useState < 'connecting' | 'open' | 'closed' | 'error'>('connecting')