@@ -158,12 +158,22 @@ export class FileURIComponent {
158
158
return base ?? window . location . toString ( ) ;
159
159
} ;
160
160
161
- #updateWaiter: unknown ;
162
161
#frame?: number ;
163
162
#qps: URLSearchParams | undefined ;
163
+ #tokens: unknown [ ] = [ ] ;
164
164
#updateQPs = async ( rawText : string , format : Format ) => {
165
+ this . #tokens. push ( queueWaiter . beginAsync ( ) ) ;
166
+ this . #_updateQPs( rawText , format ) ;
167
+ } ;
168
+
169
+ #cleanup = ( ) => {
170
+ this . #tokens. forEach ( ( token ) => {
171
+ queueWaiter . endAsync ( token ) ;
172
+ } ) ;
173
+ } ;
174
+
175
+ #_updateQPs = async ( rawText : string , format : Format ) => {
165
176
if ( this . #frame) cancelAnimationFrame ( this . #frame) ;
166
- if ( ! this . #updateWaiter) this . #updateWaiter = queueWaiter . beginAsync ( ) ;
167
177
168
178
let encoded = compressToEncodedURIComponent ( rawText ) ;
169
179
let qps = new URLSearchParams ( location . search ) ;
@@ -172,15 +182,27 @@ export class FileURIComponent {
172
182
qps . delete ( 't' ) ;
173
183
qps . set ( 'format' , formatFrom ( format ) ) ;
174
184
185
+ // @ts -expect-error this works
186
+ if ( this . #qps?. c === qps . get ( 'c' ) && this . #qps?. format === qps . get ( 'format' ) ) {
187
+ // no-op, we should not have gotten here
188
+ // it's a mistake to have tried to have update QPs.
189
+ // Someone should debug this.
190
+ this . #cleanup( ) ;
191
+
192
+ return ;
193
+ }
194
+
195
+ localStorage . setItem ( 'format' , formatFrom ( format ) ) ;
196
+ localStorage . setItem ( 'document' , rawText ) ;
197
+
175
198
this . #qps = {
176
199
...this . #qps,
177
200
...qps ,
178
201
} ;
179
202
180
203
this . #frame = requestAnimationFrame ( async ( ) => {
181
204
if ( isDestroyed ( this ) || isDestroying ( this ) ) {
182
- queueWaiter . endAsync ( this . #updateWaiter) ;
183
- this . #updateWaiter = null ;
205
+ this . #cleanup( ) ;
184
206
185
207
return ;
186
208
}
@@ -191,30 +213,34 @@ export class FileURIComponent {
191
213
await new Promise ( ( resolve ) => setTimeout ( resolve , DEBOUNCE_MS ) ) ;
192
214
193
215
if ( isDestroyed ( this ) || isDestroying ( this ) ) {
194
- queueWaiter . endAsync ( this . #updateWaiter) ;
195
- this . #updateWaiter = null ;
216
+ this . #cleanup( ) ;
196
217
197
218
return ;
198
219
}
199
220
200
- queueWaiter . endAsync ( this . #updateWaiter) ;
201
- this . #updateWaiter = null ;
202
-
203
221
// On initial load, if we call #updateQPs,
204
222
// we may not have a currentURL, because the first transition has yet to complete
205
223
let base = this . router . currentURL ?. split ( '?' ) [ 0 ] ;
206
224
207
225
if ( macroCondition ( isTesting ( ) ) ) {
208
226
// eslint-disable-next-line @typescript-eslint/no-explicit-any
209
227
base ??= ( this . router as any ) /* private API? */ ?. location ?. path ;
228
+ // @ts -expect-error private api
229
+ base = base . split ( '?' ) [ 0 ] ;
210
230
} else {
211
231
base ??= window . location . pathname ;
212
232
}
213
233
234
+ /**
235
+ * At some point this added qps
236
+ * we don't want them though, so we'll strip them
237
+ */
238
+
214
239
let next = `${ base } ?${ qps } ` ;
215
240
216
241
this . router . replaceWith ( next ) ;
217
242
this . #text = rawText ;
243
+ this . #cleanup( ) ;
218
244
} ) ;
219
245
} ;
220
246
}
0 commit comments