@@ -5,15 +5,15 @@ import { isSkeleton } from '../Accessor/skeleton';
55import { deepCopy , select } from '../Helpers' ;
66import { crawl } from './crawl' ;
77import {
8+ type CacheNormalizationHandler ,
89 deepNormalizeObject ,
910 defaultNormalizationHandler ,
10- type CacheNormalizationHandler ,
1111 type NormalizedObjectShell ,
1212} from './normalization' ;
1313import {
14+ type CacheSnapshot ,
1415 exportCacheSnapshot ,
1516 importCacheSnapshot ,
16- type CacheSnapshot ,
1717} from './persistence' ;
1818import { isCacheObject } from './utils' ;
1919
@@ -182,18 +182,34 @@ export class Cache {
182182 /** Subscription paths and it's listener function. */
183183 #subscriptions = new Map < readonly string [ ] , CacheListener > ( ) ;
184184
185+ /** Subscriptions for all paths. */
186+ #globalSubscriptions = new Set < CacheListener > ( ) ;
187+
185188 /** Subscription paths that reached a normalized object. */
186189 #normalizedSubscriptions = new MultiDict < CacheObject , CacheListener > ( ) ;
187190
188191 /** Subscribe to cache changes. */
189- subscribe ( paths : string [ ] , fn : CacheListener ) {
190- const pathsSnapshot = Object . freeze ( [ ...paths ] ) ;
192+ subscribe ( fn : CacheListener ) : ( ) => void ;
193+ subscribe ( paths : string [ ] , fn : CacheListener ) : ( ) => void ;
194+ subscribe ( arg1 : string [ ] | CacheListener , arg2 ?: CacheListener ) {
195+ if ( typeof arg1 === 'function' ) {
196+ this . #globalSubscriptions. add ( arg1 ) ;
197+
198+ return ( ) => {
199+ this . #globalSubscriptions. delete ( arg1 ) ;
200+ } ;
201+ }
202+
203+ if ( ! arg2 ) return ( ) => { } ;
204+
205+ const fn = arg2 ! ;
206+ const paths = Object . freeze ( arg1 ) ;
191207
192- this . #subscriptions. set ( pathsSnapshot , fn ) ;
193- this . #subscribeNormalized( pathsSnapshot , fn ) ;
208+ this . #subscriptions. set ( paths , fn ) ;
209+ this . #subscribeNormalized( paths , fn ) ;
194210
195211 return ( ) => {
196- this . #subscriptions. delete ( pathsSnapshot ) ;
212+ this . #subscriptions. delete ( paths ) ;
197213 this . #normalizedSubscriptions. delete ( fn ) ;
198214 } ;
199215 }
@@ -230,7 +246,7 @@ export class Cache {
230246 #notifySubscribers = ( value : CacheRoot ) => {
231247 // Collect all relevant listeners from both path selections and
232248 // normalized objects in a unique Set.
233- const listeners = new Set < CacheListener > ( ) ;
249+ const listeners = new Set < CacheListener > ( this . #globalSubscriptions ) ;
234250 const subs = this . #subscriptions;
235251 const nsubs = this . #normalizedSubscriptions;
236252 const getId = this . normalizationOptions ?. identity ;
0 commit comments