Skip to content

Commit 78dd1e2

Browse files
committed
DRY
1 parent ed17212 commit 78dd1e2

File tree

3 files changed

+20
-35
lines changed

3 files changed

+20
-35
lines changed

packages/svelte/src/internal/client/dom/blocks/async.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { async_derived } from '../../reactivity/deriveds.js';
44
import { current_batch } from '../../reactivity/batch.js';
55
import { active_effect, schedule_effect } from '../../runtime.js';
6-
import { capture } from './boundary.js';
6+
import { capture, get_pending_boundary } from './boundary.js';
77

88
/**
99
* @param {TemplateNode} node
@@ -15,19 +15,10 @@ export function async(node, expressions, fn) {
1515

1616
var batch = /** @type {Batch} */ (current_batch);
1717
var effect = /** @type {Effect} */ (active_effect);
18+
var boundary = get_pending_boundary(effect);
1819

1920
var restore = capture();
2021

21-
let boundary = effect.b;
22-
23-
while (boundary !== null && !boundary.has_pending_snippet()) {
24-
boundary = boundary.parent;
25-
}
26-
27-
if (boundary === null) {
28-
throw new Error('TODO cannot create async derived outside a boundary with a pending snippet');
29-
}
30-
3122
boundary.increment();
3223

3324
Promise.all(expressions.map((fn) => async_derived(fn))).then((result) => {

packages/svelte/src/internal/client/dom/blocks/boundary.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,21 @@ function move_effect(effect, fragment) {
327327
}
328328
}
329329

330+
/** @param {Effect} effect */
331+
export function get_pending_boundary(effect) {
332+
let boundary = effect.b;
333+
334+
while (boundary !== null && !boundary.has_pending_snippet()) {
335+
boundary = boundary.parent;
336+
}
337+
338+
if (boundary === null) {
339+
e.await_outside_boundary();
340+
}
341+
342+
return boundary;
343+
}
344+
330345
export function capture(track = true) {
331346
var previous_effect = active_effect;
332347
var previous_reaction = active_reaction;
@@ -352,20 +367,7 @@ export function capture(track = true) {
352367

353368
// TODO we should probably be incrementing the current batch, not the boundary?
354369
export function suspend() {
355-
let boundary = /** @type {Effect} */ (active_effect).b;
356-
357-
while (boundary !== null) {
358-
// TODO pretty sure this is wrong
359-
if (boundary.has_pending_snippet()) {
360-
break;
361-
}
362-
363-
boundary = boundary.parent;
364-
}
365-
366-
if (boundary === null) {
367-
e.await_outside_boundary();
368-
}
370+
let boundary = get_pending_boundary(/** @type {Effect} */ (active_effect));
369371

370372
boundary.increment();
371373

packages/svelte/src/internal/client/reactivity/deriveds.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { destroy_effect, render_effect } from './effects.js';
2929
import { inspect_effects, internal_set, set_inspect_effects, source } from './sources.js';
3030
import { get_stack } from '../dev/tracing.js';
3131
import { tracing_mode_flag } from '../../flags/index.js';
32-
import { capture } from '../dom/blocks/boundary.js';
32+
import { capture, get_pending_boundary } from '../dom/blocks/boundary.js';
3333
import { component_context } from '../context.js';
3434
import { UNINITIALIZED } from '../../../constants.js';
3535
import { current_batch } from './batch.js';
@@ -101,15 +101,7 @@ export function async_derived(fn, location) {
101101
throw new Error('TODO cannot create unowned async derived');
102102
}
103103

104-
let boundary = parent.b;
105-
106-
while (boundary !== null && !boundary.has_pending_snippet()) {
107-
boundary = boundary.parent;
108-
}
109-
110-
if (boundary === null) {
111-
throw new Error('TODO cannot create async derived outside a boundary with a pending snippet');
112-
}
104+
let boundary = get_pending_boundary(parent);
113105

114106
var promise = /** @type {Promise<V>} */ (/** @type {unknown} */ (undefined));
115107
var signal = source(/** @type {V} */ (UNINITIALIZED));

0 commit comments

Comments
 (0)