phecda-web 1.0.1-beta.14 → 2.0.0
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 +2 -0
- package/dist/index.d.ts +28 -14
- package/dist/index.js +202 -51
- package/dist/index.mjs +195 -46
- package/package.json +11 -6
package/README.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,35 +1,49 @@
|
|
|
1
1
|
import { Events, Construct } from 'phecda-core';
|
|
2
2
|
export * from 'phecda-core';
|
|
3
3
|
|
|
4
|
-
interface ActiveInstance {
|
|
5
|
-
state: Record<string | symbol, any>;
|
|
6
|
-
origin: WeakMap<any, any>;
|
|
7
|
-
cache: WeakMap<any, any>;
|
|
8
|
-
[key: string]: any;
|
|
9
|
-
}
|
|
10
4
|
interface PhecdaEmitter {
|
|
11
5
|
on<N extends keyof Events>(eventName: N, cb: (args: Events[N]) => void): void;
|
|
12
6
|
off<N extends keyof Events>(eventName: N, cb?: (args: Events[N]) => void): void;
|
|
13
7
|
emit<N extends keyof Events>(eventName: N, param: Events[N]): void;
|
|
14
8
|
}
|
|
9
|
+
type DeepPartial<T> = {
|
|
10
|
+
[K in keyof T]?: DeepPartial<T[K]>;
|
|
11
|
+
};
|
|
15
12
|
|
|
16
13
|
declare const emitter: PhecdaEmitter;
|
|
17
14
|
declare function defaultWebInject(): void;
|
|
18
15
|
|
|
19
|
-
declare function
|
|
20
|
-
declare function
|
|
21
|
-
declare
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
16
|
+
declare function wait(...instances: InstanceType<Construct>[]): Promise<any[]>;
|
|
17
|
+
declare function bindMethod(instance: any): any;
|
|
18
|
+
declare class WebPhecda {
|
|
19
|
+
protected proxyFn: Function;
|
|
20
|
+
origin: Record<string, any>;
|
|
21
|
+
state: Record<string | symbol, any>;
|
|
22
|
+
modelMap: WeakMap<object, any>;
|
|
23
|
+
constructor(proxyFn: Function);
|
|
24
|
+
init<Model extends Construct>(model: Model): InstanceType<Model>;
|
|
25
|
+
patch<Model extends Construct>(model: Model, data: DeepPartial<InstanceType<Model>>): void;
|
|
26
|
+
wait(...modelOrTag: (Construct | PropertyKey)[]): Promise<any[]>;
|
|
27
|
+
get<Model extends Construct>(model: Model): InstanceType<Model>;
|
|
28
|
+
has<Model extends Construct>(model: Model): boolean;
|
|
29
|
+
reset<Model extends Construct>(model: Model): InstanceType<Model> | undefined;
|
|
30
|
+
unmount(modelOrTag: Construct | PropertyKey): Promise<void>;
|
|
31
|
+
unmountAll(): Promise<void[]>;
|
|
32
|
+
ismount(modelOrTag: Construct | PropertyKey): boolean;
|
|
33
|
+
serialize(): string;
|
|
34
|
+
load(str: string): void;
|
|
35
|
+
}
|
|
25
36
|
|
|
26
|
-
declare class
|
|
37
|
+
declare class Base {
|
|
27
38
|
constructor();
|
|
28
39
|
get tag(): PropertyKey;
|
|
29
40
|
then(cb: () => void, reject?: (e: any) => void): any;
|
|
30
41
|
on<Key extends keyof Events>(type: Key, handler: (arg: Events[Key]) => void): void;
|
|
31
42
|
emit<Key extends keyof Events>(type: Key, param: Events[Key]): void;
|
|
32
43
|
off<Key extends keyof Events>(type: Key, handler?: (arg: Events[Key]) => void): void;
|
|
44
|
+
private readonly __UNMOUNT_SYMBOL__;
|
|
45
|
+
private onUnmount;
|
|
46
|
+
private _unmount;
|
|
33
47
|
}
|
|
34
48
|
|
|
35
|
-
export {
|
|
49
|
+
export { Base, DeepPartial, PhecdaEmitter, WebPhecda, bindMethod, defaultWebInject, emitter, wait };
|
package/dist/index.js
CHANGED
|
@@ -20,6 +20,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
20
20
|
};
|
|
21
21
|
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
22
22
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
23
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
24
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
25
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
26
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
23
27
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
24
28
|
mod
|
|
25
29
|
));
|
|
@@ -28,15 +32,12 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
28
32
|
// src/index.ts
|
|
29
33
|
var src_exports = {};
|
|
30
34
|
__export(src_exports, {
|
|
31
|
-
|
|
35
|
+
Base: () => Base,
|
|
36
|
+
WebPhecda: () => WebPhecda,
|
|
37
|
+
bindMethod: () => bindMethod,
|
|
32
38
|
defaultWebInject: () => defaultWebInject,
|
|
33
39
|
emitter: () => emitter,
|
|
34
|
-
|
|
35
|
-
isModuleLoad: () => isModuleLoad,
|
|
36
|
-
resetActiveInstance: () => resetActiveInstance,
|
|
37
|
-
serializeState: () => serializeState,
|
|
38
|
-
unmountModule: () => unmountModule,
|
|
39
|
-
waitUntilInit: () => waitUntilInit
|
|
40
|
+
wait: () => wait
|
|
40
41
|
});
|
|
41
42
|
module.exports = __toCommonJS(src_exports);
|
|
42
43
|
__reExport(src_exports, require("phecda-core"), module.exports);
|
|
@@ -46,6 +47,8 @@ var import_phecda_core = require("phecda-core");
|
|
|
46
47
|
var import_mitt = __toESM(require("mitt"));
|
|
47
48
|
var emitter = (0, import_mitt.default)();
|
|
48
49
|
function defaultWebInject() {
|
|
50
|
+
if (typeof window === "undefined")
|
|
51
|
+
return;
|
|
49
52
|
if (!(0, import_phecda_core.getInject)("watcher")) {
|
|
50
53
|
(0, import_phecda_core.setInject)("watcher", ({ eventName, instance, key, options }) => {
|
|
51
54
|
const fn = typeof instance[key] === "function" ? instance[key].bind(instance) : (v) => instance[key] = v;
|
|
@@ -87,50 +90,186 @@ __name(defaultWebInject, "defaultWebInject");
|
|
|
87
90
|
|
|
88
91
|
// src/core.ts
|
|
89
92
|
var import_phecda_core2 = require("phecda-core");
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
function resetActiveInstance(instance) {
|
|
96
|
-
activeInstance = instance || {
|
|
97
|
-
state: {},
|
|
98
|
-
origin: /* @__PURE__ */ new WeakMap(),
|
|
99
|
-
cache: /* @__PURE__ */ new WeakMap()
|
|
100
|
-
};
|
|
93
|
+
var import_reflect_metadata = require("reflect-metadata");
|
|
94
|
+
|
|
95
|
+
// src/utils.ts
|
|
96
|
+
function isObject(o) {
|
|
97
|
+
return Object.prototype.toString.call(o) === "[object Object]";
|
|
101
98
|
}
|
|
102
|
-
__name(
|
|
103
|
-
function
|
|
104
|
-
|
|
99
|
+
__name(isObject, "isObject");
|
|
100
|
+
function deepMerge(target, patchToApply) {
|
|
101
|
+
for (const key in patchToApply) {
|
|
102
|
+
if (!patchToApply.hasOwnProperty(key))
|
|
103
|
+
continue;
|
|
104
|
+
const subPatch = patchToApply[key];
|
|
105
|
+
const targetValue = target[key];
|
|
106
|
+
if (isObject(targetValue) && isObject(subPatch) && target.hasOwnProperty(key)) {
|
|
107
|
+
target[key] = deepMerge(targetValue, subPatch);
|
|
108
|
+
} else {
|
|
109
|
+
target[key] = subPatch;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return target;
|
|
105
113
|
}
|
|
106
|
-
__name(
|
|
107
|
-
|
|
108
|
-
|
|
114
|
+
__name(deepMerge, "deepMerge");
|
|
115
|
+
|
|
116
|
+
// src/core.ts
|
|
117
|
+
function wait(...instances) {
|
|
118
|
+
return Promise.all(instances.map((i) => i._promise));
|
|
109
119
|
}
|
|
110
|
-
__name(
|
|
111
|
-
function
|
|
112
|
-
|
|
113
|
-
const tag = (0, import_phecda_core2.getTag)(model);
|
|
114
|
-
if (tag in state) {
|
|
115
|
-
if (origin.get(state[tag]) !== model)
|
|
116
|
-
throw new Error(`Synonym module: Module taged "${String(tag)}" (but not "${model.name}") has been loaded before`);
|
|
117
|
-
return true;
|
|
118
|
-
}
|
|
119
|
-
return false;
|
|
120
|
+
__name(wait, "wait");
|
|
121
|
+
function getParamtypes(Model, key) {
|
|
122
|
+
return Reflect.getMetadata("design:paramtypes", Model, key);
|
|
120
123
|
}
|
|
121
|
-
__name(
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
124
|
+
__name(getParamtypes, "getParamtypes");
|
|
125
|
+
function bindMethod(instance) {
|
|
126
|
+
const cache = /* @__PURE__ */ new WeakMap();
|
|
127
|
+
return new Proxy(instance, {
|
|
128
|
+
get(target, p) {
|
|
129
|
+
if (typeof target[p] === "function" && !target[p].toString().startsWith("(")) {
|
|
130
|
+
if (!cache.has(target[p]))
|
|
131
|
+
cache.set(target[p], target[p].bind(target));
|
|
132
|
+
return cache.get(target[p]);
|
|
133
|
+
}
|
|
134
|
+
return target[p];
|
|
135
|
+
}
|
|
136
|
+
});
|
|
128
137
|
}
|
|
129
|
-
__name(
|
|
138
|
+
__name(bindMethod, "bindMethod");
|
|
139
|
+
var WebPhecda = class {
|
|
140
|
+
proxyFn;
|
|
141
|
+
origin;
|
|
142
|
+
state;
|
|
143
|
+
modelMap;
|
|
144
|
+
constructor(proxyFn) {
|
|
145
|
+
this.proxyFn = proxyFn;
|
|
146
|
+
this.origin = {};
|
|
147
|
+
this.state = {};
|
|
148
|
+
this.modelMap = /* @__PURE__ */ new WeakMap();
|
|
149
|
+
defaultWebInject();
|
|
150
|
+
}
|
|
151
|
+
init(model) {
|
|
152
|
+
const tag = (0, import_phecda_core2.getTag)(model);
|
|
153
|
+
const initModel = /* @__PURE__ */ __name(() => {
|
|
154
|
+
const paramtypes = getParamtypes(model);
|
|
155
|
+
let instance2;
|
|
156
|
+
if (paramtypes) {
|
|
157
|
+
const paramtypesInstances = [];
|
|
158
|
+
for (const i in paramtypes)
|
|
159
|
+
paramtypesInstances[i] = this.init(paramtypes[i]);
|
|
160
|
+
instance2 = this.proxyFn(new model(...paramtypesInstances));
|
|
161
|
+
} else {
|
|
162
|
+
instance2 = this.proxyFn(new model());
|
|
163
|
+
}
|
|
164
|
+
if (tag in this.origin) {
|
|
165
|
+
Object.assign(instance2, this.origin[tag]);
|
|
166
|
+
delete this.origin[tag];
|
|
167
|
+
}
|
|
168
|
+
if (typeof window !== "undefined")
|
|
169
|
+
instance2._promise = (0, import_phecda_core2.invokeHandler)("init", instance2);
|
|
170
|
+
return bindMethod(instance2);
|
|
171
|
+
}, "initModel");
|
|
172
|
+
const { state, modelMap: map } = this;
|
|
173
|
+
if ((0, import_phecda_core2.get)(model.prototype, "isolate"))
|
|
174
|
+
return initModel();
|
|
175
|
+
if (tag in state) {
|
|
176
|
+
if (process.env.NODE_ENV === "development") {
|
|
177
|
+
if (map.get(state[tag]) === model)
|
|
178
|
+
return state[tag];
|
|
179
|
+
} else {
|
|
180
|
+
if (map.get(state[tag]) !== model)
|
|
181
|
+
console.warn(`Synonym model: Module taged "${String(tag)}" has been loaded before, so won't load Module "${model.name}"`);
|
|
182
|
+
return state[tag];
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const instance = initModel();
|
|
186
|
+
state[tag] = instance;
|
|
187
|
+
map.set(instance, model);
|
|
188
|
+
return instance;
|
|
189
|
+
}
|
|
190
|
+
patch(model, data) {
|
|
191
|
+
const tag = (0, import_phecda_core2.getTag)(model);
|
|
192
|
+
const { state } = this;
|
|
193
|
+
deepMerge(state[tag], data);
|
|
194
|
+
}
|
|
195
|
+
wait(...modelOrTag) {
|
|
196
|
+
const { state } = this;
|
|
197
|
+
return Promise.all(modelOrTag.map((i) => {
|
|
198
|
+
if (typeof i === "function")
|
|
199
|
+
i = (0, import_phecda_core2.getTag)(i);
|
|
200
|
+
return state[i]._promise;
|
|
201
|
+
}));
|
|
202
|
+
}
|
|
203
|
+
get(model) {
|
|
204
|
+
const { state } = this;
|
|
205
|
+
return state[(0, import_phecda_core2.getTag)(model)];
|
|
206
|
+
}
|
|
207
|
+
has(model) {
|
|
208
|
+
const { state } = this;
|
|
209
|
+
return (0, import_phecda_core2.getTag)(model) in state;
|
|
210
|
+
}
|
|
211
|
+
reset(model) {
|
|
212
|
+
const { state } = this;
|
|
213
|
+
const tag = (0, import_phecda_core2.getTag)(model);
|
|
214
|
+
if (!(tag in state))
|
|
215
|
+
return this.init(model);
|
|
216
|
+
const instance = this.init(model);
|
|
217
|
+
const newInstance = new model();
|
|
218
|
+
Object.assign(instance, newInstance);
|
|
219
|
+
for (const key in instance) {
|
|
220
|
+
if (!(key in newInstance))
|
|
221
|
+
delete instance[key];
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
async unmount(modelOrTag) {
|
|
225
|
+
const tag = typeof modelOrTag === "function" ? (0, import_phecda_core2.getTag)(modelOrTag) : modelOrTag;
|
|
226
|
+
const { state } = this;
|
|
227
|
+
await (0, import_phecda_core2.invokeHandler)("unmount", state[tag]);
|
|
228
|
+
delete state[tag];
|
|
229
|
+
}
|
|
230
|
+
async unmountAll() {
|
|
231
|
+
const { state } = this;
|
|
232
|
+
return Promise.all(Object.keys(state).map((tag) => this.unmount(tag)));
|
|
233
|
+
}
|
|
234
|
+
ismount(modelOrTag) {
|
|
235
|
+
const { state } = this;
|
|
236
|
+
const tag = typeof modelOrTag === "function" ? (0, import_phecda_core2.getTag)(modelOrTag) : modelOrTag;
|
|
237
|
+
if (tag in state)
|
|
238
|
+
return true;
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
serialize() {
|
|
242
|
+
const { state } = this;
|
|
243
|
+
return JSON.stringify(state, (_key, value) => {
|
|
244
|
+
if (this.modelMap.has(value))
|
|
245
|
+
return null;
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
load(str) {
|
|
249
|
+
this.origin = JSON.parse(str);
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
__name(WebPhecda, "WebPhecda");
|
|
130
253
|
|
|
131
254
|
// src/base.ts
|
|
132
255
|
var import_phecda_core3 = require("phecda-core");
|
|
133
|
-
|
|
256
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
257
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
258
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
259
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
260
|
+
else
|
|
261
|
+
for (var i = decorators.length - 1; i >= 0; i--)
|
|
262
|
+
if (d = decorators[i])
|
|
263
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
264
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
265
|
+
}
|
|
266
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
267
|
+
function _ts_metadata(k, v) {
|
|
268
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
|
|
269
|
+
return Reflect.metadata(k, v);
|
|
270
|
+
}
|
|
271
|
+
__name(_ts_metadata, "_ts_metadata");
|
|
272
|
+
var Base = class {
|
|
134
273
|
constructor() {
|
|
135
274
|
}
|
|
136
275
|
get tag() {
|
|
@@ -141,6 +280,7 @@ var P = class {
|
|
|
141
280
|
}
|
|
142
281
|
on(type, handler) {
|
|
143
282
|
emitter.on(type, handler);
|
|
283
|
+
this.onUnmount(() => emitter.off(type, handler));
|
|
144
284
|
}
|
|
145
285
|
emit(type, param) {
|
|
146
286
|
emitter.emit(type, param);
|
|
@@ -148,17 +288,28 @@ var P = class {
|
|
|
148
288
|
off(type, handler) {
|
|
149
289
|
emitter.off(type, handler);
|
|
150
290
|
}
|
|
291
|
+
__UNMOUNT_SYMBOL__ = [];
|
|
292
|
+
onUnmount(cb) {
|
|
293
|
+
this.__UNMOUNT_SYMBOL__.push(cb);
|
|
294
|
+
}
|
|
295
|
+
_unmount() {
|
|
296
|
+
return Promise.all(this.__UNMOUNT_SYMBOL__.map((fn) => fn()));
|
|
297
|
+
}
|
|
151
298
|
};
|
|
152
|
-
__name(
|
|
299
|
+
__name(Base, "Base");
|
|
300
|
+
_ts_decorate([
|
|
301
|
+
import_phecda_core3.Unmount,
|
|
302
|
+
_ts_metadata("design:type", Function),
|
|
303
|
+
_ts_metadata("design:paramtypes", []),
|
|
304
|
+
_ts_metadata("design:returntype", void 0)
|
|
305
|
+
], Base.prototype, "_unmount", null);
|
|
153
306
|
// Annotate the CommonJS export names for ESM import in node:
|
|
154
307
|
0 && (module.exports = {
|
|
155
|
-
|
|
308
|
+
Base,
|
|
309
|
+
WebPhecda,
|
|
310
|
+
bindMethod,
|
|
156
311
|
defaultWebInject,
|
|
157
312
|
emitter,
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
resetActiveInstance,
|
|
161
|
-
serializeState,
|
|
162
|
-
unmountModule,
|
|
163
|
-
waitUntilInit
|
|
313
|
+
wait,
|
|
314
|
+
...require("phecda-core")
|
|
164
315
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -9,6 +9,8 @@ import { getInject, setInject } from "phecda-core";
|
|
|
9
9
|
import mitt from "mitt";
|
|
10
10
|
var emitter = mitt();
|
|
11
11
|
function defaultWebInject() {
|
|
12
|
+
if (typeof window === "undefined")
|
|
13
|
+
return;
|
|
12
14
|
if (!getInject("watcher")) {
|
|
13
15
|
setInject("watcher", ({ eventName, instance, key, options }) => {
|
|
14
16
|
const fn = typeof instance[key] === "function" ? instance[key].bind(instance) : (v) => instance[key] = v;
|
|
@@ -49,51 +51,187 @@ function defaultWebInject() {
|
|
|
49
51
|
__name(defaultWebInject, "defaultWebInject");
|
|
50
52
|
|
|
51
53
|
// src/core.ts
|
|
52
|
-
import { getTag, invokeHandler } from "phecda-core";
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
function resetActiveInstance(instance) {
|
|
59
|
-
activeInstance = instance || {
|
|
60
|
-
state: {},
|
|
61
|
-
origin: /* @__PURE__ */ new WeakMap(),
|
|
62
|
-
cache: /* @__PURE__ */ new WeakMap()
|
|
63
|
-
};
|
|
54
|
+
import { get, getTag, invokeHandler } from "phecda-core";
|
|
55
|
+
import "reflect-metadata";
|
|
56
|
+
|
|
57
|
+
// src/utils.ts
|
|
58
|
+
function isObject(o) {
|
|
59
|
+
return Object.prototype.toString.call(o) === "[object Object]";
|
|
64
60
|
}
|
|
65
|
-
__name(
|
|
66
|
-
function
|
|
67
|
-
|
|
61
|
+
__name(isObject, "isObject");
|
|
62
|
+
function deepMerge(target, patchToApply) {
|
|
63
|
+
for (const key in patchToApply) {
|
|
64
|
+
if (!patchToApply.hasOwnProperty(key))
|
|
65
|
+
continue;
|
|
66
|
+
const subPatch = patchToApply[key];
|
|
67
|
+
const targetValue = target[key];
|
|
68
|
+
if (isObject(targetValue) && isObject(subPatch) && target.hasOwnProperty(key)) {
|
|
69
|
+
target[key] = deepMerge(targetValue, subPatch);
|
|
70
|
+
} else {
|
|
71
|
+
target[key] = subPatch;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return target;
|
|
68
75
|
}
|
|
69
|
-
__name(
|
|
70
|
-
|
|
71
|
-
|
|
76
|
+
__name(deepMerge, "deepMerge");
|
|
77
|
+
|
|
78
|
+
// src/core.ts
|
|
79
|
+
function wait(...instances) {
|
|
80
|
+
return Promise.all(instances.map((i) => i._promise));
|
|
72
81
|
}
|
|
73
|
-
__name(
|
|
74
|
-
function
|
|
75
|
-
|
|
76
|
-
const tag = getTag(model);
|
|
77
|
-
if (tag in state) {
|
|
78
|
-
if (origin.get(state[tag]) !== model)
|
|
79
|
-
throw new Error(`Synonym module: Module taged "${String(tag)}" (but not "${model.name}") has been loaded before`);
|
|
80
|
-
return true;
|
|
81
|
-
}
|
|
82
|
-
return false;
|
|
82
|
+
__name(wait, "wait");
|
|
83
|
+
function getParamtypes(Model, key) {
|
|
84
|
+
return Reflect.getMetadata("design:paramtypes", Model, key);
|
|
83
85
|
}
|
|
84
|
-
__name(
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
86
|
+
__name(getParamtypes, "getParamtypes");
|
|
87
|
+
function bindMethod(instance) {
|
|
88
|
+
const cache = /* @__PURE__ */ new WeakMap();
|
|
89
|
+
return new Proxy(instance, {
|
|
90
|
+
get(target, p) {
|
|
91
|
+
if (typeof target[p] === "function" && !target[p].toString().startsWith("(")) {
|
|
92
|
+
if (!cache.has(target[p]))
|
|
93
|
+
cache.set(target[p], target[p].bind(target));
|
|
94
|
+
return cache.get(target[p]);
|
|
95
|
+
}
|
|
96
|
+
return target[p];
|
|
97
|
+
}
|
|
98
|
+
});
|
|
91
99
|
}
|
|
92
|
-
__name(
|
|
100
|
+
__name(bindMethod, "bindMethod");
|
|
101
|
+
var WebPhecda = class {
|
|
102
|
+
proxyFn;
|
|
103
|
+
origin;
|
|
104
|
+
state;
|
|
105
|
+
modelMap;
|
|
106
|
+
constructor(proxyFn) {
|
|
107
|
+
this.proxyFn = proxyFn;
|
|
108
|
+
this.origin = {};
|
|
109
|
+
this.state = {};
|
|
110
|
+
this.modelMap = /* @__PURE__ */ new WeakMap();
|
|
111
|
+
defaultWebInject();
|
|
112
|
+
}
|
|
113
|
+
init(model) {
|
|
114
|
+
const tag = getTag(model);
|
|
115
|
+
const initModel = /* @__PURE__ */ __name(() => {
|
|
116
|
+
const paramtypes = getParamtypes(model);
|
|
117
|
+
let instance2;
|
|
118
|
+
if (paramtypes) {
|
|
119
|
+
const paramtypesInstances = [];
|
|
120
|
+
for (const i in paramtypes)
|
|
121
|
+
paramtypesInstances[i] = this.init(paramtypes[i]);
|
|
122
|
+
instance2 = this.proxyFn(new model(...paramtypesInstances));
|
|
123
|
+
} else {
|
|
124
|
+
instance2 = this.proxyFn(new model());
|
|
125
|
+
}
|
|
126
|
+
if (tag in this.origin) {
|
|
127
|
+
Object.assign(instance2, this.origin[tag]);
|
|
128
|
+
delete this.origin[tag];
|
|
129
|
+
}
|
|
130
|
+
if (typeof window !== "undefined")
|
|
131
|
+
instance2._promise = invokeHandler("init", instance2);
|
|
132
|
+
return bindMethod(instance2);
|
|
133
|
+
}, "initModel");
|
|
134
|
+
const { state, modelMap: map } = this;
|
|
135
|
+
if (get(model.prototype, "isolate"))
|
|
136
|
+
return initModel();
|
|
137
|
+
if (tag in state) {
|
|
138
|
+
if (process.env.NODE_ENV === "development") {
|
|
139
|
+
if (map.get(state[tag]) === model)
|
|
140
|
+
return state[tag];
|
|
141
|
+
} else {
|
|
142
|
+
if (map.get(state[tag]) !== model)
|
|
143
|
+
console.warn(`Synonym model: Module taged "${String(tag)}" has been loaded before, so won't load Module "${model.name}"`);
|
|
144
|
+
return state[tag];
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
const instance = initModel();
|
|
148
|
+
state[tag] = instance;
|
|
149
|
+
map.set(instance, model);
|
|
150
|
+
return instance;
|
|
151
|
+
}
|
|
152
|
+
patch(model, data) {
|
|
153
|
+
const tag = getTag(model);
|
|
154
|
+
const { state } = this;
|
|
155
|
+
deepMerge(state[tag], data);
|
|
156
|
+
}
|
|
157
|
+
wait(...modelOrTag) {
|
|
158
|
+
const { state } = this;
|
|
159
|
+
return Promise.all(modelOrTag.map((i) => {
|
|
160
|
+
if (typeof i === "function")
|
|
161
|
+
i = getTag(i);
|
|
162
|
+
return state[i]._promise;
|
|
163
|
+
}));
|
|
164
|
+
}
|
|
165
|
+
get(model) {
|
|
166
|
+
const { state } = this;
|
|
167
|
+
return state[getTag(model)];
|
|
168
|
+
}
|
|
169
|
+
has(model) {
|
|
170
|
+
const { state } = this;
|
|
171
|
+
return getTag(model) in state;
|
|
172
|
+
}
|
|
173
|
+
reset(model) {
|
|
174
|
+
const { state } = this;
|
|
175
|
+
const tag = getTag(model);
|
|
176
|
+
if (!(tag in state))
|
|
177
|
+
return this.init(model);
|
|
178
|
+
const instance = this.init(model);
|
|
179
|
+
const newInstance = new model();
|
|
180
|
+
Object.assign(instance, newInstance);
|
|
181
|
+
for (const key in instance) {
|
|
182
|
+
if (!(key in newInstance))
|
|
183
|
+
delete instance[key];
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
async unmount(modelOrTag) {
|
|
187
|
+
const tag = typeof modelOrTag === "function" ? getTag(modelOrTag) : modelOrTag;
|
|
188
|
+
const { state } = this;
|
|
189
|
+
await invokeHandler("unmount", state[tag]);
|
|
190
|
+
delete state[tag];
|
|
191
|
+
}
|
|
192
|
+
async unmountAll() {
|
|
193
|
+
const { state } = this;
|
|
194
|
+
return Promise.all(Object.keys(state).map((tag) => this.unmount(tag)));
|
|
195
|
+
}
|
|
196
|
+
ismount(modelOrTag) {
|
|
197
|
+
const { state } = this;
|
|
198
|
+
const tag = typeof modelOrTag === "function" ? getTag(modelOrTag) : modelOrTag;
|
|
199
|
+
if (tag in state)
|
|
200
|
+
return true;
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
serialize() {
|
|
204
|
+
const { state } = this;
|
|
205
|
+
return JSON.stringify(state, (_key, value) => {
|
|
206
|
+
if (this.modelMap.has(value))
|
|
207
|
+
return null;
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
load(str) {
|
|
211
|
+
this.origin = JSON.parse(str);
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
__name(WebPhecda, "WebPhecda");
|
|
93
215
|
|
|
94
216
|
// src/base.ts
|
|
95
|
-
import { getTag as getTag2 } from "phecda-core";
|
|
96
|
-
|
|
217
|
+
import { Unmount, getTag as getTag2 } from "phecda-core";
|
|
218
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
219
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
220
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
221
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
222
|
+
else
|
|
223
|
+
for (var i = decorators.length - 1; i >= 0; i--)
|
|
224
|
+
if (d = decorators[i])
|
|
225
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
226
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
227
|
+
}
|
|
228
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
229
|
+
function _ts_metadata(k, v) {
|
|
230
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
|
|
231
|
+
return Reflect.metadata(k, v);
|
|
232
|
+
}
|
|
233
|
+
__name(_ts_metadata, "_ts_metadata");
|
|
234
|
+
var Base = class {
|
|
97
235
|
constructor() {
|
|
98
236
|
}
|
|
99
237
|
get tag() {
|
|
@@ -104,6 +242,7 @@ var P = class {
|
|
|
104
242
|
}
|
|
105
243
|
on(type, handler) {
|
|
106
244
|
emitter.on(type, handler);
|
|
245
|
+
this.onUnmount(() => emitter.off(type, handler));
|
|
107
246
|
}
|
|
108
247
|
emit(type, param) {
|
|
109
248
|
emitter.emit(type, param);
|
|
@@ -111,16 +250,26 @@ var P = class {
|
|
|
111
250
|
off(type, handler) {
|
|
112
251
|
emitter.off(type, handler);
|
|
113
252
|
}
|
|
253
|
+
__UNMOUNT_SYMBOL__ = [];
|
|
254
|
+
onUnmount(cb) {
|
|
255
|
+
this.__UNMOUNT_SYMBOL__.push(cb);
|
|
256
|
+
}
|
|
257
|
+
_unmount() {
|
|
258
|
+
return Promise.all(this.__UNMOUNT_SYMBOL__.map((fn) => fn()));
|
|
259
|
+
}
|
|
114
260
|
};
|
|
115
|
-
__name(
|
|
261
|
+
__name(Base, "Base");
|
|
262
|
+
_ts_decorate([
|
|
263
|
+
Unmount,
|
|
264
|
+
_ts_metadata("design:type", Function),
|
|
265
|
+
_ts_metadata("design:paramtypes", []),
|
|
266
|
+
_ts_metadata("design:returntype", void 0)
|
|
267
|
+
], Base.prototype, "_unmount", null);
|
|
116
268
|
export {
|
|
117
|
-
|
|
269
|
+
Base,
|
|
270
|
+
WebPhecda,
|
|
271
|
+
bindMethod,
|
|
118
272
|
defaultWebInject,
|
|
119
273
|
emitter,
|
|
120
|
-
|
|
121
|
-
isModuleLoad,
|
|
122
|
-
resetActiveInstance,
|
|
123
|
-
serializeState,
|
|
124
|
-
unmountModule,
|
|
125
|
-
waitUntilInit
|
|
274
|
+
wait
|
|
126
275
|
};
|
package/package.json
CHANGED
|
@@ -1,19 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "phecda-web",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "provide
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "using proxy, provide phecda function to web app",
|
|
5
|
+
"author": "fgsreally",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": "https://github.com/fgsreally/phecda/tree/main/packages/web",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"phecda",
|
|
10
|
+
"web"
|
|
11
|
+
],
|
|
5
12
|
"main": "dist/index.js",
|
|
6
13
|
"module": "dist/index.mjs",
|
|
7
14
|
"types": "dist/index.d.ts",
|
|
8
|
-
"keywords": [],
|
|
9
|
-
"author": "",
|
|
10
15
|
"files": [
|
|
11
16
|
"dist"
|
|
12
17
|
],
|
|
13
|
-
"license": "MIT",
|
|
14
18
|
"dependencies": {
|
|
15
19
|
"mitt": "^3.0.0",
|
|
16
|
-
"
|
|
20
|
+
"reflect-metadata": "^0.1.13",
|
|
21
|
+
"phecda-core": "3.0.0"
|
|
17
22
|
},
|
|
18
23
|
"devDependencies": {
|
|
19
24
|
"tsup": "^6.5.0"
|