@@ -202,6 +202,9 @@ function measureOperation<T>(
202202 fn : ( ) => T ,
203203 keysCount = 1 ,
204204) : T {
205+ if ( ! metricsObserver ) {
206+ return fn ( ) ;
207+ }
205208 const start = Date . now ( ) ;
206209 try {
207210 return fn ( ) ;
@@ -234,6 +237,9 @@ function createLocalStorageWebSecureBackend(): WebSecureStorageBackend {
234237let webSecureStorageBackend : WebSecureStorageBackend | undefined =
235238 createLocalStorageWebSecureBackend ( ) ;
236239
240+ let cachedSecureBrowserStorage : BrowserStorageLike | undefined ;
241+ let cachedSecureBackendRef : WebSecureStorageBackend | undefined ;
242+
237243function getBrowserStorage ( scope : number ) : BrowserStorageLike | undefined {
238244 if ( scope === StorageScope . Disk ) {
239245 return globalThis . localStorage ;
@@ -242,16 +248,24 @@ function getBrowserStorage(scope: number): BrowserStorageLike | undefined {
242248 if ( ! webSecureStorageBackend ) {
243249 return undefined ;
244250 }
245- return {
246- setItem : ( key , value ) => webSecureStorageBackend ?. setItem ( key , value ) ,
247- getItem : ( key ) => webSecureStorageBackend ?. getItem ( key ) ?? null ,
248- removeItem : ( key ) => webSecureStorageBackend ?. removeItem ( key ) ,
249- clear : ( ) => webSecureStorageBackend ?. clear ( ) ,
250- key : ( index ) => webSecureStorageBackend ?. getAllKeys ( ) [ index ] ?? null ,
251+ if (
252+ cachedSecureBackendRef === webSecureStorageBackend &&
253+ cachedSecureBrowserStorage
254+ ) {
255+ return cachedSecureBrowserStorage ;
256+ }
257+ cachedSecureBackendRef = webSecureStorageBackend ;
258+ cachedSecureBrowserStorage = {
259+ setItem : ( key , value ) => webSecureStorageBackend ! . setItem ( key , value ) ,
260+ getItem : ( key ) => webSecureStorageBackend ! . getItem ( key ) ?? null ,
261+ removeItem : ( key ) => webSecureStorageBackend ! . removeItem ( key ) ,
262+ clear : ( ) => webSecureStorageBackend ! . clear ( ) ,
263+ key : ( index ) => webSecureStorageBackend ! . getAllKeys ( ) [ index ] ?? null ,
251264 get length ( ) {
252- return webSecureStorageBackend ? .getAllKeys ( ) . length ?? 0 ;
265+ return webSecureStorageBackend ! . getAllKeys ( ) . length ;
253266 } ,
254267 } ;
268+ return cachedSecureBrowserStorage ;
255269 }
256270 return undefined ;
257271}
@@ -429,13 +443,20 @@ function clearScopeRawCache(scope: NonMemoryScope): void {
429443}
430444
431445function notifyKeyListeners ( registry : KeyListenerRegistry , key : string ) : void {
432- registry . get ( key ) ?. forEach ( ( listener ) => listener ( ) ) ;
446+ const listeners = registry . get ( key ) ;
447+ if ( listeners ) {
448+ for ( const listener of listeners ) {
449+ listener ( ) ;
450+ }
451+ }
433452}
434453
435454function notifyAllListeners ( registry : KeyListenerRegistry ) : void {
436- registry . forEach ( ( listeners ) => {
437- listeners . forEach ( ( listener ) => listener ( ) ) ;
438- } ) ;
455+ for ( const listeners of registry . values ( ) ) {
456+ for ( const listener of listeners ) {
457+ listener ( ) ;
458+ }
459+ }
439460}
440461
441462function addKeyListener (
@@ -1077,6 +1098,8 @@ export function setWebSecureStorageBackend(
10771098 backend ?: WebSecureStorageBackend ,
10781099) : void {
10791100 webSecureStorageBackend = backend ?? createLocalStorageWebSecureBackend ( ) ;
1101+ cachedSecureBrowserStorage = undefined ;
1102+ cachedSecureBackendRef = undefined ;
10801103 hydratedWebScopeKeyIndex . delete ( StorageScope . Secure ) ;
10811104 clearScopeRawCache ( StorageScope . Secure ) ;
10821105}
@@ -1250,17 +1273,18 @@ export function createStorageItem<T = undefined>(
12501273 return memoryStore . get ( storageKey ) ;
12511274 }
12521275
1253- if (
1254- nonMemoryScope === StorageScope . Secure &&
1255- ! isBiometric &&
1256- hasPendingSecureWrite ( storageKey )
1257- ) {
1258- return readPendingSecureWrite ( storageKey ) ;
1276+ if ( nonMemoryScope === StorageScope . Secure && ! isBiometric ) {
1277+ const pending = pendingSecureWrites . get ( storageKey ) ;
1278+ if ( pending !== undefined ) {
1279+ return pending . value ;
1280+ }
12591281 }
12601282
12611283 if ( readCache ) {
1262- if ( hasCachedRawValue ( nonMemoryScope ! , storageKey ) ) {
1263- return readCachedRawValue ( nonMemoryScope ! , storageKey ) ;
1284+ const cache = getScopeRawCache ( nonMemoryScope ! ) ;
1285+ const cached = cache . get ( storageKey ) ;
1286+ if ( cached !== undefined || cache . has ( storageKey ) ) {
1287+ return cached ;
12641288 }
12651289 }
12661290
@@ -1474,14 +1498,13 @@ export function createStorageItem<T = undefined>(
14741498 ? valueOrFn ( getInternal ( ) )
14751499 : valueOrFn ;
14761500
1477- invalidateParsedCache ( ) ;
1478-
14791501 if ( validate && ! validate ( newValue ) ) {
14801502 throw new Error (
14811503 `Validation failed for key "${ storageKey } " in scope "${ StorageScope [ config . scope ] } ".` ,
14821504 ) ;
14831505 }
14841506
1507+ invalidateParsedCache ( ) ;
14851508 writeValueWithoutValidation ( newValue ) ;
14861509 } ) ;
14871510 } ;
@@ -1620,15 +1643,18 @@ export function getBatch(
16201643
16211644 items . forEach ( ( item , index ) => {
16221645 if ( scope === StorageScope . Secure ) {
1623- if ( hasPendingSecureWrite ( item . key ) ) {
1624- rawValues [ index ] = readPendingSecureWrite ( item . key ) ;
1646+ const pending = pendingSecureWrites . get ( item . key ) ;
1647+ if ( pending !== undefined ) {
1648+ rawValues [ index ] = pending . value ;
16251649 return ;
16261650 }
16271651 }
16281652
16291653 if ( item . _readCacheEnabled === true ) {
1630- if ( hasCachedRawValue ( scope , item . key ) ) {
1631- rawValues [ index ] = readCachedRawValue ( scope , item . key ) ;
1654+ const cache = getScopeRawCache ( scope ) ;
1655+ const cached = cache . get ( item . key ) ;
1656+ if ( cached !== undefined || cache . has ( item . key ) ) {
1657+ rawValues [ index ] = cached ;
16321658 return ;
16331659 }
16341660 }
0 commit comments