Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: move more code #15133

Merged
merged 3 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions packages/svelte/src/index-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,41 @@ import * as e from './internal/client/errors.js';
import { lifecycle_outside_component } from './internal/shared/errors.js';
import { legacy_mode_flag } from './internal/flags/index.js';
import { component_context } from './internal/client/context.js';
import { DEV } from 'esm-env';

if (DEV) {
/**
* @param {string} rune
*/
function throw_rune_error(rune) {
if (!(rune in globalThis)) {
// TODO if people start adjusting the "this can contain runes" config through v-p-s more, adjust this message
/** @type {any} */
let value; // let's hope noone modifies this global, but belts and braces
Object.defineProperty(globalThis, rune, {
configurable: true,
// eslint-disable-next-line getter-return
get: () => {
if (value !== undefined) {
return value;
}

e.rune_outside_svelte(rune);
},
set: (v) => {
value = v;
}
});
}
}

throw_rune_error('$state');
throw_rune_error('$effect');
throw_rune_error('$derived');
throw_rune_error('$inspect');
throw_rune_error('$props');
throw_rune_error('$bindable');
}

/**
* The `onMount` function schedules a callback to run as soon as the component has been mounted to the DOM.
Expand Down
5 changes: 5 additions & 0 deletions packages/svelte/src/internal/client/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ export function pop(component) {
return component || /** @type {T} */ ({});
}

/** @returns {boolean} */
export function is_runes() {
return !legacy_mode_flag || (component_context !== null && component_context.l === null);
}

/**
* @param {string} name
* @returns {Map<unknown, unknown>}
Expand Down
3 changes: 2 additions & 1 deletion packages/svelte/src/internal/client/dom/blocks/await.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { DEV } from 'esm-env';
import { is_promise } from '../../../shared/utils.js';
import { block, branch, pause_effect, resume_effect } from '../../reactivity/effects.js';
import { internal_set, mutable_source, source } from '../../reactivity/sources.js';
import { flush_sync, is_runes, set_active_effect, set_active_reaction } from '../../runtime.js';
import { flush_sync, set_active_effect, set_active_reaction } from '../../runtime.js';
import { hydrate_next, hydrate_node, hydrating } from '../hydration.js';
import { queue_micro_task } from '../task.js';
import { UNINITIALIZED } from '../../../../constants.js';
import {
component_context,
is_runes,
set_component_context,
set_dev_current_component_function
} from '../../context.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/dom/blocks/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { UNINITIALIZED } from '../../../../constants.js';
import { block, branch, pause_effect } from '../../reactivity/effects.js';
import { not_equal, safe_not_equal } from '../../reactivity/equality.js';
import { is_runes } from '../../runtime.js';
import { is_runes } from '../../context.js';
import { hydrate_next, hydrate_node, hydrating } from '../hydration.js';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import * as e from '../../../errors.js';
import { is } from '../../../proxy.js';
import { queue_micro_task } from '../../task.js';
import { hydrating } from '../../hydration.js';
import { is_runes, untrack } from '../../../runtime.js';
import { untrack } from '../../../runtime.js';
import { is_runes } from '../../../context.js';

/**
* @param {HTMLInputElement} input
Expand Down
4 changes: 1 addition & 3 deletions packages/svelte/src/internal/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export {
user_effect,
user_pre_effect
} from './reactivity/effects.js';
export { mutable_state, mutate, set, state } from './reactivity/sources.js';
export { mutable_state, mutate, set, state, update, update_pre } from './reactivity/sources.js';
export {
prop,
rest_props,
Expand Down Expand Up @@ -139,8 +139,6 @@ export {
flush_sync,
tick,
untrack,
update,
update_pre,
exclude_from_object,
deep_read,
deep_read_state
Expand Down
11 changes: 2 additions & 9 deletions packages/svelte/src/internal/client/reactivity/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,9 @@ import {
PROPS_IS_UPDATED
} from '../../../constants.js';
import { get_descriptor, is_function } from '../../shared/utils.js';
import { mutable_source, set, source } from './sources.js';
import { mutable_source, set, source, update } from './sources.js';
import { derived, derived_safe_equal } from './deriveds.js';
import {
active_effect,
get,
captured_signals,
set_active_effect,
untrack,
update
} from '../runtime.js';
import { active_effect, get, captured_signals, set_active_effect, untrack } from '../runtime.js';
import { safe_equals } from './equality.js';
import * as e from '../errors.js';
import {
Expand Down
32 changes: 30 additions & 2 deletions packages/svelte/src/internal/client/reactivity/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
active_effect,
untracked_writes,
get,
is_runes,
schedule_effect,
set_untracked_writes,
set_signal_status,
Expand Down Expand Up @@ -34,7 +33,7 @@ import {
import * as e from '../errors.js';
import { legacy_mode_flag, tracing_mode_flag } from '../../flags/index.js';
import { get_stack } from '../dev/tracing.js';
import { component_context } from '../context.js';
import { component_context, is_runes } from '../context.js';

export let inspect_effects = new Set();

Expand Down Expand Up @@ -226,6 +225,35 @@ export function internal_set(source, value) {
return value;
}

/**
* @template {number | bigint} T
* @param {Source<T>} source
* @param {1 | -1} [d]
* @returns {T}
*/
export function update(source, d = 1) {
var value = get(source);
var result = d === 1 ? value++ : value--;

set(source, value);

// @ts-expect-error
return result;
}

/**
* @template {number | bigint} T
* @param {Source<T>} source
* @param {1 | -1} [d]
* @returns {T}
*/
export function update_pre(source, d = 1) {
var value = get(source);

// @ts-expect-error
return set(source, d === 1 ? ++value : --value);
}

/**
* @param {Value} signal
* @param {number} status should be DIRTY or MAYBE_DIRTY
Expand Down
73 changes: 3 additions & 70 deletions packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
BOUNDARY_EFFECT
} from './constants.js';
import { flush_tasks } from './dom/task.js';
import { internal_set, set } from './reactivity/sources.js';
import { internal_set } from './reactivity/sources.js';
import {
destroy_derived,
destroy_derived_effects,
Expand All @@ -35,11 +35,12 @@ import {
} from './reactivity/deriveds.js';
import * as e from './errors.js';
import { FILENAME } from '../../constants.js';
import { legacy_mode_flag, tracing_mode_flag } from '../flags/index.js';
import { tracing_mode_flag } from '../flags/index.js';
import { tracing_expressions, get_stack } from './dev/tracing.js';
import {
component_context,
dev_current_component_function,
is_runes,
set_component_context,
set_dev_current_component_function
} from './context.js';
Expand Down Expand Up @@ -161,11 +162,6 @@ export function increment_write_version() {
return ++write_version;
}

/** @returns {boolean} */
export function is_runes() {
return !legacy_mode_flag || (component_context !== null && component_context.l === null);
}

/**
* Determines whether a derived or effect is dirty.
* If it is MAYBE_DIRTY, will set the status to CLEAN
Expand Down Expand Up @@ -1095,35 +1091,6 @@ export function set_signal_status(signal, status) {
signal.f = (signal.f & STATUS_MASK) | status;
}

/**
* @template {number | bigint} T
* @param {Value<T>} signal
* @param {1 | -1} [d]
* @returns {T}
*/
export function update(signal, d = 1) {
var value = get(signal);
var result = d === 1 ? value++ : value--;

set(signal, value);

// @ts-expect-error
return result;
}

/**
* @template {number | bigint} T
* @param {Value<T>} signal
* @param {1 | -1} [d]
* @returns {T}
*/
export function update_pre(signal, d = 1) {
var value = get(signal);

// @ts-expect-error
return set(signal, d === 1 ? ++value : --value);
}

/**
* @param {Record<string, unknown>} obj
* @param {string[]} keys
Expand Down Expand Up @@ -1215,37 +1182,3 @@ export function deep_read(value, visited = new Set()) {
}
}
}

if (DEV) {
/**
* @param {string} rune
*/
function throw_rune_error(rune) {
if (!(rune in globalThis)) {
// TODO if people start adjusting the "this can contain runes" config through v-p-s more, adjust this message
/** @type {any} */
let value; // let's hope noone modifies this global, but belts and braces
Object.defineProperty(globalThis, rune, {
configurable: true,
// eslint-disable-next-line getter-return
get: () => {
if (value !== undefined) {
return value;
}

e.rune_outside_svelte(rune);
},
set: (v) => {
value = v;
}
});
}
}

throw_rune_error('$state');
throw_rune_error('$effect');
throw_rune_error('$derived');
throw_rune_error('$inspect');
throw_rune_error('$props');
throw_rune_error('$bindable');
}
10 changes: 5 additions & 5 deletions packages/svelte/tests/signals/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
render_effect,
user_effect
} from '../../src/internal/client/reactivity/effects';
import { state, set } from '../../src/internal/client/reactivity/sources';
import { state, set, update, update_pre } from '../../src/internal/client/reactivity/sources';
import type { Derived, Effect, Value } from '../../src/internal/client/types';
import { proxy } from '../../src/internal/client/proxy';
import { derived } from '../../src/internal/client/reactivity/deriveds';
Expand Down Expand Up @@ -968,14 +968,14 @@ describe('signals', () => {
return () => {
const count = state(0n);

assert.doesNotThrow(() => $.update(count));
assert.doesNotThrow(() => update(count));
assert.equal($.get(count), 1n);
assert.doesNotThrow(() => $.update(count, -1));
assert.doesNotThrow(() => update(count, -1));
assert.equal($.get(count), 0n);

assert.doesNotThrow(() => $.update_pre(count));
assert.doesNotThrow(() => update_pre(count));
assert.equal($.get(count), 1n);
assert.doesNotThrow(() => $.update_pre(count, -1));
assert.doesNotThrow(() => update_pre(count, -1));
assert.equal($.get(count), 0n);
};
});
Expand Down