Commit 581e7676 authored by Grandschtroumpf's avatar Grandschtroumpf

update store

parent 838135a7
...@@ -24,7 +24,7 @@ export class Store { ...@@ -24,7 +24,7 @@ export class Store {
constructor (name, initialState) { constructor (name, initialState) {
this.event = new EventEmitter() this.event = new EventEmitter()
this.name = name this.name = name
this.state = initialState this.state = initialState || {}
} }
/** Listen on event from the store */ /** Listen on event from the store */
...@@ -41,11 +41,10 @@ export class Store { ...@@ -41,11 +41,10 @@ export class Store {
* Update one field of the state * Update one field of the state
* @param {Partial<T>} state The part of the state updated * @param {Partial<T>} state The part of the state updated
*/ */
/* update (state) {
update(state) {
this.state = { ...this.state, ...state } this.state = { ...this.state, ...state }
this.dispatch()
} }
*/
/** /**
* Get one field of the state * Get one field of the state
...@@ -53,7 +52,7 @@ export class Store { ...@@ -53,7 +52,7 @@ export class Store {
* @param {Key} key A key of the state * @param {Key} key A key of the state
*/ */
get (key) { get (key) {
return this.state.entities[key] return this.state[key]
} }
/** Reset the state its initial value */ /** Reset the state its initial value */
...@@ -80,41 +79,54 @@ export class EntityStore extends Store { ...@@ -80,41 +79,54 @@ export class EntityStore extends Store {
/** /**
* Instanciate the store from `localStorage` first * Instanciate the store from `localStorage` first
* @param {string} name The name of the store * @param {string} name The name of the store
* @param {(string|number)} keyId The name of the key used as a unique ID for the entity
* @param {EntityState} initialState The initial state used if state is not available in `localStorage` * @param {EntityState} initialState The initial state used if state is not available in `localStorage`
*/ */
static fromLocal (name, initialState) { static fromLocal (name, keyId, initialState = {}) {
const fromLocal = localStorage.getItem(name) const fromLocal = localStorage.getItem(name)
const intial = fromLocal ? JSON.parse(fromLocal) : initialState const intial = fromLocal ? JSON.parse(fromLocal) : initialState
return new EntityStore(name, intial) return new EntityStore(name, keyId, intial || { ids: [], actives: [] })
} }
/** /**
* Create a entity Store that hold a map entity of the same model * Create a entity Store that hold a map entity of the same model
* @param {string} name The name of the store * @param {string} name The name of the store
* @param {(string|number)} keyId The name of the key used as a unique ID for the entity
* @param {EntityState} initialState The initial state used if state is not available in `localStorage` * @param {EntityState} initialState The initial state used if state is not available in `localStorage`
*/ */
/* constructor (name, keyId, initialState) {
constructor (name, initialState) {
super(name, initialState) super(name, initialState)
this.keyId = keyId || 'id'
} }
*/
/** Tne entities as a Map */ /**
* The entities as a Map
* @returns {Object}
*/
get entities () { get entities () {
return this.state.entities return this.state.entities
} }
/** List of all the ids */ /**
* List of all the ids
* @returns {(string|number)[]}
*/
get ids () { get ids () {
return this.state.ids return this.state.ids
} }
/** List of all active ID */ /**
* List of all active ID
* @returns {Object[]}
*/
get actives () { get actives () {
return this.state.actives return this.state.actives
} }
/** Return the length of the entity collection */ /**
* Return the length of the entity collection
* @returns {number}
*/
get length () { get length () {
return this.state.ids.length return this.state.ids.length
} }
...@@ -126,7 +138,7 @@ export class EntityStore extends Store { ...@@ -126,7 +138,7 @@ export class EntityStore extends Store {
add (id, entity) { add (id, entity) {
this.state.entities[id] = entity this.state.entities[id] = entity
this.state.ids.push(id) this.state.ids.push(id)
this.event.emit('add', id, entity) this.event.emit('add', entity)
} }
/** /**
...@@ -134,7 +146,11 @@ export class EntityStore extends Store { ...@@ -134,7 +146,11 @@ export class EntityStore extends Store {
* @param {Array} entities * @param {Array} entities
*/ */
addEntities (entities) { addEntities (entities) {
entities.forEach((entity) => { this.add(entity.profile.name, entity) }) entities.forEach((entity) => {
if (!entity[this.keyId]) throw new Error(`Key ${this.keyId} doesn't exist in ${entity}`)
this.add(entity[this.keyId], entity)
})
this.event.emit('add', entities)
} }
/** /**
...@@ -142,9 +158,20 @@ export class EntityStore extends Store { ...@@ -142,9 +158,20 @@ export class EntityStore extends Store {
* @param {(string|number)} id The id of the entity to remove * @param {(string|number)} id The id of the entity to remove
*/ */
remove (id) { remove (id) {
if (!this.state.entities[id]) throw new Error(`No entity with key ${id} found in store ${this.name}`)
delete this.state.entities[id] delete this.state.entities[id]
this.state.ids.splice(this.state.ids.indexOf(id), 1) this.state.ids.splice(this.state.ids.indexOf(id), 1)
this.state.actives.splice(this.state.ids.indexOf(id), 1) this.state.actives.splice(this.state.ids.indexOf(id), 1)
this.event.emit('remove', id)
}
/** Remove all entity from the state and reset actives and ids to empty */
clear () {
this.state = {
ids: [],
actives: []
}
this.event.emit('clear')
} }
/** /**
...@@ -152,14 +179,14 @@ export class EntityStore extends Store { ...@@ -152,14 +179,14 @@ export class EntityStore extends Store {
* @param {(string|number)} id The id of the entity to update * @param {(string|number)} id The id of the entity to update
* @param {Object} update The fields to update in the entity * @param {Object} update The fields to update in the entity
*/ */
/* updateOne (id, update) {
updateOne(id, update) { if (!this.state.entities[id]) throw new Error(`No entity with key ${id} found in store ${this.name}`)
this.state.entities[id] = { this.state.entities[id] = {
...this.state.entities[id], ...this.state.entities[id],
...update ...update
} }
this.event.emit('update', this.state.entities[id])
} }
*/
/** /**
* Activate one or several entity from the state * Activate one or several entity from the state
...@@ -186,6 +213,7 @@ export class EntityStore extends Store { ...@@ -186,6 +213,7 @@ export class EntityStore extends Store {
/** /**
* Get one entity * Get one entity
* @param {(string|number)} id The id of the entity to get * @param {(string|number)} id The id of the entity to get
* @returns {Object}
*/ */
getOne (id) { getOne (id) {
return this.state.entities[id] return this.state.entities[id]
...@@ -194,17 +222,24 @@ export class EntityStore extends Store { ...@@ -194,17 +222,24 @@ export class EntityStore extends Store {
/** /**
* Get many entities as an array * Get many entities as an array
* @param {(string|number)[]} ids An array of id of entity to get * @param {(string|number)[]} ids An array of id of entity to get
* @returns {Object[]}
*/ */
getMany (ids) { getMany (ids) {
return ids.map(id => this.state.entities[id]) return ids.map(id => this.state.entities[id])
} }
/** Get all the entities as an array */ /**
* Get all the entities as an array
* @returns {Object[]}
*/
getAll () { getAll () {
return this.state.ids.map(id => this.state.entities[id]) return this.state.ids.map(id => this.state.entities[id])
} }
/** Get all active entities */ /**
* Get all active entities
* @returns {Object[]}
*/
getActives () { getActives () {
return this.state.actives.map(id => this.state.entities[id]) return this.state.actives.map(id => this.state.entities[id])
} }
...@@ -212,6 +247,7 @@ export class EntityStore extends Store { ...@@ -212,6 +247,7 @@ export class EntityStore extends Store {
/** /**
* Is the entity active * Is the entity active
* @param {(string|number)} id The id of the entity to check * @param {(string|number)} id The id of the entity to check
* @returns {boolean}
*/ */
isActive (id) { isActive (id) {
return this.state.actives.includes(id) return this.state.actives.includes(id)
...@@ -220,6 +256,7 @@ export class EntityStore extends Store { ...@@ -220,6 +256,7 @@ export class EntityStore extends Store {
/** /**
* Is this id inside the store * Is this id inside the store
* @param {(string|number)} id The id of the entity to check * @param {(string|number)} id The id of the entity to check
* @returns {boolean}
*/ */
hasEntity (id) { hasEntity (id) {
return this.state.ids.includes(id) return this.state.ids.includes(id)
...@@ -228,6 +265,7 @@ export class EntityStore extends Store { ...@@ -228,6 +265,7 @@ export class EntityStore extends Store {
/** /**
* Is the state empty * Is the state empty
* @param {(string|number)} id The id of the entity to check * @param {(string|number)} id The id of the entity to check
* @returns {boolean}
*/ */
isEmpty () { isEmpty () {
return this.state.ids.length === 0 return this.state.ids.length === 0
...@@ -238,8 +276,7 @@ export class EntityStore extends Store { ...@@ -238,8 +276,7 @@ export class EntityStore extends Store {
* Store the state of the stores into LocalStorage * Store the state of the stores into LocalStorage
* @param {Store[]} stores The list of stores to store into `localStorage` * @param {Store[]} stores The list of stores to store into `localStorage`
*/ */
/* export function localState (stores) {
function localState (stores) {
stores.forEach(store => { stores.forEach(store => {
const name = store.name const name = store.name
store.on('newState', (state) => { store.on('newState', (state) => {
...@@ -247,4 +284,3 @@ function localState (stores) { ...@@ -247,4 +284,3 @@ function localState (stores) {
}) })
}) })
} }
*/
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment