ripple 0.2.85 → 0.2.87

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.
@@ -8,6 +8,7 @@ var next_sibling_getter;
8
8
  /** @type {Document} */
9
9
  export var document;
10
10
 
11
+ /** @type {boolean} */
11
12
  export var is_firefox;
12
13
 
13
14
  export function init_operations() {
@@ -44,13 +45,17 @@ export function init_operations() {
44
45
  * @param {N} node
45
46
  * @returns {Node | null}
46
47
  */
47
- /*@__NO_SIDE_EFFECTS__*/
48
48
  export function first_child(node) {
49
49
  return first_child_getter.call(node);
50
50
  }
51
51
 
52
+ /**
53
+ * @template {Node} N
54
+ * @param {N} node
55
+ * @returns {Node | null}
56
+ */
52
57
  export function child_frag(node) {
53
- var child = first_child(node);
58
+ var child = /** @type {Text} */ (first_child(node));
54
59
 
55
60
  if (child.nodeType === 8 && child.data === '') {
56
61
  return next_sibling(child);
@@ -1,12 +1,23 @@
1
+ /** @import { Block } from '#client' */
2
+
1
3
  import { branch, destroy_block, render } from './blocks.js';
2
4
  import { UNINITIALIZED } from './constants.js';
3
5
  import { handle_root_events } from './events.js';
4
6
  import { create_text } from './operations.js';
5
7
 
8
+ /**
9
+ * @param {any} _
10
+ * @param {{ target: Element, children: (anchor: Node) => void }} props
11
+ * @returns {void}
12
+ */
6
13
  export function Portal(_, props) {
14
+ /** @type {Element | symbol} */
7
15
  let target = UNINITIALIZED;
16
+ /** @type {((anchor: Node) => void) | symbol} */
8
17
  let children = UNINITIALIZED;
18
+ /** @type {Block | null} */
9
19
  var b = null;
20
+ /** @type {Text | null} */
10
21
  var anchor = null;
11
22
 
12
23
  render(() => {
@@ -18,15 +29,15 @@ export function Portal(_, props) {
18
29
  }
19
30
 
20
31
  anchor = create_text();
21
- target.append(anchor);
32
+ /** @type {Element} */ (target).append(anchor);
22
33
 
23
- const cleanup_events = handle_root_events(target);
34
+ const cleanup_events = handle_root_events(/** @type {Element} */ (target));
24
35
 
25
- b = branch(() => children(anchor));
36
+ b = branch(() => /** @type {(anchor: Node) => void} */ (children)(/** @type {Text} */ (anchor)));
26
37
 
27
38
  return () => {
28
39
  cleanup_events();
29
- anchor.remove();
40
+ /** @type {Text} */ (anchor).remove();
30
41
  };
31
42
  });
32
43
  }
@@ -1,3 +1,5 @@
1
+ /** @import { Block } from '#client' */
2
+
1
3
  import { destroy_block, ref } from './blocks.js';
2
4
  import { REF_PROP } from './constants.js';
3
5
  import {
@@ -14,6 +16,11 @@ import {
14
16
  } from '../../../utils/events.js';
15
17
  import { get } from './runtime.js';
16
18
 
19
+ /**
20
+ * @param {Text} text
21
+ * @param {any} value
22
+ * @returns {void}
23
+ */
17
24
  export function set_text(text, value) {
18
25
  // For objects, we apply string coercion
19
26
  var str = value == null ? '' : typeof value === 'object' ? value + '' : value;
@@ -25,8 +32,13 @@ export function set_text(text, value) {
25
32
  }
26
33
  }
27
34
 
35
+ /** @type {Map<string, string[]>} */
28
36
  var setters_cache = new Map();
29
37
 
38
+ /**
39
+ * @param {Element} element
40
+ * @returns {string[]}
41
+ */
30
42
  function get_setters(element) {
31
43
  var setters = setters_cache.get(element.nodeName);
32
44
  if (setters) return setters;
@@ -53,6 +65,12 @@ function get_setters(element) {
53
65
  return setters;
54
66
  }
55
67
 
68
+ /**
69
+ * @param {Element} element
70
+ * @param {string} attribute
71
+ * @param {any} value
72
+ * @returns {void}
73
+ */
56
74
  export function set_attribute(element, attribute, value) {
57
75
  // @ts-expect-error
58
76
  var attributes = (element.__attributes ??= {});
@@ -67,12 +85,17 @@ export function set_attribute(element, attribute, value) {
67
85
  if (value == null) {
68
86
  element.removeAttribute(attribute);
69
87
  } else if (typeof value !== 'string' && get_setters(element).includes(attribute)) {
70
- element[attribute] = value;
88
+ /** @type {any} */ (element)[attribute] = value;
71
89
  } else {
72
90
  element.setAttribute(attribute, value);
73
91
  }
74
92
  }
75
93
 
94
+ /**
95
+ * @param {Element} element
96
+ * @param {Record<string, any>} attributes
97
+ * @returns {void}
98
+ */
76
99
  export function set_attributes(element, attributes) {
77
100
  for (const key in attributes) {
78
101
  if (key === 'children') continue;
@@ -85,7 +108,7 @@ export function set_attributes(element, attributes) {
85
108
 
86
109
  if (key === 'class') {
87
110
  const is_html = element.namespaceURI === 'http://www.w3.org/1999/xhtml';
88
- set_class(element, value, undefined, is_html);
111
+ set_class(/** @type {HTMLElement} */ (element), value, undefined, is_html);
89
112
  } else if (key === '#class') {
90
113
  // Special case for static class when spreading props
91
114
  element.classList.add(value);
@@ -95,7 +118,7 @@ export function set_attributes(element, attributes) {
95
118
 
96
119
  if (is_delegated(event_name)) {
97
120
  // Use delegation for delegated events
98
- element['__' + event_name] = value;
121
+ /** @type {any} */ (element)['__' + event_name] = value;
99
122
  delegate([event_name]);
100
123
  } else {
101
124
  // Use addEventListener for non-delegated events
@@ -148,6 +171,11 @@ export function set_class(dom, value, hash, is_html = true) {
148
171
  }
149
172
  }
150
173
 
174
+ /**
175
+ * @param {HTMLInputElement | HTMLProgressElement} element
176
+ * @param {any} value
177
+ * @returns {void}
178
+ */
151
179
  export function set_value(element, value) {
152
180
  // @ts-expect-error
153
181
  var attributes = (element.__attributes ??= {});
@@ -157,17 +185,20 @@ export function set_value(element, value) {
157
185
  (attributes.value =
158
186
  // treat null and undefined the same for the initial value
159
187
  value ?? undefined) ||
160
- // @ts-expect-error
161
188
  // `progress` elements always need their value set when it's `0`
162
189
  (element.value === value && (value !== 0 || element.nodeName !== 'PROGRESS'))
163
190
  ) {
164
191
  return;
165
192
  }
166
193
 
167
- // @ts-expect-error
168
194
  element.value = value ?? '';
169
195
  }
170
196
 
197
+ /**
198
+ * @param {HTMLInputElement} element
199
+ * @param {boolean} checked
200
+ * @returns {void}
201
+ */
171
202
  export function set_checked(element, checked) {
172
203
  // @ts-expect-error
173
204
  var attributes = (element.__attributes ??= {});
@@ -181,10 +212,14 @@ export function set_checked(element, checked) {
181
212
  return;
182
213
  }
183
214
 
184
- // @ts-expect-error
185
215
  element.checked = checked;
186
216
  }
187
217
 
218
+ /**
219
+ * @param {HTMLOptionElement} element
220
+ * @param {boolean} selected
221
+ * @returns {void}
222
+ */
188
223
  export function set_selected(element, selected) {
189
224
  if (selected) {
190
225
  // The selected option could've changed via user selection, and
@@ -197,8 +232,15 @@ export function set_selected(element, selected) {
197
232
  }
198
233
  }
199
234
 
235
+ /**
236
+ * @param {Element} element
237
+ * @param {() => Record<string | symbol, any>} fn
238
+ * @returns {() => void}
239
+ */
200
240
  export function apply_element_spread(element, fn) {
241
+ /** @type {Record<string | symbol, any> | undefined} */
201
242
  var prev;
243
+ /** @type {Record<symbol, Block>} */
202
244
  var effects = {};
203
245
 
204
246
  return () => {
@@ -1,5 +1,4 @@
1
1
  /** @import { Block, Component, Dependency, Derived, Tracked } from '#client' */
2
- /** @import { TrackOptions } from '#public' */
3
2
 
4
3
  import {
5
4
  destroy_block,
@@ -27,7 +26,14 @@ import {
27
26
  REF_PROP,
28
27
  } from './constants.js';
29
28
  import { capture, suspend } from './try.js';
30
- import { define_property, get_descriptors, is_array, is_tracked_object } from './utils.js';
29
+ import {
30
+ define_property,
31
+ get_descriptors,
32
+ get_own_property_symbols,
33
+ is_array,
34
+ is_tracked_object,
35
+ object_keys,
36
+ } from './utils.js';
31
37
 
32
38
  const FLUSH_MICROTASK = 0;
33
39
  const FLUSH_SYNC = 1;
@@ -41,17 +47,22 @@ export let active_scope = null;
41
47
  /** @type {null | Component} */
42
48
  export let active_component = null;
43
49
 
50
+ /** @type {Map<Tracked, any>} */
44
51
  var old_values = new Map();
45
52
 
46
53
  // Used for controlling the flush of blocks
54
+ /** @type {number} */
47
55
  let scheduler_mode = FLUSH_MICROTASK;
48
56
  // Used for handling scheduling
57
+ /** @type {boolean} */
49
58
  let is_micro_task_queued = false;
59
+ /** @type {number} */
50
60
  let clock = 0;
51
61
  /** @type {Block[]} */
52
62
  let queued_root_blocks = [];
53
63
  /** @type {(() => void)[]} */
54
64
  let queued_microtasks = [];
65
+ /** @type {number} */
55
66
  let flush_count = 0;
56
67
  /** @type {null | Dependency} */
57
68
  let active_dependency = null;
@@ -59,6 +70,9 @@ let active_dependency = null;
59
70
  export let tracking = false;
60
71
  export let teardown = false;
61
72
 
73
+ /**
74
+ * @returns {number}
75
+ */
62
76
  function increment_clock() {
63
77
  return ++clock;
64
78
  }
@@ -289,8 +303,8 @@ export function derived(fn, block, get, set) {
289
303
 
290
304
  /**
291
305
  * @param {any} v
292
- * @param {Function | undefined} get
293
- * @param {Function | undefined} set
306
+ * @param {(value: any) => any | undefined} get
307
+ * @param {(next: any, prev: any) => any | undefined} set
294
308
  * @param {Block} b
295
309
  * @returns {Tracked | Derived}
296
310
  */
@@ -422,16 +436,25 @@ export function is_block_dirty(block) {
422
436
  return is_tracking_dirty(block.d);
423
437
  }
424
438
 
439
+ /**
440
+ * @param {() => Promise<any>} fn
441
+ * @param {Block} block
442
+ * @returns {Promise<Tracked>}
443
+ */
425
444
  export function async_computed(fn, block) {
445
+ /** @type {Block | Derived | null} */
426
446
  let parent = active_reaction;
427
447
  var t = tracked(UNINITIALIZED, block);
448
+ /** @type {Promise<any>} */
428
449
  var promise;
450
+ /** @type {Map<Tracked, {v: any, c: number}>} */
429
451
  var new_values = new Map();
430
452
 
431
453
  render(() => {
432
454
  var [current, deferred] = capture_deferred(() => (promise = fn()));
433
455
 
434
456
  var restore = capture();
457
+ /** @type {(() => void) | undefined} */
435
458
  var unuspend;
436
459
 
437
460
  if (deferred === null) {
@@ -444,7 +467,7 @@ export function async_computed(fn, block) {
444
467
  }
445
468
 
446
469
  promise.then((v) => {
447
- if (is_destroyed(parent)) {
470
+ if (parent && is_destroyed(/** @type {Block} */ (parent))) {
448
471
  return;
449
472
  }
450
473
  if (promise === current && t.v !== v) {
@@ -458,11 +481,12 @@ export function async_computed(fn, block) {
458
481
  }
459
482
 
460
483
  if (deferred === null) {
461
- unuspend();
484
+ unuspend?.();
462
485
  } else if (promise === current) {
463
486
  for (var i = 0; i < deferred.length; i++) {
464
487
  var tracked = deferred[i];
465
- var { v, c } = new_values.get(tracked);
488
+ var stored = /** @type {{ v: any, c: number }} */ (new_values.get(tracked));
489
+ var { v, c } = stored;
466
490
  tracked.v = v;
467
491
  tracked.c = c;
468
492
  schedule_update(tracked.b);
@@ -479,48 +503,13 @@ export function async_computed(fn, block) {
479
503
  });
480
504
  }
481
505
 
482
- export function deferred(fn) {
483
- // var parent = active_block;
484
- // var block = active_scope;
485
- // var res = [UNINITIALIZED];
486
- // // TODO implement DEFERRED flag on tracked
487
- // var t = tracked(UNINITIALIZED, block, DEFERRED);
488
- // var tracked_properties = [t];
489
- // var prev_value = UNINITIALIZED;
490
- // define_property(res, TRACKED_OBJECT, {
491
- // value: tracked_properties,
492
- // enumerable: false,
493
- // });
494
- // render(() => {
495
- // if (prev_value !== UNINITIALIZED) {
496
- // t.v = prev_value;
497
- // } else {
498
- // prev_value = t.v;
499
- // }
500
- // var prev_version = t.c;
501
- // var value = fn();
502
- // res[0] = value;
503
- // old_set_property(res, 0, value, block);
504
- // if (prev_value !== UNINITIALIZED) {
505
- // if ((t.f & DEFERRED) === 0) {
506
- // t.f ^= DEFERRED;
507
- // }
508
- // var is_awaited = flush_deferred_upodates(parent);
509
- // if ((t.f & DEFERRED) !== 0) {
510
- // t.f ^= DEFERRED;
511
- // }
512
- // if (is_awaited) {
513
- // t.c = prev_version;
514
- // t.v = prev_value;
515
- // prev_value = value;
516
- // }
517
- // }
518
- // });
519
- // return res;
520
- }
521
-
506
+ /**
507
+ * @param {() => any} fn
508
+ * @returns {[any, Tracked[] | null]}
509
+ */
522
510
  function capture_deferred(fn) {
523
511
  var value = fn();
512
+ /** @type {Tracked[] | null} */
524
513
  var deferred = null;
525
514
  var depedency = active_dependency;
526
515
 
@@ -537,36 +526,6 @@ function capture_deferred(fn) {
537
526
  return [value, deferred];
538
527
  }
539
528
 
540
- /**
541
- * @param {Block} block
542
- */
543
- function flush_deferred_upodates(block) {
544
- var current = block.first;
545
- var is_awaited = false;
546
-
547
- main_loop: while (current !== null) {
548
- var flags = current.f;
549
-
550
- if ((flags & ASYNC_BLOCK) !== 0 && is_block_dirty(current)) {
551
- is_awaited = true;
552
- run_block(current);
553
- }
554
-
555
- var parent = current.p;
556
- current = current.next;
557
-
558
- while (current === null && parent !== null) {
559
- if (parent === block) {
560
- break main_loop;
561
- }
562
- current = parent.next;
563
- parent = parent.p;
564
- }
565
- }
566
-
567
- return is_awaited;
568
- }
569
-
570
529
  /**
571
530
  * @param {Block} root_block
572
531
  */
@@ -643,6 +602,9 @@ function flush_queued_root_blocks(root_blocks) {
643
602
  }
644
603
  }
645
604
 
605
+ /**
606
+ * @returns {void}
607
+ */
646
608
  function flush_microtasks() {
647
609
  is_micro_task_queued = false;
648
610
 
@@ -800,7 +762,7 @@ export function set(tracked, value, block) {
800
762
 
801
763
  var set = tracked.a.set;
802
764
  if (set) {
803
- value = set(value);
765
+ value = set(value, old_value);
804
766
  }
805
767
 
806
768
  tracked.v = value;
@@ -876,6 +838,14 @@ export function spread_props(fn, block) {
876
838
  const obj = get_derived(computed);
877
839
  return obj[property];
878
840
  },
841
+ has(target, property) {
842
+ const obj = get_derived(computed);
843
+ return property in obj;
844
+ },
845
+ ownKeys() {
846
+ const obj = get_derived(computed);
847
+ return Reflect.ownKeys(obj);
848
+ },
879
849
  },
880
850
  );
881
851
  }
@@ -932,6 +902,13 @@ export function get_property(obj, property, chain = false) {
932
902
  return get(tracked);
933
903
  }
934
904
 
905
+ /**
906
+ * @param {any} obj
907
+ * @param {string | number | symbol} property
908
+ * @param {any} value
909
+ * @param {Block} block
910
+ * @returns {void}
911
+ */
935
912
  export function set_property(obj, property, value, block) {
936
913
  var tracked = obj[property];
937
914
  set(tracked, value, block);
@@ -981,6 +958,13 @@ export function update_pre(tracked, block, d = 1) {
981
958
  return new_value;
982
959
  }
983
960
 
961
+ /**
962
+ * @param {any} obj
963
+ * @param {string | number | symbol} property
964
+ * @param {Block} block
965
+ * @param {number} [d=1]
966
+ * @returns {number}
967
+ */
984
968
  export function update_property(obj, property, block, d = 1) {
985
969
  var tracked = obj[property];
986
970
  var value = get(tracked);
@@ -989,6 +973,13 @@ export function update_property(obj, property, block, d = 1) {
989
973
  return new_value;
990
974
  }
991
975
 
976
+ /**
977
+ * @param {any} obj
978
+ * @param {string | number | symbol} property
979
+ * @param {Block} block
980
+ * @param {number} [d=1]
981
+ * @returns {number}
982
+ */
992
983
  export function update_pre_property(obj, property, block, d = 1) {
993
984
  var tracked = obj[property];
994
985
  var value = get(tracked);
@@ -1032,6 +1023,9 @@ export function safe_scope(err = 'Cannot access outside of a component context')
1032
1023
  return /** @type {Block} */ (active_scope);
1033
1024
  }
1034
1025
 
1026
+ /**
1027
+ * @returns {void}
1028
+ */
1035
1029
  export function push_component() {
1036
1030
  var component = {
1037
1031
  c: null,
@@ -1042,6 +1036,9 @@ export function push_component() {
1042
1036
  active_component = component;
1043
1037
  }
1044
1038
 
1039
+ /**
1040
+ * @returns {void}
1041
+ */
1045
1042
  export function pop_component() {
1046
1043
  var component = /** @type {Component} */ (active_component);
1047
1044
  component.m = true;
@@ -1066,6 +1063,9 @@ export function pop_component() {
1066
1063
  active_component = component.p;
1067
1064
  }
1068
1065
 
1066
+ /**
1067
+ * @returns {symbol}
1068
+ */
1069
1069
  export function ref_prop() {
1070
1070
  return Symbol(REF_PROP);
1071
1071
  }
@@ -1081,21 +1081,35 @@ export function fallback(value, fallback) {
1081
1081
  }
1082
1082
 
1083
1083
  /**
1084
- * @param {Record<string, unknown>} obj
1085
- * @param {string[]} keys
1086
- * @returns {Record<string, unknown>}
1084
+ * @param {Record<string | symbol, unknown>} obj
1085
+ * @param {string[]} exclude_keys
1086
+ * @returns {Record<string | symbol, unknown>}
1087
1087
  */
1088
- export function exclude_from_object(obj, keys) {
1089
- obj = { ...obj };
1090
- let key;
1091
- for (key of keys) {
1092
- delete obj[key];
1088
+ export function exclude_from_object(obj, exclude_keys) {
1089
+ var keys = object_keys(obj);
1090
+ /** @type {Record<string | symbol, unknown>} */
1091
+ var new_obj = {};
1092
+
1093
+ for (const key of keys) {
1094
+ if (!exclude_keys.includes(key)) {
1095
+ new_obj[key] = obj[key];
1096
+ }
1097
+ }
1098
+
1099
+ for (const symbol of get_own_property_symbols(obj)) {
1100
+ var ref_fn = obj[symbol];
1101
+
1102
+ if (symbol.description === REF_PROP) {
1103
+ new_obj[symbol] = ref_fn;
1104
+ }
1093
1105
  }
1094
- return obj;
1106
+
1107
+ return new_obj;
1095
1108
  }
1096
1109
 
1097
1110
  /**
1098
1111
  * @param {any} v
1112
+ * @returns {Promise<() => any>}
1099
1113
  */
1100
1114
  export async function maybe_tracked(v) {
1101
1115
  var restore = capture();
@@ -1,3 +1,5 @@
1
+ /** @import { Block } from '#client' */
2
+
1
3
  import {
2
4
  TEMPLATE_FRAGMENT,
3
5
  TEMPLATE_USE_IMPORT_NODE,
@@ -49,13 +51,14 @@ function create_fragment_from_html(html, use_svg_namespace = false, use_mathml_n
49
51
  * Creates a template node or fragment from content and flags.
50
52
  * @param {string} content - The template content.
51
53
  * @param {number} flags - Flags for template type.
52
- * @returns {Node}
54
+ * @returns {() => Node}
53
55
  */
54
56
  export function template(content, flags) {
55
57
  var is_fragment = (flags & TEMPLATE_FRAGMENT) !== 0;
56
58
  var use_import_node = (flags & TEMPLATE_USE_IMPORT_NODE) !== 0;
57
59
  var use_svg_namespace = (flags & TEMPLATE_SVG_NAMESPACE) !== 0;
58
60
  var use_mathml_namespace = (flags & TEMPLATE_MATHML_NAMESPACE) !== 0;
61
+ /** @type {Node | DocumentFragment | undefined} */
59
62
  var node;
60
63
  var has_start = !content.startsWith('<!>');
61
64
 
@@ -66,17 +69,17 @@ export function template(content, flags) {
66
69
  use_svg_namespace,
67
70
  use_mathml_namespace,
68
71
  );
69
- if (!is_fragment) node = first_child(node);
72
+ if (!is_fragment) node = /** @type {Node} */ (first_child(node));
70
73
  }
71
74
 
72
75
  var clone =
73
- use_import_node || is_firefox ? document.importNode(node, true) : node.cloneNode(true);
76
+ use_import_node || is_firefox ? document.importNode(/** @type {Node} */ (node), true) : /** @type {Node} */ (node).cloneNode(true);
74
77
 
75
78
  if (is_fragment) {
76
79
  var start = first_child(clone);
77
80
  var end = clone.lastChild;
78
81
 
79
- assign_nodes(start, end);
82
+ assign_nodes(/** @type {Node} */ (start), /** @type {Node} */ (end));
80
83
  } else {
81
84
  assign_nodes(clone, clone);
82
85
  }
@@ -87,7 +90,7 @@ export function template(content, flags) {
87
90
 
88
91
  /**
89
92
  * Appends a DOM node before the anchor node.
90
- * @param {Node} anchor - The anchor node.
93
+ * @param {ChildNode} anchor - The anchor node.
91
94
  * @param {Node} dom - The DOM node to append.
92
95
  */
93
96
  export function append(anchor, dom) {