@@ -15,9 +15,9 @@ import {
15
15
} from 'packages/core/src/utils/errors/MetaBindErrors' ;
16
16
import type { PropPath } from 'packages/core/src/utils/prop/PropPath' ;
17
17
import { PropUtils } from 'packages/core/src/utils/prop/PropUtils' ;
18
- import type { Signal } from 'packages/core/src/utils/Signal' ;
18
+ import type { Writable } from 'packages/core/src/utils/Signal' ;
19
19
20
- export const METADATA_CACHE_UPDATE_CYCLE_THRESHOLD = 5 ; // {syncInterval (200)} * 5 = 1s
20
+ export const METADATA_CACHE_EXTERNAL_WRITE_LOCK_DURATION = 5 ; // {syncInterval (200)} * 5 = 1s
21
21
export const METADATA_CACHE_INACTIVE_CYCLE_THRESHOLD = 5 * 60 ; // {syncInterval (200)} * 5 * 60 = 1 minute
22
22
23
23
export type MetadataSource = IMetadataSource < IMetadataCacheItem > ;
@@ -124,7 +124,7 @@ export class MetadataManager {
124
124
125
125
public subscribe (
126
126
uuid : string ,
127
- callbackSignal : Signal < unknown > ,
127
+ callbackSignal : Writable < unknown > ,
128
128
bindTarget : BindTargetDeclaration ,
129
129
onDelete : ( ) => void ,
130
130
) : MetadataSubscription {
@@ -153,7 +153,7 @@ export class MetadataManager {
153
153
*/
154
154
public subscribeComputed (
155
155
uuid : string ,
156
- callbackSignal : Signal < unknown > ,
156
+ callbackSignal : Writable < unknown > ,
157
157
bindTarget : BindTargetDeclaration | undefined ,
158
158
dependencies : ComputedSubscriptionDependency [ ] ,
159
159
computeFunction : ComputeFunction ,
@@ -194,7 +194,7 @@ export class MetadataManager {
194
194
195
195
const cacheItem = source . unsubscribe ( subscription ) ;
196
196
if ( cacheItem . subscriptions . length === 0 ) {
197
- cacheItem . inactive = true ;
197
+ cacheItem . cyclesWithoutListeners = 0 ;
198
198
}
199
199
}
200
200
@@ -213,8 +213,7 @@ export class MetadataManager {
213
213
}
214
214
215
215
const cacheItem = source . subscribe ( subscription ) ;
216
- cacheItem . inactive = false ;
217
- cacheItem . cyclesSinceInactive = 0 ;
216
+ cacheItem . cyclesWithoutListeners = 0 ;
218
217
219
218
subscription . notify ( source . readCacheItem ( cacheItem , subscription . bindTarget . storageProp ) ) ;
220
219
}
@@ -326,21 +325,27 @@ export class MetadataManager {
326
325
for ( const cacheItem of source . iterateCacheItems ( ) ) {
327
326
source . onCycle ( cacheItem ) ;
328
327
329
- if ( cacheItem . pendingInternalChange ) {
328
+ // if the cache is dirty, sync the changes to the external source
329
+ if ( cacheItem . dirty ) {
330
330
try {
331
331
source . syncExternal ( cacheItem ) ;
332
332
} catch ( e ) {
333
- console . warn ( ' failed to update frontmatter' , e ) ;
333
+ console . warn ( ` failed to sync changes to external source for ${ source . id } ` , e ) ;
334
334
}
335
- cacheItem . pendingInternalChange = false ;
335
+ cacheItem . dirty = false ;
336
+ }
337
+ // decrease the external write lock duration
338
+ if ( cacheItem . externalWriteLock > 0 ) {
339
+ cacheItem . externalWriteLock -= 1 ;
336
340
}
337
- cacheItem . cyclesSinceInternalChange += 1 ;
338
341
339
- if ( cacheItem . inactive ) {
340
- cacheItem . cyclesSinceInactive += 1 ;
342
+ // if there are no listeners, increase the cycles without listeners
343
+ if ( cacheItem . subscriptions . length > 0 ) {
344
+ cacheItem . cyclesWithoutListeners += 1 ;
341
345
}
346
+ // if the cache is inactive, check if it should be deleted
342
347
if (
343
- cacheItem . cyclesSinceInactive > METADATA_CACHE_INACTIVE_CYCLE_THRESHOLD &&
348
+ cacheItem . cyclesWithoutListeners > METADATA_CACHE_INACTIVE_CYCLE_THRESHOLD &&
344
349
source . shouldDelete ( cacheItem )
345
350
) {
346
351
markedForDelete . push ( cacheItem ) ;
@@ -372,8 +377,8 @@ export class MetadataManager {
372
377
}
373
378
374
379
const cacheItem = source . writeCache ( value , bindTarget ) ;
375
- cacheItem . pendingInternalChange = true ;
376
- cacheItem . cyclesSinceInternalChange = 0 ;
380
+ cacheItem . dirty = true ;
381
+ cacheItem . externalWriteLock = METADATA_CACHE_EXTERNAL_WRITE_LOCK_DURATION ;
377
382
this . notifyListeners ( bindTarget , updateSourceUuid ) ;
378
383
}
379
384
@@ -402,7 +407,7 @@ export class MetadataManager {
402
407
* @param cacheItem
403
408
*/
404
409
public isCacheExternalWriteLocked ( cacheItem : IMetadataCacheItem ) : boolean {
405
- return cacheItem . cyclesSinceInternalChange < METADATA_CACHE_UPDATE_CYCLE_THRESHOLD ;
410
+ return cacheItem . externalWriteLock > 0 ;
406
411
}
407
412
408
413
/**
@@ -485,10 +490,9 @@ export class MetadataManager {
485
490
public getDefaultCacheItem ( ) : IMetadataCacheItem {
486
491
return {
487
492
subscriptions : [ ] ,
488
- cyclesSinceInternalChange : METADATA_CACHE_UPDATE_CYCLE_THRESHOLD + 1 ,
489
- pendingInternalChange : false ,
490
- cyclesSinceInactive : 0 ,
491
- inactive : true ,
493
+ externalWriteLock : 0 ,
494
+ dirty : false ,
495
+ cyclesWithoutListeners : 0 ,
492
496
} ;
493
497
}
494
498
0 commit comments