1
1
import { getSession , setSession } from '../../api/session.ts' ;
2
2
import { SvelteSet } from 'svelte/reactivity' ;
3
- import { HASH_STRING_LENGTH } from '../../api/hash.ts' ;
4
3
5
- const SIZE_LIMIT = chrome . storage . session . QUOTA_BYTES - HASH_STRING_LENGTH ;
6
4
export const sessionStore = $state ( {
7
5
bypass : < Set < string > > new SvelteSet ( ) ,
8
6
debug : < Set < string > > new SvelteSet ( ) ,
@@ -19,35 +17,36 @@ getSession().then((session) => {
19
17
} ) ;
20
18
21
19
export async function toggleBypass ( traceId : string ) {
22
- const sessionStorageSize = await chrome . storage . session . getBytesInUse ( ) ;
23
- let dirty = false ;
24
-
25
- if ( sessionStore . bypass . has ( traceId ) ) {
26
- sessionStore . bypass . delete ( traceId ) ;
27
- dirty = true ;
28
- } else if ( sessionStorageSize < SIZE_LIMIT ) {
29
- sessionStore . bypass . add ( traceId ) ;
30
- dirty = true ;
20
+ if ( await toggleSet ( sessionStore . bypass , traceId ) ) {
21
+ await setSession ( {
22
+ bypass : Array . from ( sessionStore . bypass . values ( ) ) ,
23
+ } ) ;
31
24
}
32
-
33
- dirty && await setSession ( {
34
- bypass : Array . from ( sessionStore . bypass . values ( ) ) ,
35
- } ) ;
36
25
}
37
26
38
27
export async function toggleDebug ( traceId : string ) {
39
- const sessionStorageSize = await chrome . storage . session . getBytesInUse ( ) ;
40
- let dirty = false ;
28
+ if ( await toggleSet ( sessionStore . debug , traceId ) ) {
29
+ await setSession ( {
30
+ debug : Array . from ( sessionStore . debug . values ( ) ) ,
31
+ } ) ;
32
+ }
33
+ }
41
34
42
- if ( sessionStore . debug . has ( traceId ) ) {
43
- sessionStore . debug . delete ( traceId ) ;
44
- dirty = true ;
45
- } else if ( sessionStorageSize < SIZE_LIMIT ) {
46
- sessionStore . debug . add ( traceId ) ;
47
- dirty = true ;
35
+ const QUOTA_THRESHOLD = chrome . storage . session . QUOTA_BYTES ;
36
+ const MARGINAL_SIZE = 40 ; // for ASCII string in an array
37
+ async function toggleSet ( set : Set < string > , traceId : string ) : Promise < boolean > {
38
+ if ( set . has ( traceId ) ) {
39
+ set . delete ( traceId ) ;
40
+ return true ;
48
41
}
49
42
50
- dirty && await setSession ( {
51
- debug : Array . from ( sessionStore . debug . values ( ) ) ,
52
- } ) ;
43
+ const freeSpace = QUOTA_THRESHOLD -
44
+ await chrome . storage . session . getBytesInUse ( ) ;
45
+
46
+ if ( freeSpace - traceId . length - MARGINAL_SIZE >= 0 ) {
47
+ set . add ( traceId ) ;
48
+ return true ;
49
+ }
50
+
51
+ return false ;
53
52
}
0 commit comments