Skip to content

Commit

Permalink
fix: add errors, clone deep in initContainer method and omit signals …
Browse files Browse the repository at this point in the history
…property in get method
  • Loading branch information
FabienMotte committed Feb 13, 2017
1 parent 3ff13ba commit 08783ec
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@
]
},
"dependencies": {
"lodash.clonedeep": "^4.5.0",
"lodash.isequal": "4.5.0",
"lodash.omit": "^4.5.0",
"quark-signal": "1.1.0"
}
}
45 changes: 35 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import Signal from 'quark-signal'

import isEqual from 'lodash.isequal'
import omit from 'lodash.omit'
import cloneDeep from 'lodash.clonedeep'

/**
* State class
Expand Down Expand Up @@ -31,35 +34,53 @@ class State {
* @param {string} query Query string
*
* @returns {any} Value
*
* @throws {Error} Cannot get a value from a container that does not exist
*/
get (query) {
const { container, splittedQuery } = this._parseStateQuery(query)

if (typeof container === 'undefined') {
throw new Error('State.get() : Cannot get a value from a container that does not exist')
}

let value = container

if (splittedQuery.length > 1) {
for (let i = 1, l = splittedQuery.length; i < l; i++) {
value = value[splittedQuery[i]]

if (typeof value === 'undefined' || value === null) {
// @todo throw an error if the value does not exist ?
break
}
}
}

// If it is a container, omit 'signals' property
if (splittedQuery.length === 1) {
return omit(value, 'signals')
}

return value
}

/**
* Set a value in State
* Set a value
*
* @param {string} query Query string
* @param {any} value Value to set
* @param {boolean} [forced=false] Flag to overwrite an object
*
* @throws {Error} Cannot set a value on a container that does not exist
*/
set (query, value, forced = false) {
const { container, containerId, splittedQuery } = this._parseStateQuery(query)

if (typeof container === 'undefined') {
throw new Error('State.set() : Cannot set a value on a container that does not exist')
}

let target = this._containers
for (let i = 0, l = splittedQuery.length; i < l; i++) {
const p = splittedQuery[i]
Expand All @@ -74,7 +95,7 @@ class State {
target[p] = value
} else {
target[p] = {
...target,
...oldVal,
...value
}
}
Expand Down Expand Up @@ -161,25 +182,29 @@ class State {
* @param {object} value Object to initialize the container
*/
initContainer (containerId, value) {
this._containers[containerId] = value
this._containers[containerId] = cloneDeep(value)
this._containers[containerId].signals = {}
}

/**
* Destroy a container
*
* @param {string} containerId Container id
*
* @throws {Error} Cannot destroy a container that does not exist
*/
destroyContainer (containerId) {
if (typeof this._containers[containerId] !== 'undefined') {
for (let signalProp in this._containers[containerId].signals) {
this._containers[containerId].signals[signalProp].removeAll()
this._containers[containerId].signals[signalProp] = null
}
if (typeof this._containers[containerId] === 'undefined') {
throw new Error('State.destroyContainer() : Cannot destroy a container that does not exist')
}

this._containers[containerId] = null
delete this._containers[containerId]
for (let signalProp in this._containers[containerId].signals) {
this._containers[containerId].signals[signalProp].removeAll()
this._containers[containerId].signals[signalProp] = null
}

this._containers[containerId] = null
delete this._containers[containerId]
}

/**
Expand Down

0 comments on commit 08783ec

Please sign in to comment.