Skip to content

Commit

Permalink
Improve readability: set/get→ setPartialState/getPartialState.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgebucaran committed Mar 30, 2018
1 parent 110ec4f commit ac38e01
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,19 @@ export function app(state, actions, view, container) {
return out
}

function set(path, value, source) {
function setPartialState(path, value, source) {
var target = {}
if (path.length) {
target[path[0]] =
path.length > 1 ? set(path.slice(1), value, source[path[0]]) : value
path.length > 1
? setPartialState(path.slice(1), value, source[path[0]])
: value
return clone(source, target)
}
return value
}

function get(path, source) {
function getPartialState(path, source) {
var i = 0
while (i < path.length) {
source = source[path[i++]]
Expand All @@ -114,16 +116,20 @@ export function app(state, actions, view, container) {
var result = action(data)

if (typeof result === "function") {
result = result(get(path, globalState), actions)
result = result(getPartialState(path, globalState), actions)
}

if (
result &&
result !== (state = get(path, globalState)) &&
result !== (state = getPartialState(path, globalState)) &&
!result.then // !isPromise
) {
scheduleRender(
(globalState = set(path, clone(state, result), globalState))
(globalState = setPartialState(
path,
clone(state, result),
globalState
))
)
}

Expand Down

0 comments on commit ac38e01

Please sign in to comment.