Class PropBindingControl<T>

Resolves a bindable component property while preserving the provenance of values published by the component.

PropBindingControl supports component properties that serve as both:

  • an externally supplied input; and
  • a value published back to the parent through component binding.

This distinction is important when a component resolves a property from multiple sources. Once the component publishes a resolved value back through the bound property, that value must not subsequently be interpreted as a new direct value supplied by the parent.

The controller records the last value it published. When resolve is called, a bound value that differs from the last published value is treated as an external assignment and becomes the highest-precedence direct value. Otherwise, the controller resolves the first valid candidate or returns its component-owned fallback.

Resolution precedence is:

  1. A valid external assignment to the bindable property.
  2. The first valid candidate passed to resolve.
  3. The fallback supplied to the constructor.

An invalid external assignment clears any previously established direct value, allowing candidate values or the fallback to become effective again.

The controller does not subscribe to, observe, or dispose of resolved values. It only retains references required to track resolution provenance. Consequently, a component-local instance does not require explicit cleanup.

<script lang="ts">
import { writable } from '#svelte/store';

import { PropBindingControl } from '#runtime/svelte/reactivity';
import { isMinimalWritableStore } from '#runtime/svelte/store/util';

// Combined all options object.
export let options = {};

// Individual prop
export let store = void 0;

const storeControl = new PropBindingControl(
isMinimalWritableStore,
writable(void 0) // Default internal store when no prop or options stores provided.
);

// A directly supplied `store` takes precedence. Otherwise, `options.store` is selected, followed by the
// component-owned writable fallback.
//
// Assigning the result back to `store` publishes the effective store through `bind:store` without causing
// that published value to become a permanent direct override.
$: store = storeControl.resolve(store, options.store);
</script>

Type Parameters

  • T

    Type accepted by the supplied predicate and returned by the controller.

Index
  • Creates a property binding controller.

    The fallback must satisfy the supplied predicate because it is guaranteed to be returned whenever no higher-precedence value is valid.

    Type Parameters

    • T

      Type accepted by the supplied predicate and returned by the controller.

    Parameters

    • predicate: TypePredicate<T>

      Type predicate used to validate the bindable property, candidate values, and fallback.

    • fallback: T

      Component-owned fallback returned when no direct value or candidate satisfies predicate.

    Returns PropBindingControl<T>

    TypeError Thrown when fallback does not satisfy predicate.

  • Resolves the effective value and records it as the value published back through the bindable component property.

    A boundValue differing from the value returned by the previous invocation is treated as an external assignment. When valid, it becomes the direct value and takes precedence over all candidates. When invalid, any previous direct value is cleared.

    When boundValue matches the previously published value, it is recognized as the component's own assignment and does not alter direct-value provenance.

    Candidates are evaluated from left to right. The first candidate accepted by the configured predicate is returned. If neither a direct value nor a candidate is valid, the constructor fallback is returned.

    The returned value should normally be assigned directly back to the bindable property:

    $: store = storeControl.resolve(store, inputOptions.store);
    

    Parameters

    • boundValue: unknown

      Current value of the exported bindable property.

    • ...candidates: readonly unknown[]

      Additional candidate values evaluated in descending precedence order.

    Returns T

    The valid direct value, first valid candidate, or fallback.