@@ -18,29 +18,22 @@ export interface SignalPersistentStorage {
1818export class SignalStore implements SignalPersistentStorage {
1919 static readonly DB_NAME = 'Segment Signals Buffer'
2020 static readonly STORE_NAME = 'signals'
21- private signalStore : Promise < IDBPDatabase < SignalDatabase > >
22- private signalCount = 0
21+ private db : Promise < IDBPDatabase < SignalDatabase > >
2322 private maxBufferSize : number
24-
25- public length ( ) {
26- return this . signalCount
27- }
28-
2923 static deleteDatabase ( ) {
3024 return indexedDB . deleteDatabase ( SignalStore . DB_NAME )
3125 }
3226
3327 constructor ( settings : { maxBufferSize ?: number } = { } ) {
3428 this . maxBufferSize = settings . maxBufferSize ?? 50
35- this . signalStore = this . createSignalStore ( )
36- void this . initializeSignalCount ( )
29+ this . db = this . initSignalDB ( )
3730 }
3831
3932 private getStore ( ) {
40- return this . signalStore
33+ return this . db
4134 }
4235
43- private async createSignalStore ( ) {
36+ private async initSignalDB ( ) {
4437 const db = await openDB < SignalDatabase > ( SignalStore . DB_NAME , 1 , {
4538 upgrade ( db ) {
4639 db . createObjectStore ( SignalStore . STORE_NAME , { autoIncrement : true } )
@@ -50,31 +43,28 @@ export class SignalStore implements SignalPersistentStorage {
5043 return db
5144 }
5245
53- private async initializeSignalCount ( ) {
54- const store = await this . signalStore
55- this . signalCount = await store . count ( SignalStore . STORE_NAME )
56- logger . debug (
57- `Signal count initialized with ${ this . signalCount } signals (max: ${ this . maxBufferSize } )`
58- )
46+ async add ( signal : Signal ) : Promise < void > {
47+ const db = await this . db
48+ await this . countAndDeleteOldestIfNeeded ( db , this . maxBufferSize )
49+ await db . add ( SignalStore . STORE_NAME , signal )
5950 }
6051
61- async add ( signal : Signal ) : Promise < void > {
62- const store = await this . signalStore
63- if ( this . signalCount >= this . maxBufferSize ) {
64- // Get the key of the oldest signal and delete it
65- const oldestKey = await store
66- . transaction ( SignalStore . STORE_NAME )
67- . store . getKey ( IDBKeyRange . lowerBound ( 0 ) )
68- if ( oldestKey !== undefined ) {
69- await store . delete ( SignalStore . STORE_NAME , oldestKey )
70- } else {
71- this . signalCount --
52+ private async countAndDeleteOldestIfNeeded (
53+ db : IDBPDatabase < SignalDatabase > ,
54+ maxItems : number
55+ ) {
56+ const transaction = db . transaction ( 'signals' , 'readwrite' )
57+ const store = transaction . objectStore ( 'signals' )
58+ const count = await store . count ( )
59+ if ( count >= maxItems ) {
60+ const cursor = await store . openCursor ( )
61+ if ( cursor ) {
62+ await cursor . delete ( )
63+ logger . debug ( 'Deleted item' , { count : count - 1 , key : cursor . key } )
7264 }
7365 }
74- await store . add ( SignalStore . STORE_NAME , signal )
75- this . signalCount ++
66+ return await transaction . done
7667 }
77-
7868 /**
7969 * Get list of signals from the store, with the newest signals first.
8070 */
0 commit comments