One of the Svelte stores I am using is called activeAccountIndexStore
:
export const activeAccountIndexStore: Writable<number | "native" | null> =
writable(null);
Initially, the value of the store is null
, but it gets set to either a specific number
or the string 'native'
after some network activity.
There is also a subscribe()
handler for this store, which is invoked by other Svelte components:
export const onChangeActiveAccount = (
activeAccountHandler: ActiveAccountHandler
) => {
activeAccountIndexStore.subscribe((newValue) => {
log(`➡️➡️➡️➡️➡️ activeAccountIndexStore new value`, newValue);
if (newValue !== null) {
const activeAccount = getActiveAccount();
activeAccountHandler(activeAccount);
}
});
};
An issue I'm experiencing is that the subscribe()
change handler is being called repeatedly, even when the store value remains the same in consecutive changes:
stores.ts:68 ➡️➡️➡️➡️➡️ activeAccountIndexStore new value null
stores.ts:68 ➡️➡️➡️➡️➡️ activeAccountIndexStore new value null
2stores.ts:68 ➡️➡️➡️➡️➡️ activeAccountIndexStore new value 0
stores.ts:68 ➡️➡️➡️➡️➡️ activeAccountIndexStore new value 0
stores.ts:68 ➡️➡️➡️➡️➡️ activeAccountIndexStore new value 0
stores.ts:68 ➡️➡️➡️➡️➡️ activeAccountIndexStore new value 0
stores.ts:68 ➡️➡️➡️➡️➡️ activeAccountIndexStore new value 0
The MDN documentation on Svelte stores explains:
A store is an object with a subscribe() method that allows interested parties to be notified whenever the store value changes.
However, in many cases above, the value of the store is not changing. I expected to see:
stores.ts:68 ➡️➡️➡️➡️➡️ activeAccountIndexStore new value null
stores.ts:68 ➡️➡️➡️➡️➡️ activeAccountIndexStore new value 0
Why is the subscribe() method of my Svelte store being triggered when the value hasn't changed?