@tschk/moonshine-svelte 0.4.49 → 0.4.80

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,983 @@
1
+ /** Independent Svelte 5 reactivity classes and createSubscriber on the Moonshine graph. */
2
+ import { batch, createSignal, untrack, type Signal } from "@tschk/moonshine";
3
+ import {
4
+ collectDeps,
5
+ hasValueDemand,
6
+ isTracking,
7
+ } from "@tschk/moonshine/tracking";
8
+ import { peekComponentContext } from "@tschk/moonshine-dom/component";
9
+ import { getOwner, onCleanup, type Owner } from "@tschk/moonshine-dom/reactive";
10
+ import { isSSRRendering } from "@tschk/moonshine-dom/ssr-owner";
11
+ import {
12
+ recordHistory,
13
+ trackHistoryRead,
14
+ withHistoryMutation,
15
+ } from "./history";
16
+ import {
17
+ notifyTrackableUnsubscribe,
18
+ registerDemandCheck,
19
+ } from "./store-demand";
20
+
21
+ const trackedOwners = /* @__PURE__ */ new WeakSet<Owner>();
22
+
23
+ function trackSignal(
24
+ target: object,
25
+ key: PropertyKey,
26
+ sig: Signal<number>,
27
+ ): void {
28
+ if (!isTracking()) return;
29
+ sig();
30
+ trackHistoryRead(target, key);
31
+ }
32
+
33
+ function bumpSignal(
34
+ target: object,
35
+ key: PropertyKey,
36
+ sig: Signal<number>,
37
+ ): void {
38
+ withHistoryMutation(() => {
39
+ const prev = sig.peek();
40
+ const next = prev + 1;
41
+ recordHistory(target, key, prev, next);
42
+ sig.set(next);
43
+ });
44
+ }
45
+
46
+ export function createSubscriber(
47
+ start: (update: () => void) => (() => void) | void,
48
+ ): () => void {
49
+ if (typeof start !== "function") {
50
+ throw new TypeError("createSubscriber requires a start function");
51
+ }
52
+ let active = false;
53
+ let stop: (() => void) | void;
54
+ let unregisterDemand: (() => void) | undefined;
55
+ const version = createSignal(0);
56
+ const versionNode = collectDeps(() => version())[0]!;
57
+ const originalSubscribe = versionNode.subscribe;
58
+ versionNode.subscribe = (...args) => {
59
+ const unsubscribe = originalSubscribe(...args);
60
+ return () => {
61
+ unsubscribe();
62
+ notifyTrackableUnsubscribe();
63
+ };
64
+ };
65
+ const update = () => {
66
+ if (!active) return;
67
+ bumpSignal(version, "value", version);
68
+ };
69
+ return () => {
70
+ if (
71
+ isSSRRendering() ||
72
+ peekComponentContext()?.mode === "ssr" ||
73
+ !isTracking() ||
74
+ getOwner()?.disposed
75
+ ) {
76
+ return;
77
+ }
78
+ const owner = getOwner();
79
+ if (owner && !trackedOwners.has(owner)) {
80
+ trackedOwners.add(owner);
81
+ onCleanup(notifyTrackableUnsubscribe);
82
+ }
83
+ version();
84
+ trackHistoryRead(version, "value");
85
+ if (!active) {
86
+ active = true;
87
+ stop = untrack(() => start(update));
88
+ unregisterDemand = registerDemandCheck(() => {
89
+ if (active && !hasValueDemand(versionNode)) {
90
+ active = false;
91
+ const cleanup = stop;
92
+ stop = undefined;
93
+ unregisterDemand?.();
94
+ unregisterDemand = undefined;
95
+ version.set((n) => n + 1);
96
+ if (typeof cleanup === "function") {
97
+ untrack(() => cleanup());
98
+ }
99
+ }
100
+ });
101
+ notifyTrackableUnsubscribe();
102
+ }
103
+ };
104
+ }
105
+
106
+ export class ReactiveValue<T> {
107
+ readonly #fn: () => T;
108
+ readonly #subscribe: () => void;
109
+
110
+ constructor(
111
+ fn: () => T,
112
+ onsubscribe: (update: () => void) => void | (() => void),
113
+ ) {
114
+ if (typeof fn !== "function") {
115
+ throw new TypeError("ReactiveValue requires a getter function");
116
+ }
117
+ if (typeof onsubscribe !== "function") {
118
+ throw new TypeError("ReactiveValue requires an onsubscribe function");
119
+ }
120
+ this.#fn = fn;
121
+ this.#subscribe = createSubscriber(onsubscribe);
122
+ }
123
+
124
+ get current(): T {
125
+ this.#subscribe();
126
+ return this.#fn();
127
+ }
128
+ }
129
+
130
+ const parenthesisRegex = /\(.+\)/;
131
+ const nonParenthesizedKeywords = new Set([
132
+ "all",
133
+ "print",
134
+ "screen",
135
+ "and",
136
+ "or",
137
+ "not",
138
+ "only",
139
+ ]);
140
+
141
+ export class MediaQuery extends ReactiveValue<boolean> {
142
+ constructor(query: string, fallback?: boolean) {
143
+ const fallbackValue = Boolean(fallback);
144
+ if (
145
+ isSSRRendering() ||
146
+ peekComponentContext()?.mode === "ssr" ||
147
+ typeof window === "undefined" ||
148
+ typeof window.matchMedia !== "function"
149
+ ) {
150
+ super(
151
+ () => fallbackValue,
152
+ () => {},
153
+ );
154
+ return;
155
+ }
156
+ const finalQuery =
157
+ parenthesisRegex.test(query) ||
158
+ query
159
+ .split(/[\s,]+/)
160
+ .some((keyword) => nonParenthesizedKeywords.has(keyword.trim()))
161
+ ? query
162
+ : `(${query})`;
163
+ const mql = window.matchMedia(finalQuery);
164
+ super(
165
+ () =>
166
+ isSSRRendering() || peekComponentContext()?.mode === "ssr"
167
+ ? fallbackValue
168
+ : mql.matches,
169
+ (update) => {
170
+ mql.addEventListener("change", update);
171
+ return () => {
172
+ mql.removeEventListener("change", update);
173
+ };
174
+ },
175
+ );
176
+ }
177
+ }
178
+
179
+ export class SvelteDate extends Date {
180
+ readonly #time: Signal<number>;
181
+
182
+ constructor(...params: any[]) {
183
+ super(...(params as []));
184
+ this.#time = createSignal(super.getTime());
185
+ }
186
+
187
+ #read(): void {
188
+ trackSignal(this, "time", this.#time);
189
+ }
190
+
191
+ #write(next: number): number {
192
+ const prev = this.#time.peek();
193
+ if (!Object.is(prev, next)) {
194
+ withHistoryMutation(() => {
195
+ recordHistory(this, "time", prev, next);
196
+ this.#time.set(next);
197
+ });
198
+ }
199
+ return next;
200
+ }
201
+
202
+ override getDate(): number {
203
+ this.#read();
204
+ return super.getDate();
205
+ }
206
+ override getDay(): number {
207
+ this.#read();
208
+ return super.getDay();
209
+ }
210
+ override getFullYear(): number {
211
+ this.#read();
212
+ return super.getFullYear();
213
+ }
214
+ override getHours(): number {
215
+ this.#read();
216
+ return super.getHours();
217
+ }
218
+ override getMilliseconds(): number {
219
+ this.#read();
220
+ return super.getMilliseconds();
221
+ }
222
+ override getMinutes(): number {
223
+ this.#read();
224
+ return super.getMinutes();
225
+ }
226
+ override getMonth(): number {
227
+ this.#read();
228
+ return super.getMonth();
229
+ }
230
+ override getSeconds(): number {
231
+ this.#read();
232
+ return super.getSeconds();
233
+ }
234
+ override getTime(): number {
235
+ this.#read();
236
+ return super.getTime();
237
+ }
238
+ override getTimezoneOffset(): number {
239
+ this.#read();
240
+ return super.getTimezoneOffset();
241
+ }
242
+ override getUTCDate(): number {
243
+ this.#read();
244
+ return super.getUTCDate();
245
+ }
246
+ override getUTCDay(): number {
247
+ this.#read();
248
+ return super.getUTCDay();
249
+ }
250
+ override getUTCFullYear(): number {
251
+ this.#read();
252
+ return super.getUTCFullYear();
253
+ }
254
+ override getUTCHours(): number {
255
+ this.#read();
256
+ return super.getUTCHours();
257
+ }
258
+ override getUTCMilliseconds(): number {
259
+ this.#read();
260
+ return super.getUTCMilliseconds();
261
+ }
262
+ override getUTCMinutes(): number {
263
+ this.#read();
264
+ return super.getUTCMinutes();
265
+ }
266
+ override getUTCMonth(): number {
267
+ this.#read();
268
+ return super.getUTCMonth();
269
+ }
270
+ override getUTCSeconds(): number {
271
+ this.#read();
272
+ return super.getUTCSeconds();
273
+ }
274
+ override toDateString(): string {
275
+ this.#read();
276
+ return super.toDateString();
277
+ }
278
+ override toISOString(): string {
279
+ this.#read();
280
+ return super.toISOString();
281
+ }
282
+ override toJSON(key?: any): string {
283
+ this.#read();
284
+ return super.toJSON(key);
285
+ }
286
+ override toLocaleDateString(
287
+ locales?: Intl.LocalesArgument,
288
+ options?: Intl.DateTimeFormatOptions,
289
+ ): string {
290
+ this.#read();
291
+ return super.toLocaleDateString(locales, options);
292
+ }
293
+ override toLocaleString(
294
+ locales?: Intl.LocalesArgument,
295
+ options?: Intl.DateTimeFormatOptions,
296
+ ): string {
297
+ this.#read();
298
+ return super.toLocaleString(locales, options);
299
+ }
300
+ override toLocaleTimeString(
301
+ locales?: Intl.LocalesArgument,
302
+ options?: Intl.DateTimeFormatOptions,
303
+ ): string {
304
+ this.#read();
305
+ return super.toLocaleTimeString(locales, options);
306
+ }
307
+ override toString(): string {
308
+ this.#read();
309
+ return super.toString();
310
+ }
311
+ override toTimeString(): string {
312
+ this.#read();
313
+ return super.toTimeString();
314
+ }
315
+ override toUTCString(): string {
316
+ this.#read();
317
+ return super.toUTCString();
318
+ }
319
+ override valueOf(): number {
320
+ this.#read();
321
+ return super.valueOf();
322
+ }
323
+ override [Symbol.toPrimitive](hint: "default"): string;
324
+ override [Symbol.toPrimitive](hint: "string"): string;
325
+ override [Symbol.toPrimitive](hint: "number"): number;
326
+ override [Symbol.toPrimitive](hint: string): string | number;
327
+ override [Symbol.toPrimitive](
328
+ hint: "default" | "string" | "number" | string,
329
+ ): string | number {
330
+ this.#read();
331
+ return super[Symbol.toPrimitive](hint as "default");
332
+ }
333
+
334
+ override setDate(date: number): number {
335
+ return this.#write(super.setDate(date));
336
+ }
337
+ override setFullYear(year: number, month?: number, date?: number): number {
338
+ return this.#write(
339
+ arguments.length === 1
340
+ ? super.setFullYear(year)
341
+ : arguments.length === 2
342
+ ? super.setFullYear(year, month!)
343
+ : super.setFullYear(year, month!, date!),
344
+ );
345
+ }
346
+ override setHours(
347
+ hours: number,
348
+ min?: number,
349
+ sec?: number,
350
+ ms?: number,
351
+ ): number {
352
+ return this.#write(
353
+ arguments.length === 1
354
+ ? super.setHours(hours)
355
+ : arguments.length === 2
356
+ ? super.setHours(hours, min!)
357
+ : arguments.length === 3
358
+ ? super.setHours(hours, min!, sec!)
359
+ : super.setHours(hours, min!, sec!, ms!),
360
+ );
361
+ }
362
+ override setMilliseconds(ms: number): number {
363
+ return this.#write(super.setMilliseconds(ms));
364
+ }
365
+ override setMinutes(min: number, sec?: number, ms?: number): number {
366
+ return this.#write(
367
+ arguments.length === 1
368
+ ? super.setMinutes(min)
369
+ : arguments.length === 2
370
+ ? super.setMinutes(min, sec!)
371
+ : super.setMinutes(min, sec!, ms!),
372
+ );
373
+ }
374
+ override setMonth(month: number, date?: number): number {
375
+ return this.#write(
376
+ arguments.length === 1
377
+ ? super.setMonth(month)
378
+ : super.setMonth(month, date!),
379
+ );
380
+ }
381
+ override setSeconds(sec: number, ms?: number): number {
382
+ return this.#write(
383
+ arguments.length === 1
384
+ ? super.setSeconds(sec)
385
+ : super.setSeconds(sec, ms!),
386
+ );
387
+ }
388
+ override setTime(time: number): number {
389
+ return this.#write(super.setTime(time));
390
+ }
391
+ override setUTCDate(date: number): number {
392
+ return this.#write(super.setUTCDate(date));
393
+ }
394
+ override setUTCFullYear(year: number, month?: number, date?: number): number {
395
+ return this.#write(
396
+ arguments.length === 1
397
+ ? super.setUTCFullYear(year)
398
+ : arguments.length === 2
399
+ ? super.setUTCFullYear(year, month!)
400
+ : super.setUTCFullYear(year, month!, date!),
401
+ );
402
+ }
403
+ override setUTCHours(
404
+ hours: number,
405
+ min?: number,
406
+ sec?: number,
407
+ ms?: number,
408
+ ): number {
409
+ return this.#write(
410
+ arguments.length === 1
411
+ ? super.setUTCHours(hours)
412
+ : arguments.length === 2
413
+ ? super.setUTCHours(hours, min!)
414
+ : arguments.length === 3
415
+ ? super.setUTCHours(hours, min!, sec!)
416
+ : super.setUTCHours(hours, min!, sec!, ms!),
417
+ );
418
+ }
419
+ override setUTCMilliseconds(ms: number): number {
420
+ return this.#write(super.setUTCMilliseconds(ms));
421
+ }
422
+ override setUTCMinutes(min: number, sec?: number, ms?: number): number {
423
+ return this.#write(
424
+ arguments.length === 1
425
+ ? super.setUTCMinutes(min)
426
+ : arguments.length === 2
427
+ ? super.setUTCMinutes(min, sec!)
428
+ : super.setUTCMinutes(min, sec!, ms!),
429
+ );
430
+ }
431
+ override setUTCMonth(month: number, date?: number): number {
432
+ return this.#write(
433
+ arguments.length === 1
434
+ ? super.setUTCMonth(month)
435
+ : super.setUTCMonth(month, date!),
436
+ );
437
+ }
438
+ override setUTCSeconds(sec: number, ms?: number): number {
439
+ return this.#write(
440
+ arguments.length === 1
441
+ ? super.setUTCSeconds(sec)
442
+ : super.setUTCSeconds(sec, ms!),
443
+ );
444
+ }
445
+ }
446
+
447
+ export class SvelteSet<T = any> extends Set<T> {
448
+ readonly #version = createSignal(0);
449
+ readonly #items = new Map<T, Signal<number>>();
450
+ readonly #ready: boolean;
451
+
452
+ constructor(values?: Iterable<T> | null) {
453
+ super();
454
+ if (values) {
455
+ for (const item of values) {
456
+ super.add(item);
457
+ }
458
+ }
459
+ this.#ready = true;
460
+ }
461
+
462
+ #readVersion(): void {
463
+ trackSignal(this, "size", this.#version);
464
+ }
465
+
466
+ #bumpVersion(): void {
467
+ bumpSignal(this, "size", this.#version);
468
+ }
469
+
470
+ override get size(): number {
471
+ this.#readVersion();
472
+ return super.size;
473
+ }
474
+
475
+ override has(value: T): boolean {
476
+ const exists = super.has(value);
477
+ if (isTracking()) {
478
+ if (exists) {
479
+ let sig = this.#items.get(value);
480
+ if (!sig) {
481
+ sig = createSignal(0);
482
+ this.#items.set(value, sig);
483
+ }
484
+ trackSignal(this, value as PropertyKey, sig);
485
+ } else {
486
+ this.#readVersion();
487
+ }
488
+ }
489
+ return exists;
490
+ }
491
+
492
+ override add(value: T): this {
493
+ if (!this.#ready) {
494
+ return super.add(value);
495
+ }
496
+ if (!super.has(value)) {
497
+ super.add(value);
498
+ this.#bumpVersion();
499
+ }
500
+ return this;
501
+ }
502
+
503
+ override delete(value: T): boolean {
504
+ if (!super.has(value)) {
505
+ return false;
506
+ }
507
+ const sig = this.#items.get(value);
508
+ this.#items.delete(value);
509
+ const removed = super.delete(value);
510
+ batch(() => {
511
+ if (sig) bumpSignal(this, value as PropertyKey, sig);
512
+ this.#bumpVersion();
513
+ });
514
+ return removed;
515
+ }
516
+
517
+ override clear(): void {
518
+ if (super.size === 0) return;
519
+ const entries = Array.from(this.#items.entries());
520
+ this.#items.clear();
521
+ super.clear();
522
+ batch(() => {
523
+ for (const [key, sig] of entries) {
524
+ bumpSignal(this, key as PropertyKey, sig);
525
+ }
526
+ this.#bumpVersion();
527
+ });
528
+ }
529
+
530
+ override keys(): SetIterator<T> {
531
+ this.#readVersion();
532
+ return super.keys();
533
+ }
534
+
535
+ override values(): SetIterator<T> {
536
+ this.#readVersion();
537
+ return super.values();
538
+ }
539
+
540
+ override entries(): SetIterator<[T, T]> {
541
+ this.#readVersion();
542
+ return super.entries();
543
+ }
544
+
545
+ override [Symbol.iterator](): SetIterator<T> {
546
+ this.#readVersion();
547
+ return super[Symbol.iterator]();
548
+ }
549
+
550
+ override forEach(
551
+ callbackfn: (value: T, value2: T, set: Set<T>) => void,
552
+ thisArg?: any,
553
+ ): void {
554
+ this.#readVersion();
555
+ super.forEach((v, v2) => callbackfn.call(thisArg, v, v2, this));
556
+ }
557
+ }
558
+
559
+ export class SvelteMap<K = any, V = any> extends Map<K, V> {
560
+ readonly #version = createSignal(0);
561
+ readonly #valuesVersion = createSignal(0);
562
+ readonly #items = new Map<K, Signal<number>>();
563
+ readonly #ready: boolean;
564
+
565
+ constructor(entries?: Iterable<readonly [K, V]> | null) {
566
+ super();
567
+ if (entries) {
568
+ for (const [k, v] of entries) {
569
+ super.set(k, v);
570
+ }
571
+ }
572
+ this.#ready = true;
573
+ }
574
+
575
+ #readVersion(): void {
576
+ trackSignal(this, "size", this.#version);
577
+ }
578
+
579
+ #readValuesVersion(): void {
580
+ trackSignal(this, "size", this.#version);
581
+ trackSignal(this, "values", this.#valuesVersion);
582
+ }
583
+
584
+ #bumpVersion(): void {
585
+ bumpSignal(this, "size", this.#version);
586
+ }
587
+
588
+ #bumpValuesVersion(): void {
589
+ bumpSignal(this, "values", this.#valuesVersion);
590
+ }
591
+
592
+ override get size(): number {
593
+ this.#readVersion();
594
+ return super.size;
595
+ }
596
+
597
+ override has(key: K): boolean {
598
+ const exists = super.has(key);
599
+ if (isTracking()) {
600
+ if (exists) {
601
+ let sig = this.#items.get(key);
602
+ if (!sig) {
603
+ sig = createSignal(0);
604
+ this.#items.set(key, sig);
605
+ }
606
+ trackSignal(this, key as PropertyKey, sig);
607
+ } else {
608
+ this.#readVersion();
609
+ }
610
+ }
611
+ return exists;
612
+ }
613
+
614
+ override get(key: K): V | undefined {
615
+ const exists = super.has(key);
616
+ if (isTracking()) {
617
+ if (exists) {
618
+ let sig = this.#items.get(key);
619
+ if (!sig) {
620
+ sig = createSignal(0);
621
+ this.#items.set(key, sig);
622
+ }
623
+ trackSignal(this, key as PropertyKey, sig);
624
+ } else {
625
+ this.#readVersion();
626
+ }
627
+ }
628
+ return super.get(key);
629
+ }
630
+
631
+ override set(key: K, value: V): this {
632
+ if (!this.#ready) {
633
+ return super.set(key, value);
634
+ }
635
+ const exists = super.has(key);
636
+ const prev = super.get(key);
637
+ super.set(key, value);
638
+ if (!exists) {
639
+ batch(() => {
640
+ this.#bumpVersion();
641
+ this.#bumpValuesVersion();
642
+ });
643
+ } else if (!Object.is(prev, value)) {
644
+ const sig = this.#items.get(key);
645
+ batch(() => {
646
+ if (sig) bumpSignal(this, key as PropertyKey, sig);
647
+ this.#bumpValuesVersion();
648
+ });
649
+ }
650
+ return this;
651
+ }
652
+
653
+ override delete(key: K): boolean {
654
+ if (!super.has(key)) {
655
+ return false;
656
+ }
657
+ const sig = this.#items.get(key);
658
+ this.#items.delete(key);
659
+ const removed = super.delete(key);
660
+ batch(() => {
661
+ if (sig) bumpSignal(this, key as PropertyKey, sig);
662
+ this.#bumpVersion();
663
+ this.#bumpValuesVersion();
664
+ });
665
+ return removed;
666
+ }
667
+
668
+ override clear(): void {
669
+ if (super.size === 0) return;
670
+ const entries = Array.from(this.#items.entries());
671
+ this.#items.clear();
672
+ super.clear();
673
+ batch(() => {
674
+ for (const [key, sig] of entries) {
675
+ bumpSignal(this, key as PropertyKey, sig);
676
+ }
677
+ this.#bumpVersion();
678
+ this.#bumpValuesVersion();
679
+ });
680
+ }
681
+
682
+ override keys(): MapIterator<K> {
683
+ this.#readVersion();
684
+ return super.keys();
685
+ }
686
+
687
+ override values(): MapIterator<V> {
688
+ this.#readValuesVersion();
689
+ return super.values();
690
+ }
691
+
692
+ override entries(): MapIterator<[K, V]> {
693
+ this.#readValuesVersion();
694
+ return super.entries();
695
+ }
696
+
697
+ override [Symbol.iterator](): MapIterator<[K, V]> {
698
+ this.#readValuesVersion();
699
+ return super[Symbol.iterator]();
700
+ }
701
+
702
+ override forEach(
703
+ callbackfn: (value: V, key: K, map: Map<K, V>) => void,
704
+ thisArg?: any,
705
+ ): void {
706
+ this.#readValuesVersion();
707
+ super.forEach((v, k) => callbackfn.call(thisArg, v, k, this));
708
+ }
709
+ }
710
+
711
+ const urlLinkSymbol = Symbol("moonshine.svelte.url");
712
+ const paramsSyncSymbol = Symbol("moonshine.svelte.params.sync");
713
+
714
+ export class SvelteURLSearchParams extends URLSearchParams {
715
+ readonly #version = createSignal(0);
716
+ #url: SvelteURL | undefined;
717
+
718
+ [urlLinkSymbol](url: SvelteURL): void {
719
+ this.#url = url;
720
+ }
721
+
722
+ [paramsSyncSymbol](search: string): void {
723
+ const clean = search.startsWith("?") ? search.slice(1) : search;
724
+ if (super.toString() === clean) return;
725
+ for (const key of Array.from(super.keys())) {
726
+ super.delete(key);
727
+ }
728
+ const parsed = new URLSearchParams(clean);
729
+ for (const [k, v] of parsed.entries()) {
730
+ super.append(k, v);
731
+ }
732
+ bumpSignal(this, "searchParams", this.#version);
733
+ }
734
+
735
+ #read(): void {
736
+ trackSignal(this, "searchParams", this.#version);
737
+ }
738
+
739
+ #mutate(fn: () => void): void {
740
+ const before = super.toString();
741
+ fn();
742
+ const after = super.toString();
743
+ if (before !== after) {
744
+ batch(() => {
745
+ bumpSignal(this, "searchParams", this.#version);
746
+ if (this.#url) {
747
+ this.#url[paramsSyncSymbol](after);
748
+ }
749
+ });
750
+ }
751
+ }
752
+
753
+ override get size(): number {
754
+ this.#read();
755
+ return super.size;
756
+ }
757
+
758
+ override append(name: string, value: string): void {
759
+ this.#mutate(() => super.append(name, value));
760
+ }
761
+
762
+ override delete(name: string, value?: string): void {
763
+ this.#mutate(() => {
764
+ if (arguments.length > 1) {
765
+ super.delete(name, value);
766
+ } else {
767
+ super.delete(name);
768
+ }
769
+ });
770
+ }
771
+
772
+ override set(name: string, value: string): void {
773
+ this.#mutate(() => super.set(name, value));
774
+ }
775
+
776
+ override sort(): void {
777
+ this.#mutate(() => super.sort());
778
+ }
779
+
780
+ override get(name: string): string | null {
781
+ this.#read();
782
+ return super.get(name);
783
+ }
784
+
785
+ override getAll(name: string): string[] {
786
+ this.#read();
787
+ return super.getAll(name);
788
+ }
789
+
790
+ override has(name: string, value?: string): boolean {
791
+ this.#read();
792
+ return arguments.length > 1 ? super.has(name, value) : super.has(name);
793
+ }
794
+
795
+ override toString(): string {
796
+ this.#read();
797
+ return super.toString();
798
+ }
799
+
800
+ override keys(): URLSearchParamsIterator<string> {
801
+ this.#read();
802
+ return super.keys();
803
+ }
804
+
805
+ override values(): URLSearchParamsIterator<string> {
806
+ this.#read();
807
+ return super.values();
808
+ }
809
+
810
+ override entries(): URLSearchParamsIterator<[string, string]> {
811
+ this.#read();
812
+ return super.entries();
813
+ }
814
+
815
+ override [Symbol.iterator](): URLSearchParamsIterator<[string, string]> {
816
+ this.#read();
817
+ return super[Symbol.iterator]();
818
+ }
819
+
820
+ override forEach(
821
+ callbackfn: (value: string, key: string, parent: URLSearchParams) => void,
822
+ thisArg?: any,
823
+ ): void {
824
+ this.#read();
825
+ super.forEach((v, k) => callbackfn.call(thisArg, v, k, this));
826
+ }
827
+ }
828
+
829
+ export class SvelteURL extends URL {
830
+ readonly #version = createSignal(0);
831
+ readonly #searchParams: SvelteURLSearchParams;
832
+
833
+ constructor(url: string | URL, base?: string | URL) {
834
+ super(url, base);
835
+ this.#searchParams = new SvelteURLSearchParams(super.search);
836
+ this.#searchParams[urlLinkSymbol](this);
837
+ }
838
+
839
+ [paramsSyncSymbol](serialized: string): void {
840
+ const nextSearch = serialized ? `?${serialized}` : "";
841
+ if (super.search !== nextSearch) {
842
+ super.search = nextSearch;
843
+ }
844
+ bumpSignal(this, "href", this.#version);
845
+ }
846
+
847
+ #read(): void {
848
+ trackSignal(this, "href", this.#version);
849
+ }
850
+
851
+ #mutate(fn: () => void): void {
852
+ const beforeHref = super.href;
853
+ const beforeSearch = super.search;
854
+ fn();
855
+ if (super.href !== beforeHref) {
856
+ batch(() => {
857
+ if (super.search !== beforeSearch) {
858
+ this.#searchParams[paramsSyncSymbol](super.search);
859
+ }
860
+ bumpSignal(this, "href", this.#version);
861
+ });
862
+ }
863
+ }
864
+
865
+ override get searchParams(): SvelteURLSearchParams {
866
+ return this.#searchParams;
867
+ }
868
+
869
+ override get href(): string {
870
+ this.#read();
871
+ return super.href;
872
+ }
873
+ override set href(value: string) {
874
+ this.#mutate(() => {
875
+ super.href = value;
876
+ });
877
+ }
878
+
879
+ override get origin(): string {
880
+ this.#read();
881
+ return super.origin;
882
+ }
883
+
884
+ override get protocol(): string {
885
+ this.#read();
886
+ return super.protocol;
887
+ }
888
+ override set protocol(value: string) {
889
+ this.#mutate(() => {
890
+ super.protocol = value;
891
+ });
892
+ }
893
+
894
+ override get username(): string {
895
+ this.#read();
896
+ return super.username;
897
+ }
898
+ override set username(value: string) {
899
+ this.#mutate(() => {
900
+ super.username = value;
901
+ });
902
+ }
903
+
904
+ override get password(): string {
905
+ this.#read();
906
+ return super.password;
907
+ }
908
+ override set password(value: string) {
909
+ this.#mutate(() => {
910
+ super.password = value;
911
+ });
912
+ }
913
+
914
+ override get host(): string {
915
+ this.#read();
916
+ return super.host;
917
+ }
918
+ override set host(value: string) {
919
+ this.#mutate(() => {
920
+ super.host = value;
921
+ });
922
+ }
923
+
924
+ override get hostname(): string {
925
+ this.#read();
926
+ return super.hostname;
927
+ }
928
+ override set hostname(value: string) {
929
+ this.#mutate(() => {
930
+ super.hostname = value;
931
+ });
932
+ }
933
+
934
+ override get port(): string {
935
+ this.#read();
936
+ return super.port;
937
+ }
938
+ override set port(value: string) {
939
+ this.#mutate(() => {
940
+ super.port = value;
941
+ });
942
+ }
943
+
944
+ override get pathname(): string {
945
+ this.#read();
946
+ return super.pathname;
947
+ }
948
+ override set pathname(value: string) {
949
+ this.#mutate(() => {
950
+ super.pathname = value;
951
+ });
952
+ }
953
+
954
+ override get search(): string {
955
+ this.#read();
956
+ return super.search;
957
+ }
958
+ override set search(value: string) {
959
+ this.#mutate(() => {
960
+ super.search = value;
961
+ });
962
+ }
963
+
964
+ override get hash(): string {
965
+ this.#read();
966
+ return super.hash;
967
+ }
968
+ override set hash(value: string) {
969
+ this.#mutate(() => {
970
+ super.hash = value;
971
+ });
972
+ }
973
+
974
+ override toString(): string {
975
+ this.#read();
976
+ return super.toString();
977
+ }
978
+
979
+ override toJSON(): string {
980
+ this.#read();
981
+ return super.toJSON();
982
+ }
983
+ }