Skip to content

Commit ee7669a

Browse files
committed
add logic to prune signal buffer
1 parent bc5343b commit ee7669a

File tree

1 file changed

+16
-3
lines changed
  • packages/signals/signals/src/core/buffer

1 file changed

+16
-3
lines changed

packages/signals/signals/src/core/buffer/index.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export class SignalStore implements SignalPersistentStorage {
4040
},
4141
})
4242
logger.debug('Signals Buffer (indexDB) initialized')
43+
// if the signal buffer is too large, delete the oldest signals (e.g, the settings have changed)
44+
await this.countAndDeleteOldestIfNeeded(db, this.maxBufferSize, true)
4345
return db
4446
}
4547

@@ -51,20 +53,31 @@ export class SignalStore implements SignalPersistentStorage {
5153

5254
private async countAndDeleteOldestIfNeeded(
5355
db: IDBPDatabase<SignalDatabase>,
54-
maxItems: number
56+
maxItems: number,
57+
deleteMultiple = false
5558
) {
5659
const transaction = db.transaction(SignalStore.STORE_NAME, 'readwrite')
5760
const store = transaction.objectStore(SignalStore.STORE_NAME)
58-
const count = await store.count()
61+
let count = await store.count()
5962
if (count >= maxItems) {
6063
const cursor = await store.openCursor()
6164
if (cursor) {
62-
await cursor.delete()
65+
// delete up to maxItems
66+
if (deleteMultiple) {
67+
while (count >= maxItems) {
68+
await cursor.delete()
69+
count--
70+
await cursor.continue()
71+
}
72+
} else {
73+
await cursor.delete()
74+
}
6375
logger.debug('Deleted item', { count: count - 1, key: cursor.key })
6476
}
6577
}
6678
return await transaction.done
6779
}
80+
6881
/**
6982
* Get list of signals from the store, with the newest signals first.
7083
*/

0 commit comments

Comments
 (0)