@plokkke/store
v1.0.2
Published
Data store for redux pattern
Downloads
12
Readme
Project name
Installation
npm install --save @plokkke/storeImport
const Store = require('@plokkke/store');Simple case
const store = new Store();
store.subscribe(value => {
// Do something meaningful
})
store.value = 'Important data';
// Calling subscribers on next event loop.Advanced case
const project = {
id: 317,
name: '@plokkke/store',
type: 'PROJECT',
team: {
manager: { id: 78, email: '[email protected]' },
productOwner: { id: 47, email: '[email protected]' },
developers: [ {
id: 1,
email: '[email protected]',
tasks: [
{ id: 48, status: 'TODO' },
{ id: 37, status: 'IN_PROGRESS' },
{ id: 28, status: 'IN_REVIEW' },
]
} ],
}
};
const projectStore = new Store(project);
projectStore.subscribe(project => {
console.log('Project changes');
})
projectStore.get('team.manager').subscribe(manager => {
console.log('Manager changes');
})
const developerStore = projectStore.get('team.developers').find(developer => developer.id === 1);
developerStore.subscribe(developer => {
console.log('Developer changes');
})
developerStore.get('tasks').find(task => task.id === 37).subscribe(task => {
console.log('Task changes');
})
project.team.manager = { id: 42, email: '[email protected]' };
projectStore.invalidate(); // or projectStore.value = project;
// 'Project changes' - 'Manager changes'
project.team.developers[DEV_ID_1_IDX].email = '[email protected]';
projectStore.invalidate();
// 'Project changes' - 'Developer changes'
project.team.developers[DEV_ID_1_IDX].tasks[TASK_ID_37_IDX].status = 'IN_REVIEW';
projectStore.invalidate();
// 'Project changes' - 'Developer changes' - 'Task changes'
project.team.developers[DEV_ID_1_IDX].id = 8;
projectStore.invalidate();
// 'Project changes' - 'Developer changes' (undefined) - 'Task changes' (undefined)
project.team.developers.push({ id: 1, email: '[email protected]' })
projectStore.invalidate();
// 'Project changes' - 'Developer changes'API
Table of Contents
Store
Store class holding a value and calling subscriber on value changes.
Parameters
valueany? Initial value. Optional. (optional, defaultundefined)
value
Stored data with setter invalidating store.
Type: any
invalidate
Force store to refesh
Returns void
subscribe
Add subscriber function to the store
Parameters
onChangefunction Function to call when change occurs
Returns string Id tu identify subscription
unsubscribe
Remove subscription from store.
Parameters
idstring Identifier of subcribtion.
Returns void
get
- See: lodash.get
Create a scoped store to limit changes at part of value.
Parameters
pathstring Path to expected scope
Returns Store Proxy store.
find
- See: lodash.find
Create a scoped store to limit changes at part of value.
Parameters
seekerfunction Function executed on each element in the value. First match returned.
Returns Store Proxy store.
