campfire.js
    Preparing search index...

    Class MapStore<T>

    A reactive map store. Implements set(key, value), remove(key), clear(), transform(key, fn), has(key), entries(), and get(key). set() sends a "change" event, remove() sends a "deletion" event, clear() sends a "clear" event, and transform() sends a "change" event.

    Type Parameters

    • T

    Hierarchy (View Summary)

    • Store<Record<string, T>>
      • MapStore
    Index

    Constructors

    • Constructor for MapStore. Initializes the store with the provided initial key-value pairs.

      Type Parameters

      • T

      Parameters

      • Optionalinit: Record<string, T>

        Initial key-value pairs to populate the store.

      Returns MapStore<T>

    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<Record<string, T>>>,
        >;
        change?: Record<
            number,
            EventSubscriber<"change", Store<Record<string, T>>>,
        >;
        clear?: Record<number, EventSubscriber<"clear", Store<Record<string, T>>>>;
        deletion?: Record<
            number,
            EventSubscriber<"deletion", Store<Record<string, T>>>,
        >;
        update?: Record<
            number,
            EventSubscriber<"update", Store<Record<string, 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.

    Accessors

    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

      • fn: AnySubscriber<Store<Record<string, T>>>

        A callback function that will be called for all store events

      Returns void

      void

    • Removes all key-value pairs from the store.

      Returns void

      'clear' event indicating the store has been emptied.

    • 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 Record<string, 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 }
    • Retrieves the value associated with a specific key.

      Parameters

      • key: string

        The key to look up.

      Returns T

      The value associated with the key, or undefined if the key does not exist.

    • 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<Record<string, 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 key-value pair from the store.

      Parameters

      • key: string

        The key to remove.

      Returns void

      'deletion' event with:

      • key: The key that was removed
      • value: The current state of the map after deletion
    • Sets a value for a specific key in the store.

      Parameters

      • key: string

        The key to set or update.

      • value: T

        The value to associate with the key.

      Returns void

      'change' event with:

      • key: The key that was set or updated
      • value: The new value associated with the key
    • Applies a transformation function to the value of a specific key.

      Parameters

      • key: string

        The key whose value will be transformed.

      • fn: (val: T) => T

        A function that takes the current value and returns a new value.

      Returns void

      If the key does not exist in the store.

      'change' event with the transformed value (via internal set method)

    • 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: Record) => Record

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

      Returns null | Record<string, 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: Record

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

      Returns null | Record<string, 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