All files / src/internal/client runtime.js

96.42% Statements 1132/1174
94.07% Branches 238/253
95.65% Functions 44/46
96.38% Lines 1121/1163

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 11852x 2x 2x           2x                                 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 73812x 73812x 2x 2x 2x 13604x 13604x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 14522x 14522x 2x 2x 2x 2x 2x 2x 5452x 5452x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 12x 12x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 81x 81x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 918x 918x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1008x 1008x 2x 2x 39364x 39364x 2x 2x 2x 56405x 56405x 2x 2x 2x 2x 2x 2x 2x 2x 98713x 98713x 98713x 31371x 31371x 67342x 98713x 9924x 9924x 9924x 9924x 9855x 9855x 9855x 218x 352x 352x 218x 218x 218x 9855x 9855x 13067x 13067x 13067x 2636x 2636x 13066x 13066x 13066x 13066x 13066x 13067x 13067x 13067x 49x 13067x 25x 25x 13066x 13067x 6342x 6342x 13067x 9855x 3581x 3581x 9825x 3525x 3525x 9924x 60999x 60999x 60999x 2x 2x 2x 2x 2x 2x 82x 82x 82x 68x 68x 14x 14x 14x 82x 82x 82x 2x 2x 14x 14x 14x 14x 45x 16x 16x 16x 16x 16x 8x 8x 8x 16x 16x 16x 16x 14x 82x 82x 82x 82x 82x 82x 82x 82x 82x 13x 13x 13x 184x 184x 112x 112x 72x 72x 13x 13x 13x 13x 14x 14x 14x 14x 2x 2x 2x 2x 2x 2x 2x 74584x 74584x 74584x 74584x 74584x 74584x 74584x 74584x 74584x 74584x 74584x 74584x 74584x 74584x 74584x 74584x 74584x 74584x 74584x 31150x 31150x 31150x 31150x 31150x 378x 378x 1125x 1125x 31150x 30772x 30772x 31150x 31150x 31094x 49684x 49684x 31094x 74584x 168x 168x 168x 74498x 74498x 74584x 74584x 74584x 74584x 74584x 74584x 74584x 74584x 74584x 2x 2x 2x 2x 2x 2x 2x 48904x 48904x 48904x 48847x 48847x 48829x 48829x 30585x 48829x 18244x 18244x 18244x 18244x 48829x 48847x 48904x 48904x 48904x 10298x 10298x 10298x 10298x 10217x 10217x 10298x 10298x 48904x 2x 2x 2x 2x 2x 2x 2x 81317x 81317x 30731x 81277x 48904x 48904x 81317x 2x 2x 2x 2x 2x 2x 2x 91025x 91025x 91025x 91025x 33365x 33365x 33365x 33365x 91025x 2x 2x 2x 2x 2x 2x 56422x 56422x 56422x     56422x 56422x 56422x 56422x 56422x 56422x 56422x 56422x 56422x 56422x 56422x 56422x 56422x 56422x 56422x 56422x 51338x 51338x 56422x 56422x 56422x 56422x 56422x 56422x 56422x 56340x 56340x 56422x 82x 56422x 56422x 56422x 56422x 56422x 56422x 56422x 56422x 56422x 2x 26706x 26706x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x     2x 26704x 26704x 2x 2x 2x 2x 2x 20218x 20218x 20218x 9494x 9494x 10724x 10724x 10724x 10724x 10724x 10724x 20141x 40316x 40316x 40316x 40316x 140x 40311x 40176x 40176x 40176x 40176x 40176x 40176x 40316x 20218x 10722x 10722x 20218x 2x 2x 2x 2x 2x 40309x 40309x 40309x 2167x 40104x 4708x 4708x 4708x 4550x 4550x 4550x 4550x 4550x 4550x 4550x 4550x 2802x 285x 285x 2802x 2517x 2517x 2517x 2802x 4550x 4708x 40309x 2x 4236x 4236x 4236x     4236x 4236x 4236x 4236x 4138x 4138x 4138x 4138x 4138x 4236x 2x 2x 2x 2x 2x 2x 20101x 10820x 4236x 4236x 4236x 10820x 20101x 20101x 20101x 20101x 45808x 45808x 45808x 45808x 28370x 15900x 15900x 45808x 7631x 7631x 7631x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 44744x 44744x 44744x 44744x 44744x 100532x 100532x 100532x 100532x 100532x 100532x 100532x 100532x 100532x 56184x 15673x 15673x 56184x 56184x 50551x 12928x 12928x 50544x 50544x 50544x 50551x 20783x 20783x 20783x 56184x 5607x 1039x 226x 226x 226x 5607x 4568x 4568x 5607x 56184x 79516x 79516x 100529x 49176x 49176x 49176x 61173x 40171x 40171x 21002x 59796x 9005x 9005x 9005x 11997x 11997x 49176x 30340x 30340x 30340x 44737x 44737x 44737x 44744x 4568x 4568x 4568x 4568x 44744x 2x 2x 2x 2x 2x 2x 2x 2x 15982x 15982x 15982x 15982x 15982x 15982x 15982x 15982x 15982x 15982x 15982x 15982x 15982x 15982x 15982x 15982x 15982x 15982x 15982x 3230x 3230x 14973x 14973x 14973x 14973x 14973x 14973x 14973x 15982x 15982x 15982x 15982x 15982x 2x 2x 2x 2x 2x 2x 121x 121x 121x 121x 121x 2x 2x 2x 2x 2x 2x 2x 121851x 121851x 121851x     121851x 121851x 616x 616x 121851x 121851x 121851x 89291x 2x 2x 89289x 89289x 89289x 89289x 89289x 89291x 39517x 89291x 31156x 49772x 18616x 18616x 89289x 89289x 89291x 89291x 89291x 89291x 8x 89291x 2x 2x 2x 89291x 121849x 121851x 41119x 41119x 41119x 15527x 15527x 41119x 121845x 121845x 121845x 2x 2x 2x 2x 2x 2x 2x 2x 12x 12x 2x 2x 2x 2x 2x 2x 2x 314x 314x 314x 314x 314x 314x 314x 314x 314x 314x 314x         314x 314x 314x 568x 568x 154x 156x 150x 150x 156x 568x 414x 414x 568x 314x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 9697x 9697x 9697x 9697x 9697x 9697x 9697x 9697x 2x 2x 2x 2x 2x 2x 2x 2x 2x 189025x 189025x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 42x 42x 42x 42x 42x 42x 22x 22x 42x 42x 42x 42x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 32x 32x 32x 32x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x         2x 2x 2x 2x 2x 2x 2x 2x 2x 80x 80x     80x 80x 80x 2x 2x 2x 2x 2x 68x 68x 68x 54x 54x 42x 42x 12x 12x 26x 26x 2x 2x 2x 2x 2x 2x 2x 4304x 4304x 4304x 4304x 2x 2x 2x 2x 2x 2x 2x 8x 8x 2x 2x 2x 2x 2x 2x 2x 26x 26x 26x 26x 100x 26x 26x 100x 26x 26x 26x 2x 2x 2x 2x 2x 2x 2x 2x 5293x 5293x 5293x 5293x 5293x 5293x 5293x 5293x 5293x 5293x 5293x 4661x 4661x 4661x 4661x 4661x 4661x 4661x 5293x 5293x 5293x 5293x 5293x 5293x 5293x 2x 2x 2x 2x 2x 2x 2x 5265x 5265x 5265x 2611x 2611x 5265x 5265x 2250x 2250x 2250x 2250x 2284x 2284x 2284x 2284x 2250x 2250x 2250x 2250x 5265x 5265x 5265x 5265x 5265x 5265x 5265x 5265x 5265x 5265x 2x 2x 2x 2x 2x 2x 2x 2x 3037x 657x 657x 2380x 3037x 18x 3037x 2254x 462x 462x 4x 4x 462x 2254x 3037x 2x 2x 2x 2x 2x 2x 2x 2x 2x 44x 44x 44x 22x 44x 22x 44x 22x 22x 22x 22x     22x 22x 22x 22x     22x 22x 22x 22x 22x 22x 22x   22x                         22x 44x 2x 2x 2x 2x 2x 2x 102x 102x 102x 102x 102x 102x 102x 102x           102x 102x     102x 102x 102x 2x 2x 2x 2x 2x 2x 2x 2x  
/** @import { ComponentContext, Derived, Effect, Reaction, Signal, Source, Value } from '#client' */
import { DEV } from 'esm-env';
import { define_property, get_descriptors, get_prototype_of } from '../shared/utils.js';
import {
	destroy_effect,
	effect,
	execute_effect_teardown,
	unlink_effect
} from './reactivity/effects.js';
import {
	EFFECT,
	RENDER_EFFECT,
	DIRTY,
	MAYBE_DIRTY,
	CLEAN,
	DERIVED,
	UNOWNED,
	DESTROYED,
	INERT,
	BRANCH_EFFECT,
	STATE_SYMBOL,
	BLOCK_EFFECT,
	ROOT_EFFECT,
	LEGACY_DERIVED_PROP,
	DISCONNECTED
} from './constants.js';
import { flush_tasks } from './dom/task.js';
import { add_owner } from './dev/ownership.js';
import { mutate, set, source } from './reactivity/sources.js';
import { update_derived } from './reactivity/deriveds.js';
import * as e from './errors.js';
import { lifecycle_outside_component } from '../shared/errors.js';
import { FILENAME } from '../../constants.js';
 
const FLUSH_MICROTASK = 0;
const FLUSH_SYNC = 1;
 
// Used for DEV time error handling
/** @param {WeakSet<Error>} value */
const handled_errors = new WeakSet();
// Used for controlling the flush of effects.
let scheduler_mode = FLUSH_MICROTASK;
// Used for handling scheduling
let is_micro_task_queued = false;
 
export let is_flushing_effect = false;
export let is_destroying_effect = false;
 
/** @param {boolean} value */
export function set_is_flushing_effect(value) {
	is_flushing_effect = value;
}
 
/** @param {boolean} value */
export function set_is_destroying_effect(value) {
	is_destroying_effect = value;
}
 
// Handle effect queues
 
/** @type {Effect[]} */
let queued_root_effects = [];
 
let flush_count = 0;
/** @type {Effect[]} Stack of effects, dev only */
let dev_effect_stack = [];
// Handle signal reactivity tree dependencies and reactions
 
/** @type {null | Reaction} */
export let active_reaction = null;
 
/** @param {null | Reaction} reaction */
export function set_active_reaction(reaction) {
	active_reaction = reaction;
}
 
/** @type {null | Effect} */
export let active_effect = null;
 
/** @param {null | Effect} effect */
export function set_active_effect(effect) {
	active_effect = effect;
}
 
/**
 * When sources are created within a derived, we record them so that we can safely allow
 * local mutations to these sources without the side-effect error being invoked unnecessarily.
 * @type {null | Source[]}
 */
export let derived_sources = null;
 
/**
 * @param {Source[] | null} sources
 */
export function set_derived_sources(sources) {
	derived_sources = sources;
}
 
/**
 * The dependencies of the reaction that is currently being executed. In many cases,
 * the dependencies are unchanged between runs, and so this will be `null` unless
 * and until a new dependency is accessed — we track this via `skipped_deps`
 * @type {null | Value[]}
 */
export let new_deps = null;
 
let skipped_deps = 0;
 
/**
 * Tracks writes that the effect it's executed in doesn't listen to yet,
 * so that the dependency can be added to the effect later on if it then reads it
 * @type {null | Source[]}
 */
export let untracked_writes = null;
 
/** @param {null | Source[]} value */
export function set_untracked_writes(value) {
	untracked_writes = value;
}
 
/** @type {number} Used by sources and deriveds for handling updates to unowned deriveds */
let current_version = 0;
 
// If we are working with a get() chain that has no active container,
// to prevent memory leaks, we skip adding the reaction.
export let skip_reaction = false;
// Handle collecting all signals which are read during a specific time frame
export let is_signals_recorded = false;
let captured_signals = new Set();
 
// Handling runtime component context
/** @type {ComponentContext | null} */
export let component_context = null;
 
/** @param {ComponentContext | null} context */
export function set_component_context(context) {
	component_context = context;
}
 
/**
 * The current component function. Different from current component context:
 * ```html
 * <!-- App.svelte -->
 * <Foo>
 *   <Bar /> <!-- context == Foo.svelte, function == App.svelte -->
 * </Foo>
 * ```
 * @type {ComponentContext['function']}
 */
export let dev_current_component_function = null;
 
/** @param {ComponentContext['function']} fn */
export function set_dev_current_component_function(fn) {
	dev_current_component_function = fn;
}
 
export function increment_version() {
	return ++current_version;
}
 
/** @returns {boolean} */
export function is_runes() {
	return component_context !== null && component_context.l === null;
}
 
/**
 * Determines whether a derived or effect is dirty.
 * If it is MAYBE_DIRTY, will set the status to CLEAN
 * @param {Reaction} reaction
 * @returns {boolean}
 */
export function check_dirtiness(reaction) {
	var flags = reaction.f;
 
	if ((flags & DIRTY) !== 0) {
		return true;
	}
 
	if ((flags & MAYBE_DIRTY) !== 0) {
		var dependencies = reaction.deps;
		var is_unowned = (flags & UNOWNED) !== 0;
 
		if (dependencies !== null) {
			var i;
 
			if ((flags & DISCONNECTED) !== 0) {
				for (i = 0; i < dependencies.length; i++) {
					(dependencies[i].reactions ??= []).push(reaction);
				}
 
				reaction.f ^= DISCONNECTED;
			}
 
			for (i = 0; i < dependencies.length; i++) {
				var dependency = dependencies[i];
 
				if (check_dirtiness(/** @type {Derived} */ (dependency))) {
					update_derived(/** @type {Derived} */ (dependency));
				}
 
				// If we are working with an unowned signal as part of an effect (due to !skip_reaction)
				// and the version hasn't changed, we still need to check that this reaction
				// is linked to the dependency source – otherwise future updates will not be caught.
				if (
					is_unowned &&
					active_effect !== null &&
					!skip_reaction &&
					!dependency?.reactions?.includes(reaction)
				) {
					(dependency.reactions ??= []).push(reaction);
				}
 
				if (dependency.version > reaction.version) {
					return true;
				}
			}
		}
 
		// Unowned signals should never be marked as clean.
		if (!is_unowned) {
			set_signal_status(reaction, CLEAN);
		}
	}
 
	return false;
}
 
/**
 * @param {Error} error
 * @param {Effect} effect
 * @param {ComponentContext | null} component_context
 */
function handle_error(error, effect, component_context) {
	// Given we don't yet have error boundaries, we will just always throw.
	if (!DEV || handled_errors.has(error) || component_context === null) {
		throw error;
	}
 
	const component_stack = [];
 
	const effect_name = effect.fn?.name;
 
	if (effect_name) {
		component_stack.push(effect_name);
	}
 
	/** @type {ComponentContext | null} */
	let current_context = component_context;
 
	while (current_context !== null) {
		if (DEV) {
			/** @type {string} */
			var filename = current_context.function?.[FILENAME];
 
			if (filename) {
				const file = filename.split('/').pop();
				component_stack.push(file);
			}
		}
 
		current_context = current_context.p;
	}
 
	const indent = /Firefox/.test(navigator.userAgent) ? '  ' : '\t';
	define_property(error, 'message', {
		value: error.message + `\n${component_stack.map((name) => `\n${indent}in ${name}`).join('')}\n`
	});
 
	const stack = error.stack;
 
	// Filter out internal files from callstack
	if (stack) {
		const lines = stack.split('\n');
		const new_lines = [];
		for (let i = 0; i < lines.length; i++) {
			const line = lines[i];
			if (line.includes('svelte/src/internal')) {
				continue;
			}
			new_lines.push(line);
		}
		define_property(error, 'stack', {
			value: error.stack + new_lines.join('\n')
		});
	}
 
	handled_errors.add(error);
	throw error;
}
 
/**
 * @template V
 * @param {Reaction} reaction
 * @returns {V}
 */
export function update_reaction(reaction) {
	var previous_deps = new_deps;
	var previous_skipped_deps = skipped_deps;
	var previous_untracked_writes = untracked_writes;
	var previous_reaction = active_reaction;
	var previous_skip_reaction = skip_reaction;
	var prev_derived_sources = derived_sources;
 
	new_deps = /** @type {null | Value[]} */ (null);
	skipped_deps = 0;
	untracked_writes = null;
	active_reaction = (reaction.f & (BRANCH_EFFECT | ROOT_EFFECT)) === 0 ? reaction : null;
	skip_reaction = !is_flushing_effect && (reaction.f & UNOWNED) !== 0;
	derived_sources = null;
 
	try {
		var result = /** @type {Function} */ (0, reaction.fn)();
		var deps = reaction.deps;
 
		if (new_deps !== null) {
			var i;
 
			remove_reactions(reaction, skipped_deps);
 
			if (deps !== null && skipped_deps > 0) {
				deps.length = skipped_deps + new_deps.length;
				for (i = 0; i < new_deps.length; i++) {
					deps[skipped_deps + i] = new_deps[i];
				}
			} else {
				reaction.deps = deps = new_deps;
			}
 
			if (!skip_reaction) {
				for (i = skipped_deps; i < deps.length; i++) {
					(deps[i].reactions ??= []).push(reaction);
				}
			}
		} else if (deps !== null && skipped_deps < deps.length) {
			remove_reactions(reaction, skipped_deps);
			deps.length = skipped_deps;
		}
 
		return result;
	} finally {
		new_deps = previous_deps;
		skipped_deps = previous_skipped_deps;
		untracked_writes = previous_untracked_writes;
		active_reaction = previous_reaction;
		skip_reaction = previous_skip_reaction;
		derived_sources = prev_derived_sources;
	}
}
 
/**
 * @template V
 * @param {Reaction} signal
 * @param {Value<V>} dependency
 * @returns {void}
 */
function remove_reaction(signal, dependency) {
	let reactions = dependency.reactions;
	if (reactions !== null) {
		var index = reactions.indexOf(signal);
		if (index !== -1) {
			var new_length = reactions.length - 1;
			if (new_length === 0) {
				reactions = dependency.reactions = null;
			} else {
				// Swap with last element and then remove.
				reactions[index] = reactions[new_length];
				reactions.pop();
			}
		}
	}
	// If the derived has no reactions, then we can disconnect it from the graph,
	// allowing it to either reconnect in the future, or be GC'd by the VM.
	if (reactions === null && (dependency.f & DERIVED) !== 0) {
		set_signal_status(dependency, MAYBE_DIRTY);
		// If we are working with a derived that is owned by an effect, then mark it as being
		// disconnected.
		if ((dependency.f & (UNOWNED | DISCONNECTED)) === 0) {
			dependency.f ^= DISCONNECTED;
		}
		remove_reactions(/** @type {Derived} **/ (dependency), 0);
	}
}
 
/**
 * @param {Reaction} signal
 * @param {number} start_index
 * @returns {void}
 */
export function remove_reactions(signal, start_index) {
	var dependencies = signal.deps;
	if (dependencies === null) return;
 
	for (var i = start_index; i < dependencies.length; i++) {
		remove_reaction(signal, dependencies[i]);
	}
}
 
/**
 * @param {Effect} signal
 * @param {boolean} remove_dom
 * @returns {void}
 */
export function destroy_effect_children(signal, remove_dom = false) {
	var effect = signal.first;
	signal.first = signal.last = null;
 
	while (effect !== null) {
		var next = effect.next;
		destroy_effect(effect, remove_dom);
		effect = next;
	}
}
 
/**
 * @param {Effect} effect
 * @returns {void}
 */
export function update_effect(effect) {
	var flags = effect.f;
 
	if ((flags & DESTROYED) !== 0) {
		return;
	}
 
	set_signal_status(effect, CLEAN);
 
	var previous_effect = active_effect;
	var previous_component_context = component_context;
 
	active_effect = effect;
	component_context = effect.ctx;
 
	if (DEV) {
		var previous_component_fn = dev_current_component_function;
		dev_current_component_function = effect.component_function;
	}
 
	try {
		if ((flags & BLOCK_EFFECT) === 0) {
			destroy_effect_children(effect);
		}
 
		execute_effect_teardown(effect);
		var teardown = update_reaction(effect);
		effect.teardown = typeof teardown === 'function' ? teardown : null;
		effect.version = current_version;
 
		if (DEV) {
			dev_effect_stack.push(effect);
		}
	} catch (error) {
		handle_error(/** @type {Error} */ (error), effect, previous_component_context);
	} finally {
		active_effect = previous_effect;
		component_context = previous_component_context;
 
		if (DEV) {
			dev_current_component_function = previous_component_fn;
		}
	}
}
 
function infinite_loop_guard() {
	if (flush_count > 1000) {
		flush_count = 0;
		if (DEV) {
			try {
				e.effect_update_depth_exceeded();
			} catch (error) {
				// stack is garbage, ignore. Instead add a console.error message.
				define_property(error, 'stack', {
					value: ''
				});
				// eslint-disable-next-line no-console
				console.error(
					'Last ten effects were: ',
					dev_effect_stack.slice(-10).map((d) => d.fn)
				);
				dev_effect_stack = [];
				throw error;
			}
		} else {
			e.effect_update_depth_exceeded();
		}
	}
	flush_count++;
}
 
/**
 * @param {Array<Effect>} root_effects
 * @returns {void}
 */
function flush_queued_root_effects(root_effects) {
	var length = root_effects.length;
	if (length === 0) {
		return;
	}
	infinite_loop_guard();
 
	var previously_flushing_effect = is_flushing_effect;
	is_flushing_effect = true;
 
	try {
		for (var i = 0; i < length; i++) {
			var effect = root_effects[i];
 
			// When working with custom elements, the root effects might not have a root
			if (effect.first === null && (effect.f & BRANCH_EFFECT) === 0) {
				flush_queued_effects([effect]);
			} else {
				/** @type {Effect[]} */
				var collected_effects = [];
 
				process_effects(effect, collected_effects);
				flush_queued_effects(collected_effects);
			}
		}
	} finally {
		is_flushing_effect = previously_flushing_effect;
	}
}
 
/**
 * @param {Array<Effect>} effects
 * @returns {void}
 */
function flush_queued_effects(effects) {
	var length = effects.length;
	if (length === 0) return;
 
	for (var i = 0; i < length; i++) {
		var effect = effects[i];
 
		if ((effect.f & (DESTROYED | INERT)) === 0 && check_dirtiness(effect)) {
			update_effect(effect);
 
			// Effects with no dependencies or teardown do not get added to the effect tree.
			// Deferred effects (e.g. `$effect(...)`) _are_ added to the tree because we
			// don't know if we need to keep them until they are executed. Doing the check
			// here (rather than in `update_effect`) allows us to skip the work for
			// immediate effects.
			if (effect.deps === null && effect.first === null && effect.nodes_start === null) {
				if (effect.teardown === null) {
					// remove this effect from the graph
					unlink_effect(effect);
				} else {
					// keep the effect in the graph, but free up some memory
					effect.fn = null;
				}
			}
		}
	}
}
 
function process_deferred() {
	is_micro_task_queued = false;
	if (flush_count > 1001) {
		return;
	}
	const previous_queued_root_effects = queued_root_effects;
	queued_root_effects = [];
	flush_queued_root_effects(previous_queued_root_effects);
	if (!is_micro_task_queued) {
		flush_count = 0;
		if (DEV) {
			dev_effect_stack = [];
		}
	}
}
 
/**
 * @param {Effect} signal
 * @returns {void}
 */
export function schedule_effect(signal) {
	if (scheduler_mode === FLUSH_MICROTASK) {
		if (!is_micro_task_queued) {
			is_micro_task_queued = true;
			queueMicrotask(process_deferred);
		}
	}
 
	var effect = signal;
 
	while (effect.parent !== null) {
		effect = effect.parent;
		var flags = effect.f;
 
		if ((flags & BRANCH_EFFECT) !== 0) {
			if ((flags & CLEAN) === 0) return;
			set_signal_status(effect, MAYBE_DIRTY);
		}
	}
 
	queued_root_effects.push(effect);
}
 
/**
 *
 * This function both runs render effects and collects user effects in topological order
 * from the starting effect passed in. Effects will be collected when they match the filtered
 * bitwise flag passed in only. The collected effects array will be populated with all the user
 * effects to be flushed.
 *
 * @param {Effect} effect
 * @param {Effect[]} collected_effects
 * @returns {void}
 */
function process_effects(effect, collected_effects) {
	var current_effect = effect.first;
	var effects = [];
 
	main_loop: while (current_effect !== null) {
		var flags = current_effect.f;
		// TODO: we probably don't need to check for destroyed as it shouldn't be encountered?
		var is_active = (flags & (DESTROYED | INERT)) === 0;
		var is_branch = (flags & BRANCH_EFFECT) !== 0;
		var is_clean = (flags & CLEAN) !== 0;
		var child = current_effect.first;
 
		// Skip this branch if it's clean
		if (is_active && (!is_branch || !is_clean)) {
			if (is_branch) {
				set_signal_status(current_effect, CLEAN);
			}
 
			if ((flags & RENDER_EFFECT) !== 0) {
				if (!is_branch && check_dirtiness(current_effect)) {
					update_effect(current_effect);
				}
				// Child might have been mutated since running the effect or checking dirtiness
				child = current_effect.first;
 
				if (child !== null) {
					current_effect = child;
					continue;
				}
			} else if ((flags & EFFECT) !== 0) {
				if (is_branch || is_clean) {
					if (child !== null) {
						current_effect = child;
						continue;
					}
				} else {
					effects.push(current_effect);
				}
			}
		}
		var sibling = current_effect.next;
 
		if (sibling === null) {
			let parent = current_effect.parent;
 
			while (parent !== null) {
				if (effect === parent) {
					break main_loop;
				}
				var parent_sibling = parent.next;
				if (parent_sibling !== null) {
					current_effect = parent_sibling;
					continue main_loop;
				}
				parent = parent.parent;
			}
		}
 
		current_effect = sibling;
	}
 
	// We might be dealing with many effects here, far more than can be spread into
	// an array push call (callstack overflow). So let's deal with each effect in a loop.
	for (var i = 0; i < effects.length; i++) {
		child = effects[i];
		collected_effects.push(child);
		process_effects(child, collected_effects);
	}
}
 
/**
 * Internal version of `flushSync` with the option to not flush previous effects.
 * Returns the result of the passed function, if given.
 * @param {() => any} [fn]
 * @returns {any}
 */
export function flush_sync(fn) {
	var previous_scheduler_mode = scheduler_mode;
	var previous_queued_root_effects = queued_root_effects;
 
	try {
		infinite_loop_guard();
 
		/** @type {Effect[]} */
		const root_effects = [];
 
		scheduler_mode = FLUSH_SYNC;
		queued_root_effects = root_effects;
		is_micro_task_queued = false;
 
		flush_queued_root_effects(previous_queued_root_effects);
 
		var result = fn?.();
 
		flush_tasks();
		if (queued_root_effects.length > 0 || root_effects.length > 0) {
			flush_sync();
		}
 
		flush_count = 0;
		if (DEV) {
			dev_effect_stack = [];
		}
 
		return result;
	} finally {
		scheduler_mode = previous_scheduler_mode;
		queued_root_effects = previous_queued_root_effects;
	}
}
 
/**
 * Returns a promise that resolves once any pending state changes have been applied.
 * @returns {Promise<void>}
 */
export async function tick() {
	await Promise.resolve();
	// By calling flush_sync we guarantee that any pending state changes are applied after one tick.
	// TODO look into whether we can make flushing subsequent updates synchronously in the future.
	flush_sync();
}
 
/**
 * @template V
 * @param {Value<V>} signal
 * @returns {V}
 */
export function get(signal) {
	var flags = signal.f;
 
	if ((flags & DESTROYED) !== 0) {
		return signal.v;
	}
 
	if (is_signals_recorded) {
		captured_signals.add(signal);
	}
 
	// Register the dependency on the current reaction signal.
	if (active_reaction !== null) {
		if (derived_sources !== null && derived_sources.includes(signal)) {
			e.state_unsafe_local_read();
		}
		var deps = active_reaction.deps;
 
		// If the signal is accessing the same dependencies in the same
		// order as it did last time, increment `skipped_deps`
		// rather than updating `new_deps`, which creates GC cost
		if (new_deps === null && deps !== null && deps[skipped_deps] === signal) {
			skipped_deps++;
		} else if (new_deps === null) {
			new_deps = [signal];
		} else {
			new_deps.push(signal);
		}
 
		if (
			untracked_writes !== null &&
			active_effect !== null &&
			(active_effect.f & CLEAN) !== 0 &&
			(active_effect.f & BRANCH_EFFECT) === 0 &&
			untracked_writes.includes(signal)
		) {
			set_signal_status(active_effect, DIRTY);
			schedule_effect(active_effect);
		}
	}
 
	if ((flags & DERIVED) !== 0) {
		var derived = /** @type {Derived} */ (signal);
 
		if (check_dirtiness(derived)) {
			update_derived(derived);
		}
	}
 
	return signal.v;
}
 
/**
 * Like `get`, but checks for `undefined`. Used for `var` declarations because they can be accessed before being declared
 * @template V
 * @param {Value<V> | undefined} signal
 * @returns {V | undefined}
 */
export function safe_get(signal) {
	return signal && get(signal);
}
 
/**
 * Invokes a function and captures all signals that are read during the invocation,
 * then invalidates them.
 * @param {() => any} fn
 */
export function invalidate_inner_signals(fn) {
	var previous_is_signals_recorded = is_signals_recorded;
	var previous_captured_signals = captured_signals;
	is_signals_recorded = true;
	captured_signals = new Set();
	var captured = captured_signals;
	var signal;
	try {
		untrack(fn);
	} finally {
		is_signals_recorded = previous_is_signals_recorded;
		if (is_signals_recorded) {
			for (signal of captured_signals) {
				previous_captured_signals.add(signal);
			}
		}
		captured_signals = previous_captured_signals;
	}
	for (signal of captured) {
		// Go one level up because derived signals created as part of props in legacy mode
		if ((signal.f & LEGACY_DERIVED_PROP) !== 0) {
			for (const dep of /** @type {Derived} */ (signal).deps || []) {
				if ((dep.f & DERIVED) === 0) {
					mutate(dep, null /* doesnt matter */);
				}
			}
		} else {
			mutate(signal, null /* doesnt matter */);
		}
	}
}
 
/**
 * Use `untrack` to prevent something from being treated as an `$effect`/`$derived` dependency.
 *
 * https://svelte-5-preview.vercel.app/docs/functions#untrack
 * @template T
 * @param {() => T} fn
 * @returns {T}
 */
export function untrack(fn) {
	const previous_reaction = active_reaction;
	try {
		active_reaction = null;
		return fn();
	} finally {
		active_reaction = previous_reaction;
	}
}
 
const STATUS_MASK = ~(DIRTY | MAYBE_DIRTY | CLEAN);
 
/**
 * @param {Signal} signal
 * @param {number} status
 * @returns {void}
 */
export function set_signal_status(signal, status) {
	signal.f = (signal.f & STATUS_MASK) | status;
}
 
/**
 * Retrieves the context that belongs to the closest parent component with the specified `key`.
 * Must be called during component initialisation.
 *
 * https://svelte.dev/docs/svelte#getcontext
 * @template T
 * @param {any} key
 * @returns {T}
 */
export function getContext(key) {
	const context_map = get_or_init_context_map('getContext');
	const result = /** @type {T} */ (context_map.get(key));
 
	if (DEV) {
		const fn = /** @type {ComponentContext} */ (component_context).function;
		if (fn) {
			add_owner(result, fn, true);
		}
	}
 
	return result;
}
 
/**
 * Associates an arbitrary `context` object with the current component and the specified `key`
 * and returns that object. The context is then available to children of the component
 * (including slotted content) with `getContext`.
 *
 * Like lifecycle functions, this must be called during component initialisation.
 *
 * https://svelte.dev/docs/svelte#setcontext
 * @template T
 * @param {any} key
 * @param {T} context
 * @returns {T}
 */
export function setContext(key, context) {
	const context_map = get_or_init_context_map('setContext');
	context_map.set(key, context);
	return context;
}
 
/**
 * Checks whether a given `key` has been set in the context of a parent component.
 * Must be called during component initialisation.
 *
 * https://svelte.dev/docs/svelte#hascontext
 * @param {any} key
 * @returns {boolean}
 */
export function hasContext(key) {
	const context_map = get_or_init_context_map('hasContext');
	return context_map.has(key);
}
 
/**
 * Retrieves the whole context map that belongs to the closest parent component.
 * Must be called during component initialisation. Useful, for example, if you
 * programmatically create a component and want to pass the existing context to it.
 *
 * https://svelte.dev/docs/svelte#getallcontexts
 * @template {Map<any, any>} [T=Map<any, any>]
 * @returns {T}
 */
export function getAllContexts() {
	const context_map = get_or_init_context_map('getAllContexts');
 
	if (DEV) {
		const fn = component_context?.function;
		if (fn) {
			for (const value of context_map.values()) {
				add_owner(value, fn, true);
			}
		}
	}
 
	return /** @type {T} */ (context_map);
}
 
/**
 * @param {string} name
 * @returns {Map<unknown, unknown>}
 */
function get_or_init_context_map(name) {
	if (component_context === null) {
		lifecycle_outside_component(name);
	}
 
	return (component_context.c ??= new Map(get_parent_context(component_context) || undefined));
}
 
/**
 * @param {ComponentContext} component_context
 * @returns {Map<unknown, unknown> | null}
 */
function get_parent_context(component_context) {
	let parent = component_context.p;
	while (parent !== null) {
		const context_map = parent.c;
		if (context_map !== null) {
			return context_map;
		}
		parent = parent.p;
	}
	return null;
}
 
/**
 * @param {Value<number>} signal
 * @param {1 | -1} [d]
 * @returns {number}
 */
export function update(signal, d = 1) {
	var value = +get(signal);
	set(signal, value + d);
	return value;
}
 
/**
 * @param {Value<number>} signal
 * @param {1 | -1} [d]
 * @returns {number}
 */
export function update_pre(signal, d = 1) {
	return set(signal, +get(signal) + d);
}
 
/**
 * @param {Record<string, unknown>} obj
 * @param {string[]} keys
 * @returns {Record<string, unknown>}
 */
export function exclude_from_object(obj, keys) {
	/** @type {Record<string, unknown>} */
	var result = {};
 
	for (var key in obj) {
		if (!keys.includes(key)) {
			result[key] = obj[key];
		}
	}
 
	return result;
}
 
/**
 * @param {Record<string, unknown>} props
 * @param {any} runes
 * @param {Function} [fn]
 * @returns {void}
 */
export function push(props, runes = false, fn) {
	component_context = {
		p: component_context,
		c: null,
		e: null,
		m: false,
		s: props,
		x: null,
		l: null
	};
 
	if (!runes) {
		component_context.l = {
			s: null,
			u: null,
			r1: [],
			r2: source(false)
		};
	}
 
	if (DEV) {
		// component function
		component_context.function = fn;
		dev_current_component_function = fn;
	}
}
 
/**
 * @template {Record<string, any>} T
 * @param {T} [component]
 * @returns {T}
 */
export function pop(component) {
	const context_stack_item = component_context;
	if (context_stack_item !== null) {
		if (component !== undefined) {
			context_stack_item.x = component;
		}
		const component_effects = context_stack_item.e;
		if (component_effects !== null) {
			var previous_effect = active_effect;
			context_stack_item.e = null;
			try {
				for (var i = 0; i < component_effects.length; i++) {
					var component_effect = component_effects[i];
					set_active_effect(component_effect.parent);
					effect(component_effect.fn);
				}
			} finally {
				set_active_effect(previous_effect);
			}
		}
		component_context = context_stack_item.p;
		if (DEV) {
			dev_current_component_function = context_stack_item.p?.function ?? null;
		}
		context_stack_item.m = true;
	}
	// Micro-optimization: Don't set .a above to the empty object
	// so it can be garbage-collected when the return here is unused
	return component || /** @type {T} */ ({});
}
 
/**
 * Possibly traverse an object and read all its properties so that they're all reactive in case this is `$state`.
 * Does only check first level of an object for performance reasons (heuristic should be good for 99% of all cases).
 * @param {any} value
 * @returns {void}
 */
export function deep_read_state(value) {
	if (typeof value !== 'object' || !value || value instanceof EventTarget) {
		return;
	}
 
	if (STATE_SYMBOL in value) {
		deep_read(value);
	} else if (!Array.isArray(value)) {
		for (let key in value) {
			const prop = value[key];
			if (typeof prop === 'object' && prop && STATE_SYMBOL in prop) {
				deep_read(prop);
			}
		}
	}
}
 
/**
 * Deeply traverse an object and read all its properties
 * so that they're all reactive in case this is `$state`
 * @param {any} value
 * @param {Set<any>} visited
 * @returns {void}
 */
export function deep_read(value, visited = new Set()) {
	if (
		typeof value === 'object' &&
		value !== null &&
		// We don't want to traverse DOM elements
		!(value instanceof EventTarget) &&
		!visited.has(value)
	) {
		visited.add(value);
		// When working with a possible SvelteDate, this
		// will ensure we capture changes to it.
		if (value instanceof Date) {
			value.getTime();
		}
		for (let key in value) {
			try {
				deep_read(value[key], visited);
			} catch (e) {
				// continue
			}
		}
		const proto = get_prototype_of(value);
		if (
			proto !== Object.prototype &&
			proto !== Array.prototype &&
			proto !== Map.prototype &&
			proto !== Set.prototype &&
			proto !== Date.prototype
		) {
			const descriptors = get_descriptors(proto);
			for (let key in descriptors) {
				const get = descriptors[key].get;
				if (get) {
					try {
						get.call(value);
					} catch (e) {
						// continue
					}
				}
			}
		}
	}
}
 
if (DEV) {
	/**
	 * @param {string} rune
	 */
	function throw_rune_error(rune) {
		if (!(rune in globalThis)) {
			// TODO if people start adjusting the "this can contain runes" config through v-p-s more, adjust this message
			/** @type {any} */
			let value; // let's hope noone modifies this global, but belts and braces
			Object.defineProperty(globalThis, rune, {
				configurable: true,
				// eslint-disable-next-line getter-return
				get: () => {
					if (value !== undefined) {
						return value;
					}

					e.rune_outside_svelte(rune);
				},
				set: (v) => {
					value = v;
				}
			});
		}
	}
 
	throw_rune_error('$state');
	throw_rune_error('$effect');
	throw_rune_error('$derived');
	throw_rune_error('$inspect');
	throw_rune_error('$props');
	throw_rune_error('$bindable');
}