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.
@@ -1,27 +1,28 @@
1
1
  /** @import { Block, Derived } from '#client' */
2
2
 
3
3
  import {
4
- BLOCK_HAS_RUN,
5
- BRANCH_BLOCK,
6
- DERIVED,
7
- CONTAINS_TEARDOWN,
8
- DESTROYED,
9
- EFFECT_BLOCK,
10
- PAUSED,
11
- RENDER_BLOCK,
12
- ROOT_BLOCK,
13
- TRY_BLOCK,
4
+ BLOCK_HAS_RUN,
5
+ BRANCH_BLOCK,
6
+ DERIVED,
7
+ CONTAINS_TEARDOWN,
8
+ DESTROYED,
9
+ EFFECT_BLOCK,
10
+ PAUSED,
11
+ RENDER_BLOCK,
12
+ ROOT_BLOCK,
13
+ TRY_BLOCK,
14
+ HEAD_BLOCK,
14
15
  } from './constants.js';
15
16
  import { next_sibling } from './operations.js';
16
17
  import { apply_element_spread } from './render.js';
17
18
  import {
18
- active_block,
19
- active_component,
20
- active_reaction,
21
- is_block_dirty,
22
- run_block,
23
- run_teardown,
24
- schedule_update,
19
+ active_block,
20
+ active_component,
21
+ active_reaction,
22
+ is_block_dirty,
23
+ run_block,
24
+ run_teardown,
25
+ schedule_update,
25
26
  } from './runtime.js';
26
27
  import { suspend } from './try.js';
27
28
 
@@ -29,32 +30,32 @@ import { suspend } from './try.js';
29
30
  * @param {Function} fn
30
31
  */
31
32
  export function user_effect(fn) {
32
- if (active_block === null) {
33
- throw new Error(
34
- 'effect() must be called within an active context, such as a component or effect',
35
- );
36
- }
37
-
38
- var component = active_component;
39
- if (component !== null && !component.m) {
40
- var e = (component.e ??= []);
41
- e.push({
42
- b: active_block,
43
- fn,
44
- r: active_reaction,
45
- });
46
-
47
- return;
48
- }
49
-
50
- return block(EFFECT_BLOCK, fn);
33
+ if (active_block === null) {
34
+ throw new Error(
35
+ 'effect() must be called within an active context, such as a component or effect',
36
+ );
37
+ }
38
+
39
+ var component = active_component;
40
+ if (component !== null && !component.m) {
41
+ var e = (component.e ??= []);
42
+ e.push({
43
+ b: active_block,
44
+ fn,
45
+ r: active_reaction,
46
+ });
47
+
48
+ return;
49
+ }
50
+
51
+ return block(EFFECT_BLOCK, fn);
51
52
  }
52
53
 
53
54
  /**
54
55
  * @param {Function} fn
55
56
  */
56
57
  export function effect(fn) {
57
- return block(EFFECT_BLOCK, fn);
58
+ return block(EFFECT_BLOCK, fn);
58
59
  }
59
60
 
60
61
  /**
@@ -62,7 +63,7 @@ export function effect(fn) {
62
63
  * @param {number} [flags]
63
64
  */
64
65
  export function render(fn, flags = 0) {
65
- return block(RENDER_BLOCK | flags, fn);
66
+ return block(RENDER_BLOCK | flags, fn);
66
67
  }
67
68
 
68
69
  /**
@@ -71,7 +72,7 @@ export function render(fn, flags = 0) {
71
72
  * @param {number} [flags]
72
73
  */
73
74
  export function render_spread(element, fn, flags = 0) {
74
- return block(RENDER_BLOCK | flags, apply_element_spread(element, fn));
75
+ return block(RENDER_BLOCK | flags, apply_element_spread(element, fn));
75
76
  }
76
77
 
77
78
  /**
@@ -79,18 +80,18 @@ export function render_spread(element, fn, flags = 0) {
79
80
  * @param {number} [flags]
80
81
  */
81
82
  export function branch(fn, flags = 0) {
82
- return block(BRANCH_BLOCK | flags, fn);
83
+ return block(BRANCH_BLOCK | flags, fn);
83
84
  }
84
85
 
85
86
  /**
86
87
  * @param {() => any} fn
87
88
  */
88
89
  export function async(fn) {
89
- return block(BRANCH_BLOCK, async () => {
90
- const unsuspend = suspend();
91
- await fn();
92
- unsuspend();
93
- });
90
+ return block(BRANCH_BLOCK, async () => {
91
+ const unsuspend = suspend();
92
+ await fn();
93
+ unsuspend();
94
+ });
94
95
  }
95
96
 
96
97
  /**
@@ -99,27 +100,27 @@ export function async(fn) {
99
100
  * @returns {Block}
100
101
  */
101
102
  export function ref(element, get_fn) {
102
- /** @type {(element: Element) => (void | (() => void) | undefined)} */
103
- var ref_fn;
104
- /** @type {Block | null} */
105
- var e;
106
-
107
- return block(RENDER_BLOCK, () => {
108
- if (ref_fn !== (ref_fn = get_fn())) {
109
- if (e) {
110
- destroy_block(e);
111
- e = null;
112
- }
113
-
114
- if (ref_fn) {
115
- e = branch(() => {
116
- effect(() => {
117
- return ref_fn(element);
118
- });
119
- });
120
- }
121
- }
122
- });
103
+ /** @type {(element: Element) => (void | (() => void) | undefined)} */
104
+ var ref_fn;
105
+ /** @type {Block | null} */
106
+ var e;
107
+
108
+ return block(RENDER_BLOCK, () => {
109
+ if (ref_fn !== (ref_fn = get_fn())) {
110
+ if (e) {
111
+ destroy_block(e);
112
+ e = null;
113
+ }
114
+
115
+ if (ref_fn) {
116
+ e = branch(() => {
117
+ effect(() => {
118
+ return ref_fn(element);
119
+ });
120
+ });
121
+ }
122
+ }
123
+ });
123
124
  }
124
125
 
125
126
  /**
@@ -127,7 +128,7 @@ export function ref(element, get_fn) {
127
128
  * @returns {Block}
128
129
  */
129
130
  export function root(fn) {
130
- return block(ROOT_BLOCK, fn);
131
+ return block(ROOT_BLOCK, fn);
131
132
  }
132
133
 
133
134
  /**
@@ -136,7 +137,7 @@ export function root(fn) {
136
137
  * @returns {Block}
137
138
  */
138
139
  export function create_try_block(fn, state) {
139
- return block(TRY_BLOCK, fn, state);
140
+ return block(TRY_BLOCK, fn, state);
140
141
  }
141
142
 
142
143
  /**
@@ -144,14 +145,14 @@ export function create_try_block(fn, state) {
144
145
  * @param {Block} parent_block
145
146
  */
146
147
  function push_block(block, parent_block) {
147
- var parent_last = parent_block.last;
148
- if (parent_last === null) {
149
- parent_block.last = parent_block.first = block;
150
- } else {
151
- parent_last.next = block;
152
- block.prev = parent_last;
153
- parent_block.last = block;
154
- }
148
+ var parent_last = parent_block.last;
149
+ if (parent_last === null) {
150
+ parent_block.last = parent_block.first = block;
151
+ } else {
152
+ parent_last.next = block;
153
+ block.prev = parent_last;
154
+ parent_block.last = block;
155
+ }
155
156
  }
156
157
 
157
158
  /**
@@ -161,37 +162,38 @@ function push_block(block, parent_block) {
161
162
  * @returns {Block}
162
163
  */
163
164
  export function block(flags, fn, state = null) {
164
- /** @type {Block} */
165
- var block = {
166
- co: active_component,
167
- d: null,
168
- first: null,
169
- f: flags,
170
- fn,
171
- last: null,
172
- next: null,
173
- p: active_block,
174
- prev: null,
175
- s: state,
176
- t: null,
177
- };
178
-
179
- if (active_reaction !== null && (active_reaction.f & DERIVED) !== 0) {
180
- /** @type {Derived} */ ((active_reaction).blocks ??= []).push(block);
181
- }
182
-
183
- if (active_block !== null) {
184
- push_block(block, active_block);
185
- }
186
-
187
- if ((flags & EFFECT_BLOCK) !== 0) {
188
- schedule_update(block);
189
- } else {
190
- run_block(block);
191
- block.f ^= BLOCK_HAS_RUN;
192
- }
193
-
194
- return block;
165
+ /** @type {Block} */
166
+ var block = {
167
+ co: active_component,
168
+ d: null,
169
+ first: null,
170
+ f: flags,
171
+ fn,
172
+ last: null,
173
+ next: null,
174
+ p: active_block,
175
+ prev: null,
176
+ s: state,
177
+ t: null,
178
+ };
179
+
180
+ if (active_reaction !== null && (active_reaction.f & DERIVED) !== 0) {
181
+ /* prettier-ignore */
182
+ (/** @type {Derived} */ (active_reaction).blocks ??= []).push(block);
183
+ }
184
+
185
+ if (active_block !== null) {
186
+ push_block(block, active_block);
187
+ }
188
+
189
+ if ((flags & EFFECT_BLOCK) !== 0) {
190
+ schedule_update(block);
191
+ } else {
192
+ run_block(block);
193
+ block.f ^= BLOCK_HAS_RUN;
194
+ }
195
+
196
+ return block;
195
197
  }
196
198
 
197
199
  /**
@@ -199,16 +201,16 @@ export function block(flags, fn, state = null) {
199
201
  * @param {boolean} [remove_dom]
200
202
  */
201
203
  export function destroy_block_children(parent, remove_dom = false) {
202
- var block = parent.first;
203
- parent.first = parent.last = null;
204
-
205
- if ((parent.f & CONTAINS_TEARDOWN) !== 0) {
206
- while (block !== null) {
207
- var next = block.next;
208
- destroy_block(block, remove_dom);
209
- block = next;
210
- }
211
- }
204
+ var block = parent.first;
205
+ parent.first = parent.last = null;
206
+
207
+ if ((parent.f & CONTAINS_TEARDOWN) !== 0) {
208
+ while (block !== null) {
209
+ var next = block.next;
210
+ destroy_block(block, remove_dom);
211
+ block = next;
212
+ }
213
+ }
212
214
  }
213
215
 
214
216
  /**
@@ -216,82 +218,82 @@ export function destroy_block_children(parent, remove_dom = false) {
216
218
  * @param {boolean} [remove_dom]
217
219
  */
218
220
  export function destroy_non_branch_children(parent, remove_dom = false) {
219
- var block = parent.first;
220
-
221
- if (
222
- (parent.f & CONTAINS_TEARDOWN) === 0 &&
223
- parent.first !== null &&
224
- (parent.first.f & BRANCH_BLOCK) === 0
225
- ) {
226
- parent.first = parent.last = null;
227
- } else {
228
- while (block !== null) {
229
- var next = block.next;
230
- if ((block.f & BRANCH_BLOCK) === 0) {
231
- destroy_block(block, remove_dom);
232
- }
233
- block = next;
234
- }
235
- }
221
+ var block = parent.first;
222
+
223
+ if (
224
+ (parent.f & CONTAINS_TEARDOWN) === 0 &&
225
+ parent.first !== null &&
226
+ (parent.first.f & BRANCH_BLOCK) === 0
227
+ ) {
228
+ parent.first = parent.last = null;
229
+ } else {
230
+ while (block !== null) {
231
+ var next = block.next;
232
+ if ((block.f & BRANCH_BLOCK) === 0) {
233
+ destroy_block(block, remove_dom);
234
+ }
235
+ block = next;
236
+ }
237
+ }
236
238
  }
237
239
 
238
240
  /**
239
241
  * @param {Block} block
240
242
  */
241
243
  export function unlink_block(block) {
242
- var parent = block.p;
243
- var prev = block.prev;
244
- var next = block.next;
244
+ var parent = block.p;
245
+ var prev = block.prev;
246
+ var next = block.next;
245
247
 
246
- if (prev !== null) prev.next = next;
247
- if (next !== null) next.prev = prev;
248
+ if (prev !== null) prev.next = next;
249
+ if (next !== null) next.prev = prev;
248
250
 
249
- if (parent !== null) {
250
- if (parent.first === block) parent.first = next;
251
- if (parent.last === block) parent.last = prev;
252
- }
251
+ if (parent !== null) {
252
+ if (parent.first === block) parent.first = next;
253
+ if (parent.last === block) parent.last = prev;
254
+ }
253
255
  }
254
256
 
255
257
  /**
256
258
  * @param {Block} block
257
259
  */
258
260
  export function pause_block(block) {
259
- if ((block.f & PAUSED) !== 0) {
260
- return;
261
- }
262
- block.f ^= PAUSED;
261
+ if ((block.f & PAUSED) !== 0) {
262
+ return;
263
+ }
264
+ block.f ^= PAUSED;
263
265
 
264
- var child = block.first;
266
+ var child = block.first;
265
267
 
266
- while (child !== null) {
267
- var next = child.next;
268
- pause_block(child);
269
- child = next;
270
- }
268
+ while (child !== null) {
269
+ var next = child.next;
270
+ pause_block(child);
271
+ child = next;
272
+ }
271
273
 
272
- run_teardown(block);
274
+ run_teardown(block);
273
275
  }
274
276
 
275
277
  /**
276
278
  * @param {Block} block
277
279
  */
278
280
  export function resume_block(block) {
279
- if ((block.f & PAUSED) === 0) {
280
- return;
281
- }
282
- block.f ^= PAUSED;
283
-
284
- if (is_block_dirty(block)) {
285
- schedule_update(block);
286
- }
287
-
288
- var child = block.first;
289
-
290
- while (child !== null) {
291
- var next = child.next;
292
- resume_block(child);
293
- child = next;
294
- }
281
+ if ((block.f & PAUSED) === 0) {
282
+ return;
283
+ }
284
+ block.f ^= PAUSED;
285
+
286
+ if (is_block_dirty(block)) {
287
+ schedule_update(block);
288
+ }
289
+
290
+ var child = block.first;
291
+
292
+ while (child !== null) {
293
+ var next = child.next;
294
+ resume_block(child);
295
+ child = next;
296
+ }
295
297
  }
296
298
 
297
299
  /**
@@ -299,21 +301,21 @@ export function resume_block(block) {
299
301
  * @returns {boolean}
300
302
  */
301
303
  export function is_destroyed(target_block) {
302
- /** @type {Block | null} */
303
- var block = target_block;
304
-
305
- while (block !== null) {
306
- var flags = block.f;
307
-
308
- if ((flags & DESTROYED) !== 0) {
309
- return true;
310
- }
311
- if ((flags & ROOT_BLOCK) !== 0) {
312
- return false;
313
- }
314
- block = block.p;
315
- }
316
- return true;
304
+ /** @type {Block | null} */
305
+ var block = target_block;
306
+
307
+ while (block !== null) {
308
+ var flags = block.f;
309
+
310
+ if ((flags & DESTROYED) !== 0) {
311
+ return true;
312
+ }
313
+ if ((flags & ROOT_BLOCK) !== 0) {
314
+ return false;
315
+ }
316
+ block = block.p;
317
+ }
318
+ return true;
317
319
  }
318
320
 
319
321
  /**
@@ -321,34 +323,36 @@ export function is_destroyed(target_block) {
321
323
  * @param {boolean} [remove_dom]
322
324
  */
323
325
  export function destroy_block(block, remove_dom = true) {
324
- block.f ^= DESTROYED;
326
+ block.f ^= DESTROYED;
325
327
 
326
- var removed = false;
328
+ var removed = false;
329
+ var f = block.f;
327
330
 
328
- if (remove_dom && (block.f & (BRANCH_BLOCK | ROOT_BLOCK)) !== 0) {
329
- var node = block.s.start;
330
- var end = block.s.end;
331
+ if ((remove_dom && (f & (BRANCH_BLOCK | ROOT_BLOCK)) !== 0) || (f & HEAD_BLOCK) !== 0) {
332
+ var s = block.s;
333
+ var node = s.start;
334
+ var end = s.end;
331
335
 
332
- while (node !== null) {
333
- var next = node === end ? null : next_sibling(node);
336
+ while (node !== null) {
337
+ var next = node === end ? null : next_sibling(node);
334
338
 
335
- node.remove();
336
- node = next;
337
- }
339
+ node.remove();
340
+ node = next;
341
+ }
338
342
 
339
- removed = true;
340
- }
343
+ removed = true;
344
+ }
341
345
 
342
- destroy_block_children(block, remove_dom && !removed);
346
+ destroy_block_children(block, remove_dom && !removed);
343
347
 
344
- run_teardown(block);
348
+ run_teardown(block);
345
349
 
346
- var parent = block.p;
350
+ var parent = block.p;
347
351
 
348
- // If the parent doesn't have any children, then skip this work altogether
349
- if (parent !== null && parent.first !== null) {
350
- unlink_block(block);
351
- }
352
+ // If the parent doesn't have any children, then skip this work altogether
353
+ if (parent !== null && parent.first !== null) {
354
+ unlink_block(block);
355
+ }
352
356
 
353
- block.fn = block.s = block.d = block.p = null;
357
+ block.fn = block.s = block.d = block.p = block.d = block.co = block.t = null;
354
358
  }
@@ -1,9 +1,18 @@
1
+ /** @import { Block, Component } from '#client' */
2
+
1
3
  import { branch, destroy_block, render } from './blocks.js';
2
4
  import { COMPOSITE_BLOCK } from './constants.js';
3
5
  import { active_block } from './runtime.js';
4
6
 
7
+ /**
8
+ * @param {() => (anchor: Node, props: Record<string, any>, block: Block | null) => void} get_component
9
+ * @param {Node} node
10
+ * @param {Record<string, any>} props
11
+ * @returns {void}
12
+ */
5
13
  export function composite(get_component, node, props) {
6
14
  var anchor = node;
15
+ /** @type {Block | null} */
7
16
  var b = null;
8
17
 
9
18
  render(() => {