Skip to content

Commit 8b8a3e4

Browse files
update
1 parent 462a334 commit 8b8a3e4

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

chrome/public/bundle/hook.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,7 @@
12301230
Dispatcher.useLayoutEffect(function () { });
12311231
Dispatcher.useInsertionEffect(function () { });
12321232
Dispatcher.useEffect(function () { });
1233+
Dispatcher.useOptimistic(null, function (s, a) { return s; });
12331234
Dispatcher.useImperativeHandle(undefined, function () { return null; });
12341235
Dispatcher.useDebugValue(null, function () { });
12351236
Dispatcher.useCallback(function () { });
@@ -1560,7 +1561,7 @@
15601561
if (hook && hook.type !== reactShared.HOOK_TYPE.useTransition) {
15611562
throw new Error("Invalid hook type, look like a bug for @my-react/devtools");
15621563
}
1563-
var isPending = hook ? hook.result[0] : false;
1564+
var isPending = hook ? (Array.isArray(hook.result) ? hook.result[0] : hook.result.value) : false;
15641565
hookLog.push({
15651566
displayName: null,
15661567
primitive: "Transition",
@@ -1615,6 +1616,24 @@
16151616
});
16161617
return [value, function () { }];
16171618
}
1619+
function useOptimistic(passthrough, reducer) {
1620+
var _a;
1621+
var hook = nextHook();
1622+
// TODO update
1623+
// @ts-ignore
1624+
if (hook && hook.type !== 16) {
1625+
throw new Error("Invalid hook type, look like a bug for @my-react/devtools");
1626+
}
1627+
var state = hook ? (_a = hook.result) === null || _a === void 0 ? void 0 : _a.value : passthrough;
1628+
hookLog.push({
1629+
displayName: null,
1630+
primitive: "Optimistic",
1631+
stackError: new Error(),
1632+
value: state,
1633+
dispatcherHookName: "Optimistic",
1634+
});
1635+
return [state, function (action) { }];
1636+
}
16181637
var Dispatcher = {
16191638
readContext: readContext,
16201639
use: use,
@@ -1634,6 +1653,7 @@
16341653
useSyncExternalStore: useSyncExternalStore,
16351654
useId: useId,
16361655
useSignal: useSignal,
1656+
useOptimistic: useOptimistic,
16371657
};
16381658
// create a proxy to throw a custom error
16391659
// in case future versions of React adds more hooks

chrome/public/bundle/proxy.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,7 @@
11301130
Dispatcher.useLayoutEffect(function () { });
11311131
Dispatcher.useInsertionEffect(function () { });
11321132
Dispatcher.useEffect(function () { });
1133+
Dispatcher.useOptimistic(null, function (s, a) { return s; });
11331134
Dispatcher.useImperativeHandle(undefined, function () { return null; });
11341135
Dispatcher.useDebugValue(null, function () { });
11351136
Dispatcher.useCallback(function () { });
@@ -1460,7 +1461,7 @@
14601461
if (hook && hook.type !== reactShared.HOOK_TYPE.useTransition) {
14611462
throw new Error("Invalid hook type, look like a bug for @my-react/devtools");
14621463
}
1463-
var isPending = hook ? hook.result[0] : false;
1464+
var isPending = hook ? (Array.isArray(hook.result) ? hook.result[0] : hook.result.value) : false;
14641465
hookLog.push({
14651466
displayName: null,
14661467
primitive: "Transition",
@@ -1515,6 +1516,24 @@
15151516
});
15161517
return [value, function () { }];
15171518
}
1519+
function useOptimistic(passthrough, reducer) {
1520+
var _a;
1521+
var hook = nextHook();
1522+
// TODO update
1523+
// @ts-ignore
1524+
if (hook && hook.type !== 16) {
1525+
throw new Error("Invalid hook type, look like a bug for @my-react/devtools");
1526+
}
1527+
var state = hook ? (_a = hook.result) === null || _a === void 0 ? void 0 : _a.value : passthrough;
1528+
hookLog.push({
1529+
displayName: null,
1530+
primitive: "Optimistic",
1531+
stackError: new Error(),
1532+
value: state,
1533+
dispatcherHookName: "Optimistic",
1534+
});
1535+
return [state, function (action) { }];
1536+
}
15181537
var Dispatcher = {
15191538
readContext: readContext,
15201539
use: use,
@@ -1534,6 +1553,7 @@
15341553
useSyncExternalStore: useSyncExternalStore,
15351554
useId: useId,
15361555
useSignal: useSignal,
1556+
useOptimistic: useOptimistic,
15371557
};
15381558
// create a proxy to throw a custom error
15391559
// in case future versions of React adds more hooks

packages/core/src/hook.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ function getPrimitiveStackCache(): Map<string, Array<any>> {
4343
Dispatcher.useLayoutEffect(() => {});
4444
Dispatcher.useInsertionEffect(() => {});
4545
Dispatcher.useEffect(() => {});
46+
Dispatcher.useOptimistic(null, (s: any, a: any) => s);
4647
Dispatcher.useImperativeHandle(undefined, () => null);
4748
Dispatcher.useDebugValue(null, () => {});
4849
Dispatcher.useCallback(() => {});
@@ -444,7 +445,7 @@ function useTransition(): [boolean, (callback: () => void, options?: any) => voi
444445
throw new Error("Invalid hook type, look like a bug for @my-react/devtools");
445446
}
446447

447-
const isPending = hook ? hook.result[0] : false;
448+
const isPending = hook ? (Array.isArray(hook.result) ? hook.result[0] : hook.result.value) : false;
448449

449450
hookLog.push({
450451
displayName: null,
@@ -517,6 +518,27 @@ function useSignal<T>(initial: T | (() => T)) {
517518
return [value, () => {}];
518519
}
519520

521+
function useOptimistic<S, A>(passthrough: S, reducer?: (S, A) => S): [S, (A) => void] {
522+
const hook = nextHook();
523+
524+
// TODO update
525+
// @ts-ignore
526+
if (hook && hook.type !== 16) {
527+
throw new Error("Invalid hook type, look like a bug for @my-react/devtools");
528+
}
529+
530+
const state = hook ? hook.result?.value : passthrough;
531+
532+
hookLog.push({
533+
displayName: null,
534+
primitive: "Optimistic",
535+
stackError: new Error(),
536+
value: state,
537+
dispatcherHookName: "Optimistic",
538+
});
539+
return [state, (action: A) => {}];
540+
}
541+
520542
const Dispatcher = {
521543
readContext,
522544

@@ -537,6 +559,7 @@ const Dispatcher = {
537559
useSyncExternalStore,
538560
useId,
539561
useSignal,
562+
useOptimistic,
540563
};
541564

542565
export type DispatcherType = typeof Dispatcher & { proxy: typeof Dispatcher | null };

0 commit comments

Comments
 (0)