@@ -109,47 +109,53 @@ describe("browser thread triage storage", () => {
109109 } ) ,
110110 ) ;
111111
112- it . effect ( "cancels a pending lock request before it can write" , ( ) =>
113- Effect . gen ( function * ( ) {
114- const requested = yield * Queue . unbounded < {
115- signal : AbortSignal | undefined ;
116- grant : ( ) => Promise < void > ;
117- } > ( ) ;
118- const setItem = vi . fn ( ) ;
119- vi . stubGlobal ( "window" , { localStorage : { getItem : ( ) => null , setItem } } ) ;
120- vi . stubGlobal ( "navigator" , {
121- locks : {
122- request : (
123- _name : string ,
124- options : LockOptions | ( ( ) => Promise < unknown > ) ,
125- callback ?: ( ) => Promise < unknown > ,
126- ) =>
127- new Promise ( ( resolve , reject ) => {
128- const signal = typeof options === "function" ? undefined : options . signal ;
129- const acquired = typeof options === "function" ? options : callback ! ;
130- signal ?. addEventListener ( "abort" , ( ) => reject ( signal . reason ) , { once : true } ) ;
131- Queue . offerUnsafe ( requested , {
132- signal,
133- grant : async ( ) => {
134- if ( ! signal ?. aborted ) resolve ( await acquired ( ) ) ;
135- } ,
136- } ) ;
137- } ) ,
138- } ,
139- } ) ;
112+ it . effect . each ( [ "queued" , "already granted" ] as const ) (
113+ "cancels a lock request before it can write (%s)" ,
114+ ( state ) =>
115+ Effect . gen ( function * ( ) {
116+ const requested = yield * Queue . unbounded < {
117+ signal : AbortSignal | undefined ;
118+ grant : ( ) => Promise < void > ;
119+ } > ( ) ;
120+ const setItem = vi . fn ( ) ;
121+ vi . stubGlobal ( "window" , { localStorage : { getItem : ( ) => null , setItem } } ) ;
122+ vi . stubGlobal ( "navigator" , {
123+ locks : {
124+ request : (
125+ _name : string ,
126+ options : LockOptions | ( ( ) => Promise < unknown > ) ,
127+ callback ?: ( ) => Promise < unknown > ,
128+ ) =>
129+ new Promise ( ( resolve , reject ) => {
130+ const signal = typeof options === "function" ? undefined : options . signal ;
131+ const acquired = typeof options === "function" ? options : callback ! ;
132+ if ( state === "queued" ) {
133+ signal ?. addEventListener ( "abort" , ( ) => reject ( signal . reason ) , { once : true } ) ;
134+ }
135+ Queue . offerUnsafe ( requested , {
136+ signal,
137+ grant : async ( ) => {
138+ if ( state === "already granted" || ! signal ?. aborted ) resolve ( await acquired ( ) ) ;
139+ } ,
140+ } ) ;
141+ } ) ,
142+ } ,
143+ } ) ;
140144
141- const saving = yield * browserThreadTriageOutboxStore . put ( queuedSettle ) . pipe ( Effect . forkChild ) ;
142- const request = yield * Queue . take ( requested ) ;
143- try {
144- // A broken uninterruptible acquisition must still be released after the assertion.
145- yield * Effect . sync ( ( ) => saving . interruptUnsafe ( ) ) ;
146- expect ( request . signal ?. aborted ) . toBe ( true ) ;
147- } finally {
148- yield * Effect . promise ( request . grant ) ;
149- yield * Fiber . interrupt ( saving ) ;
150- }
151- expect ( setItem ) . not . toHaveBeenCalled ( ) ;
152- } ) ,
145+ const saving = yield * browserThreadTriageOutboxStore
146+ . put ( queuedSettle )
147+ . pipe ( Effect . forkChild ) ;
148+ const request = yield * Queue . take ( requested ) ;
149+ try {
150+ // A broken uninterruptible acquisition must still be released after the assertion.
151+ yield * Effect . sync ( ( ) => saving . interruptUnsafe ( ) ) ;
152+ expect ( request . signal ?. aborted ) . toBe ( true ) ;
153+ } finally {
154+ yield * Effect . promise ( request . grant ) ;
155+ yield * Fiber . interrupt ( saving ) ;
156+ }
157+ expect ( setItem ) . not . toHaveBeenCalled ( ) ;
158+ } ) ,
153159 ) ;
154160
155161 it . effect ( "uses the caller's logger during a locked storage read" , ( ) =>
0 commit comments