Options
All
  • Public
  • Public/Protected
  • All
Menu

Class ReadonlyAtom<T>

ReadonlyAtom is a specialized atom that does not provide any public method for setting their values. This means they do not have an update or set function that can be called by other code and can only be read via value; however, this does not mean that their value cannot be modified after they are created.

There are two ways to use ReadonlyAtom's, the first of which is as a normal atom with a value that will never change. You can do this by using the readonlyAtom function and passing in just it's first argument:

const ten = readonlyAtom(10);

In this example ten will always hold the value 10, since we did not pass a start function that would receive the setter we can be assured that it's value is static for the lifetime of the atom. While it is still possible to subscribe to these static atoms, they are essentially no-op, since there would be no way for the atom's value to change subscribe requests are ignored.

The other, and maybe more useful, method of using a ReadonlyAtom is one that receives a start function in addition to the intitial value that allows it's value to be updated in response to something. These kinds of ReadonlyAtoms are created by providing a start function as the second argument to readonlyAtom, a smiple example would be an atom that acts like a clock:

const clock = readonlyAtom(new Date(), set => {
  set(new Date());

  const interval = setInterval(() => {
    set(new Date());
  }, 1000);

  return () => {
    clearInterval(interval);
  };
});

In the above example we create a ReadonlyAtom that will have it's value updated ever second via an interval. We do this by passing in a start function which receives one argument, set which lets you set the value of the atom and should return a stop function to clean up any intervals or timeouts or anything else that should be stopped.

Since the value will change we can subscribe to listen for the value updates like any other atom. And subscription is key for these atoms. For efficiency sake the start function will not be called until there is at least one subscriber on the atom and it will be halted when the last subscriber unsubscribes. This is why you would typically want to update the value at the beginning of your start function so that subscribers can read and use the latest values.

If you do need your readonly atom to run even without subscribers you can use setLive to switch it to a live atom which means that it will call the start function and will not call stop regardless of how many subscribers it has. This should be used sparingly because any resources will be constantly running, it's better to try and work in using the subscribe functions for the lifetime you use the atom instead of having it constatly live.

internal

DO NOT USE Prefer using the readonlyAtom factory function to manually creating instances of this class.

Type parameters

  • T

    The type of the atom's value.

Hierarchy

Index

Constructors

constructor

Properties

Private #live

#live: boolean

Live will determine if the readonly atom should continue running without subscribers. By default readonly atoms are not live, but they can be set live with setLive function. If they are set live than start will be called and they will continue to run with or without subscribers until they are marked as no longer live with a follow up call to setLive.

Private #start

#start: undefined | ReadonlyAtomStarter<T>

The start function of the readonly atom. This will be called when a subscriber subscribes and should kick off any timeout/interval/fetch/etc... that will update the subscriber. This value is optional though, so it may be undefined in which case there is not start function to call.

Private #stop

#stop: undefined | VoidFunction

When the readonly atom has a subscriber and start (if present) is called then this is the return value of the start function. It is expected to stop any updates that are currently pending (if possible) and will be called when the last subscriber unsubscribes. This would tyipcally complement whatever start does, say by clearTimeout or clearInterval or anything else that was set up.

Private #subscribers

#subscribers: AtomSubscriber<ReadonlyAtom<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

setLive

  • setLive(isLive: boolean): void
  • This method allows you to mark the atom as live, or not live, which provides some additional controls on the how the ReadonlyAtom behaves. If the atom is marked as live then it will run as if there is always at least one subscriber and if it's then marked as no longer live it will stop as if the last subscriber has stopped.

    Parameters

    • isLive: boolean

      Whether or not to mark this atom as live or not.

    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

Private setter

  • setter(value: T): void
  • This is the set function passed to the start function provided to a ReadonlyAtom. It receives the next intended value of the atom and notifies all subscribers of the change.

    Parameters

    • value: T

      The new value of the atom.

    Returns void

subscribe

  • subscribe(subscriber: AtomSubscriber<ReadonlyAtom<T>>): void
  • For the most part, this behaves like BaseAtom's subscribe function; however, the difference is that if this ReadonlyAtom is not live and has no subscribers this will also call the provided start function (if provided). If there is no start function provided to the ReadonlyAtom then this basically becomes a no-op function since there would be no changes to respond to.

    Parameters

    • subscriber: AtomSubscriber<ReadonlyAtom<T>>

      The subscribe function that will be called when the value of this atom is mutated.

    Returns void

unsubscribe

  • unsubscribe(subscriber: null | AtomSubscriber<ReadonlyAtom<T>>): void
  • For the most part, this behaves like BaseAtom's unsubscribe function; however, the difference is that if this ReadonlyAtom is not live and the last subscriber unsubscribes then we call the stop function retuned by the provided start (if they both exist). If no start function is provided this method will essentially become a no-op since we do not accept subscribers in this situation as well.

    Parameters

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

      The function that was passed to the subscribe method and that should be removed from the list of subscribers.

    Returns void

Static isReadonlyAtom

  • isReadonlyAtom(obj: unknown): obj is ReadonlyAtom<unknown>

Generated using TypeDoc