Abstract
Protected
equalCompares 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.
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.
Protected
notCompares 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.
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
Protected
Optional
onFunction 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.
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...'
Protected
setReplaces store's state with the provided value. Equivalent of Writable.set, but internal to the store.
value to be used as the new state of a store.
Default Implementation of the SubscribableStore.subscribe, not meant to be overridden.
Protected
updateUpdates store's state by using an Updater function. Equivalent of Writable.update, but internal to the store.
Base class that can be extended to easily create a custom Readable store.
Example