Custom stores

As long as an object correctly implements the subscribe method, it’s a store. Beyond that, anything goes. It’s very easy, therefore, to create custom stores with domain-specific logic.

For example, the count store from our earlier example could include increment, decrement and reset methods and avoid exposing set and update:

  1. function createCount() {
  2. const { subscribe, set, update } = writable(0);
  3. return {
  4. subscribe,
  5. increment: () => update(n => n + 1),
  6. decrement: () => update(n => n - 1),
  7. reset: () => set(0)
  8. };
  9. }