2022-09-30 22:42:50 +00:00
|
|
|
import { FilterFn, find } from "../webpack";
|
2022-08-31 18:47:07 +00:00
|
|
|
import { React } from "../webpack/common";
|
2022-08-31 02:07:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes a lazy function. On first call, the value is computed.
|
|
|
|
* On subsequent calls, the same computed value will be returned
|
|
|
|
* @param factory Factory function
|
|
|
|
*/
|
|
|
|
export function lazy<T>(factory: () => T): () => T {
|
|
|
|
let cache: T;
|
2022-09-30 22:42:50 +00:00
|
|
|
return () => cache ?? (cache = factory());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do a lazy webpack search. Searches the module on first property access
|
|
|
|
* @param filter Filter function
|
2022-10-08 17:23:05 +00:00
|
|
|
* @returns A proxy to the webpack module. Not all traps are implemented, may produce unexpected results.
|
2022-09-30 22:42:50 +00:00
|
|
|
*/
|
|
|
|
export function lazyWebpack<T = any>(filter: FilterFn): T {
|
|
|
|
const getMod = lazy(() => find(filter));
|
|
|
|
|
2022-10-08 17:23:05 +00:00
|
|
|
return new Proxy(() => null, {
|
2022-09-30 22:42:50 +00:00
|
|
|
get: (_, prop) => getMod()[prop],
|
2022-10-08 17:23:05 +00:00
|
|
|
set: (_, prop, value) => getMod()[prop] = value,
|
|
|
|
has: (_, prop) => prop in getMod(),
|
|
|
|
apply: (_, $this, args) => (getMod() as Function).apply($this, args),
|
|
|
|
ownKeys: () => Reflect.ownKeys(getMod()),
|
2022-10-08 19:11:14 +00:00
|
|
|
construct: (_, args, newTarget) => Reflect.construct(getMod(), args, newTarget),
|
2022-10-08 17:23:05 +00:00
|
|
|
deleteProperty: (_, prop) => delete getMod()[prop],
|
|
|
|
defineProperty: (_, property, attributes) => !!Object.defineProperty(getMod(), property, attributes)
|
2022-10-09 20:58:08 +00:00
|
|
|
}) as any as T;
|
2022-08-31 02:07:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Await a promise
|
|
|
|
* @param factory Factory
|
|
|
|
* @param fallbackValue The fallback value that will be used until the promise resolved
|
2022-08-31 18:47:07 +00:00
|
|
|
* @returns [value, error, isPending]
|
2022-08-31 02:07:16 +00:00
|
|
|
*/
|
2022-08-31 18:47:07 +00:00
|
|
|
export function useAwaiter<T>(factory: () => Promise<T>): [T | null, any, boolean];
|
|
|
|
export function useAwaiter<T>(factory: () => Promise<T>, fallbackValue: T): [T, any, boolean];
|
|
|
|
export function useAwaiter<T>(factory: () => Promise<T>, fallbackValue: T | null = null): [T | null, any, boolean] {
|
|
|
|
const [state, setState] = React.useState({
|
|
|
|
value: fallbackValue,
|
|
|
|
error: null as any,
|
|
|
|
pending: true
|
|
|
|
});
|
2022-08-31 02:07:16 +00:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
2022-08-31 18:47:07 +00:00
|
|
|
let isAlive = true;
|
|
|
|
factory()
|
|
|
|
.then(value => isAlive && setState({ value, error: null, pending: false }))
|
|
|
|
.catch(error => isAlive && setState({ value: null, error, pending: false }));
|
|
|
|
|
|
|
|
return () => void (isAlive = false);
|
2022-08-31 02:07:16 +00:00
|
|
|
}, []);
|
|
|
|
|
2022-08-31 18:47:07 +00:00
|
|
|
return [state.value, state.error, state.pending];
|
2022-10-08 18:36:57 +00:00
|
|
|
}
|
2022-08-31 02:07:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A lazy component. The factory method is called on first render. For example useful
|
|
|
|
* for const Component = LazyComponent(() => findByDisplayName("...").default)
|
|
|
|
* @param factory Function returning a Component
|
|
|
|
* @returns Result of factory function
|
|
|
|
*/
|
|
|
|
export function LazyComponent<T = any>(factory: () => React.ComponentType<T>) {
|
|
|
|
return (props: T) => {
|
|
|
|
const Component = React.useMemo(factory, []);
|
2022-09-30 22:42:50 +00:00
|
|
|
return <Component {...props as any /* I hate react typings ??? */} />;
|
2022-08-31 02:07:16 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-09-16 20:59:34 +00:00
|
|
|
* Recursively merges defaults into an object and returns the same object
|
2022-08-31 02:07:16 +00:00
|
|
|
* @param obj Object
|
|
|
|
* @param defaults Defaults
|
|
|
|
* @returns obj
|
|
|
|
*/
|
|
|
|
export function mergeDefaults<T>(obj: T, defaults: T): T {
|
|
|
|
for (const key in defaults) {
|
|
|
|
const v = defaults[key];
|
|
|
|
if (typeof v === "object" && !Array.isArray(v)) {
|
|
|
|
obj[key] ??= {} as any;
|
|
|
|
mergeDefaults(obj[key], v);
|
|
|
|
} else {
|
|
|
|
obj[key] ??= v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return obj;
|
2022-09-01 19:41:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Join an array of strings in a human readable way (1, 2 and 3)
|
|
|
|
* @param elements Elements
|
|
|
|
*/
|
|
|
|
export function humanFriendlyJoin(elements: string[]): string;
|
|
|
|
/**
|
|
|
|
* Join an array of strings in a human readable way (1, 2 and 3)
|
|
|
|
* @param elements Elements
|
|
|
|
* @param mapper Function that converts elements to a string
|
|
|
|
*/
|
|
|
|
export function humanFriendlyJoin<T>(elements: T[], mapper: (e: T) => string): string;
|
|
|
|
export function humanFriendlyJoin(elements: any[], mapper: (e: any) => string = s => s): string {
|
|
|
|
const { length } = elements;
|
|
|
|
if (length === 0) return "";
|
|
|
|
if (length === 1) return mapper(elements[0]);
|
|
|
|
|
|
|
|
let s = "";
|
|
|
|
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
s += mapper(elements[i]);
|
|
|
|
if (length - i > 2) s += ", ";
|
|
|
|
else if (length - i > 1) s += " and ";
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
2022-09-30 22:42:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calls .join(" ") on the arguments
|
|
|
|
* classes("one", "two") => "one two"
|
|
|
|
*/
|
|
|
|
export function classes(...classes: string[]) {
|
|
|
|
return classes.join(" ");
|
|
|
|
}
|
2022-10-09 17:48:42 +00:00
|
|
|
|
|
|
|
export function sleep(ms: number): Promise<void> {
|
|
|
|
return new Promise(r => setTimeout(r, ms));
|
|
|
|
}
|