@e7w/easy-model 0.0.11 → 0.1.0
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/LICENSE.md +9 -0
- package/README.md +33 -19
- package/dist/index.d.ts +10 -2
- package/dist/index.es.js +124 -173
- package/dist/index.umd.js +1 -1
- package/package.json +64 -23
- package/dist/helpers.d.ts +0 -8
- package/dist/observe.d.ts +0 -3
- package/dist/provide.d.ts +0 -5
- package/src/helpers.ts +0 -103
- package/src/index.ts +0 -2
- package/src/observe.ts +0 -79
- package/src/provide.ts +0 -176
package/LICENSE.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Kamil Bysiec
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,28 +1,42 @@
|
|
|
1
|
-
#
|
|
1
|
+
# easy-model
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
> A simple state management lib for Typescript
|
|
3
|
+
一个简单的react状态管理库
|
|
5
4
|
|
|
6
|
-
##
|
|
5
|
+
## 示例
|
|
7
6
|
|
|
8
|
-
```
|
|
9
|
-
import {
|
|
7
|
+
```tsx
|
|
8
|
+
import { useModel } from "@e7w/easy-model";
|
|
10
9
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
msg = "1";
|
|
10
|
+
class MTest {
|
|
11
|
+
constructor(public name: string) {}
|
|
14
12
|
|
|
15
|
-
|
|
13
|
+
value = 0;
|
|
16
14
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
15
|
+
random() {
|
|
16
|
+
this.value = Math.random();
|
|
20
17
|
}
|
|
21
|
-
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function TestComA() {
|
|
21
|
+
const { value, random } = useModel(MTest, ["test"]);
|
|
22
|
+
return (
|
|
23
|
+
<div>
|
|
24
|
+
<span>{value}</span>
|
|
25
|
+
<button onClick={random}>changeValue</button>
|
|
26
|
+
</div>
|
|
27
|
+
);
|
|
28
|
+
}
|
|
22
29
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
30
|
+
function TestComB() {
|
|
31
|
+
const { value } = useModel(MTest, ["test"]);
|
|
32
|
+
return <span>{value}</span>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
render(
|
|
36
|
+
<div>
|
|
37
|
+
<TestComA />
|
|
38
|
+
<TestComB />
|
|
39
|
+
</div>
|
|
40
|
+
);
|
|
41
|
+
// 当点击ComA的div时,两个组件都会更新
|
|
28
42
|
```
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
// Generated by dts-bundle-generator v9.5.1
|
|
2
|
+
|
|
3
|
+
export type WatchCallback = (path: Array<string | symbol>, oldValue: any, newValue: any) => void;
|
|
4
|
+
export declare function watch(target: object, callback: WatchCallback): () => void;
|
|
5
|
+
export declare function provide<T extends new (...args: any[]) => InstanceType<T>>(ctor: T): T & ((...args: ConstructorParameters<T>) => InstanceType<T>);
|
|
6
|
+
export declare function revoke(model: object): void;
|
|
7
|
+
export declare function useModel<T extends new (...args: any[]) => InstanceType<T>>(Ctor: T, args: ConstructorParameters<T>): InstanceType<T>;
|
|
8
|
+
export declare function useModel<T extends new (...args: any[]) => InstanceType<T>>(Ctor: T, args: null): Partial<InstanceType<T>>;
|
|
9
|
+
|
|
10
|
+
export {};
|
package/dist/index.es.js
CHANGED
|
@@ -1,185 +1,136 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
function
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const
|
|
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
|
-
set(t, e, r, s) {
|
|
35
|
-
if (r && (r = o(r)), r === o(Reflect.get(t, e)))
|
|
36
|
-
return !0;
|
|
37
|
-
const i = j(), u = Reflect.set(t, e, r, s);
|
|
38
|
-
return y(this.obj, [e]), M(i), u;
|
|
39
|
-
}
|
|
40
|
-
getTrigger(t) {
|
|
41
|
-
return t in this.triggers || (this.triggers[t] = (e) => y(this.obj, [t, ...e])), this.triggers[t];
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
const k = /* @__PURE__ */ new Set();
|
|
45
|
-
let d = null;
|
|
46
|
-
const l = [
|
|
47
|
-
"push",
|
|
48
|
-
"pop",
|
|
49
|
-
"shift",
|
|
50
|
-
"unshift",
|
|
51
|
-
"splice",
|
|
52
|
-
"sort",
|
|
53
|
-
"reverse"
|
|
54
|
-
], x = ["includes", "indexOf", "lastIndexOf"], W = ["forEach", "map"];
|
|
55
|
-
function R(n, t) {
|
|
56
|
-
return t === "hasOwnProperty" ? function(e) {
|
|
57
|
-
return o(this).hasOwnProperty(e);
|
|
58
|
-
} : Array.isArray(n) && typeof l[t] == "function" ? W.includes(t) ? n[t] : function(...e) {
|
|
59
|
-
const r = l[t].apply(o(this), e);
|
|
60
|
-
return x.includes(t) && (r === -1 || r === !1) ? l[t].apply(o(this), ...e.map(o)) : (l.includes(t) && y(this, [t]), r);
|
|
61
|
-
} : null;
|
|
62
|
-
}
|
|
63
|
-
const p = /* @__PURE__ */ new WeakMap();
|
|
64
|
-
function m(n) {
|
|
65
|
-
return n = o(n), p.has(n) || p.set(n, /* @__PURE__ */ new Set()), p.get(n);
|
|
1
|
+
import { useReducer as O, useEffect as E } from "react";
|
|
2
|
+
const b = /* @__PURE__ */ new WeakMap(), y = /* @__PURE__ */ new WeakMap(), P = Symbol("origin");
|
|
3
|
+
let l = null;
|
|
4
|
+
const _ = ["includes", "indexOf", "lastIndexOf"], j = [Promise, WeakMap, WeakSet, Map, Set];
|
|
5
|
+
function d(e) {
|
|
6
|
+
if (e = u(e), !e || j.some((r) => e instanceof r)) return e;
|
|
7
|
+
if (b.has(e)) return b.get(e);
|
|
8
|
+
const o = {}, c = {}, i = new Proxy(e, {
|
|
9
|
+
get(r, n, s) {
|
|
10
|
+
if (n === P) return r;
|
|
11
|
+
let t = R(r, n);
|
|
12
|
+
return t || (t = Reflect.get(r, n, s), typeof t == "function" ? (c[n] || (c[n] = t.bind(d(r))), t = c[n]) : typeof t == "object" && t !== null && (o[n] || (o[n] = m(t, v(e, n))), t = d(t)), t);
|
|
13
|
+
},
|
|
14
|
+
set(r, n, s, t) {
|
|
15
|
+
let f = Reflect.get(r, n, t);
|
|
16
|
+
if (f = u(f), s = u(s), f === s) return !0;
|
|
17
|
+
const k = Reflect.set(r, n, s, t);
|
|
18
|
+
if (k) {
|
|
19
|
+
o[n] && (o[n](), delete o[n]), typeof s == "object" && s !== null && (o[n] = m(s, v(e, n))), c[n] && delete c[n];
|
|
20
|
+
const S = l;
|
|
21
|
+
l = /* @__PURE__ */ new WeakSet(), M(r, [n], f, s), l = S;
|
|
22
|
+
}
|
|
23
|
+
return k;
|
|
24
|
+
},
|
|
25
|
+
deleteProperty(r, n) {
|
|
26
|
+
let s = Reflect.get(r, n);
|
|
27
|
+
s = u(s);
|
|
28
|
+
const t = Reflect.deleteProperty(r, n);
|
|
29
|
+
return t && (o[n] && (o[n](), delete o[n]), c[n] && delete c[n], M(r, [n], s, void 0)), t;
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
return b.set(e, i), i;
|
|
66
33
|
}
|
|
67
|
-
function
|
|
68
|
-
|
|
34
|
+
function m(e, o) {
|
|
35
|
+
e = u(e), y.has(e) || y.set(e, /* @__PURE__ */ new Map());
|
|
36
|
+
const c = y.get(e), i = Symbol();
|
|
37
|
+
return c.set(i, o), function() {
|
|
38
|
+
c.delete(i);
|
|
39
|
+
};
|
|
69
40
|
}
|
|
70
|
-
function
|
|
71
|
-
|
|
72
|
-
const i =
|
|
73
|
-
|
|
74
|
-
}, r = () => {
|
|
75
|
-
m(n).delete(e);
|
|
41
|
+
function v(e, o) {
|
|
42
|
+
return function(...c) {
|
|
43
|
+
const [i, ...r] = c;
|
|
44
|
+
M(e, [o, ...i], ...r);
|
|
76
45
|
};
|
|
77
|
-
return _(n, e), r;
|
|
78
46
|
}
|
|
79
|
-
function
|
|
80
|
-
|
|
81
|
-
|
|
47
|
+
function M(e, ...o) {
|
|
48
|
+
if (e = u(e), l != null && l.has(e)) return;
|
|
49
|
+
l == null || l.add(e);
|
|
50
|
+
const c = y.get(e);
|
|
51
|
+
if (!c) return;
|
|
52
|
+
[...c.values()].forEach((r) => {
|
|
53
|
+
const n = new EventTarget(), s = new Event("__trigger__");
|
|
54
|
+
n.addEventListener(s.type, t), n.dispatchEvent(s);
|
|
55
|
+
function t() {
|
|
56
|
+
n.removeEventListener(s.type, t), r(...o);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
82
59
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const t = new EventTarget(), e = new Event(b);
|
|
86
|
-
t.addEventListener(b, () => {
|
|
87
|
-
t.removeEventListener(b, n), n();
|
|
88
|
-
}), t.dispatchEvent(e);
|
|
60
|
+
function u(e) {
|
|
61
|
+
return typeof e != "object" || e === null ? e : e[P] ?? e;
|
|
89
62
|
}
|
|
90
|
-
function
|
|
91
|
-
|
|
63
|
+
function R(e, o) {
|
|
64
|
+
if (e = u(e), ["constructor", "__proto__"].includes(o))
|
|
65
|
+
return Reflect.get(e, o);
|
|
66
|
+
if (o === "hasOwnProperty")
|
|
67
|
+
return function(c) {
|
|
68
|
+
return Object.prototype.hasOwnProperty.call(u(this), c);
|
|
69
|
+
};
|
|
70
|
+
if (Array.isArray(e) && _.includes(o))
|
|
71
|
+
return function(...c) {
|
|
72
|
+
const i = _[o].apply(u(this), c);
|
|
73
|
+
return i === -1 || i === !1 ? _[o].apply(u(this), c.map(u)) : i;
|
|
74
|
+
};
|
|
92
75
|
}
|
|
93
|
-
|
|
94
|
-
|
|
76
|
+
const a = Symbol("instance"), w = /* @__PURE__ */ new Map(), p = /* @__PURE__ */ new WeakMap(), h = /* @__PURE__ */ new WeakMap(), W = /* @__PURE__ */ new WeakSet();
|
|
77
|
+
function x(e) {
|
|
78
|
+
if (W.has(e))
|
|
79
|
+
return e;
|
|
80
|
+
if (h.has(e))
|
|
81
|
+
return h.get(e);
|
|
82
|
+
const o = {};
|
|
83
|
+
i.prototype = new Proxy(
|
|
84
|
+
{
|
|
85
|
+
constructor: e,
|
|
86
|
+
__proto__: e.prototype
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
get(r, n, s) {
|
|
90
|
+
let t = R(r, n);
|
|
91
|
+
return t || (t = Reflect.get(r, n, s), typeof n == "symbol" || ["constructor", "__proto__"].includes(n) || typeof t != "function") ? t : (o[n] || (o[n] = function(...f) {
|
|
92
|
+
return t.apply(d(this), f);
|
|
93
|
+
}), o[n]);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
);
|
|
97
|
+
const c = new Proxy(e, {
|
|
98
|
+
apply(r, n, s) {
|
|
99
|
+
w.has(e) || w.set(e, /* @__PURE__ */ new Map());
|
|
100
|
+
let t = w.get(e);
|
|
101
|
+
for (let f = 0; f < s.length; f++)
|
|
102
|
+
t.has(s[f]) || t.set(s[f], /* @__PURE__ */ new Map()), t = t.get(s[f]);
|
|
103
|
+
if (!t.has(a)) {
|
|
104
|
+
t.set(a, void 0);
|
|
105
|
+
const f = d(Reflect.construct(r, s, i));
|
|
106
|
+
t.set(a, f), p.set(f, s);
|
|
107
|
+
}
|
|
108
|
+
return t.get(a);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
return h.set(e, c), W.add(c), h.get(e);
|
|
112
|
+
function i() {
|
|
113
|
+
}
|
|
95
114
|
}
|
|
96
|
-
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
|
|
115
|
+
function C(e) {
|
|
116
|
+
const o = d(e);
|
|
117
|
+
if (!p.has(o)) return;
|
|
118
|
+
const c = p.get(o);
|
|
119
|
+
let i = w.get(o.constructor);
|
|
120
|
+
for (; c.length; )
|
|
121
|
+
i = i.get(c.shift());
|
|
122
|
+
i.delete(a), p.delete(o);
|
|
100
123
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
124
|
+
const A = /* @__PURE__ */ Object.create(null);
|
|
125
|
+
function L(e, o) {
|
|
126
|
+
const c = x(e), i = o ? c(...o) : A, [, r] = O((n) => (n + 1) % 100, 0);
|
|
127
|
+
return E(() => m(i, () => {
|
|
128
|
+
r();
|
|
129
|
+
}), [i]), i;
|
|
104
130
|
}
|
|
105
|
-
const O = class {
|
|
106
|
-
constructor(t) {
|
|
107
|
-
f(this, "fakeCtor");
|
|
108
|
-
f(this, "caches", /* @__PURE__ */ new Map());
|
|
109
|
-
this.Ctor = t;
|
|
110
|
-
const e = function() {
|
|
111
|
-
};
|
|
112
|
-
e.prototype = new Proxy(
|
|
113
|
-
{
|
|
114
|
-
constructor: t,
|
|
115
|
-
__proto__: t.prototype
|
|
116
|
-
},
|
|
117
|
-
O.prototypeHandlers
|
|
118
|
-
), this.fakeCtor = e;
|
|
119
|
-
}
|
|
120
|
-
apply(t, e, r) {
|
|
121
|
-
const s = h;
|
|
122
|
-
h = this.fakeCtor;
|
|
123
|
-
const i = this.getInstance(r) || this.register(
|
|
124
|
-
r,
|
|
125
|
-
Reflect.construct(this.Ctor, this.filterArgs(r), this.fakeCtor)
|
|
126
|
-
);
|
|
127
|
-
return h = s, i;
|
|
128
|
-
}
|
|
129
|
-
construct(t, e, r) {
|
|
130
|
-
const s = Reflect.construct(
|
|
131
|
-
t,
|
|
132
|
-
this.filterArgs(e),
|
|
133
|
-
r === this.Ctor ? this.fakeCtor : r
|
|
134
|
-
);
|
|
135
|
-
return h !== r ? c(s) : s;
|
|
136
|
-
}
|
|
137
|
-
set(t, e, r, s) {
|
|
138
|
-
if (r && (r = o(r)), r === o(Reflect.get(t, e)))
|
|
139
|
-
return !0;
|
|
140
|
-
const i = j(), u = Reflect.set(t, e, r, s);
|
|
141
|
-
return this.caches.forEach((w) => {
|
|
142
|
-
const S = w.deref();
|
|
143
|
-
S && y(S, []);
|
|
144
|
-
}), M(i), u;
|
|
145
|
-
}
|
|
146
|
-
getInstance(t) {
|
|
147
|
-
for (const e of this.caches.keys()) {
|
|
148
|
-
if (e.length !== t.length || !e.every(
|
|
149
|
-
(s, i) => s === t[i] ? !0 : typeof s == "object" && typeof t[i] == "object" ? this.isShallowEqualObj(s, t[i]) : !1
|
|
150
|
-
))
|
|
151
|
-
continue;
|
|
152
|
-
const r = this.caches.get(e);
|
|
153
|
-
return r.deref() ? c(r.deref()) : (this.caches.delete(e), null);
|
|
154
|
-
}
|
|
155
|
-
return null;
|
|
156
|
-
}
|
|
157
|
-
register(t, e) {
|
|
158
|
-
return this.caches.set(t, new WeakRef(e)), c(e);
|
|
159
|
-
}
|
|
160
|
-
isShallowEqualObj(t, e) {
|
|
161
|
-
const r = Object.keys(t), s = e.keys(e);
|
|
162
|
-
return r.length === s.length && r.every((i) => t[i] === e[i]);
|
|
163
|
-
}
|
|
164
|
-
filterArgs(t) {
|
|
165
|
-
return t.filter((e) => e !== F);
|
|
166
|
-
}
|
|
167
|
-
};
|
|
168
|
-
let a = O;
|
|
169
|
-
f(a, "prototypeHandlers", {
|
|
170
|
-
get(t, e, r) {
|
|
171
|
-
const s = R(t, e);
|
|
172
|
-
if (s)
|
|
173
|
-
return s;
|
|
174
|
-
const i = Reflect.get(t, e, r);
|
|
175
|
-
return typeof e == "symbol" || ["constructor", "__proto__"].includes(e) || typeof i != "function" ? i : function(...u) {
|
|
176
|
-
return i.apply(c(this), u);
|
|
177
|
-
};
|
|
178
|
-
}
|
|
179
|
-
});
|
|
180
131
|
export {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
L as
|
|
184
|
-
|
|
132
|
+
x as provide,
|
|
133
|
+
C as revoke,
|
|
134
|
+
L as useModel,
|
|
135
|
+
m as watch
|
|
185
136
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(l,d){typeof exports=="object"&&typeof module<"u"?d(exports,require("react")):typeof define=="function"&&define.amd?define(["exports","react"],d):(l=typeof globalThis<"u"?globalThis:l||self,d(l["@e7w/easy-model"]={},l.React))})(this,function(l,d){"use strict";const _=new WeakMap,w=new WeakMap,W=Symbol("origin");let a=null;const v=["includes","indexOf","lastIndexOf"],E=[Promise,WeakMap,WeakSet,Map,Set];function h(e){if(e=u(e),!e||E.some(i=>e instanceof i))return e;if(_.has(e))return _.get(e);const o={},c={},r=new Proxy(e,{get(i,n,s){if(n===W)return i;let t=P(i,n);return t||(t=Reflect.get(i,n,s),typeof t=="function"?(c[n]||(c[n]=t.bind(h(i))),t=c[n]):typeof t=="object"&&t!==null&&(o[n]||(o[n]=p(t,S(e,n))),t=h(t)),t)},set(i,n,s,t){let f=Reflect.get(i,n,t);if(f=u(f),s=u(s),f===s)return!0;const j=Reflect.set(i,n,s,t);if(j){o[n]&&(o[n](),delete o[n]),typeof s=="object"&&s!==null&&(o[n]=p(s,S(e,n))),c[n]&&delete c[n];const F=a;a=new WeakSet,k(i,[n],f,s),a=F}return j},deleteProperty(i,n){let s=Reflect.get(i,n);s=u(s);const t=Reflect.deleteProperty(i,n);return t&&(o[n]&&(o[n](),delete o[n]),c[n]&&delete c[n],k(i,[n],s,void 0)),t}});return _.set(e,r),r}function p(e,o){e=u(e),w.has(e)||w.set(e,new Map);const c=w.get(e),r=Symbol();return c.set(r,o),function(){c.delete(r)}}function S(e,o){return function(...c){const[r,...i]=c;k(e,[o,...r],...i)}}function k(e,...o){if(e=u(e),a!=null&&a.has(e))return;a==null||a.add(e);const c=w.get(e);if(!c)return;[...c.values()].forEach(i=>{const n=new EventTarget,s=new Event("__trigger__");n.addEventListener(s.type,t),n.dispatchEvent(s);function t(){n.removeEventListener(s.type,t),i(...o)}})}function u(e){return typeof e!="object"||e===null?e:e[W]??e}function P(e,o){if(e=u(e),["constructor","__proto__"].includes(o))return Reflect.get(e,o);if(o==="hasOwnProperty")return function(c){return Object.prototype.hasOwnProperty.call(u(this),c)};if(Array.isArray(e)&&v.includes(o))return function(...c){const r=v[o].apply(u(this),c);return r===-1||r===!1?v[o].apply(u(this),c.map(u)):r}}const y=Symbol("instance"),b=new Map,m=new WeakMap,M=new WeakMap,R=new WeakSet;function O(e){if(R.has(e))return e;if(M.has(e))return M.get(e);const o={};r.prototype=new Proxy({constructor:e,__proto__:e.prototype},{get(i,n,s){let t=P(i,n);return t||(t=Reflect.get(i,n,s),typeof n=="symbol"||["constructor","__proto__"].includes(n)||typeof t!="function")?t:(o[n]||(o[n]=function(...f){return t.apply(h(this),f)}),o[n])}});const c=new Proxy(e,{apply(i,n,s){b.has(e)||b.set(e,new Map);let t=b.get(e);for(let f=0;f<s.length;f++)t.has(s[f])||t.set(s[f],new Map),t=t.get(s[f]);if(!t.has(y)){t.set(y,void 0);const f=h(Reflect.construct(i,s,r));t.set(y,f),m.set(f,s)}return t.get(y)}});return M.set(e,c),R.add(c),M.get(e);function r(){}}function x(e){const o=h(e);if(!m.has(o))return;const c=m.get(o);let r=b.get(o.constructor);for(;c.length;)r=r.get(c.shift());r.delete(y),m.delete(o)}const T=Object.create(null);function A(e,o){const c=O(e),r=o?c(...o):T,[,i]=d.useReducer(n=>(n+1)%100,0);return d.useEffect(()=>p(r,()=>{i()}),[r]),r}l.provide=O,l.revoke=x,l.useModel=A,l.watch=p,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,34 +1,75 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e7w/easy-model",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"description": "一个简单的状态管理包, A simple state management lib for Typescript",
|
|
5
|
-
"main": "./src/index.ts",
|
|
3
|
+
"version": "0.1.0",
|
|
6
4
|
"module": "./dist/index.es.js",
|
|
7
|
-
"types": "./src/index.ts",
|
|
8
|
-
"typings": "./dist/index.d.ts",
|
|
9
|
-
"exports": {
|
|
10
|
-
"import": "./dist/index.es.js",
|
|
11
|
-
"require": "./dist/index.umd.js"
|
|
12
|
-
},
|
|
13
5
|
"publishConfig": {
|
|
14
6
|
"access": "public",
|
|
15
7
|
"registry": "https://registry.npmjs.org/"
|
|
16
8
|
},
|
|
17
9
|
"type": "module",
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
"
|
|
24
|
-
"
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": "./dist/index.es.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"scripts": {
|
|
17
|
+
"dev": "vite --host",
|
|
18
|
+
"build": "npm run build:lib && npm run build:dts",
|
|
19
|
+
"build:lib": "rimraf dist && tsc && vite build",
|
|
20
|
+
"build:dts": "dts-bundle-generator --config ./dts-bundle-generator.config.cjs",
|
|
21
|
+
"test": "vitest",
|
|
22
|
+
"test:coverage": "vitest --coverage",
|
|
23
|
+
"lint:scripts": "eslint . --fix --ext .ts",
|
|
24
|
+
"lint:styles": "stylelint ./**/*.{css,scss}",
|
|
25
|
+
"format:scripts": "prettier . --write",
|
|
26
|
+
"format:styles": "stylelint ./**/*.{css,scss} --fix",
|
|
27
|
+
"format": "npm run format:scripts && npm run format:styles",
|
|
28
|
+
"prepare": "husky && echo 'npx lint-staged' > .husky/pre-commit && git add .husky/pre-commit",
|
|
29
|
+
"uninstall-husky": "npm uninstall husky --no-save && git config --unset core.hooksPath && npx rimraf .husky"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"react": ">=17.0.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@eslint/js": "^9.24.0",
|
|
36
|
+
"@testing-library/jest-dom": "^6.6.3",
|
|
37
|
+
"@testing-library/react": "^16.3.0",
|
|
38
|
+
"@types/jsdom": "^21.1.7",
|
|
39
|
+
"@types/node": "^22.13.5",
|
|
40
|
+
"@types/react": ">=17.0.0",
|
|
41
|
+
"@vitejs/plugin-react-swc": "^3.8.1",
|
|
42
|
+
"@vitest/coverage-v8": "^3.0.7",
|
|
43
|
+
"copyfiles": "^2.4.1",
|
|
44
|
+
"dts-bundle-generator": "^9.5.1",
|
|
45
|
+
"eslint": "^9.24.0",
|
|
46
|
+
"eslint-config-prettier": "^10.0.1",
|
|
47
|
+
"eslint-plugin-autofix": "^2.2.0",
|
|
48
|
+
"eslint-plugin-prettier": "^5.2.3",
|
|
49
|
+
"globals": "^16.0.0",
|
|
50
|
+
"husky": "^9.1.7",
|
|
51
|
+
"jiti": "^2.4.2",
|
|
52
|
+
"jsdom": "^26.0.0",
|
|
53
|
+
"lint-staged": "^15.4.3",
|
|
54
|
+
"postcss": "^8.5.3",
|
|
55
|
+
"postcss-scss": "^4.0.9",
|
|
56
|
+
"prettier": "^3.5.2",
|
|
57
|
+
"rimraf": "^6.0.1",
|
|
58
|
+
"stylelint": "^16.14.1",
|
|
59
|
+
"stylelint-config-recommended": "^15.0.0",
|
|
60
|
+
"stylelint-config-sass-guidelines": "^12.1.0",
|
|
61
|
+
"stylelint-order": "^6.0.4",
|
|
62
|
+
"stylelint-prettier": "^5.0.3",
|
|
63
|
+
"ts-node": "^10.9.2",
|
|
64
|
+
"typescript": "^5.7.3",
|
|
65
|
+
"typescript-eslint": "^8.29.1",
|
|
66
|
+
"vite": "^6.2.0",
|
|
67
|
+
"vitest": "^3.0.7"
|
|
68
|
+
},
|
|
25
69
|
"files": [
|
|
26
|
-
"package.json",
|
|
27
|
-
"src",
|
|
28
70
|
"dist",
|
|
29
|
-
"README.md"
|
|
71
|
+
"README.md",
|
|
72
|
+
"LICENSE.md"
|
|
30
73
|
],
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
}
|
|
74
|
+
"license": "MIT"
|
|
75
|
+
}
|
package/dist/helpers.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export type Ctor = new (...args: any[]) => any;
|
|
2
|
-
export type Obj = Record<string | symbol | number, any>;
|
|
3
|
-
export declare function safeGet(target: Obj, p: string | symbol): any;
|
|
4
|
-
export declare function track(target: Obj, handler: (path: (string | symbol)[]) => void): void;
|
|
5
|
-
export declare function subscribe(target: Obj, handler: (path: (string | symbol)[]) => void): () => void;
|
|
6
|
-
export declare function trigger(target: Obj, path: (string | symbol)[]): void;
|
|
7
|
-
export declare function startTrigger(): symbol | null;
|
|
8
|
-
export declare function endTrigger(symbol: symbol | null): void;
|
package/dist/observe.d.ts
DELETED
package/dist/provide.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { type Ctor } from "./helpers";
|
|
2
|
-
export declare const filterSymbol: unique symbol;
|
|
3
|
-
export declare function provide<T extends Ctor>(Ctor: T): T & ((...args: ConstructorParameters<T>) => InstanceType<T>);
|
|
4
|
-
export declare function extend<T extends ReturnType<typeof provide>, P extends Partial<InstanceType<T>>>(Provider: T, extendData: P): P & Partial<Omit<InstanceType<T>, keyof P>>;
|
|
5
|
-
export declare function extend<T extends ReturnType<typeof provide>, P extends Partial<InstanceType<T>>, M extends Parameters<T>>(Provider: T, extendData: P, args: M): InstanceType<T>;
|
package/src/helpers.ts
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import { getOrigin } from "./observe";
|
|
2
|
-
|
|
3
|
-
const triggered = new Set();
|
|
4
|
-
let triggerSymbol: Symbol | null = null;
|
|
5
|
-
|
|
6
|
-
export type Ctor = new (...args: any[]) => any;
|
|
7
|
-
export type Obj = Record<string | symbol | number, any>;
|
|
8
|
-
const fakeArr: any = [
|
|
9
|
-
"push",
|
|
10
|
-
"pop",
|
|
11
|
-
"shift",
|
|
12
|
-
"unshift",
|
|
13
|
-
"splice",
|
|
14
|
-
"sort",
|
|
15
|
-
"reverse",
|
|
16
|
-
];
|
|
17
|
-
const findMethods: any = ["includes", "indexOf", "lastIndexOf"];
|
|
18
|
-
const safeMethods: any = ["forEach", "map"];
|
|
19
|
-
export function safeGet(target: Obj, p: string | symbol): any {
|
|
20
|
-
if (p === "hasOwnProperty") {
|
|
21
|
-
return function (this: Obj, key: string) {
|
|
22
|
-
return getOrigin(this).hasOwnProperty(key);
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
if (Array.isArray(target) && typeof fakeArr[p] === "function") {
|
|
26
|
-
if (safeMethods.includes(p)) return target[p as keyof typeof target];
|
|
27
|
-
return function (this: Obj, ...args: any) {
|
|
28
|
-
const ret = fakeArr[p].apply(getOrigin(this), args);
|
|
29
|
-
if (findMethods.includes(p) && (ret === -1 || ret === false)) {
|
|
30
|
-
return fakeArr[p].apply(getOrigin(this), ...args.map(getOrigin));
|
|
31
|
-
}
|
|
32
|
-
if (fakeArr.includes(p)) {
|
|
33
|
-
trigger(this, [p]);
|
|
34
|
-
}
|
|
35
|
-
return ret;
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const updateHandlersMap = new WeakMap();
|
|
42
|
-
|
|
43
|
-
function getUpdateHandlers(
|
|
44
|
-
target: Obj
|
|
45
|
-
): Set<(path: (string | symbol)[]) => void> {
|
|
46
|
-
target = getOrigin(target);
|
|
47
|
-
if (!updateHandlersMap.has(target)) updateHandlersMap.set(target, new Set());
|
|
48
|
-
return updateHandlersMap.get(target);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export function track(
|
|
52
|
-
target: Obj,
|
|
53
|
-
handler: (path: (string | symbol)[]) => void
|
|
54
|
-
): void {
|
|
55
|
-
const handlers = getUpdateHandlers(target);
|
|
56
|
-
handlers.add(handler);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export function subscribe(
|
|
60
|
-
target: Obj,
|
|
61
|
-
handler: (path: (string | symbol)[]) => void
|
|
62
|
-
): () => void {
|
|
63
|
-
const wrappedHandler: typeof handler = (path) => {
|
|
64
|
-
const isTriggered = triggered.has(handler);
|
|
65
|
-
triggered.add(handler);
|
|
66
|
-
if (!isTriggered) triggerTop(() => handler(path));
|
|
67
|
-
track(target, wrappedHandler);
|
|
68
|
-
};
|
|
69
|
-
const unsubscribe = () => {
|
|
70
|
-
getUpdateHandlers(target).delete(wrappedHandler);
|
|
71
|
-
};
|
|
72
|
-
track(target, wrappedHandler);
|
|
73
|
-
return unsubscribe;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export function trigger(target: Obj, path: (string | symbol)[]): void {
|
|
77
|
-
const handlers = getUpdateHandlers(target);
|
|
78
|
-
const cbs = [...handlers];
|
|
79
|
-
handlers.clear();
|
|
80
|
-
cbs.forEach((fn) => fn(path));
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const eventName = "__easy_model_event__";
|
|
84
|
-
function triggerTop(cb: () => void): void {
|
|
85
|
-
const target = new EventTarget();
|
|
86
|
-
const e = new Event(eventName);
|
|
87
|
-
target.addEventListener(eventName, () => {
|
|
88
|
-
target.removeEventListener(eventName, cb);
|
|
89
|
-
cb();
|
|
90
|
-
});
|
|
91
|
-
target.dispatchEvent(e);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export function startTrigger() {
|
|
95
|
-
if (triggerSymbol) return null;
|
|
96
|
-
return (triggerSymbol = Symbol());
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export function endTrigger(symbol: symbol | null) {
|
|
100
|
-
if (triggerSymbol !== symbol) return;
|
|
101
|
-
triggerSymbol = null;
|
|
102
|
-
triggered.clear();
|
|
103
|
-
}
|
package/src/index.ts
DELETED
package/src/observe.ts
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type Obj,
|
|
3
|
-
safeGet,
|
|
4
|
-
track,
|
|
5
|
-
trigger,
|
|
6
|
-
startTrigger,
|
|
7
|
-
endTrigger,
|
|
8
|
-
} from "./helpers";
|
|
9
|
-
|
|
10
|
-
const unSafeCtor = [Promise, WeakMap, WeakSet, Map, Set];
|
|
11
|
-
function isUnsafeInstance<T extends Obj>(target: T): boolean {
|
|
12
|
-
return unSafeCtor.some((ctor) => target instanceof ctor);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const observedMap = new WeakMap();
|
|
16
|
-
const origin = Symbol("origin");
|
|
17
|
-
export function observe<T extends Obj>(target: T): T {
|
|
18
|
-
target = getOrigin(target);
|
|
19
|
-
if (isUnsafeInstance(target)) return target;
|
|
20
|
-
if (observedMap.has(target)) return observedMap.get(target);
|
|
21
|
-
const ret = new Proxy(target, new ObserveHandler(target));
|
|
22
|
-
observedMap.set(target, ret);
|
|
23
|
-
return ret;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function getOrigin<T extends Obj>(target: T): T {
|
|
27
|
-
if (!target) return target;
|
|
28
|
-
return target[origin] || target;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
class ObserveHandler<T extends Obj> implements ProxyHandler<T> {
|
|
32
|
-
private triggers: Record<
|
|
33
|
-
string | symbol,
|
|
34
|
-
(path: (string | symbol)[]) => void
|
|
35
|
-
> = {};
|
|
36
|
-
|
|
37
|
-
private cachedFns: { [K in keyof T]?: T[K] } = {};
|
|
38
|
-
|
|
39
|
-
public constructor(private obj: T) {}
|
|
40
|
-
|
|
41
|
-
public get(target: T, p: string | symbol, receiver: any): any {
|
|
42
|
-
if (p === origin) return this.obj;
|
|
43
|
-
if (isUnsafeInstance(target)) return Reflect.get(getOrigin(target), p);
|
|
44
|
-
let ret = safeGet(target, p) || Reflect.get(target, p, receiver);
|
|
45
|
-
if (typeof ret === "function")
|
|
46
|
-
return (
|
|
47
|
-
this.cachedFns[p] ||
|
|
48
|
-
(this.cachedFns[p as keyof T] = ret.bind(observe(target)))
|
|
49
|
-
);
|
|
50
|
-
if (typeof ret === "object") {
|
|
51
|
-
ret = observe(ret);
|
|
52
|
-
track(ret, this.getTrigger(p));
|
|
53
|
-
}
|
|
54
|
-
return ret;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
public set(
|
|
58
|
-
target: T,
|
|
59
|
-
p: string | symbol,
|
|
60
|
-
newValue: any,
|
|
61
|
-
receiver: any
|
|
62
|
-
): boolean {
|
|
63
|
-
if (newValue) newValue = getOrigin(newValue);
|
|
64
|
-
if (newValue === getOrigin(Reflect.get(target, p))) return true;
|
|
65
|
-
const flag = startTrigger();
|
|
66
|
-
const ret = Reflect.set(target, p, newValue, receiver);
|
|
67
|
-
trigger(this.obj, [p]);
|
|
68
|
-
endTrigger(flag);
|
|
69
|
-
return ret;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
private getTrigger(p: string | symbol): (path: (string | symbol)[]) => void {
|
|
73
|
-
if (!(p in this.triggers)) {
|
|
74
|
-
this.triggers[p] = (path: (typeof p)[]) =>
|
|
75
|
-
trigger(this.obj, [p, ...path]);
|
|
76
|
-
}
|
|
77
|
-
return this.triggers[p];
|
|
78
|
-
}
|
|
79
|
-
}
|
package/src/provide.ts
DELETED
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type Ctor,
|
|
3
|
-
type Obj,
|
|
4
|
-
safeGet,
|
|
5
|
-
trigger,
|
|
6
|
-
startTrigger,
|
|
7
|
-
endTrigger,
|
|
8
|
-
} from "./helpers";
|
|
9
|
-
import { getOrigin, observe } from "./observe";
|
|
10
|
-
|
|
11
|
-
let creatingTarget: any = null;
|
|
12
|
-
|
|
13
|
-
export const filterSymbol = Symbol("filterSymbol");
|
|
14
|
-
|
|
15
|
-
export function provide<T extends Ctor>(
|
|
16
|
-
Ctor: T
|
|
17
|
-
): T & ((...args: ConstructorParameters<T>) => InstanceType<T>) {
|
|
18
|
-
return new Proxy(Ctor, new Provider(Ctor)) as any;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
// type RequireAtLeastOne<T> = {
|
|
22
|
-
// [K in keyof T]-?: undefined extends T[K]
|
|
23
|
-
// ? never
|
|
24
|
-
// : T[K] extends Function
|
|
25
|
-
// ? never
|
|
26
|
-
// : Required<Pick<T, K>> & Partial<Pick<T, Exclude<keyof T, K>>>;
|
|
27
|
-
// }[keyof T];
|
|
28
|
-
// todo: 想办法过滤出所有field
|
|
29
|
-
export function extend<
|
|
30
|
-
T extends ReturnType<typeof provide>,
|
|
31
|
-
P extends Partial<InstanceType<T>>
|
|
32
|
-
>(Provider: T, extendData: P): P & Partial<Omit<InstanceType<T>, keyof P>>;
|
|
33
|
-
export function extend<
|
|
34
|
-
T extends ReturnType<typeof provide>,
|
|
35
|
-
P extends Partial<InstanceType<T>>,
|
|
36
|
-
M extends Parameters<T>
|
|
37
|
-
>(Provider: T, extendData: P, args: M): InstanceType<T>;
|
|
38
|
-
export function extend<
|
|
39
|
-
T extends ReturnType<typeof provide>,
|
|
40
|
-
P extends Partial<InstanceType<T>>,
|
|
41
|
-
M extends Parameters<T> | null | undefined
|
|
42
|
-
>(Provider: T, extendData: P, args?: M) {
|
|
43
|
-
const instance = args ? Provider(...args) : Object.create(Provider.prototype);
|
|
44
|
-
return observe(Object.assign(instance, extendData));
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
class Provider<T extends Ctor> implements ProxyHandler<T> {
|
|
48
|
-
private static prototypeHandlers: ProxyHandler<Obj> = {
|
|
49
|
-
get(target, p, receiver) {
|
|
50
|
-
const safeVal = safeGet(target, p);
|
|
51
|
-
if (safeVal) return safeVal;
|
|
52
|
-
const ret = Reflect.get(target, p, receiver);
|
|
53
|
-
|
|
54
|
-
if (
|
|
55
|
-
typeof p === "symbol" ||
|
|
56
|
-
["constructor", "__proto__"].includes(p) ||
|
|
57
|
-
typeof ret !== "function"
|
|
58
|
-
)
|
|
59
|
-
return ret;
|
|
60
|
-
return function (this: Obj, ...args: any[]) {
|
|
61
|
-
return ret.apply(observe(this), args);
|
|
62
|
-
};
|
|
63
|
-
},
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
public fakeCtor: T;
|
|
67
|
-
private caches = new Map<
|
|
68
|
-
ConstructorParameters<T>,
|
|
69
|
-
WeakRef<InstanceType<T>>
|
|
70
|
-
>();
|
|
71
|
-
|
|
72
|
-
public constructor(private Ctor: T) {
|
|
73
|
-
const fakeCtor: any = function () {};
|
|
74
|
-
fakeCtor.prototype = new Proxy(
|
|
75
|
-
{
|
|
76
|
-
constructor: Ctor,
|
|
77
|
-
__proto__: Ctor.prototype,
|
|
78
|
-
},
|
|
79
|
-
Provider.prototypeHandlers
|
|
80
|
-
);
|
|
81
|
-
this.fakeCtor = fakeCtor;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
public apply(
|
|
85
|
-
_target: T,
|
|
86
|
-
_thisArg: any,
|
|
87
|
-
argArray: ConstructorParameters<T>
|
|
88
|
-
): InstanceType<T> {
|
|
89
|
-
// todo: 循环引用创建时抛出错误
|
|
90
|
-
const last = creatingTarget;
|
|
91
|
-
creatingTarget = this.fakeCtor;
|
|
92
|
-
const instance =
|
|
93
|
-
this.getInstance(argArray) ||
|
|
94
|
-
this.register(
|
|
95
|
-
argArray,
|
|
96
|
-
Reflect.construct(this.Ctor, this.filterArgs(argArray), this.fakeCtor)
|
|
97
|
-
);
|
|
98
|
-
creatingTarget = last;
|
|
99
|
-
return instance;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
public construct(
|
|
103
|
-
target: T,
|
|
104
|
-
argArray: any[],
|
|
105
|
-
newTarget: any
|
|
106
|
-
): InstanceType<T> {
|
|
107
|
-
const instance = Reflect.construct(
|
|
108
|
-
target,
|
|
109
|
-
this.filterArgs(argArray),
|
|
110
|
-
newTarget === this.Ctor ? this.fakeCtor : newTarget
|
|
111
|
-
);
|
|
112
|
-
if (creatingTarget !== newTarget) return observe(instance);
|
|
113
|
-
return instance;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
public set(
|
|
117
|
-
target: T,
|
|
118
|
-
p: string | symbol,
|
|
119
|
-
newValue: any,
|
|
120
|
-
receiver: any
|
|
121
|
-
): boolean {
|
|
122
|
-
if (newValue) newValue = getOrigin(newValue);
|
|
123
|
-
if (newValue === getOrigin(Reflect.get(target, p) as any)) return true;
|
|
124
|
-
const flag = startTrigger();
|
|
125
|
-
const ret = Reflect.set(target, p, newValue, receiver);
|
|
126
|
-
this.caches.forEach((ref) => {
|
|
127
|
-
const instance = ref.deref();
|
|
128
|
-
if (instance) trigger(instance, []);
|
|
129
|
-
});
|
|
130
|
-
endTrigger(flag);
|
|
131
|
-
return ret;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
private getInstance(args: ConstructorParameters<T>): InstanceType<T> | null {
|
|
135
|
-
for (const cachedArgs of this.caches.keys()) {
|
|
136
|
-
if (
|
|
137
|
-
cachedArgs.length !== args.length ||
|
|
138
|
-
!cachedArgs.every((arg, index) =>
|
|
139
|
-
arg === args[index]
|
|
140
|
-
? true
|
|
141
|
-
: typeof arg === "object" && typeof args[index] === "object"
|
|
142
|
-
? this.isShallowEqualObj(arg, args[index]) // 只考虑 array 和 object 的浅比较, 其他奇怪的对象不应该出现在入参里.
|
|
143
|
-
: false
|
|
144
|
-
)
|
|
145
|
-
)
|
|
146
|
-
continue;
|
|
147
|
-
|
|
148
|
-
const instance = this.caches.get(cachedArgs)!;
|
|
149
|
-
if (instance.deref()) return observe(instance.deref()!);
|
|
150
|
-
this.caches.delete(cachedArgs);
|
|
151
|
-
return null;
|
|
152
|
-
}
|
|
153
|
-
return null;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
private register(
|
|
157
|
-
args: ConstructorParameters<T>,
|
|
158
|
-
instance: InstanceType<T>
|
|
159
|
-
): InstanceType<T> {
|
|
160
|
-
this.caches.set(args, new WeakRef(instance));
|
|
161
|
-
return observe(instance);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
private isShallowEqualObj(objectA: Obj, objectB: Obj): Boolean {
|
|
165
|
-
const kOfA = Object.keys(objectA);
|
|
166
|
-
const kOfB = objectB.keys(objectB);
|
|
167
|
-
return (
|
|
168
|
-
kOfA.length === kOfB.length &&
|
|
169
|
-
kOfA.every((key) => objectA[key] === objectB[key])
|
|
170
|
-
);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
private filterArgs(args: any[]): any[] {
|
|
174
|
-
return args.filter((arg) => arg !== filterSymbol);
|
|
175
|
-
}
|
|
176
|
-
}
|