Skip to content

Commit 9638402

Browse files
committed
fix: roll back SSR behavior of error-boundary
1 parent 71fe085 commit 9638402

File tree

6 files changed

+17
-109
lines changed

6 files changed

+17
-109
lines changed

packages/core/src/boundary/boundary.tsx

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,44 @@
1-
import type { DarkElement, SlotProps, Callback, AppResource } from '../shared';
2-
import { detectIsFunction, createError } from '../utils';
1+
import type { DarkElement, SlotProps, Callback } from '../shared';
32
import { __useCursor as useCursor } from '../internal';
3+
import { detectIsFunction } from '../utils';
44
import { useUpdate } from '../use-update';
55
import { useEffect } from '../use-effect';
66
import { component } from '../component';
77
import { useState } from '../use-state';
88
import { useEvent } from '../use-event';
9-
import { useMemo } from '../use-memo';
10-
import { $$scope } from '../scope';
119

12-
function useError(__id?: number): [Error | null, Callback] {
10+
function useError(): [Error | null, Callback] {
1311
const cursor = useCursor();
14-
const $scope = $$scope();
15-
const inBoundary = cursor.parent?.hook?.getIsBoundary();
16-
const res = inBoundary ? $scope.getResource(__id) : null;
17-
const [error, setError] = useState<Error>(() => (inBoundary ? init(res) : null));
18-
const reset = useEvent(() => {
19-
inBoundary && $scope.setResource(__id, null);
20-
setError(null);
21-
});
12+
const update = useUpdate();
13+
const [error, setError] = useState<Error>(null);
14+
const reset = useEvent(() => setError(null));
2215

23-
if (inBoundary) {
24-
cursor.parent.hook.setCatch(setError);
25-
} else {
26-
cursor.hook.setCatch(setError);
27-
}
16+
cursor.hook.setIsBoundary(true);
17+
cursor.hook.setCatch(setError);
18+
cursor.hook.setUpdate(update);
2819

2920
return [error, reset];
3021
}
3122

32-
const init = (res: AppResource) => (res?.[1] ? createError(res?.[1]) : null);
33-
3423
type ErrorBoundaryProps = {
3524
fallback?: DarkElement;
3625
renderFallback?: (x: RenderFallbackOptions) => DarkElement;
3726
onError?: (e: Error) => void;
3827
} & Required<SlotProps>;
3928

4029
const ErrorBoundary = component<ErrorBoundaryProps>(
41-
props => {
42-
const cursor = useCursor();
43-
const update = useUpdate();
44-
const $scope = $$scope();
45-
const id = useMemo(() => $scope.getNextResourceId(), []);
46-
47-
cursor.hook.setIsBoundary(true);
48-
cursor.hook.setUpdate(update);
49-
cursor.hook.setLevel($scope.getMountLevel());
50-
cursor.hook.setResId(id);
51-
52-
return <ErrorBoundaryInternal {...props} id={id} />;
53-
},
54-
{
55-
displayName: 'ErrorBoundary',
56-
},
57-
);
58-
59-
type ErrorBoundaryInternalProps = {
60-
id: number;
61-
} & ErrorBoundaryProps;
62-
63-
const ErrorBoundaryInternal = component<ErrorBoundaryInternalProps>(
64-
({ id, fallback = null, renderFallback, onError, slot }) => {
65-
const [error, reset] = useError(id);
30+
({ fallback = null, renderFallback, onError, slot }) => {
31+
const [error, reset] = useError();
6632

6733
useEffect(() => {
6834
detectIsFunction(onError) && onError(error);
6935
}, [error]);
7036

7137
return error ? (detectIsFunction(renderFallback) ? renderFallback({ error, reset }) : fallback) : slot;
7238
},
73-
{ displayName: 'ErrorBoundaryInternal' },
39+
{
40+
displayName: 'ErrorBoundary',
41+
},
7442
);
7543

7644
type RenderFallbackOptions = {

packages/core/src/fiber/fiber.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -187,24 +187,6 @@ class Hook<T = unknown> {
187187
getPendings() {
188188
return this.box?.pendings;
189189
}
190-
191-
setResId(x: number) {
192-
this.__box();
193-
this.box.resId = x;
194-
}
195-
196-
getResId() {
197-
return this.box?.resId;
198-
}
199-
200-
setLevel(x: number) {
201-
this.__box();
202-
this.box.level = x;
203-
}
204-
205-
getLevel() {
206-
return this.box?.level;
207-
}
208190
}
209191

210192
function getHook(alt: Fiber, prevInst: Instance, nextInst: Instance): Hook | null {
@@ -220,8 +202,6 @@ type Box = {
220202
catch?: Catch;
221203
pendings?: number;
222204
update?: Callback;
223-
resId?: number;
224-
level?: number;
225205
};
226206

227207
type Batch = {

packages/core/src/scope/scope.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,6 @@ class Scope {
156156
this.mountLevel = this.mountLevel - 1;
157157
}
158158

159-
setMount(level: number) {
160-
this.mountLevel = level;
161-
this.mountNav[this.mountLevel] = 0;
162-
}
163-
164159
navToPrev() {
165160
const idx = this.getMountIndex();
166161

@@ -173,10 +168,6 @@ class Scope {
173168
}
174169
}
175170

176-
getMountLevel() {
177-
return this.mountLevel;
178-
}
179-
180171
getMountIndex() {
181172
return this.mountNav[this.mountLevel];
182173
}

packages/core/src/utils/utils.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,7 @@ const createIndexKey = (idx: number) => `${INDEX_KEY}:${idx}`;
9999

100100
const mapRecord = <T extends object>(record: T) => keys(record).map(x => record[x]);
101101

102-
function createError(reason: unknown) {
103-
return reason instanceof Error ? reason : new Error(String(reason).replace('Error: ', ''));
104-
}
102+
const createError = (x: unknown) => (x instanceof Error ? x : new Error(String(x)));
105103

106104
export {
107105
detectIsFunction,

packages/core/src/workloop/workloop.ts

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ function mount(fiber: Fiber, prev: Fiber, $scope: Scope) {
411411
throw promise;
412412
}
413413
} else {
414-
handleAsync(promise, boundary, reset, $scope);
414+
reset();
415415
throw promise;
416416
}
417417
} else {
@@ -432,19 +432,6 @@ function mount(fiber: Fiber, prev: Fiber, $scope: Scope) {
432432
return inst;
433433
}
434434

435-
async function handleAsync(promise: Promise<unknown>, boundary: Fiber, reset: Callback, $scope: Scope) {
436-
let isRejected = false;
437-
438-
try {
439-
await promise;
440-
} catch (reason) {
441-
isRejected = true;
442-
boundary && restartFromBoundary(boundary, reason, $scope);
443-
} finally {
444-
(!isRejected || !boundary) && reset();
445-
}
446-
}
447-
448435
const createReset = (fiber: Fiber, prev: Fiber, $scope: Scope) => () => {
449436
if (prev) {
450437
fiber.hook.owner = null;
@@ -458,17 +445,6 @@ const createReset = (fiber: Fiber, prev: Fiber, $scope: Scope) => () => {
458445
}
459446
};
460447

461-
function restartFromBoundary(fiber: Fiber, reason: unknown, $scope: Scope) {
462-
const resId = fiber.hook.getResId();
463-
464-
fiber.child = null;
465-
fiber.cec = 0;
466-
Fiber.setNextId(fiber.id);
467-
$scope.setMount(fiber.hook.getLevel());
468-
$scope.setNextUnitOfWork(fiber);
469-
$scope.setResource(resId, [null, reason instanceof Error ? reason.stack : (reason as string)]);
470-
}
471-
472448
function extractKeys(alt: Fiber, children: Array<Instance>) {
473449
let nextFiber = alt;
474450
let idx = 0;

packages/data/src/use-query/use-query.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,7 @@ function useQuery<T, V extends Variables>(key: string, query: Query<T, V>, optio
9494
cache.__emit({ type: 'query', phase: 'error', key, id: $cacheId, data: error, initiator });
9595

9696
if (isServer) {
97-
if (inBoundary) {
98-
throwThis(error);
99-
} else {
100-
logError(error);
101-
}
102-
97+
logError(error);
10398
$scope.setResource(id, [null, String(error)]);
10499
} else {
105100
if (inBoundary && !isStateOnly) {

0 commit comments

Comments
 (0)