@@ -5,15 +5,15 @@ import { isSkeleton } from '../Accessor/skeleton';
5
5
import { deepCopy , select } from '../Helpers' ;
6
6
import { crawl } from './crawl' ;
7
7
import {
8
+ type CacheNormalizationHandler ,
8
9
deepNormalizeObject ,
9
10
defaultNormalizationHandler ,
10
- type CacheNormalizationHandler ,
11
11
type NormalizedObjectShell ,
12
12
} from './normalization' ;
13
13
import {
14
+ type CacheSnapshot ,
14
15
exportCacheSnapshot ,
15
16
importCacheSnapshot ,
16
- type CacheSnapshot ,
17
17
} from './persistence' ;
18
18
import { isCacheObject } from './utils' ;
19
19
@@ -182,18 +182,32 @@ export class Cache {
182
182
/** Subscription paths and it's listener function. */
183
183
#subscriptions = new Map < readonly string [ ] , CacheListener > ( ) ;
184
184
185
+ /** Subscriptions for all paths. */
186
+ #globalSubscriptions = new Set < CacheListener > ( ) ;
187
+
185
188
/** Subscription paths that reached a normalized object. */
186
189
#normalizedSubscriptions = new MultiDict < CacheObject , CacheListener > ( ) ;
187
190
188
191
/** 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
+ const fn = arg2 ! ;
204
+ const paths = Object . freeze ( arg1 ) ;
191
205
192
- this . #subscriptions. set ( pathsSnapshot , fn ) ;
193
- this . #subscribeNormalized( pathsSnapshot , fn ) ;
206
+ this . #subscriptions. set ( paths , fn ) ;
207
+ this . #subscribeNormalized( paths , fn ) ;
194
208
195
209
return ( ) => {
196
- this . #subscriptions. delete ( pathsSnapshot ) ;
210
+ this . #subscriptions. delete ( paths ) ;
197
211
this . #normalizedSubscriptions. delete ( fn ) ;
198
212
} ;
199
213
}
@@ -230,7 +244,7 @@ export class Cache {
230
244
#notifySubscribers = ( value : CacheRoot ) => {
231
245
// Collect all relevant listeners from both path selections and
232
246
// normalized objects in a unique Set.
233
- const listeners = new Set < CacheListener > ( ) ;
247
+ const listeners = new Set < CacheListener > ( this . #globalSubscriptions ) ;
234
248
const subs = this . #subscriptions;
235
249
const nsubs = this . #normalizedSubscriptions;
236
250
const getId = this . normalizationOptions ?. identity ;
0 commit comments