8000 fix: Avoid `derived()` store updates on invalid upstream state. by mnrx · Pull Request #9458 · sveltejs/svelte · GitHub
[go: up one dir, main page]

Skip to content

fix: Avoid derived() store updates on invalid upstream state. #9458

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
revert some unnecessary changes, to reduce diff size
  • Loading branch information
Rich-Harris committed Jan 31, 2024
commit a06a4e609fda7edea2674c4638c878f1b904a4d7
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { subscribe_to_store } from '../../store/index.js';
import { DEV } from 'esm-env';
import { subscribe_to_store } from '../../store/utils.js';
import { noop, run_all } from '../common.js';
import {
array_prototype,
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/server/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as $ from '../client/runtime.js';
import { set_is_ssr } from '../client/runtime.js';
import { is_promise, noop } from '../common.js';
import { subscribe_to_store } from '../../store/index.js';
import { subscribe_to_store } from '../../store/utils.js';
import { DOMBooleanAttributes } from '../../constants.js';

export * from '../client/validate.js';
Expand Down
5 changes: 1 addition & 4 deletions packages/svelte/src/motion/spring.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ function tick_spring(ctx, last_value, current_value, target_value) {
}

/**
* The spring function in Svelte creates a store whose value is animated, with a motion that
* simulates the behavior of a spring. This means when the value changes, instead of transitioning
* at a steady rate, it "bounces" like a spring would, depending on the physics parameters provided.
* This adds a level of realism to the transitions and can enhance the user experience.
* The spring function in Svelte creates a store whose value is animated, with a motion that simulates the behavior of a spring. This means when the value changes, instead of transitioning at a steady rate, it "bounces" like a spring would, depending on the physics parameters provided. This adds a level of realism to the transitions and can enhance the user experience.
*
* https://svelte.dev/docs/svelte-motion#spring
* @template [T=any]
Expand Down
7 changes: 3 additions & 4 deletions packages/svelte/src/motion/tweened.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ function get_interpolator(a, b) {
}

/**
* A tweened store in Svelte is a special type of store that provides smooth transitions between
* state values over time.
* A tweened store in Svelte is a special type of store that provides smooth transitions between state values over time.
*
* https://svelte.dev/docs/svelte-motion#tweened
* @template T
Expand Down Expand Up @@ -125,9 +124,9 @@ export function tweened(value, defaults = {}) {
if (now < start) return true;
if (!started) {
fn = interpolate(/** @type {any} */ (value), new_value);
if (typeof duration === 'function') {
if (typeof duration === 'function')
duration = duration(/** @type {any} */ (value), new_value);
}

started = true;
}
if (previous_task) {
Expand Down
24 changes: 0 additions & 24 deletions packages/svelte/src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,27 +501,3 @@ export function get_store_value(store, allow_stale = false) {
}

export { get_store_value as get };

/**
* @template T
* @param {import('./public.js').Readable<T> | import('./public.js').ExternalReadable<T> | null | undefined} store
* @param {(value: T) => void} run
* @param {(value: T) => void} [invalidate]
* @returns {() => void}
*/
export function subscribe_to_store(store, run, invalidate) {
if (store == null) {
// @ts-expect-error
run(undefined);

// @ts-expect-error
if (invalidate) invalidate(undefined);

return noop;
}

// @ts-expect-error `SubscriberInvalidator<T>` is not public.
const unsubscribe = store.subscribe([run, invalidate]);
const is_rxjs = typeof unsubscribe !== 'function';
return is_rxjs ? unsubscribe.unsubscribe : unsubscribe;
}
31 changes: 31 additions & 0 deletions packages/svelte/src/store/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { noop } from '../internal/common.js';

/**
* @template T
* @param {import('./public').Readable<T> | null | undefined} store
* @param {(value: T) => void} run
* @param {(value: T) => void} [invalidate]
* @returns {() => void}
*/
export function subscribe_to_store(store, run, invalidate) {
if (store == null) {
// @ts-expect-error
run(undefined);

// @ts-expect-error
if (invalidate) invalidate(undefined);

return noop;
}

// Svelte store takes a private second argument
const unsub = store.subscribe(
run,
// @ts-expect-error
invalidate
);

// Also support RxJS
// @ts-expect-error TODO fix this in the types?
return unsub.unsubscribe ? () => unsub.unsubscribe() : unsub;
}
0