Class DerivedStore<T, S>Abstract

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

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

Hierarchy (view full)

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.

    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.

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

    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

    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...'
  • 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>

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

    Returns void