svelte 5.59.1 → 5.59.2

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.
Files changed (52) hide show
  1. package/LICENSE.md +1 -1
  2. package/compiler/index.js +1 -1
  3. package/package.json +4 -4
  4. package/src/compiler/legacy.js +2 -2
  5. package/src/compiler/phases/1-parse/acorn.js +13 -1
  6. package/src/compiler/phases/1-parse/index.js +50 -7
  7. package/src/compiler/phases/1-parse/read/context.js +1 -1
  8. package/src/compiler/phases/1-parse/read/style.js +9 -8
  9. package/src/compiler/phases/1-parse/state/tag.js +28 -2
  10. package/src/compiler/phases/2-analyze/index.js +2 -1
  11. package/src/compiler/phases/2-analyze/visitors/BindDirective.js +4 -8
  12. package/src/compiler/phases/3-transform/client/transform-client.js +2 -0
  13. package/src/compiler/phases/3-transform/client/visitors/StaticBlock.js +12 -0
  14. package/src/compiler/phases/3-transform/client/visitors/shared/utils.js +10 -7
  15. package/src/compiler/phases/3-transform/css/index.js +35 -26
  16. package/src/compiler/phases/patterns.js +2 -0
  17. package/src/compiler/phases/scope.js +8 -1
  18. package/src/compiler/print/index.js +18 -9
  19. package/src/compiler/validate-options.js +1 -1
  20. package/src/internal/client/custom-renderer/cross.js +4 -4
  21. package/src/internal/client/dev/elements.js +18 -7
  22. package/src/internal/client/dom/blocks/await.js +5 -0
  23. package/src/internal/client/dom/blocks/boundary.js +2 -2
  24. package/src/internal/client/dom/blocks/branches.js +3 -3
  25. package/src/internal/client/dom/blocks/each.js +16 -13
  26. package/src/internal/client/dom/blocks/html.js +8 -8
  27. package/src/internal/client/dom/blocks/slot.js +8 -13
  28. package/src/internal/client/dom/blocks/snippet.js +6 -7
  29. package/src/internal/client/dom/blocks/svelte-element.js +9 -13
  30. package/src/internal/client/dom/blocks/svelte-head.js +2 -2
  31. package/src/internal/client/dom/css.js +1 -1
  32. package/src/internal/client/dom/elements/attributes.js +15 -2
  33. package/src/internal/client/dom/elements/bindings/select.js +1 -1
  34. package/src/internal/client/dom/elements/transitions.js +88 -83
  35. package/src/internal/client/dom/operations.js +36 -8
  36. package/src/internal/client/dom/template.js +4 -2
  37. package/src/internal/client/proxy.js +19 -11
  38. package/src/internal/client/reactivity/batch.js +58 -18
  39. package/src/internal/client/reactivity/sources.js +39 -6
  40. package/src/internal/client/render.js +2 -2
  41. package/src/internal/client/runtime.js +3 -0
  42. package/src/internal/server/hydratable.js +21 -7
  43. package/src/internal/shared/utils.js +10 -4
  44. package/src/reactivity/create-subscriber.js +1 -1
  45. package/src/reactivity/date.js +3 -2
  46. package/src/reactivity/map.js +26 -11
  47. package/src/reactivity/set.js +23 -10
  48. package/src/reactivity/url-search-params.js +18 -6
  49. package/src/reactivity/url.js +30 -20
  50. package/src/version.js +1 -1
  51. package/types/index.d.ts +24 -16
  52. package/types/index.d.ts.map +1 -1
@@ -2,7 +2,7 @@
2
2
  import { hydrate_node, hydrating, reset, set_hydrate_node } from './hydration.js';
3
3
  import { DEV } from 'esm-env';
4
4
  import { init_array_prototype_warnings } from '../dev/equality.js';
5
- import { get_descriptor, is_extensible } from '../../shared/utils.js';
5
+ import { define_property, get_descriptor, is_extensible } from '../../shared/utils.js';
6
6
  import { active_effect } from '../runtime.js';
7
7
  import { async_mode_flag } from '../../flags/index.js';
8
8
  import {
@@ -71,22 +71,50 @@ export function init_operations() {
71
71
  }
72
72
 
73
73
  if (DEV) {
74
- // @ts-expect-error
75
- element_prototype.__svelte_meta = null;
74
+ define_property(element_prototype, '__svelte_meta', {
75
+ value: null,
76
+ enumerable: false,
77
+ writable: true,
78
+ configurable: true
79
+ });
76
80
 
77
81
  init_array_prototype_warnings();
78
82
  }
79
83
  }
80
84
 
81
85
  /**
86
+ * Creates a text node with the given content. To mark a position, use `create_anchor` instead,
87
+ * and to create an empty text node while hydrating, use `create_hydration_text`
82
88
  * @param {string} value
83
89
  * @returns {Text}
84
90
  */
85
- export function create_text(value = '') {
91
+ export function create_text(value) {
86
92
  if (current_renderer) return /** @type {Text} */ (current_renderer.createText(value));
87
93
  return document.createTextNode(value);
88
94
  }
89
95
 
96
+ /**
97
+ * Creates the text node of an `{expression}` that was empty during SSR, so there was no text
98
+ * node to hydrate. Only use this while hydrating: elsewhere, an empty text node is an anchor,
99
+ * which should be created with `create_anchor`
100
+ * @returns {Text}
101
+ */
102
+ export function create_hydration_text() {
103
+ if (current_renderer) return /** @type {Text} */ (current_renderer.createText(''));
104
+ return document.createTextNode('');
105
+ }
106
+
107
+ /**
108
+ * Creates a node that only marks a position. In the DOM this is an empty text node; with a
109
+ * custom renderer it's an empty comment, so that renderers can treat every comment as
110
+ * plumbing and every text node as content
111
+ * @returns {Text}
112
+ */
113
+ export function create_anchor() {
114
+ if (current_renderer) return /** @type {Text} */ (current_renderer.createComment(''));
115
+ return document.createTextNode('');
116
+ }
117
+
90
118
  /**
91
119
  * @template {Node} N
92
120
  * @param {N} node
@@ -125,9 +153,9 @@ export function child(node, is_text) {
125
153
 
126
154
  // Child can be null if we have an element with a single child, like `<p>{text}</p>`, where `text` is empty
127
155
  if (child === null) {
128
- child = /** @type {TemplateNode} */ (append_child(hydrate_node, create_text()));
156
+ child = /** @type {TemplateNode} */ (append_child(hydrate_node, create_hydration_text()));
129
157
  } else if (is_text && node_type(child) !== TEXT_NODE) {
130
- var text = create_text();
158
+ var text = create_hydration_text();
131
159
  insert_before(child, text);
132
160
  set_hydrate_node(text);
133
161
  return text;
@@ -161,7 +189,7 @@ export function first_child(node, is_text = false) {
161
189
  // if an {expression} is empty during SSR, there might be no
162
190
  // text node to hydrate — we must therefore create one
163
191
  if (node_type(hydrate_node) !== TEXT_NODE) {
164
- var text = create_text();
192
+ var text = create_hydration_text();
165
193
 
166
194
  if (hydrate_node) insert_before(hydrate_node, text);
167
195
  set_hydrate_node(text);
@@ -218,7 +246,7 @@ export function sibling(node, count = 1, is_text = false) {
218
246
  // if a sibling {expression} is empty during SSR, there might be no
219
247
  // text node to hydrate — we must therefore create one
220
248
  if (node_type(next_sibling) !== TEXT_NODE) {
221
- var text = create_text();
249
+ var text = create_hydration_text();
222
250
  // If the next sibling is `null` and we're handling text then it's because
223
251
  // the SSR content was empty for the text, so we need to generate a new text
224
252
  // node and insert it after the last sibling
@@ -24,7 +24,9 @@ import {
24
24
  create_comment,
25
25
  create_element,
26
26
  create_fragment,
27
+ create_anchor,
27
28
  create_text,
29
+ create_hydration_text,
28
30
  get_first_child,
29
31
  get_last_child,
30
32
  get_node_value,
@@ -345,7 +347,7 @@ export function text(value = '') {
345
347
 
346
348
  if (node_type(node) !== TEXT_NODE) {
347
349
  // if an {expression} is empty during SSR, we need to insert an empty text node
348
- insert_before(node, (node = create_text()));
350
+ insert_before(node, (node = create_hydration_text()));
349
351
  set_hydrate_node(node);
350
352
  } else {
351
353
  merge_text_nodes(/** @type {Text} */ (node));
@@ -367,7 +369,7 @@ export function comment() {
367
369
 
368
370
  var frag = create_fragment();
369
371
  var start = create_comment('');
370
- var anchor = create_text();
372
+ var anchor = create_anchor();
371
373
  append_child(frag, start);
372
374
  append_child(frag, anchor);
373
375
 
@@ -17,8 +17,9 @@ import {
17
17
  } from '../shared/utils.js';
18
18
  import {
19
19
  state as source,
20
- set,
21
- increment,
20
+ check_mutation,
21
+ unchecked_set,
22
+ unchecked_increment,
22
23
  flush_eager_effects,
23
24
  set_eager_effects_deferred
24
25
  } from './reactivity/sources.js';
@@ -129,6 +130,9 @@ export function proxy(value) {
129
130
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/getOwnPropertyDescriptor#invariants
130
131
  e.state_descriptors_fixed();
131
132
  }
133
+
134
+ check_mutation(version);
135
+
132
136
  var s = sources.get(prop);
133
137
  if (s === undefined) {
134
138
  with_parent(() => {
@@ -140,28 +144,30 @@ export function proxy(value) {
140
144
  return s;
141
145
  });
142
146
  } else {
143
- set(s, descriptor.value, true);
147
+ unchecked_set(s, descriptor.value, true);
144
148
  }
145
149
 
146
150
  return true;
147
151
  },
148
152
 
149
153
  deleteProperty(target, prop) {
154
+ check_mutation(version);
155
+
150
156
  var s = sources.get(prop);
151
157
 
152
158
  if (s === undefined) {
153
159
  if (prop in target) {
154
160
  const s = with_parent(() => source(UNINITIALIZED, stack));
155
161
  sources.set(prop, s);
156
- increment(version);
162
+ unchecked_increment(version);
157
163
 
158
164
  if (DEV) {
159
165
  tag(s, get_label(path, prop));
160
166
  }
161
167
  }
162
168
  } else {
163
- set(s, UNINITIALIZED);
164
- increment(version);
169
+ unchecked_set(s, UNINITIALIZED);
170
+ unchecked_increment(version);
165
171
  }
166
172
 
167
173
  return true;
@@ -269,6 +275,8 @@ export function proxy(value) {
269
275
  },
270
276
 
271
277
  set(target, prop, value, receiver) {
278
+ check_mutation(version);
279
+
272
280
  var s = sources.get(prop);
273
281
  var has = prop in target;
274
282
 
@@ -277,7 +285,7 @@ export function proxy(value) {
277
285
  for (var i = value; i < /** @type {Source<number>} */ (s).v; i += 1) {
278
286
  var other_s = sources.get(i + '');
279
287
  if (other_s !== undefined) {
280
- set(other_s, UNINITIALIZED);
288
+ unchecked_set(other_s, UNINITIALIZED);
281
289
  } else if (i in target) {
282
290
  // If the item exists in the original, we need to create an uninitialized source,
283
291
  // else a later read of the property would result in a source being created with
@@ -303,7 +311,7 @@ export function proxy(value) {
303
311
  if (DEV) {
304
312
  tag(s, get_label(path, prop));
305
313
  }
306
- set(s, proxy(value));
314
+ unchecked_set(s, proxy(value));
307
315
 
308
316
  sources.set(prop, s);
309
317
  }
@@ -311,7 +319,7 @@ export function proxy(value) {
311
319
  has = s.v !== UNINITIALIZED;
312
320
 
313
321
  var p = with_parent(() => proxy(value));
314
- set(s, p);
322
+ unchecked_set(s, p);
315
323
  }
316
324
 
317
325
  var descriptor = Reflect.getOwnPropertyDescriptor(target, prop);
@@ -331,11 +339,11 @@ export function proxy(value) {
331
339
  var n = Number(prop);
332
340
 
333
341
  if (Number.isInteger(n) && n >= ls.v) {
334
- set(ls, n + 1);
342
+ unchecked_set(ls, n + 1);
335
343
  }
336
344
  }
337
345
 
338
- increment(version);
346
+ unchecked_increment(version);
339
347
  }
340
348
 
341
349
  return true;
@@ -512,6 +512,22 @@ export class Batch {
512
512
  }
513
513
 
514
514
  #process() {
515
+ // Processing a batch frequently hands over to another batch (the next one, an earlier one it
516
+ // merged into, etc). Do that iteratively rather than recursively, so that the call stack doesn't
517
+ // grow with each hand-over — deep stacks make DEV stack capture (see `get_stack`) quadratic
518
+ /** @type {Batch | null} */
519
+ var batch = this;
520
+
521
+ while (batch !== null) {
522
+ batch = batch.#process_step();
523
+ }
524
+ }
525
+
526
+ /**
527
+ * Processes this batch and returns the batch that needs to be processed next, if any
528
+ * @returns {Batch | null}
529
+ */
530
+ #process_step() {
515
531
  current_batch = processing_batch = this;
516
532
 
517
533
  // A different batch view may apply, even when processing continues within the same flush.
@@ -631,8 +647,7 @@ export class Batch {
631
647
  }
632
648
 
633
649
  // Revalidate once all the writes and pending work belong to the same batch.
634
- target.#process();
635
- return;
650
+ return target;
636
651
  }
637
652
  }
638
653
 
@@ -646,10 +661,10 @@ export class Batch {
646
661
  }
647
662
 
648
663
  if (updates.length > 0) {
649
- /** @type {Batch} */ (/** @type {unknown} */ (current_batch)).#process();
664
+ return /** @type {Batch} */ (/** @type {unknown} */ (current_batch));
650
665
  }
651
666
 
652
- return;
667
+ return null;
653
668
  }
654
669
 
655
670
  const earlier_batch = this.#find_earlier_batch();
@@ -660,9 +675,8 @@ export class Batch {
660
675
  this.#defer_effects(render_effects);
661
676
  this.#defer_effects(effects);
662
677
  earlier_batch.#merge(this);
663
- earlier_batch.#process();
664
678
 
665
- return;
679
+ return earlier_batch;
666
680
  }
667
681
 
668
682
  for (const boundary of revealed_boundaries) {
@@ -678,14 +692,23 @@ export class Batch {
678
692
  // append/remove branches
679
693
  this.#run_callbacks(this.#commit_callbacks);
680
694
 
681
- var earlier_writes = flush_queued_effects(render_effects);
695
+ var was_flushing_effects = is_flushing_effects;
696
+ var previous_captured_writes = captured_writes;
682
697
 
683
- // the content has been updated, and user effects are about to see it
684
- if (custom_renderers_flag && before_effects !== null) {
685
- before_effects();
686
- }
698
+ try {
699
+ is_flushing_effects = true;
700
+ var earlier_writes = flush_queued_effects(render_effects);
687
701
 
688
- flush_queued_effects(effects, earlier_writes);
702
+ // the content has been updated, and user effects are about to see it
703
+ if (custom_renderers_flag && before_effects !== null) {
704
+ before_effects();
705
+ }
706
+
707
+ flush_queued_effects(effects, earlier_writes);
708
+ } finally {
709
+ is_flushing_effects = was_flushing_effects;
710
+ captured_writes = previous_captured_writes;
711
+ }
689
712
 
690
713
  this.#deferred?.resolve();
691
714
 
@@ -715,9 +738,9 @@ export class Batch {
715
738
  }
716
739
  }
717
740
  }
718
-
719
- next_batch.#process();
720
741
  }
742
+
743
+ return next_batch;
721
744
  }
722
745
 
723
746
  /**
@@ -997,6 +1020,10 @@ export class Batch {
997
1020
 
998
1021
  this.current.set(source, entry);
999
1022
 
1023
+ if (is_flushing_effects) {
1024
+ (captured_writes ??= new Set()).add(source);
1025
+ }
1026
+
1000
1027
  // The active view of the world sees writes made while it's active, even if they don't belong to the batch whose
1001
1028
  // view it is — e.g. those of effects that run while a batch commits, which go to a new batch. (Async results
1002
1029
  // are only captured while no view is active, see `async_derived`)
@@ -1631,6 +1658,15 @@ function infinite_loop_guard() {
1631
1658
  }
1632
1659
  }
1633
1660
 
1661
+ var is_flushing_effects = false;
1662
+
1663
+ /**
1664
+ * Sources captured since the last effect in `flush_queued_effects` finished, including
1665
+ * writes by its eager block reruns. Nested flushes use their own collector.
1666
+ * @type {Set<Value> | null}
1667
+ */
1668
+ var captured_writes = null;
1669
+
1634
1670
  /** @type {Set<Effect> | null} */
1635
1671
  export let eager_block_effects = null;
1636
1672
 
@@ -1758,15 +1794,19 @@ function flush_queued_effects(effects, earlier_writes = null) {
1758
1794
  }
1759
1795
  }
1760
1796
 
1761
- // if the effect — or the block effects — wrote anything
1762
- if (write_version !== version && current_batch !== null) {
1763
- for (const [value, write] of current_batch.current) {
1764
- if (write.wv > version) {
1797
+ // Only visit sources captured by this effect or its eager block reruns.
1798
+ // A nested flush may have drained their batch, so use the current entry.
1799
+ if (write_version !== version && current_batch !== null && captured_writes !== null) {
1800
+ for (const value of captured_writes) {
1801
+ var write = current_batch.current.get(value);
1802
+ if (write !== undefined && write.wv > version) {
1765
1803
  (earlier_writes ??= new Map()).set(value, write);
1766
1804
  }
1767
1805
  }
1768
1806
  }
1769
1807
  }
1808
+
1809
+ captured_writes?.clear();
1770
1810
  }
1771
1811
 
1772
1812
  eager_block_effects = null;
@@ -139,13 +139,13 @@ export function mutate(source, value) {
139
139
  }
140
140
 
141
141
  /**
142
- * @template V
143
- * @param {Value<V>} source
144
- * @param {V} value
145
- * @param {boolean} [should_proxy]
146
- * @returns {V}
142
+ * Throws if `source` cannot be written to in the current reaction. Called by `set`, and by
143
+ * objects like `SvelteMap` that must check before changing data of their own — those then
144
+ * write the rest of their sources with `unchecked_set`, so that they either change
145
+ * entirely or not at all
146
+ * @param {Value} source
147
147
  */
148
- export function set(source, value, should_proxy = false) {
148
+ export function check_mutation(source) {
149
149
  if (
150
150
  active_reaction !== null &&
151
151
  // since we are untracking the function inside `$inspect.with` we need to add this check
@@ -157,7 +157,32 @@ export function set(source, value, should_proxy = false) {
157
157
  ) {
158
158
  e.state_unsafe_mutation();
159
159
  }
160
+ }
160
161
 
162
+ /**
163
+ * @template V
164
+ * @param {Value<V>} source
165
+ * @param {V} value
166
+ * @param {boolean} [should_proxy]
167
+ * @returns {V}
168
+ */
169
+ export function set(source, value, should_proxy = false) {
170
+ check_mutation(source);
171
+ return unchecked_set(source, value, should_proxy);
172
+ }
173
+
174
+ /**
175
+ * Like `set`, but without `check_mutation`. Only use this after checking a source that
176
+ * belongs to the same object (such as its version)
177
+ * TOOD 6.0 many calls to `unchecked_set` could be `internal_set`, once we
178
+ * get rid of `legacy_updates`
179
+ * @template V
180
+ * @param {Value<V>} source
181
+ * @param {V} value
182
+ * @param {boolean} [should_proxy]
183
+ * @returns {V}
184
+ */
185
+ export function unchecked_set(source, value, should_proxy = false) {
161
186
  let new_value = should_proxy ? proxy(value) : value;
162
187
 
163
188
  if (DEV) {
@@ -317,3 +342,11 @@ export function update_pre(source, d = 1) {
317
342
  export function increment(source) {
318
343
  set(source, source.v + 1);
319
344
  }
345
+
346
+ /**
347
+ * Like `increment`, but without `check_mutation` — see `unchecked_set`
348
+ * @param {Source<number>} source
349
+ */
350
+ export function unchecked_increment(source) {
351
+ unchecked_set(source, source.v + 1);
352
+ }
@@ -4,7 +4,7 @@
4
4
  import { DEV } from 'esm-env';
5
5
  import {
6
6
  clear_text_content,
7
- create_text,
7
+ create_anchor,
8
8
  get_first_child,
9
9
  get_next_sibling,
10
10
  append_child,
@@ -199,7 +199,7 @@ function _mount_inner(
199
199
 
200
200
  var unmount = component_root(() => {
201
201
  var anchor_node =
202
- anchor ?? /** @type {Text} */ (append_child(/** @type {Node} */ (target), create_text()));
202
+ anchor ?? /** @type {Text} */ (append_child(/** @type {Node} */ (target), create_anchor()));
203
203
 
204
204
  boundary(
205
205
  /** @type {TemplateNode} */ (anchor_node),
@@ -1112,6 +1112,9 @@ function remove_reaction(signal, dependency) {
1112
1112
 
1113
1113
  if ((derived.f & CONNECTED) !== 0) {
1114
1114
  derived.f ^= CONNECTED;
1115
+
1116
+ // `mark` no longer reaches the derived, so nothing would remove its view from the cache when it becomes stale
1117
+ view_cache?.delete(derived);
1115
1118
  }
1116
1119
 
1117
1120
  // In a fork it's possible that a derived is executed and gets reactions, then commits, but is
@@ -1,4 +1,5 @@
1
1
  /** @import { HydratableLookupEntry } from '#server' */
2
+ /** @import { UnevalReplacer } from 'devalue' */
2
3
  import { async_mode_flag } from '../flags/index.js';
3
4
  import { get_render_context } from './render-context.js';
4
5
  import * as e from './errors.js';
@@ -54,14 +55,18 @@ function encode(key, value, unresolved) {
54
55
 
55
56
  let uid = 1;
56
57
 
57
- entry.serialized = devalue.uneval(entry.value, (value, uneval) => {
58
+ /** @type {UnevalReplacer} */
59
+ const replacer = (value, js) => {
58
60
  if (is_promise(value)) {
59
- // we serialize promises as `"${i}"`, because it's impossible for that string
60
- // to occur 'naturally' (since the quote marks would have to be escaped)
61
+ // we serialize promises as `<${i}>`, because it's impossible for that string
62
+ // to occur 'naturally' — devalue escapes `<` as `\u003C` in every string, key,
63
+ // regex source and URL it outputs, and never emits it in generated code.
64
+ // (a quoted placeholder like `"1"` is not safe, since it's how the string '1' is serialized.)
61
65
  // this placeholder is returned synchronously from `uneval`, which includes it in the
62
66
  // serialized string. Later (at least one microtask from now), when `p.then` runs, it'll
63
67
  // be replaced.
64
- const placeholder = `"${uid++}"`;
68
+ const id = uid++;
69
+ const placeholder = `<${id}>`;
65
70
  const p = value
66
71
  .then((v) => {
67
72
  entry.serialized = entry.serialized.replace(
@@ -69,7 +74,7 @@ function encode(key, value, unresolved) {
69
74
  // use the function form here to prevent any string replacement characters from being interpreted
70
75
  // in `v`, as it's potentially user-controlled and therefore potentially malicious.
71
76
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_the_replacement
72
- () => `r(${uneval(v)})`
77
+ () => `r(${devalue.uneval(v, replacer)})`
73
78
  );
74
79
  })
75
80
  .catch((devalue_error) =>
@@ -84,9 +89,18 @@ function encode(key, value, unresolved) {
84
89
  p.catch(() => {}).finally(() => unresolved?.delete(p));
85
90
 
86
91
  (entry.promises ??= []).push(p);
87
- return placeholder;
92
+
93
+ // the replacer must return a `js` fragment — interpolated values are serialized,
94
+ // so `<${id}>` renders as e.g. `<1>`, matching `placeholder`
95
+ const fragment = js`<${id}>`;
96
+
97
+ // A user may still be using SvelteKit 2/3 which may resolve to devalue v5
98
+ // rather than v6 on the server because of npm's node_modules structure
99
+ return typeof fragment === 'string' ? /** @type {any} */ (placeholder) : fragment;
88
100
  }
89
- });
101
+ };
102
+
103
+ entry.serialized = devalue.uneval(entry.value, replacer);
90
104
 
91
105
  return entry;
92
106
  }
@@ -111,13 +111,19 @@ export function to_array(value, n) {
111
111
  return value;
112
112
  }
113
113
 
114
- // if value is not iterable, or `n` is unspecified (indicates a rest
115
- // element, which means we're not concerned about unbounded iterables)
116
- // convert to an array with `Array.from`
117
- if (n === undefined || !(Symbol.iterator in value)) {
114
+ // if `n` is unspecified (indicates a rest element, which means
115
+ // we're not concerned about unbounded iterables) convert to an
116
+ // array with `Array.from`
117
+ if (n === undefined) {
118
118
  return Array.from(value);
119
119
  }
120
120
 
121
+ // if value is array-like but not iterable, copy at most `n` values
122
+ if (!(Symbol.iterator in value)) {
123
+ const length = Math.min(n, /** @type {ArrayLike<T>} */ (value).length);
124
+ return Array.from({ length }, (_, i) => /** @type {ArrayLike<T>} */ (value)[i]);
125
+ }
126
+
121
127
  // otherwise, populate an array with `n` values
122
128
 
123
129
  /** @type {T[]} */
@@ -76,7 +76,7 @@ export function createSubscriber(start) {
76
76
  queue_micro_task(() => {
77
77
  // Only count down after a microtask, else we would reach 0 before our own render effect reruns,
78
78
  // but reach 1 again when the tick callback of the prior teardown runs. That would mean we
79
- // re-subcribe unnecessarily and create a memory leak because the old subscription is never cleaned up.
79
+ // re-subscribe unnecessarily and create a memory leak because the old subscription is never cleaned up.
80
80
  subscribers -= 1;
81
81
 
82
82
  if (subscribers === 0) {
@@ -1,6 +1,6 @@
1
1
  /** @import { Derived } from '#client' */
2
2
  import { derived } from '../internal/client/index.js';
3
- import { set, state } from '../internal/client/reactivity/sources.js';
3
+ import { state, check_mutation, unchecked_set } from '../internal/client/reactivity/sources.js';
4
4
  import { tag } from '../internal/client/dev/tracing.js';
5
5
  import { active_reaction, get, set_active_reaction } from '../internal/client/runtime.js';
6
6
  import { DEV } from 'esm-env';
@@ -111,9 +111,10 @@ export class SvelteDate extends Date {
111
111
  if (method.startsWith('set')) {
112
112
  // @ts-ignore
113
113
  proto[method] = function (...args) {
114
+ check_mutation(this.#time);
114
115
  // @ts-ignore
115
116
  var result = date_proto[method].apply(this, args);
116
- set(this.#time, date_proto.getTime.call(this));
117
+ unchecked_set(this.#time, date_proto.getTime.call(this));
117
118
  return result;
118
119
  };
119
120
  }
@@ -1,6 +1,12 @@
1
1
  /** @import { Source } from '#client' */
2
2
  import { DEV } from 'esm-env';
3
- import { set, source, state, increment } from '../internal/client/reactivity/sources.js';
3
+ import {
4
+ source,
5
+ state,
6
+ check_mutation,
7
+ unchecked_set,
8
+ unchecked_increment
9
+ } from '../internal/client/reactivity/sources.js';
4
10
  import { label, tag } from '../internal/client/dev/tracing.js';
5
11
  import { get, update_version } from '../internal/client/runtime.js';
6
12
 
@@ -170,6 +176,7 @@ export class SvelteMap extends Map {
170
176
  */
171
177
  getOrInsertComputed(key, callbackFn) {
172
178
  if (!super.has(key)) {
179
+ check_mutation(this.#version);
173
180
  this.set(key, callbackFn(key));
174
181
  }
175
182
  return /** @type {V} */ (this.get(key));
@@ -183,6 +190,9 @@ export class SvelteMap extends Map {
183
190
  var sources = this.#sources;
184
191
  var s = sources.get(key);
185
192
  var prev_res = super.get(key);
193
+
194
+ check_mutation(this.#version);
195
+
186
196
  var res = super.set(key, value);
187
197
  var version = this.#version;
188
198
 
@@ -194,10 +204,10 @@ export class SvelteMap extends Map {
194
204
  }
195
205
 
196
206
  sources.set(key, s);
197
- set(this.#size, super.size);
198
- increment(version);
207
+ unchecked_set(this.#size, super.size);
208
+ unchecked_increment(version);
199
209
  } else if (prev_res !== value) {
200
- increment(s);
210
+ unchecked_increment(s);
201
211
 
202
212
  // if not every reaction of s is a reaction of version we need to also include version
203
213
  var v_reactions = version.reactions === null ? null : new Set(version.reactions);
@@ -207,7 +217,7 @@ export class SvelteMap extends Map {
207
217
  /** @type {NonNullable<typeof v_reactions>} */ (v_reactions).has(r)
208
218
  );
209
219
  if (needs_version_increase) {
210
- increment(version);
220
+ unchecked_increment(version);
211
221
  }
212
222
  }
213
223
 
@@ -218,33 +228,38 @@ export class SvelteMap extends Map {
218
228
  delete(key) {
219
229
  var sources = this.#sources;
220
230
  var s = sources.get(key);
231
+
232
+ check_mutation(this.#version);
233
+
221
234
  var res = super.delete(key);
222
235
 
223
236
  if (s !== undefined) {
224
237
  sources.delete(key);
225
- set(s, -1);
238
+ unchecked_set(s, -1);
226
239
  }
227
240
 
228
241
  if (res) {
229
- set(this.#size, super.size);
230
- increment(this.#version);
242
+ unchecked_set(this.#size, super.size);
243
+ unchecked_increment(this.#version);
231
244
  }
232
245
 
233
246
  return res;
234
247
  }
235
248
 
236
249
  clear() {
250
+ check_mutation(this.#version);
251
+
237
252
  if (super.size === 0) {
238
253
  return;
239
254
  }
240
255
  // Clear first, so we get nice console.log outputs with $inspect
241
256
  super.clear();
242
257
  var sources = this.#sources;
243
- set(this.#size, 0);
258
+ unchecked_set(this.#size, 0);
244
259
  for (var s of sources.values()) {
245
- set(s, -1);
260
+ unchecked_set(s, -1);
246
261
  }
247
- increment(this.#version);
262
+ unchecked_increment(this.#version);
248
263
  sources.clear();
249
264
  }
250
265