store支持深层代理

master 1.1.2
yutent 2023-09-04 14:00:23 +08:00
parent f46b35f9bf
commit d1e81474c0
2 changed files with 24 additions and 11 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "wkitd", "name": "wkitd",
"version": "1.1.1", "version": "1.1.2",
"type": "module", "type": "module",
"main": "dist/index.js", "main": "dist/index.js",
"files": [ "files": [

View File

@ -7,6 +7,28 @@
import { Component } from 'wkit' import { Component } from 'wkit'
import { __STORE__ } from './constants.js' 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 = {}) { export function createStore(obj = {}) {
let defined = false let defined = false
@ -21,16 +43,7 @@ export function createStore(obj = {}) {
'Can not set readonly property $store of Component' 'Can not set readonly property $store of Component'
) )
} }
window.wkitd.set( window.wkitd.set(__STORE__, observe(val))
__STORE__,
new Proxy(val, {
set(target, prop, value) {
target[prop] = value
window.wkitd.broadcast()
return true
}
})
)
defined = true defined = true
}, },
enumerable: false enumerable: false