@@ -96,9 +96,12 @@ const extensionConnector = {
9696 const api : Connection [ 'api' ] = {
9797 subscribe : vi . fn ( ( f : ( m : unknown ) => void ) => {
9898 subscribers . push ( f )
99- return ( ) => { }
99+ return ( ) => {
100+ subscribers . splice ( subscribers . indexOf ( f ) , 1 )
101+ }
100102 } ) ,
101103 unsubscribe : vi . fn ( ( ) => {
104+ subscribers . splice ( 0 )
102105 connectionMap . delete (
103106 areNameUndefinedMapsNeeded ? options . testConnectionId : key ,
104107 )
@@ -2687,6 +2690,136 @@ describe('when create devtools was called multiple times with `name` and `store`
26872690} )
26882691
26892692describe ( 'cleanup' , ( ) => {
2693+ it ( 'should preserve a replacement store when the original store is cleaned up' , ( ) => {
2694+ const options = {
2695+ name : 'replacement-before-cleanup' ,
2696+ store : 'shared' ,
2697+ enabled : true ,
2698+ }
2699+ const original = createStore ( devtools ( ( ) => ( { count : 0 } ) , options ) )
2700+ const replacement = createStore ( devtools ( ( ) => ( { count : 0 } ) , options ) )
2701+ const [ connection ] = getNamedConnectionApis ( options . name )
2702+
2703+ original . devtools . cleanup ( )
2704+
2705+ expect ( connection . unsubscribe ) . not . toHaveBeenCalled ( )
2706+ const [ subscriber ] = getNamedConnectionSubscribers ( options . name )
2707+ subscriber ( {
2708+ type : 'DISPATCH' ,
2709+ payload : { type : 'JUMP_TO_STATE' } ,
2710+ state : JSON . stringify ( { shared : { count : 20 } } ) ,
2711+ } )
2712+ expect ( replacement . getState ( ) ) . toEqual ( { count : 20 } )
2713+ expect ( original . getState ( ) ) . toEqual ( { count : 0 } )
2714+
2715+ original . devtools . cleanup ( )
2716+ expect ( connection . unsubscribe ) . not . toHaveBeenCalled ( )
2717+ replacement . devtools . cleanup ( )
2718+ expect ( connection . unsubscribe ) . toHaveBeenCalledTimes ( 1 )
2719+ } )
2720+
2721+ it . each ( [ 1 , 2 ] ) (
2722+ 'should preserve other stores on a shared connection after %i cleanup calls' ,
2723+ ( cleanupCalls ) => {
2724+ const name = `shared-cleanup-${ cleanupCalls } `
2725+ const store1 = createStore (
2726+ devtools ( ( ) => ( { count : 0 } ) , { name, store : 'first' , enabled : true } ) ,
2727+ )
2728+ const store2 = createStore (
2729+ devtools ( ( ) => ( { count : 0 } ) , {
2730+ name,
2731+ store : 'second' ,
2732+ enabled : true ,
2733+ } ) ,
2734+ )
2735+ const subscribers = namedConnections . get ( name ) ! . subscribers
2736+
2737+ for ( let i = 0 ; i < cleanupCalls ; i ++ ) {
2738+ store1 . devtools . cleanup ( )
2739+ }
2740+ subscribers . forEach ( ( subscriber ) =>
2741+ subscriber ( {
2742+ type : 'DISPATCH' ,
2743+ payload : { type : 'JUMP_TO_STATE' } ,
2744+ state : JSON . stringify ( {
2745+ first : { count : 10 } ,
2746+ second : { count : 20 } ,
2747+ } ) ,
2748+ } ) ,
2749+ )
2750+
2751+ expect ( store1 . getState ( ) ) . toEqual ( { count : 0 } )
2752+ expect ( store2 . getState ( ) ) . toEqual ( { count : 20 } )
2753+ store2 . devtools . cleanup ( )
2754+ } ,
2755+ )
2756+
2757+ it ( 'should close a shared connection only after its last store is cleaned up' , ( ) => {
2758+ const name = 'last-store-cleanup'
2759+ const store1 = createStore (
2760+ devtools ( ( ) => ( { count : 0 } ) , { name, store : 'first' , enabled : true } ) ,
2761+ )
2762+ const store2 = createStore (
2763+ devtools ( ( ) => ( { count : 0 } ) , { name, store : 'second' , enabled : true } ) ,
2764+ )
2765+ const [ connection ] = getNamedConnectionApis ( name )
2766+
2767+ store1 . devtools . cleanup ( )
2768+ expect ( connection . unsubscribe ) . not . toHaveBeenCalled ( )
2769+ store2 . devtools . cleanup ( )
2770+ expect ( connection . unsubscribe ) . toHaveBeenCalledTimes ( 1 )
2771+
2772+ const replacement = createStore (
2773+ devtools ( ( ) => ( { count : 0 } ) , { name, store : 'first' , enabled : true } ) ,
2774+ )
2775+ const [ subscriber ] = getNamedConnectionSubscribers ( name )
2776+ store1 . devtools . cleanup ( )
2777+ subscriber ( {
2778+ type : 'DISPATCH' ,
2779+ payload : { type : 'JUMP_TO_STATE' } ,
2780+ state : JSON . stringify ( { first : { count : 30 } } ) ,
2781+ } )
2782+
2783+ expect ( replacement . getState ( ) ) . toEqual ( { count : 30 } )
2784+ const connectCalls = extensionConnector . connect . mock . calls . length
2785+ const sibling = createStore (
2786+ devtools ( ( ) => ( { count : 0 } ) , { name, store : 'second' , enabled : true } ) ,
2787+ )
2788+ expect ( extensionConnector . connect ) . toHaveBeenCalledTimes ( connectCalls )
2789+
2790+ sibling . devtools . cleanup ( )
2791+ replacement . devtools . cleanup ( )
2792+ } )
2793+
2794+ it ( 'should close an untracked connection while a tracked connection uses the same name' , ( ) => {
2795+ const name = 'mixed-connection-cleanup'
2796+ const options = { name, testStore : 'untracked' , enabled : true }
2797+ const untrackedStore = createStore ( devtools ( ( ) => ( { count : 0 } ) , options ) )
2798+ const trackedStore = createStore (
2799+ devtools ( ( ) => ( { count : 0 } ) , { name, store : 'tracked' , enabled : true } ) ,
2800+ )
2801+ const [ untrackedConnection , trackedConnection ] = getNamedConnectionApis (
2802+ `${ name } |untracked` ,
2803+ name ,
2804+ )
2805+
2806+ untrackedStore . devtools . cleanup ( )
2807+
2808+ expect ( untrackedConnection . unsubscribe ) . toHaveBeenCalledTimes ( 1 )
2809+ expect ( trackedConnection . unsubscribe ) . not . toHaveBeenCalled ( )
2810+ const [ subscriber ] = getNamedConnectionSubscribers ( name )
2811+ subscriber ( {
2812+ type : 'DISPATCH' ,
2813+ payload : { type : 'JUMP_TO_STATE' } ,
2814+ state : JSON . stringify ( { tracked : { count : 20 } } ) ,
2815+ } )
2816+ expect ( trackedStore . getState ( ) ) . toEqual ( { count : 20 } )
2817+ expect ( untrackedStore . getState ( ) ) . toEqual ( { count : 0 } )
2818+
2819+ trackedStore . devtools . cleanup ( )
2820+ expect ( trackedConnection . unsubscribe ) . toHaveBeenCalledTimes ( 1 )
2821+ } )
2822+
26902823 it ( 'should unsubscribe from devtools when cleanup is called' , async ( ) => {
26912824 const options = { name : 'test' }
26922825 const store = createStore ( devtools ( ( ) => ( { count : 0 } ) , options ) )
0 commit comments