Class AttachedProperty<T, U>

A class which attaches a value to an external object.

Notes

Attached properties are used to extend the state of an object with semantic data from an unrelated class. They also encapsulate value creation, coercion, and notification.

Because attached property values are stored in a hash table, which in turn is stored in a WeakMap keyed on the owner object, there is non-trivial storage overhead involved in their use. The pattern is therefore best used for the storage of rare data.

Type Parameters

  • T
  • U

Constructors

Properties

_changed: null | ((owner, oldValue, newValue) => void)

Type declaration

    • (owner, oldValue, newValue): void
    • Parameters

      • owner: T
      • oldValue: U
      • newValue: U

      Returns void

_coerce: null | ((owner, value) => U)

Type declaration

    • (owner, value): U
    • Parameters

      • owner: T
      • value: U

      Returns U

_compare: null | ((oldValue, newValue) => boolean)

Type declaration

    • (oldValue, newValue): boolean
    • Parameters

      • oldValue: U
      • newValue: U

      Returns boolean

_create: ((owner) => U)

Type declaration

    • (owner): U
    • Parameters

      • owner: T

      Returns U

_pid: string = ...
name: string

The human readable name for the property.

Methods

  • Coerce the value for the given owner.

    Parameters

    • owner: T
    • value: U

    Returns U

  • Compare the old value and new value for equality.

    Parameters

    • oldValue: U
    • newValue: U

    Returns boolean

  • Get or create the default value for the given owner.

    Parameters

    • owner: T

    Returns U

  • Run the change notification if the given values are different.

    Parameters

    • owner: T
    • oldValue: U
    • newValue: U

    Returns void

  • Explicitly coerce the current property value for a given owner.

    Parameters

    • owner: T

      The property owner of interest.

      Notes

      If the value has not yet been set, the default value will be computed and used as the previous value for the comparison.

    Returns void

  • Get the current value of the property for a given owner.

    Parameters

    • owner: T

      The property owner of interest.

    Returns U

    The current value of the property.

    Notes

    If the value has not yet been set, the default value will be computed and assigned as the current value of the property.

  • Set the current value of the property for a given owner.

    Parameters

    • owner: T

      The property owner of interest.

    • value: U

      The value for the property.

      Notes

      If the value has not yet been set, the default value will be computed and used as the previous value for the comparison.

    Returns void