Skip to content

Commit 09b53f5

Browse files
committed
tidy up server exports
1 parent 76329e4 commit 09b53f5

File tree

13 files changed

+145
-39
lines changed

13 files changed

+145
-39
lines changed

packages/svelte/src/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export const TRANSITION_GLOBAL = 1 << 2;
1919
export const TEMPLATE_FRAGMENT = 1;
2020
export const TEMPLATE_USE_IMPORT_NODE = 1 << 1;
2121

22+
export const UNINITIALIZED = Symbol();
23+
2224
/** List of Element events that will be delegated */
2325
export const DelegatedEvents = [
2426
'beforeinput',

packages/svelte/src/index-server.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { current_component_context } from './internal/client/runtime.js';
1+
import { current_component } from './internal/server/context.js';
22
import { noop } from './internal/common.js';
33

4-
export { getAllContexts, getContext, hasContext, setContext } from './index-client.js';
5-
64
/** @param {() => void} fn */
75
export function onDestroy(fn) {
8-
const context = /** @type {import('#client').ComponentContext} */ (current_component_context);
9-
(context.ondestroy ??= []).push(fn);
6+
var context = /** @type {import('#server').Component} */ (current_component);
7+
(context.d ??= []).push(fn);
108
}
119

1210
export {
@@ -44,3 +42,5 @@ export function unstate(value) {
4442
// There's no signals/proxies on the server, so just return the value
4543
return value;
4644
}
45+
46+
export { getAllContexts, getContext, hasContext, setContext } from './internal/server/context.js';

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ export const DESTROYED = 1 << 12;
1313
export const IS_ELSEIF = 1 << 13;
1414
export const EFFECT_RAN = 1 << 14;
1515

16-
export const UNINITIALIZED = Symbol();
1716
export const STATE_SYMBOL = Symbol('$state');

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { UNINITIALIZED } from '../../constants.js';
1+
import { UNINITIALIZED } from '../../../../constants.js';
22
import { block, branch, pause_effect } from '../../reactivity/effects.js';
33
import { safe_not_equal } from '../../reactivity/equality.js';
44

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import {
1313
} from './utils.js';
1414
import { add_owner, check_ownership, strip_owner } from './dev/ownership.js';
1515
import { mutable_source, source, set } from './reactivity/sources.js';
16-
import { STATE_SYMBOL, UNINITIALIZED } from './constants.js';
16+
import { STATE_SYMBOL } from './constants.js';
1717
import { updating_derived } from './reactivity/deriveds.js';
18+
import { UNINITIALIZED } from '../../constants.js';
1819

1920
/**
2021
* @template T

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
current_effect,
77
current_untracked_writes,
88
current_untracking,
9-
flush_sync,
109
get,
1110
is_batching_effect,
1211
is_runes,
@@ -18,7 +17,8 @@ import {
1817
untrack
1918
} from '../runtime.js';
2019
import { equals, safe_equals } from './equality.js';
21-
import { CLEAN, DERIVED, DIRTY, BRANCH_EFFECT, UNINITIALIZED } from '../constants.js';
20+
import { CLEAN, DERIVED, DIRTY, BRANCH_EFFECT } from '../constants.js';
21+
import { UNINITIALIZED } from '../../../constants.js';
2222

2323
/**
2424
* @template V

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { subscribe_to_store } from '../../../store/utils.js';
22
import { noop } from '../../common.js';
3-
import { UNINITIALIZED } from '../constants.js';
3+
import { UNINITIALIZED } from '../../../constants.js';
44
import { get, untrack } from '../runtime.js';
55
import { effect } from './effects.js';
66
import { mutable_source, set } from './sources.js';

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,8 +1055,7 @@ export function push(props, runes = false, fn) {
10551055
l1: [],
10561056
l2: source(false),
10571057
// update_callbacks
1058-
u: null,
1059-
ondestroy: null
1058+
u: null
10601059
};
10611060

10621061
if (DEV) {
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import { DEV } from 'esm-env';
2+
import { on_destroy } from './index.js';
3+
4+
/** @type {import('#server').Component | null} */
5+
export var current_component = null;
6+
7+
/**
8+
* Retrieves the context that belongs to the closest parent component with the specified `key`.
9+
* Must be called during component initialisation.
10+
*
11+
* https://svelte.dev/docs/svelte#getcontext
12+
* @template T
13+
* @param {any} key
14+
* @returns {T}
15+
*/
16+
export function getContext(key) {
17+
const context_map = getAllContexts();
18+
const result = /** @type {T} */ (context_map.get(key));
19+
20+
return result;
21+
}
22+
23+
/**
24+
* Associates an arbitrary `context` object with the current component and the specified `key`
25+
* and returns that object. The context is then available to children of the component
26+
* (including slotted content) with `getContext`.
27+
*
28+
* Like lifecycle functions, this must be called during component initialisation.
29+
*
30+
* https://svelte.dev/docs/svelte#setcontext
31+
* @template T
32+
* @param {any} key
33+
* @param {T} context
34+
* @returns {T}
35+
*/
36+
export function setContext(key, context) {
37+
getAllContexts().set(key, context);
38+
return context;
39+
}
40+
41+
/**
42+
* Checks whether a given `key` has been set in the context of a parent component.
43+
* Must be called during component initialisation.
44+
*
45+
* https://svelte.dev/docs/svelte#hascontext
46+
* @param {any} key
47+
* @returns {boolean}
48+
*/
49+
export function hasContext(key) {
50+
return getAllContexts().has(key);
51+
}
52+
53+
/**
54+
* Retrieves the whole context map that belongs to the closest parent component.
55+
* Must be called during component initialisation. Useful, for example, if you
56+
* programmatically create a component and want to pass the existing context to it.
57+
*
58+
* https://svelte.dev/docs/svelte#getallcontexts
59+
* @returns {Map<any, any>}
60+
*/
61+
export function getAllContexts() {
62+
const context = current_component;
63+
64+
if (context === null) {
65+
throw new Error(
66+
'ERR_SVELTE_ORPHAN_CONTEXT' +
67+
(DEV ? 'Context can only be used during component initialisation.' : '')
68+
);
69+
}
70+
71+
return (context.c ??= new Map(get_parent_context(context) || undefined));
72+
}
73+
74+
export function push() {
75+
current_component = {
76+
p: current_component,
77+
c: null,
78+
d: null
79+
};
80+
}
81+
82+
export function pop() {
83+
var component = /** @type {import('#server').Component} */ (current_component);
84+
85+
var ondestroy = component.d;
86+
87+
if (ondestroy) {
88+
on_destroy.push(...ondestroy);
89+
}
90+
91+
current_component = component.p;
92+
}
93+
94+
/**
95+
* @param {import('#server').Component} component_context
96+
* @returns {Map<unknown, unknown> | null}
97+
*/
98+
function get_parent_context(component_context) {
99+
let parent = component_context.p;
100+
101+
while (parent !== null) {
102+
const context_map = parent.c;
103+
if (context_map !== null) {
104+
return context_map;
105+
}
106+
parent = parent.p;
107+
}
108+
109+
return null;
110+
}

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

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as $ from '../client/runtime.js';
21
import { is_promise, noop } from '../common.js';
32
import { subscribe_to_store } from '../../store/utils.js';
43
import {
@@ -8,7 +7,8 @@ import {
87
is_tag_valid_with_parent
98
} from '../../constants.js';
109
import { DEV } from 'esm-env';
11-
import { UNINITIALIZED } from '../client/constants.js';
10+
import { UNINITIALIZED } from '../../constants.js';
11+
import { current_component, pop, push } from './context.js';
1212

1313
export * from '../client/validate.js';
1414

@@ -192,14 +192,16 @@ export function render(component, options) {
192192
payload.out += '<![>';
193193

194194
if (options.context) {
195-
$.push({});
196-
/** @type {import('../client/types.js').ComponentContext} */ ($.current_component_context).c =
197-
options.context;
195+
push();
196+
/** @type {import('#server').Component} */ (current_component).c = options.context;
198197
}
198+
199199
component(payload, options.props, {}, {});
200+
200201
if (options.context) {
201-
$.pop();
202+
pop();
202203
}
204+
203205
payload.out += '<!]>';
204206
for (const cleanup of on_destroy) cleanup();
205207
on_destroy = prev_on_destroy;
@@ -213,24 +215,6 @@ export function render(component, options) {
213215
};
214216
}
215217

216-
/**
217-
* @param {boolean} runes
218-
*/
219-
export function push(runes) {
220-
$.push({}, runes);
221-
}
222-
223-
export function pop() {
224-
var context = /** @type {import('#client').ComponentContext} */ ($.current_component_context);
225-
var ondestroy = context.ondestroy;
226-
227-
if (ondestroy) {
228-
on_destroy.push(...ondestroy);
229-
}
230-
231-
$.pop();
232-
}
233-
234218
/**
235219
* @template V
236220
* @param {V} value
@@ -672,3 +656,5 @@ export function once(get_value) {
672656
return value;
673657
};
674658
}
659+
660+
export { push, pop } from './context.js';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export interface Component {
2+
/** parent */
3+
p: null | Component;
4+
/** context */
5+
c: null | Map<unknown, unknown>;
6+
/** ondestroy */
7+
d: null | Array<() => void>;
8+
}

packages/svelte/src/reactivity/map.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { DEV } from 'esm-env';
22
import { source, set } from '../internal/client/reactivity/sources.js';
33
import { get } from '../internal/client/runtime.js';
4-
import { UNINITIALIZED } from '../internal/client/constants.js';
4+
import { UNINITIALIZED } from '../constants.js';
55
import { map } from './utils.js';
66

77
/**

packages/svelte/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"svelte/server": ["./src/server/index.js"],
2626
"svelte/store": ["./src/store/public.d.ts"],
2727
"#compiler": ["./src/compiler/types/index.d.ts"],
28-
"#client": ["./src/internal/client/types.d.ts"]
28+
"#client": ["./src/internal/client/types.d.ts"],
29+
"#server": ["./src/internal/server/types.d.ts"]
2930
}
3031
},
3132
"include": [

0 commit comments

Comments
 (0)