(blockers, sync, async, fn)
| 34 | * @param {(values: Value[]) => any} fn |
| 35 | */ |
| 36 | export 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) { |
no test coverage detected