@e7w/easy-model 0.0.1 → 0.0.2
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/README.md +3 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.es.js +69 -59
- package/dist/index.umd.js +1 -1
- package/dist/provide.d.ts +2 -0
- package/package.json +2 -2
- package/src/index.ts +1 -1
- package/src/observe.ts +7 -1
- package/src/provide.ts +38 -6
package/README.md
CHANGED
|
@@ -21,6 +21,8 @@ const A = provide(
|
|
|
21
21
|
);
|
|
22
22
|
|
|
23
23
|
A(1) === A(1); // true
|
|
24
|
+
A(1) !== new A(1); // true
|
|
24
25
|
subscribe(A(1), console.log.bind(console, "Path of changed value:"));
|
|
25
|
-
A(1)
|
|
26
|
+
const { random } = A(1);
|
|
27
|
+
random(); // Path of changed value: ["msg"]
|
|
26
28
|
```
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { subscribe } from "./helpers";
|
|
2
|
-
export { provide } from "./provide";
|
|
2
|
+
export { provide, extend } from "./provide";
|
package/dist/index.es.js
CHANGED
|
@@ -1,70 +1,76 @@
|
|
|
1
|
-
var
|
|
2
|
-
var _ = (n, t, e) => t in n ?
|
|
3
|
-
var
|
|
4
|
-
const h = /* @__PURE__ */ new WeakMap(),
|
|
5
|
-
function
|
|
1
|
+
var j = Object.defineProperty;
|
|
2
|
+
var _ = (n, t, e) => t in n ? j(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
|
|
3
|
+
var i = (n, t, e) => (_(n, typeof t != "symbol" ? t + "" : t, e), e);
|
|
4
|
+
const h = /* @__PURE__ */ new WeakMap(), k = Symbol("origin");
|
|
5
|
+
function o(n) {
|
|
6
6
|
if (n = f(n), h.has(n))
|
|
7
7
|
return h.get(n);
|
|
8
|
-
const t = new Proxy(n, new
|
|
8
|
+
const t = new Proxy(n, new C(n));
|
|
9
9
|
return h.set(n, t), t;
|
|
10
10
|
}
|
|
11
11
|
function f(n) {
|
|
12
|
-
return n[
|
|
12
|
+
return n[k] || n;
|
|
13
13
|
}
|
|
14
|
-
class
|
|
14
|
+
class C {
|
|
15
15
|
constructor(t) {
|
|
16
|
-
|
|
16
|
+
i(this, "triggers", {});
|
|
17
|
+
i(this, "cachedFns", {});
|
|
17
18
|
this.obj = t;
|
|
18
19
|
}
|
|
19
|
-
get(t, e,
|
|
20
|
-
if (e ===
|
|
20
|
+
get(t, e, s) {
|
|
21
|
+
if (e === k)
|
|
21
22
|
return this.obj;
|
|
22
|
-
let
|
|
23
|
-
return typeof
|
|
23
|
+
let r = w(t, e) || Reflect.get(t, e, s);
|
|
24
|
+
return typeof r == "function" ? this.cachedFns[e] || (this.cachedFns[e] = r.bind(o(t))) : (typeof r == "object" && (r = o(r), y(r, this.getTrigger(e))), r);
|
|
24
25
|
}
|
|
25
|
-
set(t, e,
|
|
26
|
-
const
|
|
27
|
-
return
|
|
26
|
+
set(t, e, s, r) {
|
|
27
|
+
const c = Reflect.set(t, e, s, r);
|
|
28
|
+
return d(this.obj, [e]), c;
|
|
28
29
|
}
|
|
29
30
|
getTrigger(t) {
|
|
30
|
-
return t in this.triggers || (this.triggers[t] = (e) =>
|
|
31
|
+
return t in this.triggers || (this.triggers[t] = (e) => d(this.obj, [t, ...e])), this.triggers[t];
|
|
31
32
|
}
|
|
32
33
|
}
|
|
33
34
|
const l = ["push", "pop", "shift", "unshift", "splice"];
|
|
34
|
-
function
|
|
35
|
+
function w(n, t) {
|
|
35
36
|
return t === "hasOwnProperty" ? function(e) {
|
|
36
37
|
return f(this).hasOwnProperty(e);
|
|
37
38
|
} : Array.isArray(n) && typeof l[t] == "function" ? function(...e) {
|
|
38
|
-
const
|
|
39
|
-
return l.includes(t) &&
|
|
39
|
+
const s = l[t].apply(f(this), e);
|
|
40
|
+
return l.includes(t) && d(this, [t]), s;
|
|
40
41
|
} : null;
|
|
41
42
|
}
|
|
42
43
|
const a = /* @__PURE__ */ new WeakMap();
|
|
43
|
-
function
|
|
44
|
+
function b(n) {
|
|
44
45
|
return n = f(n), a.has(n) || a.set(n, /* @__PURE__ */ new Set()), a.get(n);
|
|
45
46
|
}
|
|
46
|
-
function
|
|
47
|
-
|
|
47
|
+
function y(n, t) {
|
|
48
|
+
b(n).add(t);
|
|
48
49
|
}
|
|
49
50
|
function R(n, t) {
|
|
50
|
-
const e = (
|
|
51
|
-
t(
|
|
52
|
-
},
|
|
53
|
-
|
|
51
|
+
const e = (r) => {
|
|
52
|
+
t(r), y(n, e);
|
|
53
|
+
}, s = () => {
|
|
54
|
+
b(n).delete(e);
|
|
54
55
|
};
|
|
55
|
-
return
|
|
56
|
+
return y(n, e), s;
|
|
56
57
|
}
|
|
57
|
-
function
|
|
58
|
-
const e =
|
|
59
|
-
return e.clear(),
|
|
58
|
+
function d(n, t) {
|
|
59
|
+
const e = b(n), s = [...e];
|
|
60
|
+
return e.clear(), s.forEach((r) => r(t));
|
|
60
61
|
}
|
|
61
|
-
|
|
62
|
+
let p = null;
|
|
63
|
+
function M(n) {
|
|
62
64
|
return new Proxy(n, new u(n));
|
|
63
65
|
}
|
|
64
|
-
|
|
66
|
+
function S(n, t, e) {
|
|
67
|
+
const s = e ? n(...e) : Object.create(n.prototype);
|
|
68
|
+
return o(Object.assign(s, t));
|
|
69
|
+
}
|
|
70
|
+
const g = class {
|
|
65
71
|
constructor(t) {
|
|
66
|
-
|
|
67
|
-
|
|
72
|
+
i(this, "fakeCtor");
|
|
73
|
+
i(this, "caches", /* @__PURE__ */ new Map());
|
|
68
74
|
this.Ctor = t;
|
|
69
75
|
const e = function() {
|
|
70
76
|
};
|
|
@@ -73,54 +79,58 @@ const b = class {
|
|
|
73
79
|
constructor: t,
|
|
74
80
|
__proto__: t.prototype
|
|
75
81
|
},
|
|
76
|
-
|
|
82
|
+
g.prototypeHandlers
|
|
77
83
|
), this.fakeCtor = e;
|
|
78
84
|
}
|
|
79
|
-
apply(t, e,
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
85
|
+
apply(t, e, s) {
|
|
86
|
+
p = this.fakeCtor;
|
|
87
|
+
const r = this.getInstance(s) || this.register(
|
|
88
|
+
s,
|
|
89
|
+
Reflect.construct(this.Ctor, s, this.fakeCtor)
|
|
83
90
|
);
|
|
91
|
+
return p = null, r;
|
|
84
92
|
}
|
|
85
|
-
construct(t, e,
|
|
86
|
-
|
|
93
|
+
construct(t, e, s) {
|
|
94
|
+
const r = Reflect.construct(
|
|
87
95
|
t,
|
|
88
96
|
e,
|
|
89
|
-
|
|
97
|
+
s === this.Ctor ? this.fakeCtor : s
|
|
90
98
|
);
|
|
99
|
+
return p !== s ? o(r) : r;
|
|
91
100
|
}
|
|
92
101
|
getInstance(t) {
|
|
93
102
|
for (const e of this.caches.keys()) {
|
|
94
103
|
if (!e.every(
|
|
95
|
-
(
|
|
104
|
+
(r, c) => r === t[c] ? !0 : typeof r == "object" && typeof t[c] == "object" ? this.isShallowEqualObj(r, t[c]) : !1
|
|
96
105
|
))
|
|
97
106
|
continue;
|
|
98
|
-
const
|
|
99
|
-
return
|
|
107
|
+
const s = this.caches.get(e);
|
|
108
|
+
return s.deref() ? o(s.deref()) : (this.caches.delete(e), null);
|
|
100
109
|
}
|
|
101
110
|
return null;
|
|
102
111
|
}
|
|
103
112
|
register(t, e) {
|
|
104
|
-
return this.caches.set(t, new WeakRef(e)),
|
|
113
|
+
return this.caches.set(t, new WeakRef(e)), o(e);
|
|
105
114
|
}
|
|
106
115
|
isShallowEqualObj(t, e) {
|
|
107
|
-
const
|
|
108
|
-
return
|
|
116
|
+
const s = Object.keys(t), r = e.keys(e);
|
|
117
|
+
return s.length === r.length && s.every((c) => t[c] === e[c]);
|
|
109
118
|
}
|
|
110
119
|
};
|
|
111
|
-
let u =
|
|
112
|
-
|
|
113
|
-
get(t, e,
|
|
114
|
-
const
|
|
115
|
-
if (
|
|
116
|
-
return
|
|
117
|
-
const
|
|
118
|
-
return typeof e == "symbol" || ["constructor", "__proto__"].includes(e) || typeof
|
|
119
|
-
return
|
|
120
|
+
let u = g;
|
|
121
|
+
i(u, "prototypeHandlers", {
|
|
122
|
+
get(t, e, s) {
|
|
123
|
+
const r = w(t, e);
|
|
124
|
+
if (r)
|
|
125
|
+
return r;
|
|
126
|
+
const c = Reflect.get(t, e, s);
|
|
127
|
+
return typeof e == "symbol" || ["constructor", "__proto__"].includes(e) || typeof c != "function" ? c : function(...O) {
|
|
128
|
+
return c.apply(o(this), O);
|
|
120
129
|
};
|
|
121
130
|
}
|
|
122
131
|
});
|
|
123
132
|
export {
|
|
124
|
-
|
|
133
|
+
S as extend,
|
|
134
|
+
M as provide,
|
|
125
135
|
R as subscribe
|
|
126
136
|
};
|
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
|
|
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 M=Object.defineProperty;var R=(i,o,u)=>o in i?M(i,o,{enumerable:!0,configurable:!0,writable:!0,value:u}):i[o]=u;var h=(i,o,u)=>(R(i,typeof o!="symbol"?o+"":o,u),u);const o=new WeakMap,u=Symbol("origin");function f(n){if(n=l(n),o.has(n))return o.get(n);const e=new Proxy(n,new j(n));return o.set(n,e),e}function l(n){return n[u]||n}class j{constructor(e){h(this,"triggers",{});h(this,"cachedFns",{});this.obj=e}get(e,t,s){if(t===u)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(f(e))):(typeof r=="object"&&(r=f(r),b(r,this.getTrigger(t))),r)}set(e,t,s,r){const c=Reflect.set(e,t,s,r);return g(this.obj,[t]),c}getTrigger(e){return e in this.triggers||(this.triggers[e]=t=>g(this.obj,[e,...t])),this.triggers[e]}}const d=["push","pop","shift","unshift","splice"];function O(n,e){return e==="hasOwnProperty"?function(t){return l(this).hasOwnProperty(t)}:Array.isArray(n)&&typeof d[e]=="function"?function(...t){const s=d[e].apply(l(this),t);return d.includes(e)&&g(this,[e]),s}:null}const p=new WeakMap;function y(n){return n=l(n),p.has(n)||p.set(n,new Set),p.get(n)}function b(n,e){y(n).add(e)}function _(n,e){const t=r=>{e(r),b(n,t)},s=()=>{y(n).delete(t)};return b(n,t),s}function g(n,e){const t=y(n),s=[...t];return t.clear(),s.forEach(r=>r(e))}let k=null;function m(n){return new Proxy(n,new a(n))}function C(n,e,t){const s=t?n(...t):Object.create(n.prototype);return f(Object.assign(s,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,s){k=this.fakeCtor;const r=this.getInstance(s)||this.register(s,Reflect.construct(this.Ctor,s,this.fakeCtor));return k=null,r}construct(e,t,s){const r=Reflect.construct(e,t,s===this.Ctor?this.fakeCtor:s);return k!==s?f(r):r}getInstance(e){for(const t of this.caches.keys()){if(!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()?f(s.deref()):(this.caches.delete(t),null)}return null}register(e,t){return this.caches.set(e,new WeakRef(t)),f(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 a=w;h(a,"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(...H){return c.apply(f(this),H)}}}),i.extend=C,i.provide=m,i.subscribe=_,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});
|
package/dist/provide.d.ts
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
1
|
import { type Ctor } from "./helpers";
|
|
2
2
|
export declare function provide<T extends Ctor>(Ctor: T): T & ((...args: ConstructorParameters<T>) => InstanceType<T>);
|
|
3
|
+
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>>;
|
|
4
|
+
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/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e7w/easy-model",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "一个简单的状态管理包, A simple state management lib for Typescript",
|
|
5
|
-
"main": "./
|
|
5
|
+
"main": "./src/index.ts",
|
|
6
6
|
"module": "./dist/index.es.js",
|
|
7
7
|
"types": "./src/index.d.ts",
|
|
8
8
|
"typings": "./dist/index.d.ts",
|
package/src/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { subscribe } from "./helpers";
|
|
2
|
-
export { provide } from "./provide";
|
|
2
|
+
export { provide, extend } from "./provide";
|
package/src/observe.ts
CHANGED
|
@@ -20,12 +20,18 @@ class ObserveHandler<T extends Obj> implements ProxyHandler<T> {
|
|
|
20
20
|
(path: (string | symbol)[]) => void
|
|
21
21
|
> = {};
|
|
22
22
|
|
|
23
|
+
private cachedFns: { [K in keyof T]?: T[K] } = {};
|
|
24
|
+
|
|
23
25
|
public constructor(private obj: T) {}
|
|
24
26
|
|
|
25
27
|
public get(target: T, p: string | symbol, receiver: any): any {
|
|
26
28
|
if (p === origin) return this.obj;
|
|
27
29
|
let ret = safeGet(target, p) || Reflect.get(target, p, receiver);
|
|
28
|
-
if (typeof ret === "function")
|
|
30
|
+
if (typeof ret === "function")
|
|
31
|
+
return (
|
|
32
|
+
this.cachedFns[p] ||
|
|
33
|
+
(this.cachedFns[p as keyof T] = ret.bind(observe(target)))
|
|
34
|
+
);
|
|
29
35
|
if (typeof ret === "object") {
|
|
30
36
|
ret = observe(ret);
|
|
31
37
|
track(ret, this.getTrigger(p));
|
package/src/provide.ts
CHANGED
|
@@ -1,12 +1,40 @@
|
|
|
1
1
|
import { type Ctor, type Obj, safeGet } from "./helpers";
|
|
2
2
|
import { observe } from "./observe";
|
|
3
3
|
|
|
4
|
+
let creatingTarget: any = null;
|
|
5
|
+
|
|
4
6
|
export function provide<T extends Ctor>(
|
|
5
7
|
Ctor: T
|
|
6
8
|
): T & ((...args: ConstructorParameters<T>) => InstanceType<T>) {
|
|
7
9
|
return new Proxy(Ctor, new Provider(Ctor)) as any;
|
|
8
10
|
}
|
|
9
11
|
|
|
12
|
+
// type RequireAtLeastOne<T> = {
|
|
13
|
+
// [K in keyof T]-?: undefined extends T[K]
|
|
14
|
+
// ? never
|
|
15
|
+
// : T[K] extends Function
|
|
16
|
+
// ? never
|
|
17
|
+
// : Required<Pick<T, K>> & Partial<Pick<T, Exclude<keyof T, K>>>;
|
|
18
|
+
// }[keyof T];
|
|
19
|
+
// todo: 想办法过滤出所有field
|
|
20
|
+
export function extend<
|
|
21
|
+
T extends ReturnType<typeof provide>,
|
|
22
|
+
P extends Partial<InstanceType<T>>
|
|
23
|
+
>(Provider: T, extendData: P): P & Partial<Omit<InstanceType<T>, keyof P>>;
|
|
24
|
+
export function extend<
|
|
25
|
+
T extends ReturnType<typeof provide>,
|
|
26
|
+
P extends Partial<InstanceType<T>>,
|
|
27
|
+
M extends Parameters<T>
|
|
28
|
+
>(Provider: T, extendData: P, args: M): InstanceType<T>;
|
|
29
|
+
export function extend<
|
|
30
|
+
T extends ReturnType<typeof provide>,
|
|
31
|
+
P extends Partial<InstanceType<T>>,
|
|
32
|
+
M extends Parameters<T> | null | undefined
|
|
33
|
+
>(Provider: T, extendData: P, args?: M) {
|
|
34
|
+
const instance = args ? Provider(...args) : Object.create(Provider.prototype);
|
|
35
|
+
return observe(Object.assign(instance, extendData));
|
|
36
|
+
}
|
|
37
|
+
|
|
10
38
|
class Provider<T extends Ctor> implements ProxyHandler<T> {
|
|
11
39
|
private static prototypeHandlers: ProxyHandler<Obj> = {
|
|
12
40
|
get(target, p, receiver) {
|
|
@@ -50,13 +78,15 @@ class Provider<T extends Ctor> implements ProxyHandler<T> {
|
|
|
50
78
|
argArray: ConstructorParameters<T>
|
|
51
79
|
): InstanceType<T> {
|
|
52
80
|
// todo: 循环引用创建时抛出错误
|
|
53
|
-
|
|
81
|
+
creatingTarget = this.fakeCtor;
|
|
82
|
+
const instance =
|
|
54
83
|
this.getInstance(argArray) ||
|
|
55
84
|
this.register(
|
|
56
85
|
argArray,
|
|
57
86
|
Reflect.construct(this.Ctor, argArray, this.fakeCtor)
|
|
58
|
-
)
|
|
59
|
-
|
|
87
|
+
);
|
|
88
|
+
creatingTarget = null;
|
|
89
|
+
return instance;
|
|
60
90
|
}
|
|
61
91
|
|
|
62
92
|
public construct(
|
|
@@ -64,14 +94,16 @@ class Provider<T extends Ctor> implements ProxyHandler<T> {
|
|
|
64
94
|
argArray: any[],
|
|
65
95
|
newTarget: any
|
|
66
96
|
): InstanceType<T> {
|
|
67
|
-
|
|
97
|
+
const instance = Reflect.construct(
|
|
68
98
|
target,
|
|
69
99
|
argArray,
|
|
70
100
|
newTarget === this.Ctor ? this.fakeCtor : newTarget
|
|
71
101
|
);
|
|
102
|
+
if (creatingTarget !== newTarget) return observe(instance);
|
|
103
|
+
return instance;
|
|
72
104
|
}
|
|
73
105
|
|
|
74
|
-
|
|
106
|
+
private getInstance(args: ConstructorParameters<T>): InstanceType<T> | null {
|
|
75
107
|
for (const cachedArgs of this.caches.keys()) {
|
|
76
108
|
if (
|
|
77
109
|
!cachedArgs.every((arg, index) =>
|
|
@@ -92,7 +124,7 @@ class Provider<T extends Ctor> implements ProxyHandler<T> {
|
|
|
92
124
|
return null;
|
|
93
125
|
}
|
|
94
126
|
|
|
95
|
-
|
|
127
|
+
private register(
|
|
96
128
|
args: ConstructorParameters<T>,
|
|
97
129
|
instance: InstanceType<T>
|
|
98
130
|
): InstanceType<T> {
|