MCPcopy
hub / github.com/sveltejs/svelte / flatten

Function flatten

packages/svelte/src/internal/client/reactivity/async.js:36–115  ·  view source on GitHub ↗
(blockers, sync, async, fn)

Source from the content-addressed store, hash-verified

34 * @param {(values: Value[]) => any} fn
35 */
36export function flatten(blockers, sync, async, fn) {
37 const d = is_runes() ? derived : derived_safe_equal;
38
39 // Filter out already-settled blockers - no need to wait for them
40 var pending = blockers.filter((b) => !b.settled);
41
42 var deriveds = sync.map(d);
43
44 if (DEV) {
45 deriveds.forEach((d, i) => {
46 // TODO this is kinda useful for debugging but a lousy implementation —
47 // maybe the compiler could pass through the template string
48 d.label = sync[i]
49 .toString()
50 .replace('() => ', '')
51 .replaceAll('$.eager(() => ', '$state.eager(')
52 .replace(/\$\.get\((.+?)\)/g, (_, id) => id);
53 });
54 }
55
56 if (async.length === 0 && pending.length === 0) {
57 fn(deriveds);
58 return;
59 }
60
61 var parent = /** @type {Effect} */ (active_effect);
62
63 var restore = capture();
64 var blocker_promise =
65 pending.length === 1
66 ? pending[0].promise
67 : pending.length > 1
68 ? Promise.all(pending.map((b) => b.promise))
69 : null;
70
71 /**
72 * @param {Source[]} async
73 */
74 function finish(async) {
75 if ((parent.f & DESTROYED) !== 0) {
76 return;
77 }
78
79 restore();
80
81 try {
82 fn([...deriveds, ...async]);
83 } catch (error) {
84 invoke_error_boundary(error, parent);
85 }
86
87 unset_context();
88 }
89
90 var decrement_pending = increment_pending();
91
92 // Fast path: blockers but no async expressions
93 if (async.length === 0) {

Callers 5

asyncFunction · 0.90
attribute_effectFunction · 0.90
template_effectFunction · 0.90
deferred_template_effectFunction · 0.90
run_after_blockersFunction · 0.85

Calls 9

is_runesFunction · 0.90
captureFunction · 0.85
increment_pendingFunction · 0.85
finishFunction · 0.85
unset_contextFunction · 0.85
fnFunction · 0.70
runFunction · 0.70
forEachMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected