deep-state-observer
Deep state observer is an state management library which will be fired only when specified object node was changed.
You don't need to reevaluate or re-render whole app/component when only one portion of the state was modified.
Deep state observer is framework agnostic with node and browser support, so you can use it in most of your projects.
Usage
Svelte example
import { onDestroy } from 'svelte';
import State from 'deep-state-observer';
const state = new State({ some: 'value', someOther: { nested: 'value' } }, { delimeter:'.' });
let subscribers = [];
let nestedValue;
subscribers.push(
state.subscribe('someOther.nested', (value, path) => {
nestedValue = value;
})
);
let some;
subscribers.push(
state.subscribeAll(['some', 'someOther'], (value, path) => {
if (path === 'some') {
some = value;
} else if (path === 'someOther') {
nestedValue = value.nested;
}
})
);
state.update('someOther.nested', (currentValue) => {
return 'new value';
});
subscribers.push(
state.subscribe('some', (value, path) => {
state.update('someOther.nested', (oldValue) => {
return 'nested changed after some changed';
});
})
);
subscribers.push(
state.subscribe('some', (value, path) => {
state.update('someOther.nested', 'nested changed after some changed');
);
onDestroy(() => {
subscribers.forEach((unsubscribe) => unsubscribe());
});
Wildcards
import { onDestroy } from 'svelte';
import State from 'deep-state-observer';
const state = new State({ some: 'value', someOther: { nested: { node: 'ok' } } }, { delimeter: '.' });
let subscribers = [];
subscribers.push(
state.subscribe('someOther.*.n*e', (value, path) => {
})
);
subscribers.push(
state.subscribe('someOther.**', (value, path) => {
})
);
onDestroy(() => {
subscribers.forEach((unsubscribe) => unsubscribe());
});
Named wildcards (parameters)
import { onDestroy } from 'svelte';
import State from 'deep-state-observer';
const state = new State({
items: [{ val: 1 }, { val: 2 }, { val: 3 }],
byId: {
1: { val: 1 },
2: { val: 2 },
3: { val: 3 }
}
});
let subscribers = [];
subscribers.push(
state.subscribe('items.:index.val', (value, path, params) => {
})
);
subscribers.push(
state.subscribe('byId.:id.val', (value, path, params) => {
})
);
onDestroy(() => {
subscribers.forEach((unsubscribe) => unsubscribe());
});
Wildcard bulk operations (better performance)
import { onDestroy } from 'svelte';
import State from 'deep-state-observer';
const state = new State({
byId: {
1: { val: 1 },
2: { val: 2 },
3: { val: 3 }
}
});
let subscribers = [];
subscribers.push(
state.subscribe(
'byId.:id.val',
(bulk) => {
},
{ bulk: true }
)
);
subscribers.push(
state.subscribe(
'byId.*.val',
(bulk) => {
},
{ bulk: true }
)
);
onDestroy(() => {
subscribers.forEach((unsubscribe) => unsubscribe());
});
Observe all node changes (recursive, nested)
import { onDestroy } from 'svelte';
import State from 'deep-state-observer';
const state = new State({ some: 'value', someOther: { nested: { node: 'ok' } } }, { delimeter: '.' });
let subscribers = [];
subscribers.push(
state.subscribe('someOther...', (value, path) => {
})
);
subscribers.push(
state.subscribe('someOther.nested...', (value, path) => {
})
);
state.update('someOther.nested.node', 'modified');
onDestroy(() => {
subscribers.forEach((unsubscribe) => unsubscribe());
});
Vue example
import Store from 'deep-state-observer';
const state = new Store({ test: 1 });
const subscribers = [];
export default {
provide: { state }
};
export default {
template:`<div>test is equal to: {{test}}</div>`,
inject:['state'],
data(){
return {
test: 0
};
},
created(){
subscribers.push(
this.state.subscribe('test', test =>{
this.test = test;
})
);
},
beforeDestroy(){
subscribers.forEach(unsubscribe=>unsubscribe());
}
}