-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (53 loc) · 1.75 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import {useState,useEffect} from 'react';
export const config = {
funReg : /^set/,
private: false,
lazy : true,
}
export const observe = (state,options = config ) =>{
if(options.funReg === undefined) options.funReg = config.funReg;
if(options.private === undefined) options.private = config.private;
if(options.lazy === undefined) options.lazy = config.lazy;
const reRenderMap = new Map();
const renderNode = ()=>{
for (var reRenderFun of reRenderMap.values()) {
reRenderFun();
}
}
const stateProxy = new Proxy(state,{
get(target,key){
if(key.match(options.funReg) && options.private) {
const privateFun = (...arg)=>{
target[key].apply(new Proxy(state,{
set(target,key,value){
if(options.lazy && target[key] === value) return true;
target[key] = value;
renderNode();
return true;
}
}),arg)
}
return privateFun;
}
return target[key]
},
set(target,key,value){
if(options.private) return false;
if(key.match(options.funReg)) return false;
if(options.lazy && target[key] === value) return true;
target[key] = value;
renderNode()
return true;
}
})
return (key)=>{
const [r,reflash] = useState(0)
reRenderMap.set(key,()=>reflash(r + 1 ))
useEffect(()=>{
return ()=>{
reRenderMap.set(key,()=>{});
}
},[])
return stateProxy
}
}