Options
All
  • Public
  • Public/Protected
  • All
Menu

Class BaseAtom<T>

BaseAtom provides the basic functionality for atoms such as the readonly value property and handling subscribers. These are the basic requirements required to meet the IAtom interface.

internal

DO NOT INSTANTIATE This is the abstract base class for all atom-like objects. Either extend it to create your own atom-like objects or use one of the factory functions to create a usable instance of an atom based on your needs.

Type parameters

  • T

    The type of the atom's value.

Hierarchy

Index

Constructors

constructor

  • new BaseAtom<T>(value: T): BaseAtom<T>
  • Constructs a new BaseAtom, setting the initial value and initializing the subscriber list to an empty array.

    Type parameters

    • T

    Parameters

    • value: T

      The initial value of the BaseAtom.

    Returns BaseAtom<T>

Properties

Private #subscribers

#subscribers: AtomSubscriber<BaseAtom<T>>[]

The list of active subscribers listening or value changes on this atom. Subscribers are called anytime the value is updated.

typeparam

The specific class that was subscribed to.

Private #value

#value: T

The value of the atom, BaseAtom does not provide any means for updating this after the object is created.

typeparam

Teh atom's value.

Accessors

value

  • get value(): T
  • This readonly property will return the current active value of the atom.

    Returns T

    The current value of the atom.

Methods

Protected hasSubscribers

  • hasSubscribers(): boolean
  • An internal test that determines if any subscribers are currently subscribed to this atom's value changes.

    Returns boolean

    A boolean representing if there are any subscribers currently subscribed to this atom.

Protected notifySubscribers

  • notifySubscribers(): void
  • An internal helper that will fire all the subscribers passing this to them, this is typically only triggered in response to value changes.

    Returns void

Protected setValue

  • setValue(newValue: T): void
  • An internal helper that will update the value of the atom. Note that it's protected, there is no public way to modify a BaseAtom's value this is for subclasses to used.

    Parameters

    • newValue: T

      The new value for the atom's value field.

    Returns void

subscribe

  • subscribe(subscriber: AtomSubscriber<BaseAtom<T>>): void
  • Subscribe to the atom's value updates with the given subscriber. This subscriber will be called with the instance of the atom anytime it's value changes (of course the BaseAtom does not have a public mechanism for the value to change).

    Parameters

    • subscriber: AtomSubscriber<BaseAtom<T>>

      The subscriber to add to the list of subscribers listening for value changes.

    Returns void

unsubscribe

  • unsubscribe(subscriber: null | AtomSubscriber<BaseAtom<T>>): void
  • Unsubscribe the given subscriber from value updates on this atom. If the subscriber is in the list of subscribers it will be removed, and if it's no already a subscriber then nothing will happen. If null is passed instead of a subscriber then all subscribers will be removed.

    Parameters

    • subscriber: null | AtomSubscriber<BaseAtom<T>>

      The subscriber to remove or null to remove all subscribers.

    Returns void

Generated using TypeDoc