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 ExpandedPromise<T> extends Promise<T> { | |
resolve: (value: T | PromiseLike<T>) => void; | |
reject: (reason?: any) => void; | |
status: 'pending' | 'fulfilled' | 'rejected' = 'pending'; | |
constructor() { | |
const callbacks: { | |
resolve?: ExpandedPromise<T>['resolve'], | |
reject?: ExpandedPromise<T>['reject'], |
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 { createElement, CSSProperties, FC } from 'react'; | |
export type CSSBoxProps = CSSProperties & { className?: string }; | |
export const CSSBox: FC<CSSBoxProps> = ({ children, className, ...style }) => { | |
return createElement('div', { className, style }, children); | |
}; |
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 { useMemo, useRef } from 'react'; | |
export type UseValuesChangeWatcherResult<T extends Record<string, unknown>> = { | |
[K in keyof T]?: { | |
oldValue: unknown; | |
newValue: unknown; | |
}; | |
}; | |
const compareFn = <T extends [string, unknown]>(a: T, b: T) => a[0].localeCompare(b[0]); |
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 { useMemo, useRef } from 'react'; | |
import { useScope } from './useScope'; | |
export type UseThrottlerCallback = (...args: any[]) => void; | |
export const useThrottler = <T extends UseThrottlerCallback>(callback: T, delay: number): T & { clear: () => void } => { | |
const scope = useScope({ callback, delay }); | |
const timeout = useRef<number>(0); | |
return useMemo(() => { |
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 { CancelTokenSource } from 'axios'; | |
import { useCallback, useRef } from 'react'; | |
import { newCancelTokenSource } from '../network/api/Network'; | |
import { delay } from '../utils/FlowUtils'; | |
import { useOnComponentUnmount } from './useOnComponentUnmount'; | |
import { useScope } from './useScope'; | |
// when your callback returns usePoolingCallback.RETRY, it retries after delay | |
export const usePoolingCallback = <R, D>( | |
callback: (cancelTokenSource: CancelTokenSource, data: D) => Promise<R | typeof usePoolingCallback.RETRY>, |
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 { MutableRefObject, RefCallback, RefObject } from 'react'; | |
export const setRef = <T>(...refs: (RefCallback<T> | RefObject<T> | MutableRefObject<T> | undefined | null)[]) => { | |
return (instance: T | null) => { | |
for (const ref of refs) { | |
if (!ref) continue; | |
if (typeof ref === 'function') { | |
ref(instance); | |
} |
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 AbortCallback = (err: any) => void; | |
type SetAbort = (abortCallback: AbortCallback) => void; | |
export const delay = (ms: number, setAbort?: SetAbort) => { | |
return new Promise((resolve, reject) => { | |
const timeout = setTimeout(resolve, ms); | |
setAbort(err => { | |
clearTimeout(timeout); | |
reject(err); | |
}); |
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 } from 'react'; | |
export const useOnComponentUnmount = (onUnmountFn: () => void) => { | |
useEffect(() => () => onUnmountFn(), []); | |
}; |
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 { EffectAsyncCallback, useEffectAsync } from './useEffectAsync'; | |
export const useOnComponentMountAsync = (asyncEffect: EffectAsyncCallback) => { | |
useEffectAsync(asyncEffect, []); | |
}; |
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 { EffectCallback, useEffect } from 'react'; | |
export const useOnComponentMount = (onMountFn: EffectCallback) => { | |
useEffect(() => onMountFn(), []); | |
}; |