helpers: import some helpers from iOS.

master
Tom Hacohen 4 years ago
parent 14fd5832f5
commit 430d313be1

@ -127,6 +127,34 @@ export function* arrayToChunkIterator<T>(arr: T[], size: number) {
} }
} }
export function isPromise(x: any): x is Promise<any> {
return x && typeof x.then === "function";
}
export function isDefined<T>(x: T | undefined): x is T {
return x !== undefined;
}
export function startTask<T = any>(func: () => Promise<T> | T, delay = 0): Promise<T> {
return new Promise((resolve, reject) => {
setTimeout(
() => {
try {
const ret = func();
if (isPromise(ret)) {
ret.then(resolve)
.catch(reject);
} else {
resolve(ret);
}
} catch (e) {
reject(e);
}
},
delay);
});
}
export function usePromiseMemo<T>(promise: Promise<T> | undefined | null, deps: React.DependencyList, initial: T | undefined = undefined): T | undefined { export function usePromiseMemo<T>(promise: Promise<T> | undefined | null, deps: React.DependencyList, initial: T | undefined = undefined): T | undefined {
const [val, setVal] = React.useState<T>((promise as any)._returnedValue ?? initial); const [val, setVal] = React.useState<T>((promise as any)._returnedValue ?? initial);
React.useEffect(() => { React.useEffect(() => {

Loading…
Cancel
Save