svelte-deep-store

1.1.5 • Public • Published

GitHub license GitHub issues Twitter

svelte-deep-store

Svelte deep store - react only when specified property of the object was changed.

Usage

import { onDestroy } from 'svelte';
import Store from 'svelte-deep-store';
 
const state = new Store({ some: 'value', someOther: { nested: 'value' } });
 
let nestedValue;
onDestroy(
  state.subscribe('someOther.nested', (value) => {
    nestedValue = value;
  })
);
 
let some;
onDestroy(
  state.subscribeAll(['some', 'someOther'], (which, value) => {
    if (which === 'some') {
      some = value;
    } else if (which === 'someOther') {
      nestedValue = value.nested;
    }
  })
);
 
state.update('someOther.nested', (currentValue) => {
  return 'new value';
});
 
onDestroy(
  state.subscribe('some', (value) => {
    state.update('someOther.nested', (oldValue) => {
      return 'nested changed after some changed';
    });
  })
);
 
let currentState = state.get();
console.log(currentState.some);
 
onDestroy(state.destroy);

There are also watch and watchAll methods instead of subscribe and subscribeAll which do the same thing. And set is equivalent of update;

Package Sidebar

Install

npm i svelte-deep-store

Weekly Downloads

2

Version

1.1.5

License

MIT

Unpacked Size

141 kB

Total Files

11

Last publish

Collaborators

  • neuronet.io