1import { a2 as state, a as active_reaction, w as get, z as set_active_reaction, a4 as user_derived, x as set, v as source, a5 as update_version, a6 as increment } from './index-CsuPqJBw.js'; 2export { y as createSubscriber } from './index-CsuPqJBw.js'; 3import './custom-element-B4TOh4nm.js'; 4export { M as MediaQuery } from './media-query-C8CYkZ81.js'; 5import './index-6-3S3c88.js'; 6import './constants-CdtLqJ9y.js'; 7import './legacy-client-BEycUrdj.js'; 8import './utils-Bmu7F6be.js'; 9import './reactive-value-Di6jWgtv.js'; 10 11/** @import { Source } from '#client' */ 12 13var inited$1 = false; 14 15/** 16 * A reactive version of the built-in [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) object. 17 * Reading the date (whether with methods like `date.getTime()` or `date.toString()`, or via things like [`Intl.DateTimeFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat)) 18 * in an [effect](https://svelte.dev/docs/svelte/$effect) or [derived](https://svelte.dev/docs/svelte/$derived) 19 * will cause it to be re-evaluated when the value of the date changes. 20 * 21 * ```svelte 22 * <script> 23 * import { SvelteDate } from 'svelte/reactivity'; 24 * 25 * const date = new SvelteDate(); 26 * 27 * const formatter = new Intl.DateTimeFormat(undefined, { 28 * hour: 'numeric', 29 * minute: 'numeric', 30 * second: 'numeric' 31 * }); 32 * 33 * $effect(() => { 34 * const interval = setInterval(() => { 35 * date.setTime(Date.now()); 36 * }, 1000); 37 * 38 * return () => { 39 * clearInterval(interval); 40 * }; 41 * }); 42 * </script> 43 * 44 * <p>The time is {formatter.format(date)}</p> 45 * ``` 46 */ 47class SvelteDate extends Date { 48 #time = state(super.getTime()); 49 50 /** @type {Map<keyof Date, Source<unknown>>} */ 51 #deriveds = new Map(); 52 53 #reaction = active_reaction; 54 55 /** @param {any[]} params */ 56 constructor(...params) { 57 // @ts-ignore 58 super(...params); 59 60 if (!inited$1) this.#init(); 61 } 62 63 #init() { 64 inited$1 = true; 65 66 var proto = SvelteDate.prototype; 67 var date_proto = Date.prototype; 68 69 var methods = /** @type {Array<keyof Date & string>} */ ( 70 Object.getOwnPropertyNames(date_proto) 71 ); 72 73 for (const method of methods) { 74 if (method.startsWith('get') || method.startsWith('to') || method === 'valueOf') { 75 // @ts-ignore 76 proto[method] = function (...args) { 77 // don't memoize if there are arguments 78 // @ts-ignore 79 if (args.length > 0) { 80 get(this.#time); 81 // @ts-ignore 82 return date_proto[method].apply(this, args); 83 } 84 85 var d = this.#deriveds.get(method); 86 87 if (d === undefined) { 88 // lazily create the derived, but as though it were being 89 // created at the same time as the class instance 90 const reaction = active_reaction; 91 set_active_reaction(this.#reaction); 92 93 d = user_derived(() => { 94 get(this.#time); 95 // @ts-ignore 96 return date_proto[method].apply(this, args); 97 }); 98 99 this.#deriveds.set(method, d); 100 101 set_active_reaction(reaction); 102 } 103 104 return get(d); 105 }; 106 } 107 108 if (method.startsWith('set')) { 109 // @ts-ignore 110 proto[method] = function (...args) { 111 // @ts-ignore 112 var result = date_proto[method].apply(this, args); 113 set(this.#time, date_proto.getTime.call(this)); 114 return result; 115 }; 116 } 117 } 118 } 119} 120 121/** @import { Source } from '#client' */ 122 123var read_methods = ['forEach', 'isDisjointFrom', 'isSubsetOf', 'isSupersetOf']; 124var set_like_methods = ['difference', 'intersection', 'symmetricDifference', 'union']; 125 126var inited = false; 127 128/** 129 * A reactive version of the built-in [`Set`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) object. 130 * Reading contents of the set (by iterating, or by reading `set.size` or calling `set.has(...)` as in the [example](https://svelte.dev/playground/53438b51194b4882bcc18cddf9f96f15) below) in an [effect](https://svelte.dev/docs/svelte/$effect) or [derived](https://svelte.dev/docs/svelte/$derived) 131 * will cause it to be re-evaluated as necessary when the set is updated. 132 *
133 * Note that values in a reactive set are _not_ made [deeply reactive](https://svelte.dev/docs/svelte/$state#Deep-state). 134 * 135 * ```svelte 136 * <script> 137 * import { SvelteSet } from 'svelte/reactivity'; 138 * let monkeys = new SvelteSet(); 139 * 140 * function toggle(monkey) { 141 * if (monkeys.has(monkey)) { 142 * monkeys.delete(monkey); 143 * } else { 144 * monkeys.add(monkey); 145 * } 146 * } 147 * </script> 148 * 149 * {#each ['ð', 'ð', 'ð'] as monkey} 150 * <button onclick={() => toggle(monkey)}>{monkey}</button> 151 * {/each} 152 * 153 * <button onclick={() => monkeys.clear()}>clear</button> 154 * 155 * {#if monkeys.has('ð')}<p>see no evil</p>{/if} 156 * {#if monkeys.has('ð')}<p>hear no evil</p>{/if} 157 * {#if monkeys.has('ð')}<p>speak no evil</p>{/if} 158 * ``` 159 * 160 * @template T 161 * @extends {Set<T>} 162 */ 163class SvelteSet extends Set { 164 /** @type {Map<T, Source<boolean>>} */ 165 #sources = new Map(); 166 #version = state(0); 167 #size = state(0); 168 #update_version = update_version || -1; 169 170 /** 171 * @param {Iterable<T> | null | undefined} [value] 172 */ 173 constructor(value) { 174 super(); 175 176 if (value) { 177 for (var element of value) { 178 super.add(element); 179 } 180 this.#size.v = super.size; 181 } 182 183 if (!inited) this.#init(); 184 } 185 186 /** 187 * If the source is being created inside the same reaction as the SvelteSet instance, 188 * we use `state` so that it will not be a dependency of the reaction. Otherwise we 189 * use `source` so it will be. 190 * 191 * @template T 192 * @param {T} value 193 * @returns {Source<T>} 194 */ 195 #source(value) { 196 return update_version === this.#update_version ? state(value) : source(value); 197 } 198 199 // We init as part of the first instance so that we can treeshake this class 200 #init() { 201 inited = true; 202 203 var proto = SvelteSet.prototype; 204 var set_proto = Set.prototype; 205 206 for (const method of read_methods) { 207 // @ts-ignore 208 proto[method] = function (...v) { 209 get(this.#version); 210 // @ts-ignore 211 return set_proto[method].apply(this, v); 212 }; 213 } 214 215 for (const method of set_like_methods) { 216 // @ts-ignore 217 proto[method] = function (...v) { 218 get(this.#version); 219 // @ts-ignore 220 var set = /** @type {Set<T>} */ (set_proto[method].apply(this, v)); 221 return new SvelteSet(set); 222 }; 223 } 224 } 225 226 /** @param {T} value */ 227 has(value) { 228 var has = super.has(value); 229 var sources = this.#sources; 230 var s = sources.get(value); 231 232 if (s === undefined) { 233 if (!has) { 234 // If the value doesn't exist, track the version in case it's added later 235 // but don't create sources willy-nilly to track all possible values 236 get(this.#version); 237 return false; 238 } 239 240 s = this.#source(true); 241 242 sources.set(value, s); 243 } 244 245 get(s); 246 return has; 247 } 248 249 /** @param {T} value */ 250 add(value) { 251 if (!super.has(value)) { 252 super.add(value); 253 set(this.#size, super.size); 254 increment(this.#version); 255 } 256 257 return this; 258 } 259 260 /** @param {T} value */ 261 delete(value) { 262 var deleted = super.delete(value); 263 var sources = this.#sources; 264 var s = sources.get(value); 265 266 if (s !== undefined) { 267 sources.delete(value); 268 set(s, false); 269 } 270 271 if (deleted) { 272 set(this.#size, super.size); 273 increment(this.#version); 274 } 275 276 return deleted; 277 } 278 279 clear() { 280 if (super.size === 0) { 281 return; 282 } 283 // Clear first, so we get nice console.log outputs with $inspect 284 super.clear(); 285 var sources = this.#sources; 286 287 for (var s of sources.values()) { 288 set(s, false); 289 } 290 291 sources.clear(); 292 set(this.#size, 0); 293 increment(this.#version); 294 } 295 296 keys() { 297 return this.values(); 298 } 299 300 values() { 301 get(this.#version); 302 return super.values(); 303 } 304 305 entries() { 306 get(this.#version); 307 return super.entries(); 308 } 309 310 [Symbol.iterator]() { 311 return this.keys(); 312 } 313 314 get size() { 315 return get(this.#size); 316 } 317} 318 319/** @import { Source } from '#client' */ 320 321/** 322 * A reactive version of the built-in [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) object. 323 * Reading contents of the map (by iterating, or by reading `map.size` or calling `map.get(...)` or `map.has(...)` as in the [tic-tac-toe example](https://svelte.dev/playground/0b0ff4aa49c9443f9b47fe5203c78293) below) in an [effect](https://svelte.dev/docs/svelte/$effect) or [derived](https://svelte.dev/docs/svelte/$derived) 324 * will cause it to be re-evaluated as necessary when the map is updated. 325 *
326 * Note that values in a reactive map are _not_ made [deeply reactive](https://svelte.dev/docs/svelte/$state#Deep-state). 327 * 328 * ```svelte 329 * <script> 330 * import { SvelteMap } from 'svelte/reactivity'; 331 * import { result } from './game.js'; 332 * 333 * let board = new SvelteMap(); 334 * let player = $state('x'); 335 * let winner = $derived(result(board)); 336 * 337 * function reset() { 338 * player = 'x'; 339 * board.clear(); 340 * } 341 * </script> 342 * 343 * <div class="board"> 344 * {#each Array(9), i} 345 * <button 346 * disabled={board.has(i) || winner} 347 * onclick={() => { 348 * board.set(i, player); 349 * player = player === 'x' ? 'o' : 'x'; 350 * }} 351 * >{board.get(i)}</button> 352 * {/each} 353 * </div> 354 * 355 * {#if winner} 356 * <p>{winner} wins!</p> 357 * <button onclick={reset}>reset</button> 358 * {:else} 359 * <p>{player} is next</p> 360 * {/if} 361 * ``` 362 * 363 * @template K 364 * @template V 365 * @extends {Map<K, V>} 366 */ 367class SvelteMap extends Map { 368 /** @type {Map<K, Source<number>>} */ 369 #sources = new Map(); 370 #version = state(0); 371 #size = state(0); 372 #update_version = update_version || -1; 373 374 /** 375 * @param {Iterable<readonly [K, V]> | null | undefined} [value] 376 */ 377 constructor(value) { 378 super(); 379 380 if (value) { 381 for (var [key, v] of value) { 382 super.set(key, v); 383 } 384 this.#size.v = super.size; 385 } 386 } 387 388 /** 389 * If the source is being created inside the same reaction as the SvelteMap instance, 390 * we use `state` so that it will not be a dependency of the reaction. Otherwise we 391 * use `source` so it will be. 392 * 393 * @template T 394 * @param {T} value 395 * @returns {Source<T>} 396 */ 397 #source(value) { 398 return update_version === this.#update_version ? state(value) : source(value); 399 } 400 401 /** @param {K} key */ 402 has(key) { 403 var sources = this.#sources; 404 var s = sources.get(key); 405 406 if (s === undefined) { 407 var ret = super.get(key); 408 if (ret !== undefined) { 409 s = this.#source(0); 410 411 sources.set(key, s); 412 } else { 413 // We should always track the version in case 414 // the Set ever gets this value in the future. 415 get(this.#version); 416 return false; 417 } 418 } 419 420 get(s); 421 return true; 422 } 423 424 /** 425 * @param {(value: V, key: K, map: Map<K, V>) => void} callbackfn 426 * @param {any} [this_arg] 427 */ 428 forEach(callbackfn, this_arg) { 429 this.#read_all(); 430 super.forEach(callbackfn, this_arg); 431 } 432 433 /** @param {K} key */ 434 get(key) { 435 var sources = this.#sources; 436 var s = sources.get(key); 437 438 if (s === undefined) { 439 var ret = super.get(key); 440 if (ret !== undefined) { 441 s = this.#source(0); 442 443 sources.set(key, s); 444 } else { 445 // We should always track the version in case 446 // the Set ever gets this value in the future. 447 get(this.#version); 448 return undefined; 449 } 450 } 451 452 get(s); 453 return super.get(key); 454 } 455 456 /** 457 * @param {K} key 458 * @param {V} value 459 * */ 460 set(key, value) { 461 var sources = this.#sources; 462 var s = sources.get(key); 463 var prev_res = super.get(key); 464 var res = super.set(key, value); 465 var version = this.#version; 466 467 if (s === undefined) { 468 s = this.#source(0); 469 470 sources.set(key, s); 471 set(this.#size, super.size); 472 increment(version); 473 } else if (prev_res !== value) { 474 increment(s); 475 476 // if not every reaction of s is a reaction of version we need to also include version 477 var v_reactions = version.reactions === null ? null : new Set(version.reactions); 478 var needs_version_increase = 479 v_reactions === null || 480 !s.reactions?.every((r) => 481 /** @type {NonNullable<typeof v_reactions>} */ (v_reactions).has(r) 482 ); 483 if (needs_version_increase) { 484 increment(version); 485 } 486 } 487 488 return res; 489 } 490 491 /** @param {K} key */ 492 delete(key) { 493 var sources = this.#sources; 494 var s = sources.get(key); 495 var res = super.delete(key); 496 497 if (s !== undefined) { 498 sources.delete(key); 499 set(this.#size, super.size); 500 set(s, -1); 501 increment(this.#version); 502 } 503 504 return res; 505 } 506 507 clear() { 508 if (super.size === 0) { 509 return; 510 } 511 // Clear first, so we get nice console.log outputs with $inspect 512 super.clear(); 513 var sources = this.#sources; 514 set(this.#size, 0); 515 for (var s of sources.values()) { 516 set(s, -1); 517 } 518 increment(this.#version); 519 sources.clear(); 520 } 521 522 #read_all() { 523 get(this.#version); 524 525 var sources = this.#sources; 526 if (this.#size.v !== sources.size) { 527 for (var key of super.keys()) { 528 if (!sources.has(key)) { 529 var s = this.#source(0); 530 531 sources.set(key, s); 532 } 533 } 534 } 535 536 for ([, s] of this.#sources) { 537 get(s); 538 } 539 } 540 541 keys() { 542 get(this.#version); 543 return super.keys(); 544 } 545 546 values() { 547 this.#read_all(); 548 return super.values(); 549 } 550 551 entries() { 552 this.#read_all(); 553 return super.entries(); 554 } 555 556 [Symbol.iterator]() { 557 return this.entries(); 558 } 559 560 get size() { 561 get(this.#size); 562 return super.size; 563 } 564} 565 566const REPLACE = Symbol(); 567 568/** 569 * A reactive version of the built-in [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) object. 570 * Reading its contents (by iterating, or by calling `params.get(...)` or `params.getAll(...)` as in the [example](https://svelte.dev/playground/b3926c86c5384bab9f2cf993bc08c1c8) below) in an [effect](https://svelte.dev/docs/svelte/$effect) or [derived](https://svelte.dev/docs/svelte/$derived) 571 * will cause it to be re-evaluated as necessary when the params are updated. 572 * 573 * ```svelte 574 * <script> 575 * import { SvelteURLSearchParams } from 'svelte/reactivity'; 576 * 577 * const params = new SvelteURLSearchParams('message=hello'); 578 * 579 * let key = $state('key'); 580 * let value = $state('value'); 581 * </script> 582 * 583 * <input bind:value={key} /> 584 * <input bind:value={value} /> 585 * <button onclick={() => params.append(key, value)}>append</button> 586 * 587 * <p>?{params.toString()}</p> 588 * 589 * {#each params as [key, value]} 590 * <p>{key}: {value}</p> 591 * {/each} 592 * ``` 593 */ 594class SvelteURLSearchParams extends URLSearchParams { 595 #version = state(0); 596 #url = get_current_url(); 597 598 #updating = false;
599 600 #update_url() { 601 if (!this.#url || this.#updating) return; 602 this.#updating = true; 603 604 const search = this.toString(); 605 this.#url.search = search && `?${search}`; 606 607 this.#updating = false; 608 } 609 610 /** 611 * @param {URLSearchParams} params 612 * @internal 613 */ 614 [REPLACE](params) { 615 if (this.#updating) return; 616 this.#updating = true; 617 618 for (const key of [...super.keys()]) { 619 super.delete(key); 620 } 621 622 for (const [key, value] of params) { 623 super.append(key, value); 624 } 625 626 increment(this.#version); 627 this.#updating = false; 628 } 629 630 /** 631 * @param {string} name 632 * @param {string} value 633 * @returns {void} 634 */ 635 append(name, value) { 636 super.append(name, value); 637 this.#update_url(); 638 increment(this.#version); 639 } 640 641 /** 642 * @param {string} name 643 * @param {string=} value 644 * @returns {void} 645 */ 646 delete(name, value) { 647 var has_value = super.has(name, value); 648 super.delete(name, value); 649 if (has_value) { 650 this.#update_url(); 651 increment(this.#version); 652 } 653 } 654 655 /** 656 * @param {string} name 657 * @returns {string|null} 658 */ 659 get(name) { 660 get(this.#version); 661 return super.get(name); 662 } 663 664 /** 665 * @param {string} name 666 * @returns {string[]} 667 */ 668 getAll(name) { 669 get(this.#version); 670 return super.getAll(name); 671 } 672 673 /** 674 * @param {string} name 675 * @param {string=} value 676 * @returns {boolean} 677 */ 678 has(name, value) { 679 get(this.#version); 680 return super.has(name, value); 681 } 682 683 keys() { 684 get(this.#version); 685 return super.keys(); 686 } 687 688 /** 689 * @param {string} name 690 * @param {string} value 691 * @returns {void} 692 */ 693 set(name, value) { 694 var previous = super.getAll(name).join(''); 695 super.set(name, value); 696 // can't use has(name, value), because for something like https://svelte.dev?foo=1&bar=2&foo=3 697 // if you set `foo` to 1, then foo=3 gets deleted whilst `has("foo", "1")` returns true 698 if (previous !== super.getAll(name).join('')) { 699 this.#update_url(); 700 increment(this.#version); 701 } 702 } 703 704 sort() { 705 super.sort(); 706 this.#update_url(); 707 increment(this.#version); 708 } 709 710 toString() { 711 get(this.#version); 712 return super.toString(); 713 } 714 715 values() { 716 get(this.#version); 717 return super.values(); 718 } 719 720 entries() { 721 get(this.#version); 722 return super.entries(); 723 } 724 725 [Symbol.iterator]() { 726 return this.entries(); 727 } 728 729 get size() { 730 get(this.#version); 731 return super.size; 732 } 733} 734 735/** @type {SvelteURL | null} */ 736let current_url = null; 737 738function get_current_url() { 739 // ideally we'd just export `current_url` directly, but it seems Vitest doesn't respect live bindings 740 return current_url; 741} 742 743/** 744 * A reactive version of the built-in [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) object. 745 * Reading properties of the URL (such as `url.href` or `url.pathname`) in an [effect](https://svelte.dev/docs/svelte/$effect) or [derived](https://svelte.dev/docs/svelte/$derived) 746 * will cause it to be re-evaluated as necessary when the URL changes. 747 * 748 * The `searchParams` property is an instance of [SvelteURLSearchParams](https://svelte.dev/docs/svelte/svelte-reactivity#SvelteURLSearchParams). 749 * 750 * [Example](https://svelte.dev/playground/5a694758901b448c83dc40dc31c71f2a): 751 * 752 * ```svelte 753 * <script> 754 * import { SvelteURL } from 'svelte/reactivity'; 755 * 756 * const url = new SvelteURL('https://example.com/path'); 757 * </script> 758 * 759 * <!-- changes to these... --> 760 * <input bind:value={url.protocol} /> 761 * <input bind:value={url.hostname} /> 762 * <input bind:value={url.pathname} /> 763 * 764 * <hr /> 765 * 766 * <!-- will update `href` and vice versa --> 767 * <input bind:value={url.href} size="65" /> 768 * ``` 769 */ 770class SvelteURL extends URL { 771 #protocol = state(super.protocol); 772 #username = state(super.username); 773 #password = state(super.password); 774 #hostname = state(super.hostname); 775 #port = state(super.port); 776 #pathname = state(super.pathname); 777 #hash = state(super.hash); 778 #search = state(super.search); 779 #searchParams; 780 781 /** 782 * @param {string | URL} url 783 * @param {string | URL} [base] 784 */ 785 constructor(url, base) { 786 url = new URL(url, base); 787 super(url); 788 789 current_url = this; 790 this.#searchParams = new SvelteURLSearchParams(url.searchParams); 791 current_url = null; 792 } 793 794 get hash() { 795 return get(this.#hash); 796 } 797 798 set hash(value) { 799 super.hash = value; 800 set(this.#hash, super.hash); 801 } 802 803 get host() { 804 get(this.#hostname); 805 get(this.#port); 806 return super.host; 807 } 808 809 set host(value) { 810 super.host = value; 811 set(this.#hostname, super.hostname); 812 set(this.#port, super.port); 813 } 814 815 get hostname() { 816 return get(this.#hostname); 817 } 818 819 set hostname(value) { 820 super.hostname = value; 821 set(this.#hostname, super.hostname); 822 } 823 824 get href() { 825 get(this.#protocol); 826 get(this.#username); 827 get(this.#password); 828 get(this.#hostname); 829 get(this.#port); 830 get(this.#pathname); 831 get(this.#hash); 832 get(this.#search); 833 return super.href; 834 } 835 836 set href(value) { 837 super.href = value; 838 set(this.#protocol, super.protocol); 839 set(this.#username, super.username); 840 set(this.#password, super.password); 841 set(this.#hostname, super.hostname); 842 set(this.#port, super.port); 843 set(this.#pathname, super.pathname); 844 set(this.#hash, super.hash); 845 set(this.#search, super.search); 846 this.#searchParams[REPLACE](super.searchParams); 847 } 848 849 get password() { 850 return get(this.#password); 851 } 852 853 set password(value) { 854 super.password = value; 855 set(this.#password, super.password); 856 } 857 858 get pathname() { 859 return get(this.#pathname); 860 } 861 862 set pathname(value) { 863 super.pathname = value; 864 set(this.#pathname, super.pathname); 865 } 866 867 get port() { 868 return get(this.#port); 869 } 870 871 set port(value) { 872 super.port = value; 873 set(this.#port, super.port); 874 } 875 876 get protocol() { 877 return get(this.#protocol); 878 } 879 880 set protocol(value) { 881 super.protocol = value; 882 set(this.#protocol, super.protocol); 883 } 884 885 get search() { 886 return get(this.#search); 887 } 888 889 set search(value) { 890 super.search = value; 891 set(this.#search, value); 892 this.#searchParams[REPLACE](super.searchParams); 893 } 894 895 get username() { 896 return get(this.#username); 897 } 898 899 set username(value) { 900 super.username = value; 901 set(this.#username, super.username); 902 } 903 904 get origin() { 905 get(this.#protocol); 906 get(this.#hostname); 907 get(this.#port); 908 return super.origin; 909 } 910 911 get searchParams() { 912 return this.#searchParams; 913 } 914 915 toString() { 916 return this.href; 917 } 918 919 toJSON() { 920 return this.href; 921 } 922} 923 924export { SvelteDate, SvelteMap, SvelteSet, SvelteURL, SvelteURLSearchParams };
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.