@e7w/easy-model 0.0.7 → 0.0.8
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 +43 -41
- package/dist/index.umd.js +1 -1
- package/package.json +1 -1
- package/src/helpers.ts +9 -4
package/dist/index.es.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
var j = Object.defineProperty;
|
|
2
2
|
var E = (n, t, e) => t in n ? j(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
|
|
3
3
|
var i = (n, t, e) => (E(n, typeof t != "symbol" ? t + "" : t, e), e);
|
|
4
|
-
const
|
|
5
|
-
function
|
|
6
|
-
if (n = u(n),
|
|
7
|
-
return
|
|
4
|
+
const d = /* @__PURE__ */ new WeakMap(), v = Symbol("origin");
|
|
5
|
+
function c(n) {
|
|
6
|
+
if (n = u(n), d.has(n))
|
|
7
|
+
return d.get(n);
|
|
8
8
|
const t = new Proxy(n, new R(n));
|
|
9
|
-
return
|
|
9
|
+
return d.set(n, t), t;
|
|
10
10
|
}
|
|
11
11
|
function u(n) {
|
|
12
12
|
return n && (n[v] || n);
|
|
@@ -21,60 +21,62 @@ class R {
|
|
|
21
21
|
if (e === v)
|
|
22
22
|
return this.obj;
|
|
23
23
|
let r = w(t, e) || Reflect.get(t, e, s);
|
|
24
|
-
return typeof r == "function" ? this.cachedFns[e] || (this.cachedFns[e] = r.bind(
|
|
24
|
+
return typeof r == "function" ? this.cachedFns[e] || (this.cachedFns[e] = r.bind(c(t))) : (typeof r == "object" && (r = c(r), l(r, this.getTrigger(e))), r);
|
|
25
25
|
}
|
|
26
26
|
set(t, e, s, r) {
|
|
27
27
|
if (s && (s = u(s)), s === u(Reflect.get(t, e)))
|
|
28
28
|
return !0;
|
|
29
|
-
const
|
|
30
|
-
return
|
|
29
|
+
const o = Reflect.set(t, e, s, r);
|
|
30
|
+
return g(this.obj, [e]), o;
|
|
31
31
|
}
|
|
32
32
|
getTrigger(t) {
|
|
33
|
-
return t in this.triggers || (this.triggers[t] = (e) =>
|
|
33
|
+
return t in this.triggers || (this.triggers[t] = (e) => g(this.obj, [t, ...e])), this.triggers[t];
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
|
-
const
|
|
36
|
+
const b = /* @__PURE__ */ new Set(), f = ["push", "pop", "shift", "unshift", "splice", "sort"];
|
|
37
37
|
function w(n, t) {
|
|
38
38
|
return t === "hasOwnProperty" ? function(e) {
|
|
39
39
|
return u(this).hasOwnProperty(e);
|
|
40
|
-
} : Array.isArray(n) && typeof
|
|
41
|
-
|
|
42
|
-
return
|
|
40
|
+
} : Array.isArray(n) && typeof f[t] == "function" ? function(...e) {
|
|
41
|
+
let s;
|
|
42
|
+
return f.includes(t) ? (s = f[t].apply(u(this), e), g(this, [t])) : s = f[t].apply(this, e), s;
|
|
43
43
|
} : null;
|
|
44
44
|
}
|
|
45
|
-
const
|
|
45
|
+
const p = /* @__PURE__ */ new WeakMap();
|
|
46
46
|
function k(n) {
|
|
47
|
-
return n = u(n),
|
|
47
|
+
return n = u(n), p.has(n) || p.set(n, /* @__PURE__ */ new Set()), p.get(n);
|
|
48
48
|
}
|
|
49
|
-
function
|
|
49
|
+
function l(n, t) {
|
|
50
50
|
k(n).add(t);
|
|
51
51
|
}
|
|
52
52
|
function m(n, t) {
|
|
53
53
|
const e = (r) => {
|
|
54
|
-
|
|
54
|
+
if (b.has(t))
|
|
55
|
+
return l(n, e);
|
|
56
|
+
C(() => t(r)), b.add(t), l(n, e);
|
|
55
57
|
}, s = () => {
|
|
56
58
|
k(n).delete(e);
|
|
57
59
|
};
|
|
58
|
-
return
|
|
60
|
+
return l(n, e), s;
|
|
59
61
|
}
|
|
60
|
-
function
|
|
62
|
+
function g(n, t) {
|
|
61
63
|
const e = k(n), s = [...e].reverse();
|
|
62
|
-
e.clear(), s.forEach((r) => r(t)),
|
|
64
|
+
e.clear(), s.forEach((r) => r(t)), b.clear();
|
|
63
65
|
}
|
|
64
|
-
const
|
|
66
|
+
const y = "__easy_model_event__";
|
|
65
67
|
function C(n) {
|
|
66
|
-
const t = new EventTarget(), e = new Event(
|
|
67
|
-
t.addEventListener(
|
|
68
|
-
t.removeEventListener(
|
|
68
|
+
const t = new EventTarget(), e = new Event(y);
|
|
69
|
+
t.addEventListener(y, () => {
|
|
70
|
+
t.removeEventListener(y, n), n();
|
|
69
71
|
}), t.dispatchEvent(e);
|
|
70
72
|
}
|
|
71
|
-
let
|
|
73
|
+
let h = null;
|
|
72
74
|
function M(n) {
|
|
73
|
-
return new Proxy(n, new
|
|
75
|
+
return new Proxy(n, new a(n));
|
|
74
76
|
}
|
|
75
77
|
function S(n, t, e) {
|
|
76
78
|
const s = e ? n(...e) : Object.create(n.prototype);
|
|
77
|
-
return
|
|
79
|
+
return c(Object.assign(s, t));
|
|
78
80
|
}
|
|
79
81
|
const _ = class {
|
|
80
82
|
constructor(t) {
|
|
@@ -92,13 +94,13 @@ const _ = class {
|
|
|
92
94
|
), this.fakeCtor = e;
|
|
93
95
|
}
|
|
94
96
|
apply(t, e, s) {
|
|
95
|
-
const r =
|
|
96
|
-
|
|
97
|
-
const
|
|
97
|
+
const r = h;
|
|
98
|
+
h = this.fakeCtor;
|
|
99
|
+
const o = this.getInstance(s) || this.register(
|
|
98
100
|
s,
|
|
99
101
|
Reflect.construct(this.Ctor, s, this.fakeCtor)
|
|
100
102
|
);
|
|
101
|
-
return
|
|
103
|
+
return h = r, o;
|
|
102
104
|
}
|
|
103
105
|
construct(t, e, s) {
|
|
104
106
|
const r = Reflect.construct(
|
|
@@ -106,36 +108,36 @@ const _ = class {
|
|
|
106
108
|
e,
|
|
107
109
|
s === this.Ctor ? this.fakeCtor : s
|
|
108
110
|
);
|
|
109
|
-
return
|
|
111
|
+
return h !== s ? c(r) : r;
|
|
110
112
|
}
|
|
111
113
|
getInstance(t) {
|
|
112
114
|
for (const e of this.caches.keys()) {
|
|
113
115
|
if (e.length !== t.length || !e.every(
|
|
114
|
-
(r,
|
|
116
|
+
(r, o) => r === t[o] ? !0 : typeof r == "object" && typeof t[o] == "object" ? this.isShallowEqualObj(r, t[o]) : !1
|
|
115
117
|
))
|
|
116
118
|
continue;
|
|
117
119
|
const s = this.caches.get(e);
|
|
118
|
-
return s.deref() ?
|
|
120
|
+
return s.deref() ? c(s.deref()) : (this.caches.delete(e), null);
|
|
119
121
|
}
|
|
120
122
|
return null;
|
|
121
123
|
}
|
|
122
124
|
register(t, e) {
|
|
123
|
-
return this.caches.set(t, new WeakRef(e)),
|
|
125
|
+
return this.caches.set(t, new WeakRef(e)), c(e);
|
|
124
126
|
}
|
|
125
127
|
isShallowEqualObj(t, e) {
|
|
126
128
|
const s = Object.keys(t), r = e.keys(e);
|
|
127
|
-
return s.length === r.length && s.every((
|
|
129
|
+
return s.length === r.length && s.every((o) => t[o] === e[o]);
|
|
128
130
|
}
|
|
129
131
|
};
|
|
130
|
-
let
|
|
131
|
-
i(
|
|
132
|
+
let a = _;
|
|
133
|
+
i(a, "prototypeHandlers", {
|
|
132
134
|
get(t, e, s) {
|
|
133
135
|
const r = w(t, e);
|
|
134
136
|
if (r)
|
|
135
137
|
return r;
|
|
136
|
-
const
|
|
137
|
-
return typeof e == "symbol" || ["constructor", "__proto__"].includes(e) || typeof
|
|
138
|
-
return
|
|
138
|
+
const o = Reflect.get(t, e, s);
|
|
139
|
+
return typeof e == "symbol" || ["constructor", "__proto__"].includes(e) || typeof o != "function" ? o : function(...O) {
|
|
140
|
+
return o.apply(c(this), O);
|
|
139
141
|
};
|
|
140
142
|
}
|
|
141
143
|
});
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(i,o){typeof exports=="object"&&typeof module<"u"?o(exports):typeof define=="function"&&define.amd?define(["exports"],o):(i=typeof globalThis<"u"?globalThis:i||self,o(i["@e7w/easy-model"]={}))})(this,function(i){"use strict";var H=Object.defineProperty;var M=(i,o,f)=>o in i?H(i,o,{enumerable:!0,configurable:!0,writable:!0,value:f}):i[o]=f;var l=(i,o,f)=>(M(i,typeof o!="symbol"?o+"":o,f),f);const o=new WeakMap,f=Symbol("origin");function u(n){if(n=h(n),o.has(n))return o.get(n);const e=new Proxy(n,new j(n));return o.set(n,e),e}function h(n){return n&&(n[f]||n)}class j{constructor(e){l(this,"triggers",{});l(this,"cachedFns",{});this.obj=e}get(e,t,s){if(t===f)return this.obj;let r=O(e,t)||Reflect.get(e,t,s);return typeof r=="function"?this.cachedFns[t]||(this.cachedFns[t]=r.bind(u(e))):(typeof r=="object"&&(r=u(r),
|
|
1
|
+
(function(i,o){typeof exports=="object"&&typeof module<"u"?o(exports):typeof define=="function"&&define.amd?define(["exports"],o):(i=typeof globalThis<"u"?globalThis:i||self,o(i["@e7w/easy-model"]={}))})(this,function(i){"use strict";var H=Object.defineProperty;var M=(i,o,f)=>o in i?H(i,o,{enumerable:!0,configurable:!0,writable:!0,value:f}):i[o]=f;var l=(i,o,f)=>(M(i,typeof o!="symbol"?o+"":o,f),f);const o=new WeakMap,f=Symbol("origin");function u(n){if(n=h(n),o.has(n))return o.get(n);const e=new Proxy(n,new j(n));return o.set(n,e),e}function h(n){return n&&(n[f]||n)}class j{constructor(e){l(this,"triggers",{});l(this,"cachedFns",{});this.obj=e}get(e,t,s){if(t===f)return this.obj;let r=O(e,t)||Reflect.get(e,t,s);return typeof r=="function"?this.cachedFns[t]||(this.cachedFns[t]=r.bind(u(e))):(typeof r=="object"&&(r=u(r),d(r,this.getTrigger(t))),r)}set(e,t,s,r){if(s&&(s=h(s)),s===h(Reflect.get(e,t)))return!0;const c=Reflect.set(e,t,s,r);return _(this.obj,[t]),c}getTrigger(e){return e in this.triggers||(this.triggers[e]=t=>_(this.obj,[e,...t])),this.triggers[e]}}const b=new Set,a=["push","pop","shift","unshift","splice","sort"];function O(n,e){return e==="hasOwnProperty"?function(t){return h(this).hasOwnProperty(t)}:Array.isArray(n)&&typeof a[e]=="function"?function(...t){let s;return a.includes(e)?(s=a[e].apply(h(this),t),_(this,[e])):s=a[e].apply(this,t),s}:null}const g=new WeakMap;function k(n){return n=h(n),g.has(n)||g.set(n,new Set),g.get(n)}function d(n,e){k(n).add(e)}function m(n,e){const t=r=>{if(b.has(e))return d(n,t);E(()=>e(r)),b.add(e),d(n,t)},s=()=>{k(n).delete(t)};return d(n,t),s}function _(n,e){const t=k(n),s=[...t].reverse();t.clear(),s.forEach(r=>r(e)),b.clear()}const v="__easy_model_event__";function E(n){const e=new EventTarget,t=new Event(v);e.addEventListener(v,()=>{e.removeEventListener(v,n),n()}),e.dispatchEvent(t)}let p=null;function R(n){return new Proxy(n,new y(n))}function S(n,e,t){const s=t?n(...t):Object.create(n.prototype);return u(Object.assign(s,e))}const w=class{constructor(e){l(this,"fakeCtor");l(this,"caches",new Map);this.Ctor=e;const t=function(){};t.prototype=new Proxy({constructor:e,__proto__:e.prototype},w.prototypeHandlers),this.fakeCtor=t}apply(e,t,s){const r=p;p=this.fakeCtor;const c=this.getInstance(s)||this.register(s,Reflect.construct(this.Ctor,s,this.fakeCtor));return p=r,c}construct(e,t,s){const r=Reflect.construct(e,t,s===this.Ctor?this.fakeCtor:s);return p!==s?u(r):r}getInstance(e){for(const t of this.caches.keys()){if(t.length!==e.length||!t.every((r,c)=>r===e[c]?!0:typeof r=="object"&&typeof e[c]=="object"?this.isShallowEqualObj(r,e[c]):!1))continue;const s=this.caches.get(t);return s.deref()?u(s.deref()):(this.caches.delete(t),null)}return null}register(e,t){return this.caches.set(e,new WeakRef(t)),u(t)}isShallowEqualObj(e,t){const s=Object.keys(e),r=t.keys(t);return s.length===r.length&&s.every(c=>e[c]===t[c])}};let y=w;l(y,"prototypeHandlers",{get(e,t,s){const r=O(e,t);if(r)return r;const c=Reflect.get(e,t,s);return typeof t=="symbol"||["constructor","__proto__"].includes(t)||typeof c!="function"?c:function(...C){return c.apply(u(this),C)}}}),i.extend=S,i.provide=R,i.subscribe=m,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
package/src/helpers.ts
CHANGED
|
@@ -4,7 +4,7 @@ const triggered = new Set();
|
|
|
4
4
|
|
|
5
5
|
export type Ctor = new (...args: any[]) => any;
|
|
6
6
|
export type Obj = Record<string | symbol | number, any>;
|
|
7
|
-
const fakeArr: any = ["push", "pop", "shift", "unshift", "splice"];
|
|
7
|
+
const fakeArr: any = ["push", "pop", "shift", "unshift", "splice", "sort"];
|
|
8
8
|
export function safeGet(target: Obj, p: string | symbol): any {
|
|
9
9
|
if (p === "hasOwnProperty") {
|
|
10
10
|
return function (this: Obj, key: string) {
|
|
@@ -13,8 +13,13 @@ export function safeGet(target: Obj, p: string | symbol): any {
|
|
|
13
13
|
}
|
|
14
14
|
if (Array.isArray(target) && typeof fakeArr[p] === "function") {
|
|
15
15
|
return function (this: Obj, ...args: any) {
|
|
16
|
-
|
|
17
|
-
if (fakeArr.includes(p))
|
|
16
|
+
let ret: any;
|
|
17
|
+
if (fakeArr.includes(p)) {
|
|
18
|
+
ret = fakeArr[p].apply(getOrigin(this), args);
|
|
19
|
+
trigger(this, [p]);
|
|
20
|
+
} else {
|
|
21
|
+
ret = fakeArr[p].apply(this, args);
|
|
22
|
+
}
|
|
18
23
|
return ret;
|
|
19
24
|
};
|
|
20
25
|
}
|
|
@@ -44,7 +49,7 @@ export function subscribe(
|
|
|
44
49
|
handler: (path: (string | symbol)[]) => void
|
|
45
50
|
): () => void {
|
|
46
51
|
const wrappedHandler: typeof handler = (path) => {
|
|
47
|
-
if (triggered.has(handler)) return;
|
|
52
|
+
if (triggered.has(handler)) return track(target, wrappedHandler);
|
|
48
53
|
triggerTop(() => handler(path));
|
|
49
54
|
triggered.add(handler);
|
|
50
55
|
track(target, wrappedHandler);
|