npm package: @plokkke/store
Description: Data store for redux pattern
Author: Antoine Caron
Latest version: 1.0.2

# Project name

## Installation

```bash
npm install --save @plokkke/store
```

### Import

```javascript
const Store = require('@plokkke/store');
```

### Simple case

```javascript
const store = new Store();

store.subscribe(value => {
    // Do something meaningful
})

store.value = 'Important data';
// Calling subscribers on next event loop.
```

### Advanced case

```javascript
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

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

#### Table of Contents

-   [Store](#store)
    -   [Parameters](#parameters)
    -   [value](#value)
    -   [invalidate](#invalidate)
    -   [subscribe](#subscribe)
        -   [Parameters](#parameters-1)
    -   [unsubscribe](#unsubscribe)
        -   [Parameters](#parameters-2)
    -   [get](#get)
        -   [Parameters](#parameters-3)
    -   [find](#find)
        -   [Parameters](#parameters-4)

### 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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Function to call when change occurs

Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Id tu identify subscription

#### unsubscribe

Remove subscription from store.

##### Parameters

-   `id` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Identifier of subcribtion.

Returns **void** 

#### get

-   **See: lodash.get**

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

##### Parameters

-   `path` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Path to expected scope

Returns **[Store](#store)** Proxy store.

#### find

-   **See: lodash.find**

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

##### Parameters

-   `seeker` **[function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Function executed on each element in the value. First match returned.

Returns **[Store](#store)** Proxy store.