@e7w/easy-model 0.0.9 → 0.0.11
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/dist/index.es.js +99 -74
- package/dist/index.umd.js +1 -1
- package/package.json +1 -1
- package/src/helpers.ts +16 -5
- package/src/observe.ts +7 -0
- package/src/provide.ts +27 -2
package/dist/index.es.js
CHANGED
|
@@ -1,95 +1,111 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
const
|
|
5
|
-
function
|
|
6
|
-
|
|
1
|
+
var C = Object.defineProperty;
|
|
2
|
+
var A = (n, t, e) => t in n ? C(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
|
|
3
|
+
var f = (n, t, e) => (A(n, typeof t != "symbol" ? t + "" : t, e), e);
|
|
4
|
+
const H = [Promise, WeakMap, WeakSet, Map, Set];
|
|
5
|
+
function v(n) {
|
|
6
|
+
return H.some((t) => n instanceof t);
|
|
7
|
+
}
|
|
8
|
+
const g = /* @__PURE__ */ new WeakMap(), E = Symbol("origin");
|
|
9
|
+
function c(n) {
|
|
10
|
+
if (n = o(n), v(n))
|
|
11
|
+
return n;
|
|
12
|
+
if (g.has(n))
|
|
7
13
|
return g.get(n);
|
|
8
|
-
const t = new Proxy(n, new
|
|
14
|
+
const t = new Proxy(n, new T(n));
|
|
9
15
|
return g.set(n, t), t;
|
|
10
16
|
}
|
|
11
|
-
function
|
|
12
|
-
return n && (n[
|
|
17
|
+
function o(n) {
|
|
18
|
+
return n && (n[E] || n);
|
|
13
19
|
}
|
|
14
|
-
class
|
|
20
|
+
class T {
|
|
15
21
|
constructor(t) {
|
|
16
|
-
|
|
17
|
-
|
|
22
|
+
f(this, "triggers", {});
|
|
23
|
+
f(this, "cachedFns", {});
|
|
18
24
|
this.obj = t;
|
|
19
25
|
}
|
|
20
26
|
get(t, e, r) {
|
|
21
|
-
if (e ===
|
|
27
|
+
if (e === E)
|
|
22
28
|
return this.obj;
|
|
23
|
-
|
|
24
|
-
|
|
29
|
+
if (v(t))
|
|
30
|
+
return Reflect.get(o(t), e);
|
|
31
|
+
let s = R(t, e) || Reflect.get(t, e, r);
|
|
32
|
+
return typeof s == "function" ? this.cachedFns[e] || (this.cachedFns[e] = s.bind(c(t))) : (typeof s == "object" && (s = c(s), _(s, this.getTrigger(e))), s);
|
|
25
33
|
}
|
|
26
34
|
set(t, e, r, s) {
|
|
27
|
-
if (r && (r =
|
|
35
|
+
if (r && (r = o(r)), r === o(Reflect.get(t, e)))
|
|
28
36
|
return !0;
|
|
29
|
-
const i =
|
|
30
|
-
return
|
|
37
|
+
const i = j(), u = Reflect.set(t, e, r, s);
|
|
38
|
+
return y(this.obj, [e]), M(i), u;
|
|
31
39
|
}
|
|
32
40
|
getTrigger(t) {
|
|
33
|
-
return t in this.triggers || (this.triggers[t] = (e) =>
|
|
41
|
+
return t in this.triggers || (this.triggers[t] = (e) => y(this.obj, [t, ...e])), this.triggers[t];
|
|
34
42
|
}
|
|
35
43
|
}
|
|
36
|
-
const
|
|
37
|
-
let
|
|
38
|
-
const
|
|
39
|
-
|
|
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) {
|
|
40
56
|
return t === "hasOwnProperty" ? function(e) {
|
|
41
|
-
return
|
|
42
|
-
} : Array.isArray(n) && typeof
|
|
43
|
-
|
|
44
|
-
return
|
|
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);
|
|
45
61
|
} : null;
|
|
46
62
|
}
|
|
47
|
-
const
|
|
48
|
-
function
|
|
49
|
-
return n =
|
|
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);
|
|
50
66
|
}
|
|
51
|
-
function
|
|
52
|
-
|
|
67
|
+
function _(n, t) {
|
|
68
|
+
m(n).add(t);
|
|
53
69
|
}
|
|
54
|
-
function
|
|
70
|
+
function q(n, t) {
|
|
55
71
|
const e = (s) => {
|
|
56
|
-
const i =
|
|
57
|
-
|
|
72
|
+
const i = k.has(t);
|
|
73
|
+
k.add(t), i || I(() => t(s)), _(n, e);
|
|
58
74
|
}, r = () => {
|
|
59
|
-
|
|
75
|
+
m(n).delete(e);
|
|
60
76
|
};
|
|
61
|
-
return
|
|
77
|
+
return _(n, e), r;
|
|
62
78
|
}
|
|
63
|
-
function
|
|
64
|
-
const e =
|
|
79
|
+
function y(n, t) {
|
|
80
|
+
const e = m(n), r = [...e];
|
|
65
81
|
e.clear(), r.forEach((s) => s(t));
|
|
66
82
|
}
|
|
67
|
-
const
|
|
68
|
-
function
|
|
69
|
-
const t = new EventTarget(), e = new Event(
|
|
70
|
-
t.addEventListener(
|
|
71
|
-
t.removeEventListener(
|
|
83
|
+
const b = "__easy_model_event__";
|
|
84
|
+
function I(n) {
|
|
85
|
+
const t = new EventTarget(), e = new Event(b);
|
|
86
|
+
t.addEventListener(b, () => {
|
|
87
|
+
t.removeEventListener(b, n), n();
|
|
72
88
|
}), t.dispatchEvent(e);
|
|
73
89
|
}
|
|
74
|
-
function
|
|
75
|
-
return
|
|
90
|
+
function j() {
|
|
91
|
+
return d ? null : d = Symbol();
|
|
76
92
|
}
|
|
77
|
-
function
|
|
78
|
-
|
|
93
|
+
function M(n) {
|
|
94
|
+
d === n && (d = null, k.clear());
|
|
79
95
|
}
|
|
80
|
-
let
|
|
81
|
-
const
|
|
82
|
-
function
|
|
83
|
-
return new Proxy(n, new
|
|
96
|
+
let h = null;
|
|
97
|
+
const F = Symbol("filterSymbol");
|
|
98
|
+
function L(n) {
|
|
99
|
+
return new Proxy(n, new a(n));
|
|
84
100
|
}
|
|
85
|
-
function
|
|
101
|
+
function U(n, t, e) {
|
|
86
102
|
const r = e ? n(...e) : Object.create(n.prototype);
|
|
87
|
-
return
|
|
103
|
+
return c(Object.assign(r, t));
|
|
88
104
|
}
|
|
89
|
-
const
|
|
105
|
+
const O = class {
|
|
90
106
|
constructor(t) {
|
|
91
|
-
|
|
92
|
-
|
|
107
|
+
f(this, "fakeCtor");
|
|
108
|
+
f(this, "caches", /* @__PURE__ */ new Map());
|
|
93
109
|
this.Ctor = t;
|
|
94
110
|
const e = function() {
|
|
95
111
|
};
|
|
@@ -98,17 +114,17 @@ const v = class {
|
|
|
98
114
|
constructor: t,
|
|
99
115
|
__proto__: t.prototype
|
|
100
116
|
},
|
|
101
|
-
|
|
117
|
+
O.prototypeHandlers
|
|
102
118
|
), this.fakeCtor = e;
|
|
103
119
|
}
|
|
104
120
|
apply(t, e, r) {
|
|
105
|
-
const s =
|
|
106
|
-
|
|
121
|
+
const s = h;
|
|
122
|
+
h = this.fakeCtor;
|
|
107
123
|
const i = this.getInstance(r) || this.register(
|
|
108
124
|
r,
|
|
109
125
|
Reflect.construct(this.Ctor, this.filterArgs(r), this.fakeCtor)
|
|
110
126
|
);
|
|
111
|
-
return
|
|
127
|
+
return h = s, i;
|
|
112
128
|
}
|
|
113
129
|
construct(t, e, r) {
|
|
114
130
|
const s = Reflect.construct(
|
|
@@ -116,7 +132,16 @@ const v = class {
|
|
|
116
132
|
this.filterArgs(e),
|
|
117
133
|
r === this.Ctor ? this.fakeCtor : r
|
|
118
134
|
);
|
|
119
|
-
return
|
|
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;
|
|
120
145
|
}
|
|
121
146
|
getInstance(t) {
|
|
122
147
|
for (const e of this.caches.keys()) {
|
|
@@ -125,36 +150,36 @@ const v = class {
|
|
|
125
150
|
))
|
|
126
151
|
continue;
|
|
127
152
|
const r = this.caches.get(e);
|
|
128
|
-
return r.deref() ?
|
|
153
|
+
return r.deref() ? c(r.deref()) : (this.caches.delete(e), null);
|
|
129
154
|
}
|
|
130
155
|
return null;
|
|
131
156
|
}
|
|
132
157
|
register(t, e) {
|
|
133
|
-
return this.caches.set(t, new WeakRef(e)),
|
|
158
|
+
return this.caches.set(t, new WeakRef(e)), c(e);
|
|
134
159
|
}
|
|
135
160
|
isShallowEqualObj(t, e) {
|
|
136
161
|
const r = Object.keys(t), s = e.keys(e);
|
|
137
162
|
return r.length === s.length && r.every((i) => t[i] === e[i]);
|
|
138
163
|
}
|
|
139
164
|
filterArgs(t) {
|
|
140
|
-
return t.filter((e) => e !==
|
|
165
|
+
return t.filter((e) => e !== F);
|
|
141
166
|
}
|
|
142
167
|
};
|
|
143
|
-
let
|
|
144
|
-
|
|
168
|
+
let a = O;
|
|
169
|
+
f(a, "prototypeHandlers", {
|
|
145
170
|
get(t, e, r) {
|
|
146
|
-
const s =
|
|
171
|
+
const s = R(t, e);
|
|
147
172
|
if (s)
|
|
148
173
|
return s;
|
|
149
174
|
const i = Reflect.get(t, e, r);
|
|
150
|
-
return typeof e == "symbol" || ["constructor", "__proto__"].includes(e) || typeof i != "function" ? i : function(...
|
|
151
|
-
return i.apply(
|
|
175
|
+
return typeof e == "symbol" || ["constructor", "__proto__"].includes(e) || typeof i != "function" ? i : function(...u) {
|
|
176
|
+
return i.apply(c(this), u);
|
|
152
177
|
};
|
|
153
178
|
}
|
|
154
179
|
});
|
|
155
180
|
export {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
181
|
+
U as extend,
|
|
182
|
+
F as filterSymbol,
|
|
183
|
+
L as provide,
|
|
184
|
+
q as subscribe
|
|
160
185
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(o,c){typeof exports=="object"&&typeof module<"u"?c(exports):typeof define=="function"&&define.amd?define(["exports"],c):(o=typeof globalThis<"u"?globalThis:o||self,c(o["@e7w/easy-model"]={}))})(this,function(o){"use strict";var
|
|
1
|
+
(function(o,c){typeof exports=="object"&&typeof module<"u"?c(exports):typeof define=="function"&&define.amd?define(["exports"],c):(o=typeof globalThis<"u"?globalThis:o||self,c(o["@e7w/easy-model"]={}))})(this,function(o){"use strict";var L=Object.defineProperty;var U=(o,c,u)=>c in o?L(o,c,{enumerable:!0,configurable:!0,writable:!0,value:u}):o[c]=u;var h=(o,c,u)=>(U(o,typeof c!="symbol"?c+"":c,u),u);const c=[Promise,WeakMap,WeakSet,Map,Set];function u(n){return c.some(e=>n instanceof e)}const m=new WeakMap,E=Symbol("origin");function l(n){if(n=f(n),u(n))return n;if(m.has(n))return m.get(n);const e=new Proxy(n,new A(n));return m.set(n,e),e}function f(n){return n&&(n[E]||n)}class A{constructor(e){h(this,"triggers",{});h(this,"cachedFns",{});this.obj=e}get(e,t,s){if(t===E)return this.obj;if(u(e))return Reflect.get(f(e),t);let r=M(e,t)||Reflect.get(e,t,s);return typeof r=="function"?this.cachedFns[t]||(this.cachedFns[t]=r.bind(l(e))):(typeof r=="object"&&(r=l(r),v(r,this.getTrigger(t))),r)}set(e,t,s,r){if(s&&(s=f(s)),s===f(Reflect.get(e,t)))return!0;const i=R(),a=Reflect.set(e,t,s,r);return p(this.obj,[t]),w(i),a}getTrigger(e){return e in this.triggers||(this.triggers[e]=t=>p(this.obj,[e,...t])),this.triggers[e]}}const k=new Set;let d=null;const y=["push","pop","shift","unshift","splice","sort","reverse"],H=["includes","indexOf","lastIndexOf"],W=["forEach","map"];function M(n,e){return e==="hasOwnProperty"?function(t){return f(this).hasOwnProperty(t)}:Array.isArray(n)&&typeof y[e]=="function"?W.includes(e)?n[e]:function(...t){const s=y[e].apply(f(this),t);return H.includes(e)&&(s===-1||s===!1)?y[e].apply(f(this),...t.map(f)):(y.includes(e)&&p(this,[e]),s)}:null}const S=new WeakMap;function _(n){return n=f(n),S.has(n)||S.set(n,new Set),S.get(n)}function v(n,e){_(n).add(e)}function x(n,e){const t=r=>{const i=k.has(e);k.add(e),i||I(()=>e(r)),v(n,t)},s=()=>{_(n).delete(t)};return v(n,t),s}function p(n,e){const t=_(n),s=[...t];t.clear(),s.forEach(r=>r(e))}const O="__easy_model_event__";function I(n){const e=new EventTarget,t=new Event(O);e.addEventListener(O,()=>{e.removeEventListener(O,n),n()}),e.dispatchEvent(t)}function R(){return d?null:d=Symbol()}function w(n){d===n&&(d=null,k.clear())}let b=null;const T=Symbol("filterSymbol");function P(n){return new Proxy(n,new g(n))}function F(n,e,t){const s=t?n(...t):Object.create(n.prototype);return l(Object.assign(s,e))}const j=class{constructor(e){h(this,"fakeCtor");h(this,"caches",new Map);this.Ctor=e;const t=function(){};t.prototype=new Proxy({constructor:e,__proto__:e.prototype},j.prototypeHandlers),this.fakeCtor=t}apply(e,t,s){const r=b;b=this.fakeCtor;const i=this.getInstance(s)||this.register(s,Reflect.construct(this.Ctor,this.filterArgs(s),this.fakeCtor));return b=r,i}construct(e,t,s){const r=Reflect.construct(e,this.filterArgs(t),s===this.Ctor?this.fakeCtor:s);return b!==s?l(r):r}set(e,t,s,r){if(s&&(s=f(s)),s===f(Reflect.get(e,t)))return!0;const i=R(),a=Reflect.set(e,t,s,r);return this.caches.forEach(q=>{const C=q.deref();C&&p(C,[])}),w(i),a}getInstance(e){for(const t of this.caches.keys()){if(t.length!==e.length||!t.every((r,i)=>r===e[i]?!0:typeof r=="object"&&typeof e[i]=="object"?this.isShallowEqualObj(r,e[i]):!1))continue;const s=this.caches.get(t);return s.deref()?l(s.deref()):(this.caches.delete(t),null)}return null}register(e,t){return this.caches.set(e,new WeakRef(t)),l(t)}isShallowEqualObj(e,t){const s=Object.keys(e),r=t.keys(t);return s.length===r.length&&s.every(i=>e[i]===t[i])}filterArgs(e){return e.filter(t=>t!==T)}};let g=j;h(g,"prototypeHandlers",{get(e,t,s){const r=M(e,t);if(r)return r;const i=Reflect.get(e,t,s);return typeof t=="symbol"||["constructor","__proto__"].includes(t)||typeof i!="function"?i:function(...a){return i.apply(l(this),a)}}}),o.extend=F,o.filterSymbol=T,o.provide=P,o.subscribe=x,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
package/src/helpers.ts
CHANGED
|
@@ -5,7 +5,17 @@ let triggerSymbol: Symbol | null = null;
|
|
|
5
5
|
|
|
6
6
|
export type Ctor = new (...args: any[]) => any;
|
|
7
7
|
export type Obj = Record<string | symbol | number, any>;
|
|
8
|
-
const fakeArr: 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"];
|
|
9
19
|
export function safeGet(target: Obj, p: string | symbol): any {
|
|
10
20
|
if (p === "hasOwnProperty") {
|
|
11
21
|
return function (this: Obj, key: string) {
|
|
@@ -13,13 +23,14 @@ export function safeGet(target: Obj, p: string | symbol): any {
|
|
|
13
23
|
};
|
|
14
24
|
}
|
|
15
25
|
if (Array.isArray(target) && typeof fakeArr[p] === "function") {
|
|
26
|
+
if (safeMethods.includes(p)) return target[p as keyof typeof target];
|
|
16
27
|
return function (this: Obj, ...args: any) {
|
|
17
|
-
|
|
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
|
+
}
|
|
18
32
|
if (fakeArr.includes(p)) {
|
|
19
|
-
ret = fakeArr[p].apply(getOrigin(this), args);
|
|
20
33
|
trigger(this, [p]);
|
|
21
|
-
} else {
|
|
22
|
-
ret = fakeArr[p].apply(this, args);
|
|
23
34
|
}
|
|
24
35
|
return ret;
|
|
25
36
|
};
|
package/src/observe.ts
CHANGED
|
@@ -7,10 +7,16 @@ import {
|
|
|
7
7
|
endTrigger,
|
|
8
8
|
} from "./helpers";
|
|
9
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
|
+
|
|
10
15
|
const observedMap = new WeakMap();
|
|
11
16
|
const origin = Symbol("origin");
|
|
12
17
|
export function observe<T extends Obj>(target: T): T {
|
|
13
18
|
target = getOrigin(target);
|
|
19
|
+
if (isUnsafeInstance(target)) return target;
|
|
14
20
|
if (observedMap.has(target)) return observedMap.get(target);
|
|
15
21
|
const ret = new Proxy(target, new ObserveHandler(target));
|
|
16
22
|
observedMap.set(target, ret);
|
|
@@ -34,6 +40,7 @@ class ObserveHandler<T extends Obj> implements ProxyHandler<T> {
|
|
|
34
40
|
|
|
35
41
|
public get(target: T, p: string | symbol, receiver: any): any {
|
|
36
42
|
if (p === origin) return this.obj;
|
|
43
|
+
if (isUnsafeInstance(target)) return Reflect.get(getOrigin(target), p);
|
|
37
44
|
let ret = safeGet(target, p) || Reflect.get(target, p, receiver);
|
|
38
45
|
if (typeof ret === "function")
|
|
39
46
|
return (
|
package/src/provide.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
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";
|
|
3
10
|
|
|
4
11
|
let creatingTarget: any = null;
|
|
5
12
|
|
|
@@ -106,6 +113,24 @@ class Provider<T extends Ctor> implements ProxyHandler<T> {
|
|
|
106
113
|
return instance;
|
|
107
114
|
}
|
|
108
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
|
+
|
|
109
134
|
private getInstance(args: ConstructorParameters<T>): InstanceType<T> | null {
|
|
110
135
|
for (const cachedArgs of this.caches.keys()) {
|
|
111
136
|
if (
|