@cicada/render
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -43,2 +43,25 @@ 'use strict'; | ||
test('set nested object', function () { | ||
var path = 'root'; | ||
store.register(path, function () { | ||
return { obj: {} }; | ||
}); | ||
store.set(path, { obj: { a: 1, b: 2 } }); | ||
store.set(path, { obj: { c: 3 } }); | ||
expect(store.get(path + '.obj.a')).toBe(undefined); | ||
expect(store.get(path + '.obj.b')).toBe(undefined); | ||
expect(store.get(path + '.obj.c')).toBe(3); | ||
}); | ||
test('set state from descendant', function () { | ||
var path = 'root'; | ||
store.register(path, function () { | ||
return { obj: {} }; | ||
}); | ||
store.set(path, { obj: { a: { b: 1, c: 2 } }, other: 1 }); | ||
store.set(path + '.obj', 3); | ||
expect(store.get(path + '.obj')).toBe(3); | ||
expect(store.get(path + '.other')).toBe(1); | ||
}); | ||
test('merge initial state', function () { | ||
@@ -45,0 +68,0 @@ var data = function data() { |
@@ -248,4 +248,2 @@ 'use strict'; | ||
// CAUTION,只要是从外部接受过值,那么内外数据就一定一致,所以这里修正一下 | ||
instance.currentFrom = state; | ||
instance.nextFrom = state; | ||
instance.state = state; | ||
@@ -252,0 +250,0 @@ instance.stateTree.flush(); |
@@ -11,2 +11,6 @@ 'use strict'; | ||
var _keys = require('babel-runtime/core-js/object/keys'); | ||
var _keys2 = _interopRequireDefault(_keys); | ||
var _typeof2 = require('babel-runtime/helpers/typeof'); | ||
@@ -131,2 +135,8 @@ | ||
function findParentPath(path) { | ||
var arr = path.split('.'); | ||
arr.pop(); | ||
return arr.join('.'); | ||
} | ||
function findClosestInitialPath(statePath) { | ||
@@ -154,2 +164,13 @@ var result = statePath; | ||
function shapeObject(stateNodePath, relativePath, nextValue) { | ||
var origin = _exist2.default.get(stateTree, (0, _common.joinPath)([stateNodePath, relativePath])); | ||
if ((0, _util.isObject)(origin)) { | ||
(0, _keys2.default)(origin).forEach(function (key) { | ||
/* eslint-disable no-prototype-builtins*/ | ||
if (!nextValue.hasOwnProperty(key)) delete origin[key]; | ||
/* eslint-enable no-prototype-builtins*/ | ||
}); | ||
} | ||
} | ||
// CAUTION 递归 merge 规则,只对纯对象嵌套的有效,一旦中间夹有数组,就不再 merge了。 | ||
@@ -163,2 +184,7 @@ function setObject(stateNodePath, relativePath, inputValue, inputStateId, mergeLastState) { | ||
var nextValue = mergeLastState ? inputValue : (0, _extends3.default)({}, initialValue, inputValue); | ||
// CAUTION 如果不是 mergeLastState, 那么一定要裁剪掉原本数据上多余的值! | ||
if (mergeLastState === false) { | ||
shapeObject(stateNodePath, relativePath, nextValue); | ||
} | ||
(0, _util.each)(nextValue, function (value, key) { | ||
@@ -198,2 +224,15 @@ var childPath = (0, _common.joinPath)([relativePath, key]); | ||
function setStateValue(statePath, key, value, stateId, inputStatePath, mergeLastState, initialState) { | ||
var changes = []; | ||
if ((typeof value === 'undefined' ? 'undefined' : (0, _typeof3.default)(value)) !== 'object') { | ||
changes = changes.concat(setNaive(statePath, key, value, stateId, inputStatePath)); | ||
} else if ((0, _util.isObject)(value)) { | ||
var initialValue = initialState === undefined ? undefined : initialState[key]; | ||
changes = changes.concat(setObject(statePath, key, value, stateId, mergeLastState, initialValue, inputStatePath)); | ||
} else { | ||
changes = changes.concat(setArray(statePath, key, value, stateId, mergeLastState, inputStatePath)); | ||
} | ||
return changes; | ||
} | ||
function setStateNode(statePath, inputState, mergeLastState, inputStatePath) { | ||
@@ -205,9 +244,3 @@ var changes = []; | ||
(0, _util.each)(finalState, function (value, key) { | ||
if ((typeof value === 'undefined' ? 'undefined' : (0, _typeof3.default)(value)) !== 'object') { | ||
changes = changes.concat(setNaive(statePath, key, value, stateId, inputStatePath)); | ||
} else if ((0, _util.isObject)(value)) { | ||
changes = changes.concat(setObject(statePath, key, value, stateId, mergeLastState, initialState[key], inputStatePath)); | ||
} else { | ||
changes = changes.concat(setArray(statePath, key, value, stateId, mergeLastState, inputStatePath)); | ||
} | ||
changes = changes.concat(setStateValue(statePath, key, value, stateId, inputStatePath, mergeLastState, initialState)); | ||
}); | ||
@@ -218,15 +251,32 @@ | ||
function patchChanges(stateNodePath, relativeChildPath, stateId, inputStatePath) { | ||
return relativeChildPath === '' ? [] : relativeChildPath.split('.').reduce(function (last, current) { | ||
var nextPath = last.length > 0 ? last[last.length - 1].valuePath + '.' + current : current; | ||
return last.concat({ | ||
stateId: stateId, | ||
statePath: stateNodePath, | ||
valuePath: nextPath, | ||
inputStatePath: inputStatePath | ||
}); | ||
}, []); | ||
} | ||
function setStateNodeFromDescendant(inputStatePath, inputState, mergeLastState) { | ||
var stateNodePath = findClosestInitialPath(inputStatePath); | ||
var stateId = get(stateNodePath)._id; | ||
var initialState = initialStates[stateId].getInitialState(); | ||
var relativeChildPath = findRelativePath(stateNodePath, inputStatePath); | ||
var parentPath = findParentPath(inputStatePath); | ||
var childPath = findRelativePath(parentPath, inputStatePath); | ||
var initialStateValue = _exist2.default.get(initialState, findParentPath(relativeChildPath), undefined); | ||
// CAUTION patchChange 第二个参数一定要 findParentPath, 因为后面的 setStateValue 也会记录最后一个 path | ||
return patchChanges(stateNodePath, findParentPath(relativeChildPath), stateId, inputStatePath).concat(setStateValue(parentPath, childPath, inputState, stateId, inputStatePath, mergeLastState, initialStateValue)); | ||
} | ||
function set(inputStatePath, inputState) { | ||
var mergeLastState = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; | ||
var statePath = inputStatePath; | ||
var nextState = inputState; | ||
var last = get(statePath, {}); | ||
if (!last._id) { | ||
statePath = findClosestInitialPath(statePath); | ||
nextState = {}; | ||
_exist2.default.set(nextState, findRelativePath(statePath, inputStatePath), inputState, true); | ||
} | ||
return setStateNode(statePath, nextState, mergeLastState, inputStatePath); | ||
return !get(inputStatePath, {})._id ? setStateNodeFromDescendant(inputStatePath, inputState, mergeLastState) : setStateNode(inputStatePath, inputState, mergeLastState, inputStatePath); | ||
} | ||
@@ -233,0 +283,0 @@ |
{ | ||
"name": "@cicada/render", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"main": "./lib/index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
320560
7758