@@ -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+
520542const Dispatcher = {
521543 readContext,
522544
@@ -537,6 +559,7 @@ const Dispatcher = {
537559 useSyncExternalStore,
538560 useId,
539561 useSignal,
562+ useOptimistic,
540563} ;
541564
542565export type DispatcherType = typeof Dispatcher & { proxy : typeof Dispatcher | null } ;
0 commit comments