@domql/state 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -0
- package/src/index.js +51 -0
package/package.json
ADDED
package/src/index.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
import { on } from '../event'
|
|
4
|
+
import { deepClone, exec, isFunction, overwriteDeep } from '../utils'
|
|
5
|
+
|
|
6
|
+
export const parseState = function () {
|
|
7
|
+
const state = this
|
|
8
|
+
const parseState = {}
|
|
9
|
+
for (const param in state) {
|
|
10
|
+
if (param !== '__element' && param !== 'update' && param !== 'parse') {
|
|
11
|
+
parseState[param] = state[param]
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return parseState
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const updateState = function (obj, options = {}) {
|
|
18
|
+
const state = this
|
|
19
|
+
const element = state.__element
|
|
20
|
+
|
|
21
|
+
// run `on.stateUpdated`
|
|
22
|
+
if (element.on && isFunction(element.on.initStateUpdated)) {
|
|
23
|
+
on.initStateUpdated(element.on.initStateUpdated, element, state)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
overwriteDeep(state, obj, ['update', 'parse', '__element'])
|
|
27
|
+
if (!options.preventUpdate) element.update()
|
|
28
|
+
|
|
29
|
+
// run `on.stateUpdated`
|
|
30
|
+
if (element.on && isFunction(element.on.stateUpdated)) {
|
|
31
|
+
on.stateUpdated(element.on.stateUpdated, element, state)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function state (element, parent) {
|
|
36
|
+
let { state } = element
|
|
37
|
+
if (!state) return (parent && parent.state) || {}
|
|
38
|
+
if (isFunction(state)) state = exec(state, element)
|
|
39
|
+
|
|
40
|
+
state = deepClone(state, ['update', 'parse', '__element'])
|
|
41
|
+
state.__element = element
|
|
42
|
+
state.parse = parseState
|
|
43
|
+
state.update = updateState
|
|
44
|
+
|
|
45
|
+
// run `on.stateCreated`
|
|
46
|
+
if (element.on && isFunction(element.on.stateCreated)) {
|
|
47
|
+
on.stateCreated(element.on.stateCreated, element, element.state)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return state
|
|
51
|
+
}
|