11/// <reference types="chrome" />
22import { version } from '../package.json' ;
3+ import deepEqual from 'fast-deep-equal/es6' ;
34
45type ListenerFunction = (
56 changes : { [ key : string ] : chrome . storage . StorageChange } ,
@@ -18,13 +19,13 @@ function createMockStorageArea(areaName: chrome.storage.AreaName) {
1819 if ( Array . isArray ( keys ) ) {
1920 return keys . reduce (
2021 ( acc , key ) => {
21- acc [ key ] = data [ key ] ;
22+ acc [ key ] = structuredClone ( data [ key ] ) ;
2223 return acc ;
2324 } ,
2425 { } as Record < string , any >
2526 ) ;
2627 }
27- if ( typeof keys === 'string' ) return { [ keys ] : data [ keys ] } ;
28+ if ( typeof keys === 'string' ) return { [ keys ] : structuredClone ( data [ keys ] ) } ;
2829 return keys ;
2930 } ,
3031 async getBytesInUse ( ) {
@@ -40,11 +41,20 @@ function createMockStorageArea(areaName: chrome.storage.AreaName) {
4041 }
4142 } ,
4243 async set ( items : { [ key : string ] : any } ) {
43- for ( const key in items ) {
44+ let hasUpdates = false ;
45+ const updates : Record < string , chrome . storage . StorageChange > = { } ;
46+ for ( const [ key , newValue ] of Object . entries ( items ) ) {
4447 const oldValue = data [ key ] ;
45- data [ key ] = JSON . parse ( JSON . stringify ( items [ key ] ) ) ;
48+ if ( ! deepEqual ( newValue , oldValue ) ) {
49+ hasUpdates = true ;
50+ data [ key ] = structuredClone ( newValue ) ;
51+ updates [ key ] = { oldValue, newValue } ;
52+ }
53+ }
54+
55+ if ( hasUpdates ) {
4656 for ( const listener of allListeners . values ( ) ) {
47- listener ( { [ key ] : { oldValue , newValue : data [ key ] } } , areaName ) ;
57+ listener ( structuredClone ( updates ) , areaName ) ;
4858 }
4959 }
5060 } ,
0 commit comments