campfire.js
    Preparing search index...

    Class Store<T>

    A simple reactive store. Store

    Type Parameters

    • T

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    _dead: boolean = false

    A value describing whether or not the store has been disposed of.

    _subscriberCounts: Record<string, number> = {}

    The subscribers currently registered to the store.

    _subscribers: {
        append?: Record<number, EventSubscriber<"append", Store<T>>>;
        change?: Record<number, EventSubscriber<"change", Store<T>>>;
        clear?: Record<number, EventSubscriber<"clear", Store<T>>>;
        deletion?: Record<number, EventSubscriber<"deletion", Store<T>>>;
        update?: Record<number, EventSubscriber<"update", Store<T>>>;
    } = {}

    The subscribers currently registered to the store.

    id: string = ...

    A unique ID for the store, to track nested reactive elements to warn the user.

    Methods

    • Subscribes the provided function to all store events. This is a convenience method that registers the function for 'change', 'append', 'clear', and 'deletion' events.

      Parameters

      Returns void

      void

    • Get a deep clone of the current store value.

      Added in 4.0.0-rc15 as the recommended way to access store values since the value property is now protected.

      Returns T

      A deep clone of the store's current value

      const user = store({ value: { name: "John", age: 30 } });
      const userData = user.current(); // { name: "John", age: 30 }
    • Add an event listener to the store.

      Type Parameters

      • K extends "clear" | "change" | "append" | "update" | "deletion"

      Parameters

      • type: K

        The type of event to listen for. Supported event types include:

        • update: Triggered when the store's value is updated via update().
        • append: For ListStore - Triggered when an item is added to the list.
        • deletion: For ListStore/MapStore - Triggered when an item is removed.
        • change: For ListStore/MapStore: Triggered when an item at an index/key has its value set via the corresponding store's set() method.
        • 'clear': Triggered when the store is cleared.
      • fn: EventSubscriber<K, Store<T>>

        A callback function that will be invoked when the specified event occurs. The function receives a StoreEvent object with details about the event.

      • OptionalcallNow: true

        Optional parameter to immediately trigger the callback with current value

      Returns number

      A unique subscriber ID that can be used to unsubscribe the listener.

      // Subscribe to updates
      const counter = store({ value: 0 });
      counter.on("update", (event) => {
      console.log(`Counter updated to: ${event.value}`);
      });

      // Subscribe and trigger immediately with current value
      counter.on("update", (event) => {
      console.log(`Current value: ${event.value}`);
      }, true);
    • Removes a specific event listener from the store.

      Parameters

      • type: "clear" | "change" | "append" | "update" | "deletion"

        The type of event from which to unsubscribe.

      • id: number

        The subscriber ID returned by the on() method when the listener was registered.

      Returns void

      Will throw an error if the subscriber ID is invalid or not found.

    • Updates the store's value and notifies all subscribers.

      As of 4.0.0-rc15, this method can also accept a transform function that receives the current value and returns a new value.

      Parameters

      • value: (arg: T) => T

        The new value to set for the store, or a transform function that takes the current value and returns a new value.

      Returns null | T

      The updated value, or null if the store has been disposed.

      'update' event with the new value when successfully updated.

      // Direct update
      counter.update(5);

      // Update using a transform function
      counter.update(current => current + 1);

      // Complex transform
      userStore.update(user => ({
      ...user,
      visits: user.visits + 1,
      lastVisit: new Date()
      }));
    • Updates the store's value and notifies all subscribers.

      As of 4.0.0-rc15, this method can also accept a transform function that receives the current value and returns a new value.

      Parameters

      • value: T

        The new value to set for the store, or a transform function that takes the current value and returns a new value.

      Returns null | T

      The updated value, or null if the store has been disposed.

      'update' event with the new value when successfully updated.

      // Direct update
      counter.update(5);

      // Update using a transform function
      counter.update(current => current + 1);

      // Complex transform
      userStore.update(user => ({
      ...user,
      visits: user.visits + 1,
      lastVisit: new Date()
      }));
    • Internal

      Utility method to check if a value is a transform function

      Type Parameters

      • T

      Parameters

      • val: unknown

      Returns val is (arg: T) => T