Class PropChangeTracker<T, K>

Explicitly checks selected properties against their previously supplied values.

PropertyChangeTracker has no subscriptions or reactive behavior of its own. A host, such as a Svelte reactive statement, determines when PropChangeTracker.check is called and retains full control over any resulting side effects.

Each check compares and then commits the latest values. A comparator may therefore consider two distinct values equal, while the most recently supplied value still becomes the baseline for the next check.

PropChangeTracker maintains a snapshot of selected object properties and reports which tracked properties changed between successive check() calls. It is intended for explicit reactive control flow and does not introduce subscriptions or hidden reactivity.

Track changes to a subset of resolved component properties.

<script lang="ts">
import { PropChangeTracker } from '#runtime/svelte/reactivity';

import { isObject } from '#runtime/util/object';

import {
isBoolean,
isString,
resolveByPredicate } from '#runtime/util/predicate';

interface Props
{
foo?: boolean;
bar?: string;
}

// Combined props options object with `foo` and / or `bar`.
export let options?: Props = void 0;

// Individual prop `foo`
export let foo ?: string = void 0;

// Individual prop `bar`
export let bar?: string = void 0;

const tracker = new PropChangeTracker<Props>({
keys: ['foo', 'bar']
});

// Stores all resolved props.
const props: Props = {};

// Normalize the combined options separately from resolution of the individual exported props. The individual
// props will take precedence over the combined options object.

$: inputOptions: Props = isObject(options) ? options : {};

$: props.foo = resolveByPredicate(isBoolean, foo, inputOptions.foo) ?? true;

$: props.bar = resolveByPredicate(isString, bar, inputOptions?.bar);

// Control based logic based on prop changes.
$: {
const changes = tracker.check(props);

if (changes.changed)
{
if (changes.has('foo'))
{
// `foo` state changed.
}

if (changes.hasAny('foo', 'bar'))
{
// `foo` and / or `bar` changed.
}
}
}
</script>

Type Parameters

  • T extends object

    Source object shape.

  • K extends keyof T = keyof T

    Tracked properties from the source object.

Index
  • get initialized(): boolean

    Whether a baseline has been captured by check or sync.

    Returns boolean

  • Compares tracked properties with their previous values and commits the supplied values as the next baseline.

    The returned PropChangeTracker.Data.ChangeSet is reused by subsequent checks.

    Parameters

    • value: T

      Source object containing the tracked properties.

    Returns ChangeSet<K>

  • Clears retained values and returns the tracker to its initial state.

    Returns void

  • Captures tracked property values as the new baseline without reporting changes.

    Parameters

    • value: T

      Source object containing the tracked properties.

    Returns void