Skip to content

Commit 8e83127

Browse files
authored
chore: move more code (#15133)
* move more code * lint
1 parent c8bbb15 commit 8e83127

File tree

10 files changed

+86
-92
lines changed

10 files changed

+86
-92
lines changed

packages/svelte/src/index-client.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,41 @@ import * as e from './internal/client/errors.js';
88
import { lifecycle_outside_component } from './internal/shared/errors.js';
99
import { legacy_mode_flag } from './internal/flags/index.js';
1010
import { component_context } from './internal/client/context.js';
11+
import { DEV } from 'esm-env';
12+
13+
if (DEV) {
14+
/**
15+
* @param {string} rune
16+
*/
17+
function throw_rune_error(rune) {
18+
if (!(rune in globalThis)) {
19+
// TODO if people start adjusting the "this can contain runes" config through v-p-s more, adjust this message
20+
/** @type {any} */
21+
let value; // let's hope noone modifies this global, but belts and braces
22+
Object.defineProperty(globalThis, rune, {
23+
configurable: true,
24+
// eslint-disable-next-line getter-return
25+
get: () => {
26+
if (value !== undefined) {
27+
return value;
28+
}
29+
30+
e.rune_outside_svelte(rune);
31+
},
32+
set: (v) => {
33+
value = v;
34+
}
35+
});
36+
}
37+
}
38+
39+
throw_rune_error('$state');
40+
throw_rune_error('$effect');
41+
throw_rune_error('$derived');
42+
throw_rune_error('$inspect');
43+
throw_rune_error('$props');
44+
throw_rune_error('$bindable');
45+
}
1146

1247
/**
1348
* The `onMount` function schedules a callback to run as soon as the component has been mounted to the DOM.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ export function pop(component) {
185185
return component || /** @type {T} */ ({});
186186
}
187187

188+
/** @returns {boolean} */
189+
export function is_runes() {
190+
return !legacy_mode_flag || (component_context !== null && component_context.l === null);
191+
}
192+
188193
/**
189194
* @param {string} name
190195
* @returns {Map<unknown, unknown>}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import { DEV } from 'esm-env';
33
import { is_promise } from '../../../shared/utils.js';
44
import { block, branch, pause_effect, resume_effect } from '../../reactivity/effects.js';
55
import { internal_set, mutable_source, source } from '../../reactivity/sources.js';
6-
import { flush_sync, is_runes, set_active_effect, set_active_reaction } from '../../runtime.js';
6+
import { flush_sync, set_active_effect, set_active_reaction } from '../../runtime.js';
77
import { hydrate_next, hydrate_node, hydrating } from '../hydration.js';
88
import { queue_micro_task } from '../task.js';
99
import { UNINITIALIZED } from '../../../../constants.js';
1010
import {
1111
component_context,
12+
is_runes,
1213
set_component_context,
1314
set_dev_current_component_function
1415
} from '../../context.js';

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { UNINITIALIZED } from '../../../../constants.js';
33
import { block, branch, pause_effect } from '../../reactivity/effects.js';
44
import { not_equal, safe_not_equal } from '../../reactivity/equality.js';
5-
import { is_runes } from '../../runtime.js';
5+
import { is_runes } from '../../context.js';
66
import { hydrate_next, hydrate_node, hydrating } from '../hydration.js';
77

88
/**

packages/svelte/src/internal/client/dom/elements/bindings/input.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import * as e from '../../../errors.js';
55
import { is } from '../../../proxy.js';
66
import { queue_micro_task } from '../../task.js';
77
import { hydrating } from '../../hydration.js';
8-
import { is_runes, untrack } from '../../../runtime.js';
8+
import { untrack } from '../../../runtime.js';
9+
import { is_runes } from '../../../context.js';
910

1011
/**
1112
* @param {HTMLInputElement} input

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export {
110110
user_effect,
111111
user_pre_effect
112112
} from './reactivity/effects.js';
113-
export { mutable_state, mutate, set, state } from './reactivity/sources.js';
113+
export { mutable_state, mutate, set, state, update, update_pre } from './reactivity/sources.js';
114114
export {
115115
prop,
116116
rest_props,
@@ -139,8 +139,6 @@ export {
139139
flush_sync,
140140
tick,
141141
untrack,
142-
update,
143-
update_pre,
144142
exclude_from_object,
145143
deep_read,
146144
deep_read_state

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,9 @@ import {
88
PROPS_IS_UPDATED
99
} from '../../../constants.js';
1010
import { get_descriptor, is_function } from '../../shared/utils.js';
11-
import { mutable_source, set, source } from './sources.js';
11+
import { mutable_source, set, source, update } from './sources.js';
1212
import { derived, derived_safe_equal } from './deriveds.js';
13-
import {
14-
active_effect,
15-
get,
16-
captured_signals,
17-
set_active_effect,
18-
untrack,
19-
update
20-
} from '../runtime.js';
13+
import { active_effect, get, captured_signals, set_active_effect, untrack } from '../runtime.js';
2114
import { safe_equals } from './equality.js';
2215
import * as e from '../errors.js';
2316
import {

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
active_effect,
66
untracked_writes,
77
get,
8-
is_runes,
98
schedule_effect,
109
set_untracked_writes,
1110
set_signal_status,
@@ -34,7 +33,7 @@ import {
3433
import * as e from '../errors.js';
3534
import { legacy_mode_flag, tracing_mode_flag } from '../../flags/index.js';
3635
import { get_stack } from '../dev/tracing.js';
37-
import { component_context } from '../context.js';
36+
import { component_context, is_runes } from '../context.js';
3837

3938
export let inspect_effects = new Set();
4039

@@ -226,6 +225,35 @@ export function internal_set(source, value) {
226225
return value;
227226
}
228227

228+
/**
229+
* @template {number | bigint} T
230+
* @param {Source<T>} source
231+
* @param {1 | -1} [d]
232+
* @returns {T}
233+
*/
234+
export function update(source, d = 1) {
235+
var value = get(source);
236+
var result = d === 1 ? value++ : value--;
237+
238+
set(source, value);
239+
240+
// @ts-expect-error
241+
return result;
242+
}
243+
244+
/**
245+
* @template {number | bigint} T
246+
* @param {Source<T>} source
247+
* @param {1 | -1} [d]
248+
* @returns {T}
249+
*/
250+
export function update_pre(source, d = 1) {
251+
var value = get(source);
252+
253+
// @ts-expect-error
254+
return set(source, d === 1 ? ++value : --value);
255+
}
256+
229257
/**
230258
* @param {Value} signal
231259
* @param {number} status should be DIRTY or MAYBE_DIRTY

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

Lines changed: 3 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
BOUNDARY_EFFECT
2727
} from './constants.js';
2828
import { flush_tasks } from './dom/task.js';
29-
import { internal_set, set } from './reactivity/sources.js';
29+
import { internal_set } from './reactivity/sources.js';
3030
import {
3131
destroy_derived,
3232
destroy_derived_effects,
@@ -35,11 +35,12 @@ import {
3535
} from './reactivity/deriveds.js';
3636
import * as e from './errors.js';
3737
import { FILENAME } from '../../constants.js';
38-
import { legacy_mode_flag, tracing_mode_flag } from '../flags/index.js';
38+
import { tracing_mode_flag } from '../flags/index.js';
3939
import { tracing_expressions, get_stack } from './dev/tracing.js';
4040
import {
4141
component_context,
4242
dev_current_component_function,
43+
is_runes,
4344
set_component_context,
4445
set_dev_current_component_function
4546
} from './context.js';
@@ -161,11 +162,6 @@ export function increment_write_version() {
161162
return ++write_version;
162163
}
163164

164-
/** @returns {boolean} */
165-
export function is_runes() {
166-
return !legacy_mode_flag || (component_context !== null && component_context.l === null);
167-
}
168-
169165
/**
170166
* Determines whether a derived or effect is dirty.
171167
* If it is MAYBE_DIRTY, will set the status to CLEAN
@@ -1095,35 +1091,6 @@ export function set_signal_status(signal, status) {
10951091
signal.f = (signal.f & STATUS_MASK) | status;
10961092
}
10971093

1098-
/**
1099-
* @template {number | bigint} T
1100-
* @param {Value<T>} signal
1101-
* @param {1 | -1} [d]
1102-
* @returns {T}
1103-
*/
1104-
export function update(signal, d = 1) {
1105-
var value = get(signal);
1106-
var result = d === 1 ? value++ : value--;
1107-
1108-
set(signal, value);
1109-
1110-
// @ts-expect-error
1111-
return result;
1112-
}
1113-
1114-
/**
1115-
* @template {number | bigint} T
1116-
* @param {Value<T>} signal
1117-
* @param {1 | -1} [d]
1118-
* @returns {T}
1119-
*/
1120-
export function update_pre(signal, d = 1) {
1121-
var value = get(signal);
1122-
1123-
// @ts-expect-error
1124-
return set(signal, d === 1 ? ++value : --value);
1125-
}
1126-
11271094
/**
11281095
* @param {Record<string, unknown>} obj
11291096
* @param {string[]} keys
@@ -1215,37 +1182,3 @@ export function deep_read(value, visited = new Set()) {
12151182
}
12161183
}
12171184
}
1218-
1219-
if (DEV) {
1220-
/**
1221-
* @param {string} rune
1222-
*/
1223-
function throw_rune_error(rune) {
1224-
if (!(rune in globalThis)) {
1225-
// TODO if people start adjusting the "this can contain runes" config through v-p-s more, adjust this message
1226-
/** @type {any} */
1227-
let value; // let's hope noone modifies this global, but belts and braces
1228-
Object.defineProperty(globalThis, rune, {
1229-
configurable: true,
1230-
// eslint-disable-next-line getter-return
1231-
get: () => {
1232-
if (value !== undefined) {
1233-
return value;
1234-
}
1235-
1236-
e.rune_outside_svelte(rune);
1237-
},
1238-
set: (v) => {
1239-
value = v;
1240-
}
1241-
});
1242-
}
1243-
}
1244-
1245-
throw_rune_error('$state');
1246-
throw_rune_error('$effect');
1247-
throw_rune_error('$derived');
1248-
throw_rune_error('$inspect');
1249-
throw_rune_error('$props');
1250-
throw_rune_error('$bindable');
1251-
}

packages/svelte/tests/signals/test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
render_effect,
99
user_effect
1010
} from '../../src/internal/client/reactivity/effects';
11-
import { state, set } from '../../src/internal/client/reactivity/sources';
11+
import { state, set, update, update_pre } from '../../src/internal/client/reactivity/sources';
1212
import type { Derived, Effect, Value } from '../../src/internal/client/types';
1313
import { proxy } from '../../src/internal/client/proxy';
1414
import { derived } from '../../src/internal/client/reactivity/deriveds';
@@ -968,14 +968,14 @@ describe('signals', () => {
968968
return () => {
969969
const count = state(0n);
970970

971-
assert.doesNotThrow(() => $.update(count));
971+
assert.doesNotThrow(() => update(count));
972972
assert.equal($.get(count), 1n);
973-
assert.doesNotThrow(() => $.update(count, -1));
973+
assert.doesNotThrow(() => update(count, -1));
974974
assert.equal($.get(count), 0n);
975975

976-
assert.doesNotThrow(() => $.update_pre(count));
976+
assert.doesNotThrow(() => update_pre(count));
977977
assert.equal($.get(count), 1n);
978-
assert.doesNotThrow(() => $.update_pre(count, -1));
978+
assert.doesNotThrow(() => update_pre(count, -1));
979979
assert.equal($.get(count), 0n);
980980
};
981981
});

0 commit comments

Comments
 (0)