parent
f46b35f9bf
commit
d1e81474c0
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "wkitd",
|
||||
"version": "1.1.1",
|
||||
"version": "1.1.2",
|
||||
"type": "module",
|
||||
"main": "dist/index.js",
|
||||
"files": [
|
||||
|
|
33
src/store.js
33
src/store.js
|
@ -7,6 +7,28 @@
|
|||
import { Component } from 'wkit'
|
||||
import { __STORE__ } from './constants.js'
|
||||
|
||||
function observe(obj) {
|
||||
if (obj === null) {
|
||||
return obj
|
||||
}
|
||||
|
||||
return new Proxy(obj, {
|
||||
get(target, key, receiver) {
|
||||
let value = Reflect.get(target, key, receiver)
|
||||
// 当访问的值是对象时,需要对这个对象也进行代理
|
||||
if (typeof value === 'object') {
|
||||
return observe(value)
|
||||
}
|
||||
return value
|
||||
},
|
||||
set(target, key, value, receiver) {
|
||||
Reflect.set(target, key, value, receiver)
|
||||
window.wkitd.broadcast()
|
||||
return true
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function createStore(obj = {}) {
|
||||
let defined = false
|
||||
|
||||
|
@ -21,16 +43,7 @@ export function createStore(obj = {}) {
|
|||
'Can not set readonly property $store of Component'
|
||||
)
|
||||
}
|
||||
window.wkitd.set(
|
||||
__STORE__,
|
||||
new Proxy(val, {
|
||||
set(target, prop, value) {
|
||||
target[prop] = value
|
||||
window.wkitd.broadcast()
|
||||
return true
|
||||
}
|
||||
})
|
||||
)
|
||||
window.wkitd.set(__STORE__, observe(val))
|
||||
defined = true
|
||||
},
|
||||
enumerable: false
|
||||
|
|
Loading…
Reference in New Issue