@plokkke/store

1.0.2 • Public • Published

Project name

Installation

npm install --save @plokkke/store

Import

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: 'fred@company.com' },
        productOwner: { id: 47, email: 'alfred@company.com' },
        developers: [ {
            id: 1,
            email: 'mistake@company.com',
            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: 'sarah@company.com' };
projectStore.invalidate(); // or projectStore.value = project;
// 'Project changes' - 'Manager changes'

project.team.developers[DEV_ID_1_IDX].email = 'tony@company.com';
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: 'tony@company.com' })
projectStore.invalidate();
// 'Project changes' - 'Developer changes'

API

Table of Contents

Store

Store class holding a value and calling subscriber on value changes.

Parameters

  • value any? Initial value. Optional. (optional, default undefined)

value

Stored data with setter invalidating store.

Type: any

invalidate

Force store to refesh

Returns void

subscribe

Add subscriber function to the store

Parameters
  • onChange function Function to call when change occurs

Returns string Id tu identify subscription

unsubscribe

Remove subscription from store.

Parameters
  • id string Identifier of subcribtion.

Returns void

get

  • See: lodash.get

Create a scoped store to limit changes at part of value.

Parameters
  • path string Path to expected scope

Returns Store Proxy store.

find

  • See: lodash.find

Create a scoped store to limit changes at part of value.

Parameters
  • seeker function Function executed on each element in the value. First match returned.

Returns Store Proxy store.

Package Sidebar

Install

npm i @plokkke/store

Weekly Downloads

1

Version

1.0.2

License

MIT

Unpacked Size

8.22 kB

Total Files

5

Last publish

Collaborators

  • plokkke