<commit_before># Justore


We don't need flux , just the store please!


**The library itself require a Promise supported environment.**

[TodoList Example](https://github.com/regou/justore-todo)

### Installation

```sh
npm install justore --save
```

### Basic Usage

```js
var justore = require("justore");
var initData = {};
var store = new justore(initData,'Store Name');


//Listen change
//Add event listeners before change
store.change.on('todos',function(newVal,prevVal){
  store.read('todos') === newVal //true
});

//Write or change data to store, return a Promise
store.write('todos',['drink','cook']);

//Read data (Writing data into store is synchronous,so you can read & write in a same block)
store.read('todos');

//Listen all changes
store.change.on('*',function(changedKeys){
  changedKeys.length == 1 //true
});
```


### Advanced usage

- `store.write(key,data [,options])`

    Write data to the store, return a Promise
    ```js
    store.write('todos',['drink','cook'],{
          //Boolean. If true, change the store without trigger any events
          mute:false, 
          
          //Wait for other actions, such as http request, MUST return a Promise 
          waitFor: function(data){ 
            return new Promise(function(res){
              resolve(data.concat['eat']);//Overwrite corresponding store value with resolved data  
            }) 
          }
    });
    ```    
      
    You can wait for other stores to change, remember `store.write` return a Promise too!  
    ```js
    options:{
          waitFor: function(data){ 
            return store2.write('exampleKey',2);
          }
    }
    ```
    Note use '*' as key can overwrite all value by passing an ImmutableJS Map
        
- `store.change`

    The [EventEmiter](https://nodejs.org/api/events.html#events_class_events_eventemitter) of the store

- `store.trigger(key)`

    Just trigger the events.*Normally you don't need to do this.*
    ```js
    store.trigger('todos');
    ```

- `store.read(key)`

    Get value for attribute by passing the key.
    ```js
    store.read("todos") --> ['drink','cook','eat']
    ```
    Note use '*' as key can read all value as an ImmutableJS Map

- `store.readAsClone(key,isDeep)`

    Similar to `store.read(key)` ,but return a [DeepCloned](https://lodash.com/docs#clone) value
    

### React Mixin helper
- `store.createReactMixin(key)`
 
    Return a mixin. Will call `onStoreChange` method on a React component when store change.



### Why sometimes change event not fired?
You may read the JavaScript Mutable objects (Array , Object),and change them directly without cloning.
Clone before mutate it or try one of these:

- Use [Immutable](https://facebook.github.io/immutable-js/) data structures (Recommend)

- Trigger events by your self.  `store.trigger(key)`

- Read the data as clone.  `store.readAsClone(key)`

<commit_msg>update readme: read and write *<commit_after># Justore


We don't need flux , just the store!


**The library itself require a Promise supported environment.**

[TodoList Example](https://github.com/regou/justore-todo)

### Installation

```sh
npm install justore --save
```

### Basic Usage

```js
var justore = require("justore");
var initData = {};
var store = new justore(initData,'Store Name');


//Listen change
//Add event listeners before change
store.change.on('todos',function(newVal,prevVal){
  store.read('todos') === newVal //true
});

//Write or change data to store, return a Promise
store.write('todos',['drink','cook']);

//Read data (Writing data into store is synchronous,so you can read & write on same block)
store.read('todos');

//Listen all changes
store.change.on('*',function(changedKeys){
  changedKeys.length == 1 //true
});
```


### Advanced usage

- `store.read('*')`

	Read all value (As an ImmutableJS Map)

- `store.write('*',ImmutablejsMap)`

	Overwrite store by passing an ImmutableJS Map (most likely read and modified from `store.read('*')`)
	
**Read & write `'*'` won't affect performance and they are recommended usage.**

	

- `store.write(key,data [,options])`

    Write data to the store, return a Promise
    ```js
    store.write('todos',['drink','cook'],{
          //Boolean. If true, change the store without trigger any events
          mute:false, 
          
          //Wait for other actions, such as http request, MUST return a Promise 
          waitFor: function(data){ 
            return new Promise(function(res){
              resolve(data.concat['eat']);//Overwrite corresponding store value with resolved data  
            }) 
          }
    });
    ```    
      
    You can wait for other stores to change, remember `store.write` return a Promise too!  
    ```js
    options:{
          waitFor: function(data){ 
            return store2.write('exampleKey',2);
          }
    }
    ```
    Note use '*' as key can overwrite all value by passing an ImmutableJS Map
        
- `store.change`

    The [EventEmiter](https://nodejs.org/api/events.html#events_class_events_eventemitter) of the store

- `store.trigger(key)`

    Just trigger the events.*Normally you don't need to do this.*
    ```js
    store.trigger('todos');
    ```

- `store.read(key)`

    Get value for attribute by passing the key.
    ```js
    store.read("todos") --> ['drink','cook','eat']
    ```

- `store.readAsClone(key,isDeep)`

    Similar to `store.read(key)` ,but return a [DeepCloned](https://lodash.com/docs#clone) value
    

### React Mixin helper
- `store.createReactMixin(key)`
 
    Return a mixin. Will call `onStoreChange` method on a React component when store change.



### Why sometimes change event not fired?
You may read the JavaScript Mutable objects (Array , Object),and change them directly without cloning.
Clone before mutate it or try one of these:

- Use [Immutable](https://facebook.github.io/immutable-js/) data structures (Recommend)

- Trigger events by your self.  `store.trigger(key)`

- Read the data as clone.  `store.readAsClone(key)`


{'fraction_non_alphanumeric': 0.13379930104842735, 'fraction_numerical': 0.000998502246630055, 'mean_word_length': 3.124914207275223, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '275206', 'n_tokens_mistral': 1851, 'n_tokens_neox': 1779, 'n_words': 588}