@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 CHANGED
@@ -1,95 +1,95 @@
1
- var S = Object.defineProperty;
2
- var j = (n, t, e) => t in n ? S(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
3
- var c = (n, t, e) => (j(n, typeof t != "symbol" ? t + "" : t, e), e);
4
- const g = /* @__PURE__ */ new WeakMap(), O = Symbol("origin");
5
- function o(n) {
6
- if (n = f(n), g.has(n))
7
- return g.get(n);
8
- const t = new Proxy(n, new E(n));
9
- return g.set(n, t), t;
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 f(n) {
12
- return n && (n[O] || n);
11
+ function o(n) {
12
+ return n && (n[S] || n);
13
13
  }
14
- class E {
14
+ class H {
15
15
  constructor(t) {
16
- c(this, "triggers", {});
17
- c(this, "cachedFns", {});
16
+ f(this, "triggers", {});
17
+ f(this, "cachedFns", {});
18
18
  this.obj = t;
19
19
  }
20
20
  get(t, e, r) {
21
- if (e === O)
21
+ if (e === S)
22
22
  return this.obj;
23
- let s = m(t, e) || Reflect.get(t, e, r);
24
- return typeof s == "function" ? this.cachedFns[e] || (this.cachedFns[e] = s.bind(o(t))) : (typeof s == "object" && (s = o(s), k(s, this.getTrigger(e))), s);
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 = f(r)), r === f(Reflect.get(t, e)))
27
+ if (r && (r = o(r)), r === o(Reflect.get(t, e)))
28
28
  return !0;
29
- const i = A(), y = Reflect.set(t, e, r, s);
30
- return _(this.obj, [e]), C(i), y;
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) => _(this.obj, [t, ...e])), this.triggers[t];
33
+ return t in this.triggers || (this.triggers[t] = (e) => g(this.obj, [t, ...e])), this.triggers[t];
34
34
  }
35
35
  }
36
- const b = /* @__PURE__ */ new Set();
37
- let a = null;
38
- const u = ["push", "pop", "shift", "unshift", "splice", "sort"];
39
- function m(n, t) {
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 f(this).hasOwnProperty(e);
42
- } : Array.isArray(n) && typeof u[t] == "function" ? function(...e) {
41
+ return o(this).hasOwnProperty(e);
42
+ } : Array.isArray(n) && typeof l[t] == "function" ? function(...e) {
43
43
  let r;
44
- return u.includes(t) ? (r = u[t].apply(f(this), e), _(this, [t])) : r = u[t].apply(this, e), r;
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 d = /* @__PURE__ */ new WeakMap();
48
- function w(n) {
49
- return n = f(n), d.has(n) || d.set(n, /* @__PURE__ */ new Set()), d.get(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 k(n, t) {
52
- w(n).add(t);
51
+ function _(n, t) {
52
+ v(n).add(t);
53
53
  }
54
- function M(n, t) {
54
+ function F(n, t) {
55
55
  const e = (s) => {
56
- const i = b.has(t);
57
- b.add(t), i || R(() => t(s)), k(n, e);
56
+ const i = k.has(t);
57
+ k.add(t), i || T(() => t(s)), _(n, e);
58
58
  }, r = () => {
59
- w(n).delete(e);
59
+ v(n).delete(e);
60
60
  };
61
- return k(n, e), r;
61
+ return _(n, e), r;
62
62
  }
63
- function _(n, t) {
64
- const e = w(n), r = [...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 p = "__easy_model_event__";
68
- function R(n) {
69
- const t = new EventTarget(), e = new Event(p);
70
- t.addEventListener(p, () => {
71
- t.removeEventListener(p, n), n();
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 A() {
75
- return a ? null : a = Symbol();
74
+ function E() {
75
+ return y ? null : y = Symbol();
76
76
  }
77
- function C(n) {
78
- a === n && (a = null, b.clear());
77
+ function R(n) {
78
+ y === n && (y = null, k.clear());
79
79
  }
80
- let l = null;
81
- const H = Symbol("filterSymbol");
82
- function x(n) {
83
- return new Proxy(n, new h(n));
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 F(n, t, e) {
85
+ function q(n, t, e) {
86
86
  const r = e ? n(...e) : Object.create(n.prototype);
87
- return o(Object.assign(r, t));
87
+ return c(Object.assign(r, t));
88
88
  }
89
- const v = class {
89
+ const O = class {
90
90
  constructor(t) {
91
- c(this, "fakeCtor");
92
- c(this, "caches", /* @__PURE__ */ new Map());
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
- v.prototypeHandlers
101
+ O.prototypeHandlers
102
102
  ), this.fakeCtor = e;
103
103
  }
104
104
  apply(t, e, r) {
105
- const s = l;
106
- l = this.fakeCtor;
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 l = s, i;
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 l !== r ? o(s) : s;
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() ? o(r.deref()) : (this.caches.delete(e), null);
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)), o(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 !== H);
149
+ return t.filter((e) => e !== M);
141
150
  }
142
151
  };
143
- let h = v;
144
- c(h, "prototypeHandlers", {
152
+ let a = O;
153
+ f(a, "prototypeHandlers", {
145
154
  get(t, e, r) {
146
- const s = m(t, e);
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(...y) {
151
- return i.apply(o(this), y);
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
- F as extend,
157
- H as filterSymbol,
158
- x as provide,
159
- M as subscribe
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 F=Object.defineProperty;var P=(o,c,f)=>c in o?F(o,c,{enumerable:!0,configurable:!0,writable:!0,value:f}):o[c]=f;var h=(o,c,f)=>(P(o,typeof c!="symbol"?c+"":c,f),f);const c=new WeakMap,f=Symbol("origin");function u(n){if(n=l(n),c.has(n))return c.get(n);const e=new Proxy(n,new T(n));return c.set(n,e),e}function l(n){return n&&(n[f]||n)}class T{constructor(e){h(this,"triggers",{});h(this,"cachedFns",{});this.obj=e}get(e,t,r){if(t===f)return this.obj;let s=O(e,t)||Reflect.get(e,t,r);return typeof s=="function"?this.cachedFns[t]||(this.cachedFns[t]=s.bind(u(e))):(typeof s=="object"&&(s=u(s),m(s,this.getTrigger(t))),s)}set(e,t,r,s){if(r&&(r=l(r)),r===l(Reflect.get(e,t)))return!0;const i=A(),S=Reflect.set(e,t,r,s);return _(this.obj,[t]),C(i),S}getTrigger(e){return e in this.triggers||(this.triggers[e]=t=>_(this.obj,[e,...t])),this.triggers[e]}}const p=new Set;let a=null;const d=["push","pop","shift","unshift","splice","sort"];function O(n,e){return e==="hasOwnProperty"?function(t){return l(this).hasOwnProperty(t)}:Array.isArray(n)&&typeof d[e]=="function"?function(...t){let r;return d.includes(e)?(r=d[e].apply(l(this),t),_(this,[e])):r=d[e].apply(this,t),r}:null}const b=new WeakMap;function k(n){return n=l(n),b.has(n)||b.set(n,new Set),b.get(n)}function m(n,e){k(n).add(e)}function E(n,e){const t=s=>{const i=p.has(e);p.add(e),i||R(()=>e(s)),m(n,t)},r=()=>{k(n).delete(t)};return m(n,t),r}function _(n,e){const t=k(n),r=[...t];t.clear(),r.forEach(s=>s(e))}const v="__easy_model_event__";function R(n){const e=new EventTarget,t=new Event(v);e.addEventListener(v,()=>{e.removeEventListener(v,n),n()}),e.dispatchEvent(t)}function A(){return a?null:a=Symbol()}function C(n){a===n&&(a=null,p.clear())}let y=null;const j=Symbol("filterSymbol");function H(n){return new Proxy(n,new g(n))}function M(n,e,t){const r=t?n(...t):Object.create(n.prototype);return u(Object.assign(r,e))}const w=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},w.prototypeHandlers),this.fakeCtor=t}apply(e,t,r){const s=y;y=this.fakeCtor;const i=this.getInstance(r)||this.register(r,Reflect.construct(this.Ctor,this.filterArgs(r),this.fakeCtor));return y=s,i}construct(e,t,r){const s=Reflect.construct(e,this.filterArgs(t),r===this.Ctor?this.fakeCtor:r);return y!==r?u(s):s}getInstance(e){for(const t of this.caches.keys()){if(t.length!==e.length||!t.every((s,i)=>s===e[i]?!0:typeof s=="object"&&typeof e[i]=="object"?this.isShallowEqualObj(s,e[i]):!1))continue;const r=this.caches.get(t);return r.deref()?u(r.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 r=Object.keys(e),s=t.keys(t);return r.length===s.length&&r.every(i=>e[i]===t[i])}filterArgs(e){return e.filter(t=>t!==j)}};let g=w;h(g,"prototypeHandlers",{get(e,t,r){const s=O(e,t);if(s)return s;const i=Reflect.get(e,t,r);return typeof t=="symbol"||["constructor","__proto__"].includes(t)||typeof i!="function"?i:function(...S){return i.apply(u(this),S)}}}),o.extend=M,o.filterSymbol=j,o.provide=H,o.subscribe=E,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e7w/easy-model",
3
- "version": "0.0.9",
3
+ "version": "0.0.10",
4
4
  "description": "一个简单的状态管理包, A simple state management lib for Typescript",
5
5
  "main": "./src/index.ts",
6
6
  "module": "./dist/index.es.js",
package/src/provide.ts CHANGED
@@ -1,5 +1,12 @@
1
- import { type Ctor, type Obj, safeGet } from "./helpers";
2
- import { observe } from "./observe";
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 (