@@ -2714,10 +2714,44 @@ export function deep<T>(value: T): T {
27142714 walkT ( ct ) ;
27152715 }
27162716 } ;
2717+ // One pass where the tracked view is the snapshot's backing; else two walks.
2718+ const seen = new Map < object , any > ( ) ;
2719+ const fast = ( t : StoreTarget , raw : object ) : object | undefined => {
2720+ if ( t . _fam !== null || t . ch || t . ovl || ( raw !== t . px && lookupTarget ( raw , null ) !== t ) ) return ;
2721+ const src = enumerationSource ( t ) ;
2722+ if ( src !== snapshotBacking ( t ) || visited . has ( src ) || seen . has ( src ) || ! isWrappable ( src ) )
2723+ return ;
2724+ // snapshotWalk's resolution must stop at `src` too.
2725+ if ( src !== raw ) {
2726+ const st = lookupTarget ( src , null ) ;
2727+ if ( st !== undefined && st !== t ) return ;
2728+ }
2729+ visited . add ( src ) ;
2730+ readNode ( getDeepNode ( t ) ) ;
2731+ return src ;
2732+ } ;
2733+ const step : DeepStep = ( t , cv , key , trackOnly ) => {
2734+ const ct = childTarget ( t , cv , key ) ;
2735+ const src = ct && ! trackOnly ? fast ( ct , cv ) : undefined ;
2736+ if ( src ) return snapshotRecord ( src , seen , null , ct ! , step ) ;
2737+ if ( ct ) walkT ( ct ) ;
2738+ if ( ! trackOnly ) return snapshotWalk ( cv , seen , null ) ;
2739+ } ;
2740+ try {
2741+ const src = fast ( t0 , value as object ) ;
2742+ if ( src ) return snapshotRecord ( src , seen , null , t0 , step ) ;
2743+ } catch ( e ) {
2744+ // A snapshot read threw mid-pass: subscribe the rest, as the two walks did.
2745+ visited . clear ( ) ;
2746+ walkT ( t0 ) ;
2747+ throw e ;
2748+ }
27172749 walkT ( t0 ) ;
2718- return snapshot ( value ) ;
2750+ return snapshotWalk ( value , seen , t0 . _fam ) ;
27192751}
27202752
2753+ type DeepStep = ( t : StoreTarget , cv : object , key : PropertyKey , trackOnly ?: boolean ) => any ;
2754+
27212755/** Non-tracking snapshot: source identity for unowned subtrees (zero copy),
27222756 * a copy for owned ones (they are by definition modified relative to the
27232757 * source). Sees the pending backing (R27). */
@@ -2726,6 +2760,22 @@ export function snapshot<T>(value: T): T {
27262760 return snapshotWalk ( value , new Map ( ) , t ?. _fam ?? null ) ;
27272761}
27282762
2763+ /** R27: a snapshot sees the batch's pending backing — a held one (another
2764+ * transaction's future) only from its own draft, or from a pass the
2765+ * container node serves the future (its landing flush, a memo joining). */
2766+ function snapshotBacking ( t : StoreTarget ) : Record < PropertyKey , any > {
2767+ let backing = t . v ;
2768+ const pb = t . pb ;
2769+ if ( pb !== null ) {
2770+ const k = t . k ;
2771+ if ( inDraft ( t ) || writeOverride || k === null || ! ( k . _config & CONFIG_HELD ) ) backing = pb ;
2772+ else if ( stagingReader ( ) !== null ) backing = asBacking ( untrack ( ( ) => readNode ( k ) ) ) ;
2773+ // materializePB is a no-op (returns pb) without an overlay.
2774+ if ( backing === pb ) backing = materializePB ( t ) ;
2775+ }
2776+ return optHooks && optRead ( t ) ? optHooks . view ( t , backing ) : backing ;
2777+ }
2778+
27292779function snapshotWalk ( value : any , seen : Map < object , any > , fam : StoreFamily | null ) : any {
27302780 if ( value === null || typeof value !== "object" ) return value ;
27312781 let src = value ;
@@ -2740,39 +2790,42 @@ function snapshotWalk(value: any, seen: Map<object, any>, fam: StoreFamily | nul
27402790 if ( outer !== undefined ) t = outer ;
27412791 }
27422792 if ( t . _fam !== null ) fam = t . _fam ;
2743- // R27: a snapshot sees the batch's pending backing — a held one (another
2744- // transaction's future) only from its own draft, or from a pass the
2745- // container node serves the future (its landing flush, a memo joining).
2746- let backing = t . v ;
2747- const pb = t . pb ;
2748- if ( pb !== null ) {
2749- const k = t . k ;
2750- if ( inDraft ( t ) || writeOverride || k === null || ! ( k . _config & CONFIG_HELD ) ) backing = pb ;
2751- else if ( stagingReader ( ) !== null ) backing = asBacking ( untrack ( ( ) => readNode ( k ) ) ) ;
2752- // materializePB is a no-op (returns pb) without an overlay.
2753- if ( backing === pb ) backing = materializePB ( t ) ;
2754- }
2755- if ( optHooks && optRead ( t ) ) backing = optHooks . view ( t , backing ) ;
2793+ const backing = snapshotBacking ( t ) ;
27562794 if ( backing === src ) break ;
27572795 src = backing ;
27582796 }
27592797 if ( ! isWrappable ( src ) ) return src ;
27602798 const cached = seen . get ( src ) ;
2761- if ( cached !== undefined ) return cached ;
2799+ return cached !== undefined ? cached : snapshotRecord ( src , seen , fam , null , null ) ;
2800+ }
2801+
2802+ /** `t`/`step` set: `deep()`'s one pass, which also tracks `t`'s children. */
2803+ function snapshotRecord (
2804+ src : any ,
2805+ seen : Map < object , any > ,
2806+ fam : StoreFamily | null ,
2807+ t : StoreTarget | null ,
2808+ step : DeepStep | null
2809+ ) : any {
27622810 if ( isOwned ( src ) ) {
27632811 const isArr = Array . isArray ( src ) ;
27642812 const copy : any = isArr ? [ ] : Object . create ( Object . getPrototypeOf ( src ) ) ;
27652813 seen . set ( src , copy ) ;
27662814 for ( const key of Reflect . ownKeys ( src ) ) {
27672815 if ( ( isArr && key === "length" ) || key === $OWNER ) continue ;
27682816 const desc = Object . getOwnPropertyDescriptor ( src , key ) ! ;
2769- if ( typeof key === "symbol" && ! desc . enumerable ) continue ;
2817+ const cv = desc . value ;
2818+ const obj = cv !== null && typeof cv === "object" ;
2819+ if ( typeof key === "symbol" && ! desc . enumerable ) {
2820+ if ( step && obj ) step ( t ! , cv , key , true ) ;
2821+ continue ;
2822+ }
27702823 if ( desc . get || desc . set ) {
2824+ if ( t ) t . a = true ;
27712825 Object . defineProperty ( copy , key , desc ) ;
27722826 continue ;
27732827 }
2774- const cv = desc . value ;
2775- const walked = cv !== null && typeof cv === "object" ? snapshotWalk ( cv , seen , fam ) : cv ;
2828+ const walked = obj ? ( step ? step ( t ! , cv , key ) : snapshotWalk ( cv , seen , fam ) ) : cv ;
27762829 if ( desc . enumerable && desc . writable && desc . configurable ) copy [ key ] = walked ;
27772830 else Object . defineProperty ( copy , key , { ...desc , value : walked } ) ;
27782831 }
@@ -2783,10 +2836,14 @@ function snapshotWalk(value: any, seen: Map<object, any>, fam: StoreFamily | nul
27832836 let copy : any = null ;
27842837 for ( const key of Reflect . ownKeys ( src ) ) {
27852838 const desc = Object . getOwnPropertyDescriptor ( src , key ) ;
2786- if ( ! desc || desc . get || desc . set ) continue ;
2839+ if ( ! desc ) continue ;
2840+ if ( desc . get || desc . set ) {
2841+ if ( t ) t . a = true ;
2842+ continue ;
2843+ }
27872844 const cv = desc . value ;
27882845 if ( cv === null || typeof cv !== "object" ) continue ;
2789- const walked = snapshotWalk ( cv , seen , fam ) ;
2846+ const walked = step ? step ( t ! , cv , key ) : snapshotWalk ( cv , seen , fam ) ;
27902847 if ( walked !== cv ) {
27912848 if ( copy === null ) {
27922849 copy = Array . isArray ( src )
0 commit comments