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 { 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) => { |
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 { 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( |
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 { | |
| createEntityAdapter, | |
| createSlice, | |
| configureStore, | |
| EntitySelectors, | |
| EntityId, | |
| } from '@reduxjs/toolkit'; | |
| type Book = { bookId: string; title: string; authorId: string } |
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 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) { |
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 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); | |
| } | |
| } |
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
| 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) { |
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
| 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) { |
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
| 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); |
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
| /** | |
| * 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 | |
| */ |
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 { 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') |