Skip to content

Commit 74b7f89

Browse files
committed
move some code
1 parent 2f227b1 commit 74b7f89

File tree

3 files changed

+82
-68
lines changed

3 files changed

+82
-68
lines changed

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

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -366,69 +366,6 @@ export function get_pending_boundary() {
366366
return boundary;
367367
}
368368

369-
/**
370-
* Captures the current effect context so that we can restore it after
371-
* some asynchronous work has happened if `track` is true (so that e.g.
372-
* `await a + b` causes `b` to be registered as a dependency).
373-
*
374-
* If `track` is false, we just take a note of which async derived
375-
* brought us here, so that we can emit a `async_reactivity_loss`
376-
* warning when it's appropriate to do so.
377-
*
378-
* @param {boolean} track
379-
*/
380-
export function capture(track = true) {
381-
var previous_effect = active_effect;
382-
var previous_reaction = active_reaction;
383-
var previous_component_context = component_context;
384-
385-
if (DEV && !track) {
386-
var previous_async_effect = current_async_effect;
387-
}
388-
389-
return function restore() {
390-
if (track) {
391-
set_active_effect(previous_effect);
392-
set_active_reaction(previous_reaction);
393-
set_component_context(previous_component_context);
394-
}
395-
396-
if (DEV) {
397-
set_from_async_derived(track ? null : previous_async_effect);
398-
}
399-
400-
// prevent the active effect from outstaying its welcome
401-
// TODO this feels brittle
402-
queue_micro_task(exit);
403-
};
404-
}
405-
406-
/**
407-
* Wraps an `await` expression in such a way that the effect context that was
408-
* active before the expression evaluated can be reapplied afterwards —
409-
* `await a + b` becomes `(await $.save(a))() + b`
410-
* @template T
411-
* @param {Promise<T>} promise
412-
* @param {boolean} [track]
413-
* @returns {Promise<() => T>}
414-
*/
415-
export async function save(promise, track = true) {
416-
var restore = capture(track);
417-
var value = await promise;
418-
419-
return () => {
420-
restore();
421-
return value;
422-
};
423-
}
424-
425-
function exit() {
426-
set_active_effect(null);
427-
set_active_reaction(null);
428-
set_component_context(null);
429-
if (DEV) set_from_async_derived(null);
430-
}
431-
432369
export function pending() {
433370
if (active_effect === null) {
434371
e.effect_pending_outside_reaction();

packages/svelte/src/internal/client/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export {
9898
props_id,
9999
with_script
100100
} from './dom/template.js';
101+
export { save } from './reactivity/async.js';
101102
export { flushSync as flush, suspend } from './reactivity/batch.js';
102103
export {
103104
async_derived,
@@ -136,7 +137,7 @@ export {
136137
update_store,
137138
mark_store_binding
138139
} from './reactivity/store.js';
139-
export { boundary, pending, save } from './dom/blocks/boundary.js';
140+
export { boundary, pending } from './dom/blocks/boundary.js';
140141
export { set_text } from './render.js';
141142
export {
142143
get,

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

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
/** @import { Effect, Value } from '#client' */
22

33
import { DESTROYED } from '#client/constants';
4-
import { is_runes } from '../context.js';
5-
import { capture, get_pending_boundary } from '../dom/blocks/boundary.js';
4+
import { DEV } from 'esm-env';
5+
import { component_context, is_runes, set_component_context } from '../context.js';
6+
import { get_pending_boundary } from '../dom/blocks/boundary.js';
67
import { invoke_error_boundary } from '../error-handling.js';
7-
import { active_effect } from '../runtime.js';
8+
import {
9+
active_effect,
10+
active_reaction,
11+
set_active_effect,
12+
set_active_reaction
13+
} from '../runtime.js';
814
import { current_batch } from './batch.js';
9-
import { async_derived, derived, derived_safe_equal } from './deriveds.js';
15+
import {
16+
async_derived,
17+
current_async_effect,
18+
derived,
19+
derived_safe_equal,
20+
set_from_async_derived
21+
} from './deriveds.js';
22+
import { queue_micro_task } from '../dom/task.js';
1023

1124
/**
1225
*
@@ -49,3 +62,66 @@ export function flatten(sync, async, fn) {
4962
boundary.error(error);
5063
});
5164
}
65+
66+
/**
67+
* Captures the current effect context so that we can restore it after
68+
* some asynchronous work has happened if `track` is true (so that e.g.
69+
* `await a + b` causes `b` to be registered as a dependency).
70+
*
71+
* If `track` is false, we just take a note of which async derived
72+
* brought us here, so that we can emit a `async_reactivity_loss`
73+
* warning when it's appropriate to do so.
74+
*
75+
* @param {boolean} track
76+
*/
77+
export function capture(track = true) {
78+
var previous_effect = active_effect;
79+
var previous_reaction = active_reaction;
80+
var previous_component_context = component_context;
81+
82+
if (DEV && !track) {
83+
var previous_async_effect = current_async_effect;
84+
}
85+
86+
return function restore() {
87+
if (track) {
88+
set_active_effect(previous_effect);
89+
set_active_reaction(previous_reaction);
90+
set_component_context(previous_component_context);
91+
}
92+
93+
if (DEV) {
94+
set_from_async_derived(track ? null : previous_async_effect);
95+
}
96+
97+
// prevent the active effect from outstaying its welcome
98+
// TODO this feels brittle
99+
queue_micro_task(exit);
100+
};
101+
}
102+
103+
/**
104+
* Wraps an `await` expression in such a way that the effect context that was
105+
* active before the expression evaluated can be reapplied afterwards —
106+
* `await a + b` becomes `(await $.save(a))() + b`
107+
* @template T
108+
* @param {Promise<T>} promise
109+
* @param {boolean} [track]
110+
* @returns {Promise<() => T>}
111+
*/
112+
export async function save(promise, track = true) {
113+
var restore = capture(track);
114+
var value = await promise;
115+
116+
return () => {
117+
restore();
118+
return value;
119+
};
120+
}
121+
122+
function exit() {
123+
set_active_effect(null);
124+
set_active_reaction(null);
125+
set_component_context(null);
126+
if (DEV) set_from_async_derived(null);
127+
}

0 commit comments

Comments
 (0)