ripple 0.2.86 → 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 () => {
@@ -26,7 +26,14 @@ import {
26
26
  REF_PROP,
27
27
  } from './constants.js';
28
28
  import { capture, suspend } from './try.js';
29
- 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';
30
37
 
31
38
  const FLUSH_MICROTASK = 0;
32
39
  const FLUSH_SYNC = 1;
@@ -40,17 +47,22 @@ export let active_scope = null;
40
47
  /** @type {null | Component} */
41
48
  export let active_component = null;
42
49
 
50
+ /** @type {Map<Tracked, any>} */
43
51
  var old_values = new Map();
44
52
 
45
53
  // Used for controlling the flush of blocks
54
+ /** @type {number} */
46
55
  let scheduler_mode = FLUSH_MICROTASK;
47
56
  // Used for handling scheduling
57
+ /** @type {boolean} */
48
58
  let is_micro_task_queued = false;
59
+ /** @type {number} */
49
60
  let clock = 0;
50
61
  /** @type {Block[]} */
51
62
  let queued_root_blocks = [];
52
63
  /** @type {(() => void)[]} */
53
64
  let queued_microtasks = [];
65
+ /** @type {number} */
54
66
  let flush_count = 0;
55
67
  /** @type {null | Dependency} */
56
68
  let active_dependency = null;
@@ -58,6 +70,9 @@ let active_dependency = null;
58
70
  export let tracking = false;
59
71
  export let teardown = false;
60
72
 
73
+ /**
74
+ * @returns {number}
75
+ */
61
76
  function increment_clock() {
62
77
  return ++clock;
63
78
  }
@@ -421,16 +436,25 @@ export function is_block_dirty(block) {
421
436
  return is_tracking_dirty(block.d);
422
437
  }
423
438
 
439
+ /**
440
+ * @param {() => Promise<any>} fn
441
+ * @param {Block} block
442
+ * @returns {Promise<Tracked>}
443
+ */
424
444
  export function async_computed(fn, block) {
445
+ /** @type {Block | Derived | null} */
425
446
  let parent = active_reaction;
426
447
  var t = tracked(UNINITIALIZED, block);
448
+ /** @type {Promise<any>} */
427
449
  var promise;
450
+ /** @type {Map<Tracked, {v: any, c: number}>} */
428
451
  var new_values = new Map();
429
452
 
430
453
  render(() => {
431
454
  var [current, deferred] = capture_deferred(() => (promise = fn()));
432
455
 
433
456
  var restore = capture();
457
+ /** @type {(() => void) | undefined} */
434
458
  var unuspend;
435
459
 
436
460
  if (deferred === null) {
@@ -443,7 +467,7 @@ export function async_computed(fn, block) {
443
467
  }
444
468
 
445
469
  promise.then((v) => {
446
- if (is_destroyed(parent)) {
470
+ if (parent && is_destroyed(/** @type {Block} */ (parent))) {
447
471
  return;
448
472
  }
449
473
  if (promise === current && t.v !== v) {
@@ -457,11 +481,12 @@ export function async_computed(fn, block) {
457
481
  }
458
482
 
459
483
  if (deferred === null) {
460
- unuspend();
484
+ unuspend?.();
461
485
  } else if (promise === current) {
462
486
  for (var i = 0; i < deferred.length; i++) {
463
487
  var tracked = deferred[i];
464
- var { v, c } = new_values.get(tracked);
488
+ var stored = /** @type {{ v: any, c: number }} */ (new_values.get(tracked));
489
+ var { v, c } = stored;
465
490
  tracked.v = v;
466
491
  tracked.c = c;
467
492
  schedule_update(tracked.b);
@@ -478,48 +503,13 @@ export function async_computed(fn, block) {
478
503
  });
479
504
  }
480
505
 
481
- export function deferred(fn) {
482
- // var parent = active_block;
483
- // var block = active_scope;
484
- // var res = [UNINITIALIZED];
485
- // // TODO implement DEFERRED flag on tracked
486
- // var t = tracked(UNINITIALIZED, block, DEFERRED);
487
- // var tracked_properties = [t];
488
- // var prev_value = UNINITIALIZED;
489
- // define_property(res, TRACKED_OBJECT, {
490
- // value: tracked_properties,
491
- // enumerable: false,
492
- // });
493
- // render(() => {
494
- // if (prev_value !== UNINITIALIZED) {
495
- // t.v = prev_value;
496
- // } else {
497
- // prev_value = t.v;
498
- // }
499
- // var prev_version = t.c;
500
- // var value = fn();
501
- // res[0] = value;
502
- // old_set_property(res, 0, value, block);
503
- // if (prev_value !== UNINITIALIZED) {
504
- // if ((t.f & DEFERRED) === 0) {
505
- // t.f ^= DEFERRED;
506
- // }
507
- // var is_awaited = flush_deferred_upodates(parent);
508
- // if ((t.f & DEFERRED) !== 0) {
509
- // t.f ^= DEFERRED;
510
- // }
511
- // if (is_awaited) {
512
- // t.c = prev_version;
513
- // t.v = prev_value;
514
- // prev_value = value;
515
- // }
516
- // }
517
- // });
518
- // return res;
519
- }
520
-
506
+ /**
507
+ * @param {() => any} fn
508
+ * @returns {[any, Tracked[] | null]}
509
+ */
521
510
  function capture_deferred(fn) {
522
511
  var value = fn();
512
+ /** @type {Tracked[] | null} */
523
513
  var deferred = null;
524
514
  var depedency = active_dependency;
525
515
 
@@ -536,36 +526,6 @@ function capture_deferred(fn) {
536
526
  return [value, deferred];
537
527
  }
538
528
 
539
- /**
540
- * @param {Block} block
541
- */
542
- function flush_deferred_upodates(block) {
543
- var current = block.first;
544
- var is_awaited = false;
545
-
546
- main_loop: while (current !== null) {
547
- var flags = current.f;
548
-
549
- if ((flags & ASYNC_BLOCK) !== 0 && is_block_dirty(current)) {
550
- is_awaited = true;
551
- run_block(current);
552
- }
553
-
554
- var parent = current.p;
555
- current = current.next;
556
-
557
- while (current === null && parent !== null) {
558
- if (parent === block) {
559
- break main_loop;
560
- }
561
- current = parent.next;
562
- parent = parent.p;
563
- }
564
- }
565
-
566
- return is_awaited;
567
- }
568
-
569
529
  /**
570
530
  * @param {Block} root_block
571
531
  */
@@ -642,6 +602,9 @@ function flush_queued_root_blocks(root_blocks) {
642
602
  }
643
603
  }
644
604
 
605
+ /**
606
+ * @returns {void}
607
+ */
645
608
  function flush_microtasks() {
646
609
  is_micro_task_queued = false;
647
610
 
@@ -875,6 +838,14 @@ export function spread_props(fn, block) {
875
838
  const obj = get_derived(computed);
876
839
  return obj[property];
877
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
+ },
878
849
  },
879
850
  );
880
851
  }
@@ -931,6 +902,13 @@ export function get_property(obj, property, chain = false) {
931
902
  return get(tracked);
932
903
  }
933
904
 
905
+ /**
906
+ * @param {any} obj
907
+ * @param {string | number | symbol} property
908
+ * @param {any} value
909
+ * @param {Block} block
910
+ * @returns {void}
911
+ */
934
912
  export function set_property(obj, property, value, block) {
935
913
  var tracked = obj[property];
936
914
  set(tracked, value, block);
@@ -980,6 +958,13 @@ export function update_pre(tracked, block, d = 1) {
980
958
  return new_value;
981
959
  }
982
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
+ */
983
968
  export function update_property(obj, property, block, d = 1) {
984
969
  var tracked = obj[property];
985
970
  var value = get(tracked);
@@ -988,6 +973,13 @@ export function update_property(obj, property, block, d = 1) {
988
973
  return new_value;
989
974
  }
990
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
+ */
991
983
  export function update_pre_property(obj, property, block, d = 1) {
992
984
  var tracked = obj[property];
993
985
  var value = get(tracked);
@@ -1031,6 +1023,9 @@ export function safe_scope(err = 'Cannot access outside of a component context')
1031
1023
  return /** @type {Block} */ (active_scope);
1032
1024
  }
1033
1025
 
1026
+ /**
1027
+ * @returns {void}
1028
+ */
1034
1029
  export function push_component() {
1035
1030
  var component = {
1036
1031
  c: null,
@@ -1041,6 +1036,9 @@ export function push_component() {
1041
1036
  active_component = component;
1042
1037
  }
1043
1038
 
1039
+ /**
1040
+ * @returns {void}
1041
+ */
1044
1042
  export function pop_component() {
1045
1043
  var component = /** @type {Component} */ (active_component);
1046
1044
  component.m = true;
@@ -1065,6 +1063,9 @@ export function pop_component() {
1065
1063
  active_component = component.p;
1066
1064
  }
1067
1065
 
1066
+ /**
1067
+ * @returns {symbol}
1068
+ */
1068
1069
  export function ref_prop() {
1069
1070
  return Symbol(REF_PROP);
1070
1071
  }
@@ -1080,21 +1081,35 @@ export function fallback(value, fallback) {
1080
1081
  }
1081
1082
 
1082
1083
  /**
1083
- * @param {Record<string, unknown>} obj
1084
- * @param {string[]} keys
1085
- * @returns {Record<string, unknown>}
1084
+ * @param {Record<string | symbol, unknown>} obj
1085
+ * @param {string[]} exclude_keys
1086
+ * @returns {Record<string | symbol, unknown>}
1086
1087
  */
1087
- export function exclude_from_object(obj, keys) {
1088
- obj = { ...obj };
1089
- let key;
1090
- for (key of keys) {
1091
- 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
+ }
1092
1105
  }
1093
- return obj;
1106
+
1107
+ return new_obj;
1094
1108
  }
1095
1109
 
1096
1110
  /**
1097
1111
  * @param {any} v
1112
+ * @returns {Promise<() => any>}
1098
1113
  */
1099
1114
  export async function maybe_tracked(v) {
1100
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) {