@@ -25,6 +25,9 @@ interface IDBPObjectStoreSignals
2525 'readonly' | 'readwrite' | 'versionchange'
2626 > { }
2727
28+ interface StoreSettings {
29+ maxBufferSize : number
30+ }
2831export class SignalStoreIndexDB implements SignalPersistentStorage {
2932 static readonly DB_NAME = 'Segment Signals Buffer'
3033 static readonly STORE_NAME = 'signals'
@@ -46,8 +49,8 @@ export class SignalStoreIndexDB implements SignalPersistentStorage {
4649 return store
4750 }
4851
49- constructor ( settings : { maxBufferSize ?: number } = { } ) {
50- this . maxBufferSize = settings . maxBufferSize ?? 10
52+ constructor ( settings : StoreSettings ) {
53+ this . maxBufferSize = settings . maxBufferSize
5154 this . db = this . initSignalDB ( )
5255 }
5356
@@ -129,6 +132,37 @@ export class SignalStoreIndexDB implements SignalPersistentStorage {
129132 }
130133}
131134
135+ export class SignalStoreSessionStorage implements SignalPersistentStorage {
136+ private readonly storageKey = 'segment_signals_buffer'
137+ private maxBufferSize : number
138+
139+ constructor ( settings : StoreSettings ) {
140+ this . maxBufferSize = settings . maxBufferSize
141+ }
142+
143+ add ( signal : Signal ) : void {
144+ const signals = this . getAllSync ( )
145+ signals . push ( signal )
146+ if ( signals . length > this . maxBufferSize ) {
147+ signals . splice ( 0 , signals . length - this . maxBufferSize )
148+ }
149+ sessionStorage . setItem ( this . storageKey , JSON . stringify ( signals ) )
150+ }
151+
152+ getAll ( ) : Promise < Signal [ ] > {
153+ return Promise . resolve ( this . getAllSync ( ) )
154+ }
155+
156+ private getAllSync ( ) : Signal [ ] {
157+ const signals = sessionStorage . getItem ( this . storageKey )
158+ return signals ? JSON . parse ( signals ) . reverse ( ) : [ ]
159+ }
160+
161+ clear ( ) : void {
162+ sessionStorage . removeItem ( this . storageKey )
163+ }
164+ }
165+
132166export class SignalBuffer <
133167 T extends SignalPersistentStorage = SignalPersistentStorage
134168> {
@@ -165,14 +199,33 @@ export class SignalBuffer<
165199export interface SignalBufferSettingsConfig <
166200 T extends SignalPersistentStorage = SignalPersistentStorage
167201> {
202+ /**
203+ * Maximum number of signals to store. Only applies if no custom storage implementation is provided.
204+ */
168205 maxBufferSize ?: number
206+ /**
207+ * Choose between sessionStorage and indexDB. Only applies if no custom storage implementation is provided.
208+ * @default 'indexDB'
209+ */
210+ storageType ?: 'session' | 'indexDB'
211+ /**
212+ * Custom storage implementation
213+ * @default SignalStoreIndexDB
214+ */
169215 signalStorage ?: T
170216}
171217export const getSignalBuffer = <
172218 T extends SignalPersistentStorage = SignalPersistentStorage
173219> (
174220 settings : SignalBufferSettingsConfig < T >
175221) => {
176- const store = settings . signalStorage ?? new SignalStoreIndexDB ( settings )
222+ const settingsWithDefaults : StoreSettings = {
223+ maxBufferSize : 50 ,
224+ ...settings ,
225+ }
226+ const store =
227+ settings . signalStorage ?? settings . storageType === 'session'
228+ ? new SignalStoreSessionStorage ( settingsWithDefaults )
229+ : new SignalStoreIndexDB ( settingsWithDefaults )
177230 return new SignalBuffer ( store )
178231}
0 commit comments