@sigil-dev/runtime 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ Tiny runtime implementation
package/boundary.ts ADDED
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Error boundary for Sigil components.
3
+ *
4
+ * Wraps children so that errors during rendering are caught
5
+ * and handled instead of crashing the app.
6
+ *
7
+ * Usage:
8
+ * <Boundary
9
+ * fallbackError={({ error, reset }) => <button onClick={reset}>Retry</button>}
10
+ * onError={(e) => reportToSentry(e)}
11
+ * >
12
+ * <FlakyComponent />
13
+ * </Boundary>
14
+ */
15
+
16
+ import { createSignal } from "./index.js";
17
+
18
+ interface BoundaryProps {
19
+ children: any;
20
+ fallbackError?: (ctx: { error: Error; reset: () => void }) => any;
21
+ onError?: (error: Error) => void;
22
+ }
23
+
24
+ export function Boundary(props: BoundaryProps): any {
25
+ const error = createSignal<Error | null>(null);
26
+
27
+ const reset = () => error.set(null);
28
+
29
+ if (error()) {
30
+ return props.fallbackError
31
+ ? props.fallbackError({ error: error()!, reset })
32
+ : null;
33
+ }
34
+
35
+ try {
36
+ return props.children;
37
+ } catch (e) {
38
+ props.onError?.(e as Error);
39
+ error.set(e as Error);
40
+ return props.fallbackError
41
+ ? props.fallbackError({ error: e as Error, reset })
42
+ : null;
43
+ }
44
+ }