Commit 581e7676 authored by Grandschtroumpf's avatar Grandschtroumpf

update store

parent 838135a7
......@@ -24,7 +24,7 @@ export class Store {
constructor (name, initialState) {
this.event = new EventEmitter()
this.name = name
this.state = initialState
this.state = initialState || {}
}
/** Listen on event from the store */
......@@ -41,11 +41,10 @@ export class Store {
* Update one field of the state
* @param {Partial<T>} state The part of the state updated
*/
/*
update(state) {
update (state) {
this.state = { ...this.state, ...state }
this.dispatch()
}
*/
/**
* Get one field of the state
......@@ -53,7 +52,7 @@ export class Store {
* @param {Key} key A key of the state
*/
get (key) {
return this.state.entities[key]
return this.state[key]
}
/** Reset the state its initial value */
......@@ -80,41 +79,54 @@ export class EntityStore extends Store {
/**
* Instanciate the store from `localStorage` first
* @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`
*/
static fromLocal (name, initialState) {
static fromLocal (name, keyId, initialState = {}) {
const fromLocal = localStorage.getItem(name)
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
* @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`
*/
/*
constructor (name, initialState) {
constructor (name, keyId, initialState) {
super(name, initialState)
this.keyId = keyId || 'id'
}
*/
/** Tne entities as a Map */
/**
* The entities as a Map
* @returns {Object}
*/
get entities () {
return this.state.entities
}
/** List of all the ids */
/**
* List of all the ids
* @returns {(string|number)[]}
*/
get ids () {
return this.state.ids
}
/** List of all active ID */
/**
* List of all active ID
* @returns {Object[]}
*/
get actives () {
return this.state.actives
}
/** Return the length of the entity collection */
/**
* Return the length of the entity collection
* @returns {number}
*/
get length () {
return this.state.ids.length
}
......@@ -126,7 +138,7 @@ export class EntityStore extends Store {
add (id, entity) {
this.state.entities[id] = entity
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 {
* @param {Array} 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 {
* @param {(string|number)} id The id of the entity to remove
*/
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]
this.state.ids.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 {
* @param {(string|number)} id The id of the entity to update
* @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],
...update
}
this.event.emit('update', this.state.entities[id])
}
*/
/**
* Activate one or several entity from the state
......@@ -186,6 +213,7 @@ export class EntityStore extends Store {
/**
* Get one entity
* @param {(string|number)} id The id of the entity to get
* @returns {Object}
*/
getOne (id) {
return this.state.entities[id]
......@@ -194,17 +222,24 @@ export class EntityStore extends Store {
/**
* Get many entities as an array
* @param {(string|number)[]} ids An array of id of entity to get
* @returns {Object[]}
*/
getMany (ids) {
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 () {
return this.state.ids.map(id => this.state.entities[id])
}
/** Get all active entities */
/**
* Get all active entities
* @returns {Object[]}
*/
getActives () {
return this.state.actives.map(id => this.state.entities[id])
}
......@@ -212,6 +247,7 @@ export class EntityStore extends Store {
/**
* Is the entity active
* @param {(string|number)} id The id of the entity to check
* @returns {boolean}
*/
isActive (id) {
return this.state.actives.includes(id)
......@@ -220,6 +256,7 @@ export class EntityStore extends Store {
/**
* Is this id inside the store
* @param {(string|number)} id The id of the entity to check
* @returns {boolean}
*/
hasEntity (id) {
return this.state.ids.includes(id)
......@@ -228,6 +265,7 @@ export class EntityStore extends Store {
/**
* Is the state empty
* @param {(string|number)} id The id of the entity to check
* @returns {boolean}
*/
isEmpty () {
return this.state.ids.length === 0
......@@ -238,8 +276,7 @@ export class EntityStore extends Store {
* Store the state of the stores into LocalStorage
* @param {Store[]} stores The list of stores to store into `localStorage`
*/
/*
function localState (stores) {
export function localState (stores) {
stores.forEach(store => {
const name = store.name
store.on('newState', (state) => {
......@@ -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