believe it or not this is faster. it also looks cooler

pull/10760/head
Rich Harris 2 years ago
parent 7fffdd7e44
commit 4e7df6ae17

@ -9,7 +9,7 @@ import {
set_signal_status
} from '../runtime.js';
import { push_reference } from './effects.js';
import { default_equals, safe_equal } from './equality.js';
import { default_equals, safe_equals } from './equality.js';
/**
* @template V
@ -56,7 +56,7 @@ export function derived(fn) {
/*#__NO_SIDE_EFFECTS__*/
export function derived_safe_equal(fn) {
const signal = derived(fn);
signal.eq = safe_equal;
signal.eq = safe_equals;
return signal;
}

@ -1,10 +1,6 @@
/**
* @param {unknown} a
* @param {unknown} b
* @returns {boolean}
*/
export function default_equals(a, b) {
return a === b;
/** @type {import('#client').Equals} */
export function default_equals(value) {
return value === this.v;
}
/**
@ -20,11 +16,7 @@ export function safe_not_equal(a, b) {
: a !== b || (a !== null && typeof a === 'object') || typeof a === 'function';
}
/**
* @param {unknown} a
* @param {unknown} b
* @returns {boolean}
*/
export function safe_equal(a, b) {
return !safe_not_equal(a, b);
/** @type {import('#client').Equals} */
export function safe_equals(value) {
return !safe_not_equal(value, this.v);
}

@ -18,7 +18,7 @@ import {
set_signal_status,
untrack
} from '../runtime.js';
import { default_equals, safe_equal } from './equality.js';
import { default_equals, safe_equals } from './equality.js';
import { CLEAN, DERIVED, DIRTY, MANAGED, SOURCE } from '../constants.js';
/**
@ -52,7 +52,7 @@ export function source(value) {
/*#__NO_SIDE_EFFECTS__*/
export function mutable_source(initial_value) {
const s = source(initial_value);
s.eq = safe_equal;
s.eq = safe_equals;
// bind the signal to the component context, in case we need to
// track updates to trigger beforeUpdate/afterUpdate callbacks
@ -109,7 +109,7 @@ export function set(signal, value) {
: '')
);
}
if ((signal.f & SOURCE) !== 0 && !signal.eq(value, signal.v)) {
if ((signal.f & SOURCE) !== 0 && !signal.eq(value)) {
signal.v = value;
// Increment write version so that unowned signals can properly track dirtyness
signal.w++;

@ -1,4 +1,4 @@
import type { Block, ComponentContext, EqualsFunctions } from '#client';
import type { Block, ComponentContext, Equals } from '#client';
import type { EFFECT, PRE_EFFECT, RENDER_EFFECT } from '../constants';
export type EffectType = typeof EFFECT | typeof PRE_EFFECT | typeof RENDER_EFFECT;
@ -7,7 +7,7 @@ export interface Source<V = unknown> {
/** Signals that read from this signal */
reactions: null | Reaction[];
/** Equality function */
eq: EqualsFunctions;
eq: Equals;
/** Flags bitmask */
f: number;
/** The latest value for this signal */

@ -75,7 +75,7 @@ import {
import { run } from '../common.js';
import { bind_transition, trigger_transitions } from './transitions.js';
import { mutable_source, source, set } from './reactivity/sources.js';
import { safe_equal, safe_not_equal } from './reactivity/equality.js';
import { safe_equals, safe_not_equal } from './reactivity/equality.js';
import { STATE_SYMBOL } from './constants.js';
/** @type {Set<string>} */
@ -2829,7 +2829,7 @@ export function prop(props, key, flags, initial) {
return (inner_current_value.v = parent_value);
});
if (!immutable) current_value.eq = safe_equal;
if (!immutable) current_value.eq = safe_equals;
return function (/** @type {V} */ value, mutation = false) {
var current = get(current_value);

@ -693,7 +693,7 @@ function update_derived(signal, force_schedule) {
? MAYBE_DIRTY
: CLEAN;
set_signal_status(signal, status);
if (!signal.eq(value, signal.v)) {
if (!signal.eq(value)) {
signal.v = value;
mark_reactions(signal, DIRTY, force_schedule);

@ -57,7 +57,7 @@ export type ComponentContext = {
};
};
export type EqualsFunctions<T = any> = (a: T, v: T) => boolean;
export type Equals = (this: Value, value: unknown) => boolean;
export type BlockType =
| typeof ROOT_BLOCK

Loading…
Cancel
Save