@e7w/easy-model 0.0.9 → 0.0.10
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 +84 -75
- package/dist/index.umd.js +1 -1
- package/package.json +1 -1
- package/src/provide.ts +27 -2
package/dist/index.es.js
CHANGED
|
@@ -1,95 +1,95 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
const
|
|
5
|
-
function
|
|
6
|
-
if (n =
|
|
7
|
-
return
|
|
8
|
-
const t = new Proxy(n, new
|
|
9
|
-
return
|
|
1
|
+
var A = Object.defineProperty;
|
|
2
|
+
var C = (n, t, e) => t in n ? A(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
|
|
3
|
+
var f = (n, t, e) => (C(n, typeof t != "symbol" ? t + "" : t, e), e);
|
|
4
|
+
const d = /* @__PURE__ */ new WeakMap(), S = Symbol("origin");
|
|
5
|
+
function c(n) {
|
|
6
|
+
if (n = o(n), d.has(n))
|
|
7
|
+
return d.get(n);
|
|
8
|
+
const t = new Proxy(n, new H(n));
|
|
9
|
+
return d.set(n, t), t;
|
|
10
10
|
}
|
|
11
|
-
function
|
|
12
|
-
return n && (n[
|
|
11
|
+
function o(n) {
|
|
12
|
+
return n && (n[S] || n);
|
|
13
13
|
}
|
|
14
|
-
class
|
|
14
|
+
class H {
|
|
15
15
|
constructor(t) {
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
f(this, "triggers", {});
|
|
17
|
+
f(this, "cachedFns", {});
|
|
18
18
|
this.obj = t;
|
|
19
19
|
}
|
|
20
20
|
get(t, e, r) {
|
|
21
|
-
if (e ===
|
|
21
|
+
if (e === S)
|
|
22
22
|
return this.obj;
|
|
23
|
-
let s =
|
|
24
|
-
return typeof s == "function" ? this.cachedFns[e] || (this.cachedFns[e] = s.bind(
|
|
23
|
+
let s = j(t, e) || Reflect.get(t, e, r);
|
|
24
|
+
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
25
|
}
|
|
26
26
|
set(t, e, r, s) {
|
|
27
|
-
if (r && (r =
|
|
27
|
+
if (r && (r = o(r)), r === o(Reflect.get(t, e)))
|
|
28
28
|
return !0;
|
|
29
|
-
const i =
|
|
30
|
-
return
|
|
29
|
+
const i = E(), u = Reflect.set(t, e, r, s);
|
|
30
|
+
return g(this.obj, [e]), R(i), u;
|
|
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
|
|
37
|
-
let
|
|
38
|
-
const
|
|
39
|
-
function
|
|
36
|
+
const k = /* @__PURE__ */ new Set();
|
|
37
|
+
let y = null;
|
|
38
|
+
const l = ["push", "pop", "shift", "unshift", "splice", "sort"];
|
|
39
|
+
function j(n, t) {
|
|
40
40
|
return t === "hasOwnProperty" ? function(e) {
|
|
41
|
-
return
|
|
42
|
-
} : Array.isArray(n) && typeof
|
|
41
|
+
return o(this).hasOwnProperty(e);
|
|
42
|
+
} : Array.isArray(n) && typeof l[t] == "function" ? function(...e) {
|
|
43
43
|
let r;
|
|
44
|
-
return
|
|
44
|
+
return l.includes(t) ? (r = l[t].apply(o(this), e), g(this, [t])) : r = l[t].apply(this, e), r;
|
|
45
45
|
} : null;
|
|
46
46
|
}
|
|
47
|
-
const
|
|
48
|
-
function
|
|
49
|
-
return n =
|
|
47
|
+
const p = /* @__PURE__ */ new WeakMap();
|
|
48
|
+
function v(n) {
|
|
49
|
+
return n = o(n), p.has(n) || p.set(n, /* @__PURE__ */ new Set()), p.get(n);
|
|
50
50
|
}
|
|
51
|
-
function
|
|
52
|
-
|
|
51
|
+
function _(n, t) {
|
|
52
|
+
v(n).add(t);
|
|
53
53
|
}
|
|
54
|
-
function
|
|
54
|
+
function F(n, t) {
|
|
55
55
|
const e = (s) => {
|
|
56
|
-
const i =
|
|
57
|
-
|
|
56
|
+
const i = k.has(t);
|
|
57
|
+
k.add(t), i || T(() => t(s)), _(n, e);
|
|
58
58
|
}, r = () => {
|
|
59
|
-
|
|
59
|
+
v(n).delete(e);
|
|
60
60
|
};
|
|
61
|
-
return
|
|
61
|
+
return _(n, e), r;
|
|
62
62
|
}
|
|
63
|
-
function
|
|
64
|
-
const e =
|
|
63
|
+
function g(n, t) {
|
|
64
|
+
const e = v(n), r = [...e];
|
|
65
65
|
e.clear(), r.forEach((s) => s(t));
|
|
66
66
|
}
|
|
67
|
-
const
|
|
68
|
-
function
|
|
69
|
-
const t = new EventTarget(), e = new Event(
|
|
70
|
-
t.addEventListener(
|
|
71
|
-
t.removeEventListener(
|
|
67
|
+
const b = "__easy_model_event__";
|
|
68
|
+
function T(n) {
|
|
69
|
+
const t = new EventTarget(), e = new Event(b);
|
|
70
|
+
t.addEventListener(b, () => {
|
|
71
|
+
t.removeEventListener(b, n), n();
|
|
72
72
|
}), t.dispatchEvent(e);
|
|
73
73
|
}
|
|
74
|
-
function
|
|
75
|
-
return
|
|
74
|
+
function E() {
|
|
75
|
+
return y ? null : y = Symbol();
|
|
76
76
|
}
|
|
77
|
-
function
|
|
78
|
-
|
|
77
|
+
function R(n) {
|
|
78
|
+
y === n && (y = null, k.clear());
|
|
79
79
|
}
|
|
80
|
-
let
|
|
81
|
-
const
|
|
82
|
-
function
|
|
83
|
-
return new Proxy(n, new
|
|
80
|
+
let h = null;
|
|
81
|
+
const M = Symbol("filterSymbol");
|
|
82
|
+
function W(n) {
|
|
83
|
+
return new Proxy(n, new a(n));
|
|
84
84
|
}
|
|
85
|
-
function
|
|
85
|
+
function q(n, t, e) {
|
|
86
86
|
const r = e ? n(...e) : Object.create(n.prototype);
|
|
87
|
-
return
|
|
87
|
+
return c(Object.assign(r, t));
|
|
88
88
|
}
|
|
89
|
-
const
|
|
89
|
+
const O = class {
|
|
90
90
|
constructor(t) {
|
|
91
|
-
|
|
92
|
-
|
|
91
|
+
f(this, "fakeCtor");
|
|
92
|
+
f(this, "caches", /* @__PURE__ */ new Map());
|
|
93
93
|
this.Ctor = t;
|
|
94
94
|
const e = function() {
|
|
95
95
|
};
|
|
@@ -98,17 +98,17 @@ const v = class {
|
|
|
98
98
|
constructor: t,
|
|
99
99
|
__proto__: t.prototype
|
|
100
100
|
},
|
|
101
|
-
|
|
101
|
+
O.prototypeHandlers
|
|
102
102
|
), this.fakeCtor = e;
|
|
103
103
|
}
|
|
104
104
|
apply(t, e, r) {
|
|
105
|
-
const s =
|
|
106
|
-
|
|
105
|
+
const s = h;
|
|
106
|
+
h = this.fakeCtor;
|
|
107
107
|
const i = this.getInstance(r) || this.register(
|
|
108
108
|
r,
|
|
109
109
|
Reflect.construct(this.Ctor, this.filterArgs(r), this.fakeCtor)
|
|
110
110
|
);
|
|
111
|
-
return
|
|
111
|
+
return h = s, i;
|
|
112
112
|
}
|
|
113
113
|
construct(t, e, r) {
|
|
114
114
|
const s = Reflect.construct(
|
|
@@ -116,7 +116,16 @@ const v = class {
|
|
|
116
116
|
this.filterArgs(e),
|
|
117
117
|
r === this.Ctor ? this.fakeCtor : r
|
|
118
118
|
);
|
|
119
|
-
return
|
|
119
|
+
return h !== r ? c(s) : s;
|
|
120
|
+
}
|
|
121
|
+
set(t, e, r, s) {
|
|
122
|
+
if (r && (r = o(r)), r === o(Reflect.get(t, e)))
|
|
123
|
+
return !0;
|
|
124
|
+
const i = E(), u = Reflect.set(t, e, r, s);
|
|
125
|
+
return this.caches.forEach((w) => {
|
|
126
|
+
const m = w.deref();
|
|
127
|
+
m && g(m, []);
|
|
128
|
+
}), R(i), u;
|
|
120
129
|
}
|
|
121
130
|
getInstance(t) {
|
|
122
131
|
for (const e of this.caches.keys()) {
|
|
@@ -125,36 +134,36 @@ const v = class {
|
|
|
125
134
|
))
|
|
126
135
|
continue;
|
|
127
136
|
const r = this.caches.get(e);
|
|
128
|
-
return r.deref() ?
|
|
137
|
+
return r.deref() ? c(r.deref()) : (this.caches.delete(e), null);
|
|
129
138
|
}
|
|
130
139
|
return null;
|
|
131
140
|
}
|
|
132
141
|
register(t, e) {
|
|
133
|
-
return this.caches.set(t, new WeakRef(e)),
|
|
142
|
+
return this.caches.set(t, new WeakRef(e)), c(e);
|
|
134
143
|
}
|
|
135
144
|
isShallowEqualObj(t, e) {
|
|
136
145
|
const r = Object.keys(t), s = e.keys(e);
|
|
137
146
|
return r.length === s.length && r.every((i) => t[i] === e[i]);
|
|
138
147
|
}
|
|
139
148
|
filterArgs(t) {
|
|
140
|
-
return t.filter((e) => e !==
|
|
149
|
+
return t.filter((e) => e !== M);
|
|
141
150
|
}
|
|
142
151
|
};
|
|
143
|
-
let
|
|
144
|
-
|
|
152
|
+
let a = O;
|
|
153
|
+
f(a, "prototypeHandlers", {
|
|
145
154
|
get(t, e, r) {
|
|
146
|
-
const s =
|
|
155
|
+
const s = j(t, e);
|
|
147
156
|
if (s)
|
|
148
157
|
return s;
|
|
149
158
|
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(
|
|
159
|
+
return typeof e == "symbol" || ["constructor", "__proto__"].includes(e) || typeof i != "function" ? i : function(...u) {
|
|
160
|
+
return i.apply(c(this), u);
|
|
152
161
|
};
|
|
153
162
|
}
|
|
154
163
|
});
|
|
155
164
|
export {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
165
|
+
q as extend,
|
|
166
|
+
M as filterSymbol,
|
|
167
|
+
W as provide,
|
|
168
|
+
F as subscribe
|
|
160
169
|
};
|
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 W=Object.defineProperty;var q=(o,c,f)=>c in o?W(o,c,{enumerable:!0,configurable:!0,writable:!0,value:f}):o[c]=f;var h=(o,c,f)=>(q(o,typeof c!="symbol"?c+"":c,f),f);const c=new WeakMap,f=Symbol("origin");function l(n){if(n=u(n),c.has(n))return c.get(n);const t=new Proxy(n,new A(n));return c.set(n,t),t}function u(n){return n&&(n[f]||n)}class A{constructor(t){h(this,"triggers",{});h(this,"cachedFns",{});this.obj=t}get(t,e,r){if(e===f)return this.obj;let s=j(t,e)||Reflect.get(t,e,r);return typeof s=="function"?this.cachedFns[e]||(this.cachedFns[e]=s.bind(l(t))):(typeof s=="object"&&(s=l(s),v(s,this.getTrigger(e))),s)}set(t,e,r,s){if(r&&(r=u(r)),r===u(Reflect.get(t,e)))return!0;const i=w(),a=Reflect.set(t,e,r,s);return g(this.obj,[e]),E(i),a}getTrigger(t){return t in this.triggers||(this.triggers[t]=e=>g(this.obj,[t,...e])),this.triggers[t]}}const k=new Set;let d=null;const y=["push","pop","shift","unshift","splice","sort"];function j(n,t){return t==="hasOwnProperty"?function(e){return u(this).hasOwnProperty(e)}:Array.isArray(n)&&typeof y[t]=="function"?function(...e){let r;return y.includes(t)?(r=y[t].apply(u(this),e),g(this,[t])):r=y[t].apply(this,e),r}:null}const m=new WeakMap;function _(n){return n=u(n),m.has(n)||m.set(n,new Set),m.get(n)}function v(n,t){_(n).add(t)}function C(n,t){const e=s=>{const i=k.has(t);k.add(t),i||H(()=>t(s)),v(n,e)},r=()=>{_(n).delete(e)};return v(n,e),r}function g(n,t){const e=_(n),r=[...e];e.clear(),r.forEach(s=>s(t))}const S="__easy_model_event__";function H(n){const t=new EventTarget,e=new Event(S);t.addEventListener(S,()=>{t.removeEventListener(S,n),n()}),t.dispatchEvent(e)}function w(){return d?null:d=Symbol()}function E(n){d===n&&(d=null,k.clear())}let p=null;const R=Symbol("filterSymbol");function M(n){return new Proxy(n,new b(n))}function F(n,t,e){const r=e?n(...e):Object.create(n.prototype);return l(Object.assign(r,t))}const O=class{constructor(t){h(this,"fakeCtor");h(this,"caches",new Map);this.Ctor=t;const e=function(){};e.prototype=new Proxy({constructor:t,__proto__:t.prototype},O.prototypeHandlers),this.fakeCtor=e}apply(t,e,r){const s=p;p=this.fakeCtor;const i=this.getInstance(r)||this.register(r,Reflect.construct(this.Ctor,this.filterArgs(r),this.fakeCtor));return p=s,i}construct(t,e,r){const s=Reflect.construct(t,this.filterArgs(e),r===this.Ctor?this.fakeCtor:r);return p!==r?l(s):s}set(t,e,r,s){if(r&&(r=u(r)),r===u(Reflect.get(t,e)))return!0;const i=w(),a=Reflect.set(t,e,r,s);return this.caches.forEach(P=>{const T=P.deref();T&&g(T,[])}),E(i),a}getInstance(t){for(const e of this.caches.keys()){if(e.length!==t.length||!e.every((s,i)=>s===t[i]?!0:typeof s=="object"&&typeof t[i]=="object"?this.isShallowEqualObj(s,t[i]):!1))continue;const r=this.caches.get(e);return r.deref()?l(r.deref()):(this.caches.delete(e),null)}return null}register(t,e){return this.caches.set(t,new WeakRef(e)),l(e)}isShallowEqualObj(t,e){const r=Object.keys(t),s=e.keys(e);return r.length===s.length&&r.every(i=>t[i]===e[i])}filterArgs(t){return t.filter(e=>e!==R)}};let b=O;h(b,"prototypeHandlers",{get(t,e,r){const s=j(t,e);if(s)return s;const i=Reflect.get(t,e,r);return typeof e=="symbol"||["constructor","__proto__"].includes(e)||typeof i!="function"?i:function(...a){return i.apply(l(this),a)}}}),o.extend=F,o.filterSymbol=R,o.provide=M,o.subscribe=C,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
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 (
|