Class Store<T>Abstract

Base class that can be extended to easily create a custom Readable store.

Example

class CounterStore extends Store {
constructor() {
super(1); // initial value
}

reset() {
this.set(0);
}

increment() {
this.update(value => value + 1);
}
}

const store = new CounterStore(1);

// logs 1 (initial value) upon subscription
const unsubscribe = store.subscribe((value) => {
console.log(value);
});
store.increment(); // logs 2
store.reset(); // logs 0

unsubscribe(); // stops notifications and corresponding logging

Type Parameters

  • T

Hierarchy (view full)

Implements

Constructors

Methods

  • Compares two values and returns true if they are equal. It is called when setting a new value to avoid doing anything (such as notifying subscribers) if the value did not change. The default logic is to return false if a is a function or an object, or if a and b are different according to Object.is. This method can be overridden by subclasses to change the logic.

    Parameters

    • a: T

      First value to compare.

    • b: T

      Second value to compare.

    Returns boolean

    true if a and b are considered equal.

    Remarks

    For backward compatibility, the default implementation calls the deprecated Store.notEqual method and returns the negation of its return value.

  • Compares two values and returns true if they are different. It is called when setting a new value to avoid doing anything (such as notifying subscribers) if the value did not change. The default logic is to return true if a is a function or an object, or if a and b are different according to Object.is. This method can be overridden by subclasses to change the logic.

    Parameters

    • a: T

      First value to compare.

    • b: T

      Second value to compare.

    Returns boolean

    true if a and b are considered different.

    Remarks

    This method is only called by the default implementation of Store.equal, so overriding Store.equal takes precedence over overriding notEqual.

    Deprecated

    Use Store.equal instead

  • Function called when the number of subscribers changes from 0 to 1 (but not called when the number of subscribers changes from 1 to 2, ...). If a function is returned, it will be called when the number of subscribers changes from 1 to 0.

    Returns void | Unsubscriber

    Example

    class CustomStore extends Store {
    onUse() {
    console.log('Got the fist subscriber!');
    return () => {
    console.log('All subscribers are gone...');
    };
    }
    }

    const store = new CustomStore();
    const unsubscribe1 = store.subscribe(() => {}); // logs 'Got the fist subscriber!'
    const unsubscribe2 = store.subscribe(() => {}); // nothing is logged as we've got one subscriber already
    unsubscribe1(); // nothing is logged as we still have one subscriber
    unsubscribe2(); // logs 'All subscribers are gone...'
  • Puts the store in the paused state, which means it will soon update its value.

    Returns void

    Remarks

    The paused state prevents derived or computed stores (both direct and transitive) from recomputing their value using the current value of this store.

    There are two ways to put a store back into its normal state: calling set to set a new value or calling resumeSubscribers to declare that finally the value does not need to be changed.

    Note that a store should not stay in the paused state for a long time, and most of the time it is not needed to call pauseSubscribers or resumeSubscribers manually.

  • Puts the store back to the normal state without changing its value, if it was in the paused state (cf pauseSubscribers).

    Returns void

    Remarks

    Does nothing if the store was not in the paused state.

  • Replaces store's state with the provided value. Equivalent of Writable.set, but internal to the store.

    Parameters

    • value: T

      value to be used as the new state of a store.

    Returns void

  • Updates store's state by using an Updater function. Equivalent of Writable.update, but internal to the store.

    Parameters

    • updater: Updater<T, T>

      a function that takes the current state as an argument and returns the new state.

    Returns void

Generated using TypeDoc