| 1 | import { createNextState, isDraftable } from './immerImports'
|
|---|
| 2 |
|
|---|
| 3 | export function getTimeMeasureUtils(maxDelay: number, fnName: string) {
|
|---|
| 4 | let elapsed = 0
|
|---|
| 5 | return {
|
|---|
| 6 | measureTime<T>(fn: () => T): T {
|
|---|
| 7 | const started = Date.now()
|
|---|
| 8 | try {
|
|---|
| 9 | return fn()
|
|---|
| 10 | } finally {
|
|---|
| 11 | const finished = Date.now()
|
|---|
| 12 | elapsed += finished - started
|
|---|
| 13 | }
|
|---|
| 14 | },
|
|---|
| 15 | warnIfExceeded() {
|
|---|
| 16 | if (elapsed > maxDelay) {
|
|---|
| 17 | console.warn(`${fnName} took ${elapsed}ms, which is more than the warning threshold of ${maxDelay}ms.
|
|---|
| 18 | If your state or actions are very large, you may want to disable the middleware as it might cause too much of a slowdown in development mode. See https://redux-toolkit.js.org/api/getDefaultMiddleware for instructions.
|
|---|
| 19 | It is disabled in production builds, so you don't need to worry about that.`)
|
|---|
| 20 | }
|
|---|
| 21 | },
|
|---|
| 22 | }
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | export function delay(ms: number) {
|
|---|
| 26 | return new Promise((resolve) => setTimeout(resolve, ms))
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | export class Tuple<Items extends ReadonlyArray<unknown> = []> extends Array<
|
|---|
| 30 | Items[number]
|
|---|
| 31 | > {
|
|---|
| 32 | constructor(length: number)
|
|---|
| 33 | constructor(...items: Items)
|
|---|
| 34 | constructor(...items: any[]) {
|
|---|
| 35 | super(...items)
|
|---|
| 36 | Object.setPrototypeOf(this, Tuple.prototype)
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | static override get [Symbol.species]() {
|
|---|
| 40 | return Tuple as any
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | override concat<AdditionalItems extends ReadonlyArray<unknown>>(
|
|---|
| 44 | items: Tuple<AdditionalItems>,
|
|---|
| 45 | ): Tuple<[...Items, ...AdditionalItems]>
|
|---|
| 46 | override concat<AdditionalItems extends ReadonlyArray<unknown>>(
|
|---|
| 47 | items: AdditionalItems,
|
|---|
| 48 | ): Tuple<[...Items, ...AdditionalItems]>
|
|---|
| 49 | override concat<AdditionalItems extends ReadonlyArray<unknown>>(
|
|---|
| 50 | ...items: AdditionalItems
|
|---|
| 51 | ): Tuple<[...Items, ...AdditionalItems]>
|
|---|
| 52 | override concat(...arr: any[]) {
|
|---|
| 53 | return super.concat.apply(this, arr)
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | prepend<AdditionalItems extends ReadonlyArray<unknown>>(
|
|---|
| 57 | items: Tuple<AdditionalItems>,
|
|---|
| 58 | ): Tuple<[...AdditionalItems, ...Items]>
|
|---|
| 59 | prepend<AdditionalItems extends ReadonlyArray<unknown>>(
|
|---|
| 60 | items: AdditionalItems,
|
|---|
| 61 | ): Tuple<[...AdditionalItems, ...Items]>
|
|---|
| 62 | prepend<AdditionalItems extends ReadonlyArray<unknown>>(
|
|---|
| 63 | ...items: AdditionalItems
|
|---|
| 64 | ): Tuple<[...AdditionalItems, ...Items]>
|
|---|
| 65 | prepend(...arr: any[]) {
|
|---|
| 66 | if (arr.length === 1 && Array.isArray(arr[0])) {
|
|---|
| 67 | return new Tuple(...arr[0].concat(this))
|
|---|
| 68 | }
|
|---|
| 69 | return new Tuple(...arr.concat(this))
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | export function freezeDraftable<T>(val: T) {
|
|---|
| 74 | return isDraftable(val) ? createNextState(val, () => {}) : val
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | export function getOrInsert<K extends object, V>(
|
|---|
| 78 | map: WeakMap<K, V>,
|
|---|
| 79 | key: K,
|
|---|
| 80 | value: V,
|
|---|
| 81 | ): V
|
|---|
| 82 | export function getOrInsert<K, V>(map: Map<K, V>, key: K, value: V): V
|
|---|
| 83 | export function getOrInsert<K extends object, V>(
|
|---|
| 84 | map: Map<K, V> | WeakMap<K, V>,
|
|---|
| 85 | key: K,
|
|---|
| 86 | value: V,
|
|---|
| 87 | ): V {
|
|---|
| 88 | if (map.has(key)) return map.get(key) as V
|
|---|
| 89 |
|
|---|
| 90 | return map.set(key, value).get(key) as V
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | export function getOrInsertComputed<K extends object, V>(
|
|---|
| 94 | map: WeakMap<K, V>,
|
|---|
| 95 | key: K,
|
|---|
| 96 | compute: (key: K) => V,
|
|---|
| 97 | ): V
|
|---|
| 98 | export function getOrInsertComputed<K, V>(
|
|---|
| 99 | map: Map<K, V>,
|
|---|
| 100 | key: K,
|
|---|
| 101 | compute: (key: K) => V,
|
|---|
| 102 | ): V
|
|---|
| 103 | export function getOrInsertComputed<K extends object, V>(
|
|---|
| 104 | map: Map<K, V> | WeakMap<K, V>,
|
|---|
| 105 | key: K,
|
|---|
| 106 | compute: (key: K) => V,
|
|---|
| 107 | ): V {
|
|---|
| 108 | if (map.has(key)) return map.get(key) as V
|
|---|
| 109 |
|
|---|
| 110 | return map.set(key, compute(key)).get(key) as V
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | export function promiseWithResolvers<T>(): {
|
|---|
| 114 | promise: Promise<T>
|
|---|
| 115 | resolve: (value: T | PromiseLike<T>) => void
|
|---|
| 116 | reject: (reason?: any) => void
|
|---|
| 117 | } {
|
|---|
| 118 | let resolve: any
|
|---|
| 119 | let reject: any
|
|---|
| 120 | const promise = new Promise<T>((res, rej) => {
|
|---|
| 121 | resolve = res
|
|---|
| 122 | reject = rej
|
|---|
| 123 | })
|
|---|
| 124 | return { promise, resolve, reject }
|
|---|
| 125 | }
|
|---|