@domql/state
Advanced tools
Comparing version 2.3.125 to 2.3.126
@@ -10,4 +10,4 @@ 'use strict' | ||
export const createState = function (element, parent, opts) { | ||
const skip = (opts && opts.skip) ? opts.skip : false | ||
export const createState = function (element, parent, options) { | ||
const skip = (options && options.skip) ? options.skip : false | ||
@@ -18,3 +18,3 @@ const objectizeState = checkForTypes(element) | ||
const whatInitReturns = triggerEventOn('stateInit', element) | ||
const whatInitReturns = triggerEventOn('stateInit', element, options) | ||
if (whatInitReturns === false) return element.state | ||
@@ -21,0 +21,0 @@ |
@@ -30,4 +30,4 @@ "use strict"; | ||
var import_inherit = require("./inherit"); | ||
const createState = function(element, parent, opts) { | ||
const skip = opts && opts.skip ? opts.skip : false; | ||
const createState = function(element, parent, options) { | ||
const skip = options && options.skip ? options.skip : false; | ||
const objectizeState = checkForTypes(element); | ||
@@ -38,3 +38,3 @@ if (objectizeState === false) | ||
element.state = (0, import_utils.deepClone)(objectizeState, import_ignore.IGNORE_STATE_PARAMS); | ||
const whatInitReturns = (0, import_event.triggerEventOn)("stateInit", element); | ||
const whatInitReturns = (0, import_event.triggerEventOn)("stateInit", element, options); | ||
if (whatInitReturns === false) | ||
@@ -41,0 +41,0 @@ return element.state; |
@@ -56,9 +56,14 @@ "use strict"; | ||
if (!options.preventStateUpdate) { | ||
state.update(state, { replace: true, skipOverwrite: true, options }); | ||
state.update(state, { replace: true, options }); | ||
} | ||
return state; | ||
}; | ||
const destroy = function() { | ||
const destroy = function(options = {}) { | ||
const state = this; | ||
const element = state.__element; | ||
const stateKey = element.__ref.__state; | ||
if ((0, import_utils.isString)(stateKey)) { | ||
element.parent.state.remove(stateKey, { isHoisted: true, ...options }); | ||
return element.state; | ||
} | ||
delete element.state; | ||
@@ -77,3 +82,3 @@ element.state = state.parent; | ||
} | ||
element.state.update(); | ||
element.state.update({}, { isHoisted: true, ...options }); | ||
return element.state; | ||
@@ -93,3 +98,3 @@ }; | ||
state.push(value); | ||
state.update(state.parse(), { replace: true, ...options }); | ||
state.update(state.parse(), { overwrite: "replace", ...options }); | ||
} else if ((0, import_utils.isObject)(state)) { | ||
@@ -106,3 +111,2 @@ const key = Object.keys(state).length; | ||
const state = this; | ||
console.log(state); | ||
if ((0, import_utils.isArray)(state)) | ||
@@ -116,4 +120,3 @@ (0, import_utils.removeFromArray)(state, key); | ||
const state = this; | ||
state.clean({ preventStateUpdate: true }); | ||
return state.update(value, { replace: true, ...options }); | ||
return state.clean({ preventStateUpdate: true }).update(value, { replace: true, ...options }); | ||
}; | ||
@@ -124,4 +127,4 @@ const apply = function(func, options = {}) { | ||
func(state); | ||
return state.update(state, { replace: true, ...options }); | ||
return state.update(state, { overwrite: "replace", ...options }); | ||
} | ||
}; |
@@ -30,4 +30,7 @@ "use strict"; | ||
const STATE_UPDATE_OPTIONS = { | ||
overwrite: true, | ||
preventHoistElementUpdate: false, | ||
updateByState: true | ||
updateByState: true, | ||
execStateFunction: true, | ||
stateFunctionOverwrite: "replace" | ||
}; | ||
@@ -37,2 +40,4 @@ const updateState = function(obj, options = STATE_UPDATE_OPTIONS) { | ||
const element = state.__element; | ||
if (!options.updateByState) | ||
(0, import_utils.merge)(options, STATE_UPDATE_OPTIONS); | ||
if (!state.__element) | ||
@@ -45,3 +50,3 @@ (0, import_report.report)("ElementOnStateIsNotDefined"); | ||
if (!options.preventInitStateUpdateListener) { | ||
const initStateUpdateReturns = (0, import_event.triggerEventOn)("initStateUpdated", element, obj); | ||
const initStateUpdateReturns = (0, import_event.triggerEventOnUpdate)("initStateUpdated", obj, element, options); | ||
if (initStateUpdateReturns === false) | ||
@@ -51,4 +56,4 @@ return element; | ||
applyOverwrite(state, obj, options); | ||
const updateIsHousted = hoistStateUpdate(state, obj, options); | ||
if (updateIsHousted) | ||
const updateIsHoisted = hoistStateUpdate(state, obj, options); | ||
if (updateIsHoisted) | ||
return state; | ||
@@ -58,3 +63,3 @@ updateDependentState(state, obj, options); | ||
if (!options.preventStateUpdateListener) { | ||
(0, import_event.triggerEventOn)("stateUpdated", element, obj); | ||
(0, import_event.triggerEventOnUpdate)("stateUpdated", obj, element, options); | ||
} | ||
@@ -64,11 +69,13 @@ return state; | ||
const applyOverwrite = (state, obj, options) => { | ||
const { skipOverwrite, shallow } = options; | ||
if (skipOverwrite === "merge") { | ||
const { overwrite } = options; | ||
if (!overwrite) | ||
return; | ||
const shallow = overwrite === "shallow"; | ||
const merge2 = overwrite === "merge"; | ||
if (merge2) { | ||
(0, import_utils.deepMerge)(state, obj, import_ignore.IGNORE_STATE_PARAMS); | ||
return; | ||
} | ||
if (!skipOverwrite) { | ||
const overwriteFunc = shallow ? import_utils.overwriteShallow : import_utils.overwriteDeep; | ||
overwriteFunc(state, obj, import_ignore.IGNORE_STATE_PARAMS); | ||
} | ||
const overwriteFunc = shallow ? import_utils.overwriteShallow : import_utils.overwriteDeep; | ||
overwriteFunc(state, obj, import_ignore.IGNORE_STATE_PARAMS); | ||
}; | ||
@@ -95,6 +102,13 @@ const hoistStateUpdate = (state, obj, options) => { | ||
targetParent.update(changesValue, { | ||
execStateFunction: false, | ||
stateFunctionOverwrite: false, | ||
isHoisted: true, | ||
...options, | ||
preventUpdate: options.preventHoistElementUpdate, | ||
skipOverwrite: options.replace | ||
overwrite: !options.replace | ||
}); | ||
const hasNotUpdated = !options.preventUpdate || !options.preventHoistElementUpdate; | ||
if (!options.preventStateUpdateListener && hasNotUpdated) { | ||
(0, import_event.triggerEventOnUpdate)("stateUpdated", obj, element, options); | ||
} | ||
return true; | ||
@@ -101,0 +115,0 @@ }; |
'use strict' | ||
import { isArray, isFunction, isObject, removeFromArray, removeFromObject } from '@domql/utils' | ||
import { isArray, isFunction, isObject, isString, removeFromArray, removeFromObject } from '@domql/utils' | ||
@@ -30,3 +30,3 @@ import { IGNORE_STATE_PARAMS } from './ignore' | ||
if (!options.preventStateUpdate) { | ||
state.update(state, { replace: true, skipOverwrite: true, options }) | ||
state.update(state, { replace: true, options }) | ||
} | ||
@@ -36,5 +36,12 @@ return state | ||
export const destroy = function () { | ||
export const destroy = function (options = {}) { | ||
const state = this | ||
const element = state.__element | ||
const stateKey = element.__ref.__state | ||
if (isString(stateKey)) { | ||
element.parent.state.remove(stateKey, { isHoisted: true, ...options }) | ||
return element.state | ||
} | ||
delete element.state | ||
@@ -56,3 +63,3 @@ element.state = state.parent | ||
element.state.update() | ||
element.state.update({}, { isHoisted: true, ...options }) | ||
return element.state | ||
@@ -73,3 +80,3 @@ } | ||
state.push(value) | ||
state.update(state.parse(), { replace: true, ...options }) | ||
state.update(state.parse(), { overwrite: 'replace', ...options }) | ||
} else if (isObject(state)) { | ||
@@ -88,3 +95,2 @@ const key = Object.keys(state).length | ||
const state = this | ||
console.log(state) | ||
if (isArray(state)) removeFromArray(state, key) | ||
@@ -97,4 +103,4 @@ if (isObject(state)) removeFromObject(state, key) | ||
const state = this | ||
state.clean({ preventStateUpdate: true }) | ||
return state.update(value, { replace: true, ...options }) | ||
return state.clean({ preventStateUpdate: true }) | ||
.update(value, { replace: true, ...options }) | ||
} | ||
@@ -106,4 +112,4 @@ | ||
func(state) | ||
return state.update(state, { replace: true, ...options }) | ||
return state.update(state, { overwrite: 'replace', ...options }) | ||
} | ||
} |
{ | ||
"name": "@domql/state", | ||
"version": "2.3.125", | ||
"version": "2.3.126", | ||
"license": "MIT", | ||
@@ -29,3 +29,3 @@ "type": "module", | ||
}, | ||
"gitHead": "c62e01157f488334d07a8bbd67f892d617d48f06" | ||
"gitHead": "6329de71d2e831d0efd9d59e765567aa8c563481" | ||
} |
'use strict' | ||
import { report } from '@domql/report' | ||
import { triggerEventOn } from '@domql/event' | ||
import { triggerEventOnUpdate } from '@domql/event' | ||
import { IGNORE_STATE_PARAMS } from './ignore' | ||
import { deepMerge, overwriteDeep, overwriteShallow } from '@domql/utils' | ||
import { deepMerge, merge, overwriteDeep, overwriteShallow } from '@domql/utils' | ||
import { checkIfInherits, createChangesByKey, findInheritedState, getParentStateInKey } from './inherit' | ||
const STATE_UPDATE_OPTIONS = { | ||
overwrite: true, | ||
preventHoistElementUpdate: false, | ||
updateByState: true | ||
updateByState: true, | ||
execStateFunction: true, | ||
stateFunctionOverwrite: 'replace' | ||
} | ||
@@ -18,2 +21,4 @@ | ||
if (!options.updateByState) merge(options, STATE_UPDATE_OPTIONS) | ||
if (!state.__element) report('ElementOnStateIsNotDefined') | ||
@@ -25,3 +30,3 @@ if (options.preventInheritAtCurrentState === true) { | ||
if (!options.preventInitStateUpdateListener) { | ||
const initStateUpdateReturns = triggerEventOn('initStateUpdated', element, obj) | ||
const initStateUpdateReturns = triggerEventOnUpdate('initStateUpdated', obj, element, options) | ||
if (initStateUpdateReturns === false) return element | ||
@@ -32,4 +37,4 @@ } | ||
const updateIsHousted = hoistStateUpdate(state, obj, options) | ||
if (updateIsHousted) return state | ||
const updateIsHoisted = hoistStateUpdate(state, obj, options) | ||
if (updateIsHoisted) return state | ||
@@ -41,3 +46,3 @@ updateDependentState(state, obj, options) | ||
if (!options.preventStateUpdateListener) { | ||
triggerEventOn('stateUpdated', element, obj) | ||
triggerEventOnUpdate('stateUpdated', obj, element, options) | ||
} | ||
@@ -49,5 +54,9 @@ | ||
const applyOverwrite = (state, obj, options) => { | ||
const { skipOverwrite, shallow } = options | ||
const { overwrite } = options | ||
if (!overwrite) return | ||
if (skipOverwrite === 'merge') { | ||
const shallow = overwrite === 'shallow' | ||
const merge = overwrite === 'merge' | ||
if (merge) { | ||
deepMerge(state, obj, IGNORE_STATE_PARAMS) | ||
@@ -57,11 +66,4 @@ return | ||
// if (skipOverwrite === 'skipOverwrite') { | ||
// deepMerge(state, obj, IGNORE_STATE_PARAMS) | ||
// return | ||
// } | ||
if (!skipOverwrite) { | ||
const overwriteFunc = shallow ? overwriteShallow : overwriteDeep | ||
overwriteFunc(state, obj, IGNORE_STATE_PARAMS) | ||
} | ||
const overwriteFunc = shallow ? overwriteShallow : overwriteDeep | ||
overwriteFunc(state, obj, IGNORE_STATE_PARAMS) | ||
} | ||
@@ -89,6 +91,13 @@ | ||
targetParent.update(changesValue, { | ||
execStateFunction: false, | ||
stateFunctionOverwrite: false, | ||
isHoisted: true, | ||
...options, | ||
preventUpdate: options.preventHoistElementUpdate, | ||
skipOverwrite: options.replace | ||
overwrite: !options.replace | ||
}) | ||
const hasNotUpdated = !options.preventUpdate || !options.preventHoistElementUpdate | ||
if (!options.preventStateUpdateListener && hasNotUpdated) { | ||
triggerEventOnUpdate('stateUpdated', obj, element, options) | ||
} | ||
return true | ||
@@ -95,0 +104,0 @@ } |
33540
891