-
-
Notifications
You must be signed in to change notification settings - Fork 883
/
flow.ts
282 lines (274 loc) · 9.09 KB
/
flow.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import {
createMemo,
untrack,
createSignal,
catchError,
children,
Accessor,
Setter,
onCleanup,
MemoOptions
} from "../reactive/signal.js";
import { mapArray, indexArray } from "../reactive/array.js";
import { sharedConfig } from "./hydration.js";
import type { JSX } from "../jsx.js";
const narrowedError = (name: string) =>
"_SOLID_DEV_"
? `Attempting to access a stale value from <${name}> that could possibly be undefined. This may occur because you are reading the accessor returned from the component at a time where it has already been unmounted. We recommend cleaning up any stale timers or async, or reading from the initial condition.`
: `Stale read from <${name}>.`;
/**
* Creates a list elements from a list
*
* it receives a map function as its child that receives a list element and an accessor with the index and returns a JSX-Element; if the list is empty, an optional fallback is returned:
* ```typescript
* <For each={items} fallback={<div>No items</div>}>
* {(item, index) => <div data-index={index()}>{item}</div>}
* </For>
* ```
* If you have a list with fixed indices and changing values, consider using `<Index>` instead.
*
* @description https://www.solidjs.com/docs/latest/api#for
*/
export function For<T extends readonly any[], U extends JSX.Element>(props: {
each: T | undefined | null | false;
fallback?: JSX.Element;
children: (item: T[number], index: Accessor<number>) => U;
}) {
const fallback = "fallback" in props && { fallback: () => props.fallback };
return ("_SOLID_DEV_"
? createMemo(
mapArray(() => props.each, props.children, fallback || undefined),
undefined,
{ name: "value" }
)
: createMemo(
mapArray(() => props.each, props.children, fallback || undefined)
)) as unknown as JSX.Element;
}
/**
* Non-keyed iteration over a list creating elements from its items
*
* To be used if you have a list with fixed indices, but changing values.
* ```typescript
* <Index each={items} fallback={<div>No items</div>}>
* {(item, index) => <div data-index={index}>{item()}</div>}
* </Index>
* ```
* If you have a list with changing indices, better use `<For>`.
*
* @description https://www.solidjs.com/docs/latest/api#index
*/
export function Index<T extends readonly any[], U extends JSX.Element>(props: {
each: T | undefined | null | false;
fallback?: JSX.Element;
children: (item: Accessor<T[number]>, index: number) => U;
}) {
const fallback = "fallback" in props && { fallback: () => props.fallback };
return ("_SOLID_DEV_"
? createMemo(
indexArray(() => props.each, props.children, fallback || undefined),
undefined,
{ name: "value" }
)
: createMemo(
indexArray(() => props.each, props.children, fallback || undefined)
)) as unknown as JSX.Element;
}
type RequiredParameter<T> = T extends () => unknown ? never : T;
/**
* Conditionally render its children or an optional fallback component
* @description https://www.solidjs.com/docs/latest/api#show
*/
export function Show<
T,
TRenderFunction extends (item: Accessor<NonNullable<T>>) => JSX.Element
>(props: {
when: T | undefined | null | false;
keyed?: false;
fallback?: JSX.Element;
children: JSX.Element | RequiredParameter<TRenderFunction>;
}): JSX.Element;
export function Show<T, TRenderFunction extends (item: NonNullable<T>) => JSX.Element>(props: {
when: T | undefined | null | false;
keyed: true;
fallback?: JSX.Element;
children: JSX.Element | RequiredParameter<TRenderFunction>;
}): JSX.Element;
export function Show<T>(props: {
when: T | undefined | null | false;
keyed?: boolean;
fallback?: JSX.Element;
children: JSX.Element | ((item: NonNullable<T> | Accessor<NonNullable<T>>) => JSX.Element);
}): JSX.Element {
const keyed = props.keyed;
const condition = createMemo<T | undefined | null | boolean>(
() => props.when,
undefined,
"_SOLID_DEV_"
? {
equals: (a, b) => (keyed ? a === b : !a === !b),
name: "condition"
}
: { equals: (a, b) => (keyed ? a === b : !a === !b) }
);
return createMemo(
() => {
const c = condition();
if (c) {
const child = props.children;
const fn = typeof child === "function" && child.length > 0;
return fn
? untrack(() =>
(child as any)(
keyed
? (c as T)
: () => {
if (!untrack(condition)) throw narrowedError("Show");
return props.when;
}
)
)
: child;
}
return props.fallback;
},
undefined,
"_SOLID_DEV_" ? { name: "value" } : undefined
) as unknown as JSX.Element;
}
type EvalConditions = readonly [number, unknown?, MatchProps<unknown>?];
/**
* Switches between content based on mutually exclusive conditions
* ```typescript
* <Switch fallback={<FourOhFour />}>
* <Match when={state.route === 'home'}>
* <Home />
* </Match>
* <Match when={state.route === 'settings'}>
* <Settings />
* </Match>
* </Switch>
* ```
* @description https://www.solidjs.com/docs/latest/api#switchmatch
*/
export function Switch(props: { fallback?: JSX.Element; children: JSX.Element }): JSX.Element {
let keyed = false;
const equals: MemoOptions<EvalConditions>["equals"] = (a, b) =>
(keyed ? a[1] === b[1] : !a[1] === !b[1]) && a[2] === b[2];
const conditions = children(() => props.children) as unknown as () => MatchProps<unknown>[],
evalConditions = createMemo(
(): EvalConditions => {
let conds = conditions();
if (!Array.isArray(conds)) conds = [conds];
for (let i = 0; i < conds.length; i++) {
const c = conds[i].when;
if (c) {
keyed = !!conds[i].keyed;
return [i, c, conds[i]];
}
}
return [-1];
},
undefined,
"_SOLID_DEV_" ? { equals, name: "eval conditions" } : { equals }
);
return createMemo(
() => {
const [index, when, cond] = evalConditions();
if (index < 0) return props.fallback;
const c = cond!.children;
const fn = typeof c === "function" && c.length > 0;
return fn
? untrack(() =>
(c as any)(
keyed
? when
: () => {
if (untrack(evalConditions)[0] !== index) throw narrowedError("Match");
return cond!.when;
}
)
)
: c;
},
undefined,
"_SOLID_DEV_" ? { name: "value" } : undefined
) as unknown as JSX.Element;
}
export type MatchProps<T> = {
when: T | undefined | null | false;
keyed?: boolean;
children: JSX.Element | ((item: NonNullable<T> | Accessor<NonNullable<T>>) => JSX.Element);
};
/**
* Selects a content based on condition when inside a `<Switch>` control flow
* ```typescript
* <Match when={condition()}>
* <Content/>
* </Match>
* ```
* @description https://www.solidjs.com/docs/latest/api#switchmatch
*/
export function Match<
T,
TRenderFunction extends (item: Accessor<NonNullable<T>>) => JSX.Element
>(props: {
when: T | undefined | null | false;
keyed?: false;
children: JSX.Element | RequiredParameter<TRenderFunction>;
}): JSX.Element;
export function Match<T, TRenderFunction extends (item: NonNullable<T>) => JSX.Element>(props: {
when: T | undefined | null | false;
keyed: true;
children: JSX.Element | RequiredParameter<TRenderFunction>;
}): JSX.Element;
export function Match<T>(props: MatchProps<T>) {
return props as unknown as JSX.Element;
}
let Errors: Set<Setter<any>>;
export function resetErrorBoundaries() {
Errors && [...Errors].forEach(fn => fn());
}
/**
* Catches uncaught errors inside components and renders a fallback content
*
* Also supports a callback form that passes the error and a reset function:
* ```typescript
* <ErrorBoundary fallback={
* (err, reset) => <div onClick={reset}>Error: {err.toString()}</div>
* }>
* <MyComp />
* </ErrorBoundary>
* ```
* Errors thrown from the fallback can be caught by a parent ErrorBoundary
*
* @description https://www.solidjs.com/docs/latest/api#errorboundary
*/
export function ErrorBoundary(props: {
fallback: JSX.Element | ((err: any, reset: () => void) => JSX.Element);
children: JSX.Element;
}): JSX.Element {
let err;
if (sharedConfig!.context && sharedConfig!.load)
err = sharedConfig.load(sharedConfig.context.id + sharedConfig.context.count);
const [errored, setErrored] = createSignal<any>(
err,
"_SOLID_DEV_" ? { name: "errored" } : undefined
);
Errors || (Errors = new Set());
Errors.add(setErrored);
onCleanup(() => Errors.delete(setErrored));
return createMemo(
() => {
let e: any;
if ((e = errored())) {
const f = props.fallback;
if ("_SOLID_DEV_" && (typeof f !== "function" || f.length == 0)) console.error(e);
return typeof f === "function" && f.length ? untrack(() => f(e, () => setErrored())) : f;
}
return catchError(() => props.children, setErrored);
},
undefined,
"_SOLID_DEV_" ? { name: "value" } : undefined
) as unknown as JSX.Element;
}