forked from adamhaile/surplus-realworld
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoading.tsx
More file actions
15 lines (14 loc) · 754 Bytes
/
Loading.tsx
File metadata and controls
15 lines (14 loc) · 754 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import S from 's-js';
import * as Surplus from 'surplus';
/**
* Re-usable loading widget, displays a placeholder while the given task is still executing.
*
* @param task - signal bearing an executing task
* @param placeholder (optional) - placeholder to display while task is executing, defaults to text "Loading..."
* @param children - function which takes result value of completed task and renders final content
*/
export function Loading<T>({ task, placeholder = "Loading ...", children } : { task : () => Promise<T>, placeholder? : JSX.Children, children: (data : T) => JSX.Children }) {
const data = S.value(null as null | T);
S(() => { data(null); task().then(data); });
return () => data() ? children(data()!) : placeholder;
}