phecda-web 2.0.2 → 2.0.4

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.
@@ -0,0 +1,98 @@
1
+ import { Events, Construct } from 'phecda-core';
2
+ export * from 'phecda-core';
3
+ import * as mitt from 'mitt';
4
+ import { Handler, WildcardHandler } from 'mitt';
5
+
6
+ interface PhecdaEmitter {
7
+ on<N extends keyof Events>(eventName: N, cb: (args: Events[N]) => void): void;
8
+ off<N extends keyof Events>(eventName: N, cb?: (args: Events[N]) => void): void;
9
+ emit<N extends keyof Events>(eventName: N, param: Events[N]): void;
10
+ }
11
+ type DeepPartial<T> = {
12
+ [K in keyof T]?: DeepPartial<T[K]>;
13
+ };
14
+
15
+ declare const emitter: PhecdaEmitter;
16
+ declare function defaultWebInject(): void;
17
+
18
+ declare function wait(...instances: InstanceType<Construct>[]): Promise<any[]>;
19
+ declare const phecdaNamespace: Map<string, WebPhecda>;
20
+ declare function setDefaultPhecda(namespace: string, phecda: WebPhecda): void;
21
+ /**
22
+ * for cases that not in ssr
23
+ */
24
+ declare function getDefaultPhecda(namespace: string): WebPhecda | undefined;
25
+ declare function delDefaultPhecda(namespace: string): boolean;
26
+ declare function bindMethod(instance: any, wrapper?: (instance: any, key: PropertyKey) => Function): any;
27
+ interface InternalEvents {
28
+ Instantiate: {
29
+ tag: PropertyKey;
30
+ };
31
+ Reset: {
32
+ tag: PropertyKey;
33
+ };
34
+ Initialize: {
35
+ tag: PropertyKey;
36
+ };
37
+ Patch: {
38
+ tag: PropertyKey;
39
+ data: any;
40
+ };
41
+ Synonym: {
42
+ tag: PropertyKey;
43
+ };
44
+ Hmr: {
45
+ tag: PropertyKey;
46
+ };
47
+ Unmount: {
48
+ tag: PropertyKey;
49
+ };
50
+ Load: {
51
+ data: any;
52
+ };
53
+ [key: string | symbol]: any;
54
+ }
55
+ declare class WebPhecda {
56
+ protected namespace: string;
57
+ protected parseModule: <Instance = any>(instance: Instance) => Instance;
58
+ /**
59
+ * for ssr or manual inject
60
+ */
61
+ memory: Record<string, any>;
62
+ state: Record<string | symbol, any>;
63
+ modelMap: WeakMap<object, any>;
64
+ emitter: mitt.Emitter<InternalEvents>;
65
+ constructor(namespace: string, parseModule: <Instance = any>(instance: Instance) => Instance);
66
+ /**
67
+ * Initialize a module that has not been created yet, and return it directly if it is cached.
68
+ */
69
+ init<Model extends Construct>(model: Model): InstanceType<Model>;
70
+ patch<Model extends Construct>(model: Model, data: DeepPartial<InstanceType<Model>>): void;
71
+ wait(...modelOrTag: (Construct | PropertyKey)[]): Promise<any[]>;
72
+ get<Model extends Construct>(modelOrTag: Model | PropertyKey): InstanceType<Model>;
73
+ getModel(tag: PropertyKey): Construct;
74
+ reset<Model extends Construct>(model: Model): InstanceType<Model> | undefined;
75
+ unmount(modelOrTag: Construct | PropertyKey): Promise<void>;
76
+ unmountAll(): Promise<void[]>;
77
+ has(modelOrTag: Construct | PropertyKey): boolean;
78
+ serialize(): string;
79
+ load(str: string): void;
80
+ emit<Key extends keyof InternalEvents>(type: Key, event?: InternalEvents[Key]): void;
81
+ on<Key extends keyof InternalEvents>(type: Key, handler: Handler<InternalEvents[Key]>): void;
82
+ on(type: '*', handler: WildcardHandler<InternalEvents>): void;
83
+ }
84
+
85
+ declare class Base {
86
+ private readonly __UNMOUNT_SYMBOL__;
87
+ private readonly __PROMISE_SYMBOL__;
88
+ constructor();
89
+ get tag(): PropertyKey;
90
+ then(cb: () => void, reject?: (e: any) => void): Promise<void>;
91
+ on<Key extends keyof Events>(type: Key, handler: (arg: Events[Key]) => void): void;
92
+ emit<Key extends keyof Events>(type: Key, param: Events[Key]): void;
93
+ off<Key extends keyof Events>(type: Key, handler?: (arg: Events[Key]) => void): void;
94
+ private onUnmount;
95
+ private _unmount;
96
+ }
97
+
98
+ export { Base, type DeepPartial, type PhecdaEmitter, WebPhecda, bindMethod, defaultWebInject, delDefaultPhecda, emitter, getDefaultPhecda, phecdaNamespace, setDefaultPhecda, wait };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  import { Events, Construct } from 'phecda-core';
2
2
  export * from 'phecda-core';
3
+ import * as mitt from 'mitt';
4
+ import { Handler, WildcardHandler } from 'mitt';
3
5
 
4
6
  interface PhecdaEmitter {
5
7
  on<N extends keyof Events>(eventName: N, cb: (args: Events[N]) => void): void;
@@ -14,38 +16,83 @@ declare const emitter: PhecdaEmitter;
14
16
  declare function defaultWebInject(): void;
15
17
 
16
18
  declare function wait(...instances: InstanceType<Construct>[]): Promise<any[]>;
17
- declare function setDefaultPhecda(phecda: WebPhecda): void;
18
- declare function getDefaultPhecda(): WebPhecda;
19
- declare function bindMethod(instance: any): any;
19
+ declare const phecdaNamespace: Map<string, WebPhecda>;
20
+ declare function setDefaultPhecda(namespace: string, phecda: WebPhecda): void;
21
+ /**
22
+ * for cases that not in ssr
23
+ */
24
+ declare function getDefaultPhecda(namespace: string): WebPhecda | undefined;
25
+ declare function delDefaultPhecda(namespace: string): boolean;
26
+ declare function bindMethod(instance: any, wrapper?: (instance: any, key: PropertyKey) => Function): any;
27
+ interface InternalEvents {
28
+ Instantiate: {
29
+ tag: PropertyKey;
30
+ };
31
+ Reset: {
32
+ tag: PropertyKey;
33
+ };
34
+ Initialize: {
35
+ tag: PropertyKey;
36
+ };
37
+ Patch: {
38
+ tag: PropertyKey;
39
+ data: any;
40
+ };
41
+ Synonym: {
42
+ tag: PropertyKey;
43
+ };
44
+ Hmr: {
45
+ tag: PropertyKey;
46
+ };
47
+ Unmount: {
48
+ tag: PropertyKey;
49
+ };
50
+ Load: {
51
+ data: any;
52
+ };
53
+ [key: string | symbol]: any;
54
+ }
20
55
  declare class WebPhecda {
56
+ protected namespace: string;
21
57
  protected parseModule: <Instance = any>(instance: Instance) => Instance;
22
- origin: Record<string, any>;
58
+ /**
59
+ * for ssr or manual inject
60
+ */
61
+ memory: Record<string, any>;
23
62
  state: Record<string | symbol, any>;
24
63
  modelMap: WeakMap<object, any>;
25
- constructor(parseModule: <Instance = any>(instance: Instance) => Instance);
64
+ emitter: mitt.Emitter<InternalEvents>;
65
+ constructor(namespace: string, parseModule: <Instance = any>(instance: Instance) => Instance);
66
+ /**
67
+ * Initialize a module that has not been created yet, and return it directly if it is cached.
68
+ */
26
69
  init<Model extends Construct>(model: Model): InstanceType<Model>;
27
70
  patch<Model extends Construct>(model: Model, data: DeepPartial<InstanceType<Model>>): void;
28
71
  wait(...modelOrTag: (Construct | PropertyKey)[]): Promise<any[]>;
29
- get<Model extends Construct>(model: Model): InstanceType<Model>;
30
- has<Model extends Construct>(model: Model): boolean;
72
+ get<Model extends Construct>(modelOrTag: Model | PropertyKey): InstanceType<Model>;
73
+ getModel(tag: PropertyKey): Construct;
31
74
  reset<Model extends Construct>(model: Model): InstanceType<Model> | undefined;
32
75
  unmount(modelOrTag: Construct | PropertyKey): Promise<void>;
33
76
  unmountAll(): Promise<void[]>;
34
- ismount(modelOrTag: Construct | PropertyKey): boolean;
77
+ has(modelOrTag: Construct | PropertyKey): boolean;
35
78
  serialize(): string;
36
79
  load(str: string): void;
80
+ emit<Key extends keyof InternalEvents>(type: Key, event?: InternalEvents[Key]): void;
81
+ on<Key extends keyof InternalEvents>(type: Key, handler: Handler<InternalEvents[Key]>): void;
82
+ on(type: '*', handler: WildcardHandler<InternalEvents>): void;
37
83
  }
38
84
 
39
85
  declare class Base {
86
+ private readonly __UNMOUNT_SYMBOL__;
87
+ private readonly __PROMISE_SYMBOL__;
40
88
  constructor();
41
89
  get tag(): PropertyKey;
42
- then(cb: () => void, reject?: (e: any) => void): any;
90
+ then(cb: () => void, reject?: (e: any) => void): Promise<void>;
43
91
  on<Key extends keyof Events>(type: Key, handler: (arg: Events[Key]) => void): void;
44
92
  emit<Key extends keyof Events>(type: Key, param: Events[Key]): void;
45
93
  off<Key extends keyof Events>(type: Key, handler?: (arg: Events[Key]) => void): void;
46
- private readonly __UNMOUNT_SYMBOL__;
47
94
  private onUnmount;
48
95
  private _unmount;
49
96
  }
50
97
 
51
- export { Base, DeepPartial, PhecdaEmitter, WebPhecda, bindMethod, defaultWebInject, emitter, getDefaultPhecda, setDefaultPhecda, wait };
98
+ export { Base, type DeepPartial, type PhecdaEmitter, WebPhecda, bindMethod, defaultWebInject, delDefaultPhecda, emitter, getDefaultPhecda, phecdaNamespace, setDefaultPhecda, wait };
package/dist/index.js CHANGED
@@ -36,8 +36,10 @@ __export(src_exports, {
36
36
  WebPhecda: () => WebPhecda,
37
37
  bindMethod: () => bindMethod,
38
38
  defaultWebInject: () => defaultWebInject,
39
+ delDefaultPhecda: () => delDefaultPhecda,
39
40
  emitter: () => emitter,
40
41
  getDefaultPhecda: () => getDefaultPhecda,
42
+ phecdaNamespace: () => phecdaNamespace,
41
43
  setDefaultPhecda: () => setDefaultPhecda,
42
44
  wait: () => wait
43
45
  });
@@ -74,8 +76,7 @@ function defaultWebInject() {
74
76
  instance[key] = data;
75
77
  } else {
76
78
  for (const i in data) {
77
- if (i)
78
- instance[i] = data[i];
79
+ if (i) instance[i] = data[i];
79
80
  }
80
81
  }
81
82
  }
@@ -91,6 +92,7 @@ __name(defaultWebInject, "defaultWebInject");
91
92
  // src/core.ts
92
93
  var import_phecda_core2 = require("phecda-core");
93
94
  var import_reflect_metadata = require("reflect-metadata");
95
+ var import_mitt2 = __toESM(require("mitt"));
94
96
 
95
97
  // src/utils.ts
96
98
  function isObject(o) {
@@ -99,8 +101,7 @@ function isObject(o) {
99
101
  __name(isObject, "isObject");
100
102
  function deepMerge(target, patchToApply) {
101
103
  for (const key in patchToApply) {
102
- if (!patchToApply.hasOwnProperty(key))
103
- continue;
104
+ if (!patchToApply.hasOwnProperty(key)) continue;
104
105
  const subPatch = patchToApply[key];
105
106
  const targetValue = target[key];
106
107
  if (isObject(targetValue) && isObject(subPatch) && target.hasOwnProperty(key)) {
@@ -115,31 +116,34 @@ __name(deepMerge, "deepMerge");
115
116
 
116
117
  // src/core.ts
117
118
  function wait(...instances) {
118
- return Promise.all(instances.map((i) => i._promise));
119
+ return Promise.all(instances.map((i) => i.__PROMISE_SYMBOL__));
119
120
  }
120
121
  __name(wait, "wait");
121
122
  function getParamtypes(Model, key) {
122
123
  return Reflect.getMetadata("design:paramtypes", Model, key);
123
124
  }
124
125
  __name(getParamtypes, "getParamtypes");
125
- var defaultPhecda;
126
- function setDefaultPhecda(phecda) {
127
- defaultPhecda = phecda;
126
+ var phecdaNamespace = /* @__PURE__ */ new Map();
127
+ function setDefaultPhecda(namespace, phecda) {
128
+ phecdaNamespace.set(namespace, phecda);
128
129
  }
129
130
  __name(setDefaultPhecda, "setDefaultPhecda");
130
- function getDefaultPhecda() {
131
- return defaultPhecda;
131
+ function getDefaultPhecda(namespace) {
132
+ return phecdaNamespace.get(namespace);
132
133
  }
133
134
  __name(getDefaultPhecda, "getDefaultPhecda");
135
+ function delDefaultPhecda(namespace) {
136
+ return phecdaNamespace.delete(namespace);
137
+ }
138
+ __name(delDefaultPhecda, "delDefaultPhecda");
134
139
  var bindCache = /* @__PURE__ */ new WeakMap();
135
- function bindMethod(instance) {
140
+ function bindMethod(instance, wrapper) {
136
141
  if (!bindCache.has(instance)) {
137
142
  const cache = /* @__PURE__ */ new WeakMap();
138
143
  bindCache.set(instance, new Proxy(instance, {
139
144
  get(target, p) {
140
145
  if (typeof target[p] === "function" && !target[p].toString().startsWith("(")) {
141
- if (!cache.has(target[p]))
142
- cache.set(target[p], target[p].bind(target));
146
+ if (!cache.has(target[p])) cache.set(target[p], wrapper ? wrapper(target, p) : target[p].bind(target));
143
147
  return cache.get(target[p]);
144
148
  }
145
149
  return target[p];
@@ -150,52 +154,72 @@ function bindMethod(instance) {
150
154
  }
151
155
  __name(bindMethod, "bindMethod");
152
156
  var WebPhecda = class {
157
+ static {
158
+ __name(this, "WebPhecda");
159
+ }
160
+ namespace;
153
161
  parseModule;
154
- origin;
162
+ /**
163
+ * for ssr or manual inject
164
+ */
165
+ memory;
155
166
  state;
156
167
  modelMap;
157
- constructor(parseModule) {
168
+ emitter;
169
+ constructor(namespace, parseModule) {
170
+ this.namespace = namespace;
158
171
  this.parseModule = parseModule;
159
- this.origin = {};
172
+ this.memory = {};
160
173
  this.state = {};
161
174
  this.modelMap = /* @__PURE__ */ new WeakMap();
175
+ this.emitter = (0, import_mitt2.default)();
162
176
  if (typeof window !== "undefined") {
163
177
  defaultWebInject();
164
- setDefaultPhecda(this);
178
+ setDefaultPhecda(namespace, this);
165
179
  }
166
180
  }
167
- // Initialize a module that has not been created yet, and return it directly if it is cached.
181
+ /**
182
+ * Initialize a module that has not been created yet, and return it directly if it is cached.
183
+ */
168
184
  init(model) {
169
185
  const tag = (0, import_phecda_core2.getTag)(model);
170
186
  const initModel = /* @__PURE__ */ __name(() => {
171
187
  const paramtypes = getParamtypes(model);
172
188
  let instance2;
189
+ this.emit("Instantiate", {
190
+ tag
191
+ });
173
192
  if (paramtypes) {
174
193
  const paramtypesInstances = [];
175
- for (const i in paramtypes)
176
- paramtypesInstances[i] = this.init(paramtypes[i]);
194
+ for (const i in paramtypes) paramtypesInstances[i] = this.init(paramtypes[i]);
177
195
  instance2 = this.parseModule(new model(...paramtypesInstances));
178
196
  } else {
179
197
  instance2 = this.parseModule(new model());
180
198
  }
181
- if (tag in this.origin) {
182
- Object.assign(instance2, this.origin[tag]);
183
- delete this.origin[tag];
199
+ if (tag in this.memory) Object.assign(instance2, this.memory[tag]);
200
+ if (typeof window !== "undefined") {
201
+ this.emit("Initialize", {
202
+ tag
203
+ });
204
+ instance2.__PROMISE_SYMBOL__ = (0, import_phecda_core2.invokeHandler)("init", instance2);
184
205
  }
185
- if (typeof window !== "undefined")
186
- instance2._promise = (0, import_phecda_core2.invokeHandler)("init", instance2);
187
206
  return instance2;
188
207
  }, "initModel");
189
208
  const { state, modelMap: map } = this;
190
- if ((0, import_phecda_core2.get)(model.prototype, "isolate"))
191
- return initModel();
209
+ if ((0, import_phecda_core2.get)(model.prototype, "isolate")) return initModel();
192
210
  if (tag in state) {
193
211
  if (process.env.NODE_ENV === "development") {
194
- if (map.get(state[tag]) === model)
195
- return state[tag];
212
+ if (map.get(state[tag]) === model) return state[tag];
213
+ else this.emit("Hmr", {
214
+ tag
215
+ });
196
216
  } else {
197
- if (map.get(state[tag]) !== model)
217
+ if (map.get(state[tag]) !== model) {
218
+ this.emit("Synonym", {
219
+ tag
220
+ });
198
221
  console.warn(`Synonym model: Module taged "${String(tag)}" has been loaded before, so won't load Module "${model.name}"`);
222
+ }
199
223
  return state[tag];
200
224
  }
201
225
  }
@@ -207,93 +231,111 @@ var WebPhecda = class {
207
231
  patch(model, data) {
208
232
  const tag = (0, import_phecda_core2.getTag)(model);
209
233
  const { state } = this;
234
+ this.emit("Patch", {
235
+ tag,
236
+ data
237
+ });
210
238
  deepMerge(state[tag], data);
211
239
  }
212
240
  wait(...modelOrTag) {
213
- const { state } = this;
214
241
  return Promise.all(modelOrTag.map((i) => {
215
- if (typeof i === "function")
216
- i = (0, import_phecda_core2.getTag)(i);
217
- return state[i]._promise;
242
+ if (typeof i === "function") i = (0, import_phecda_core2.getTag)(i);
243
+ return this.get(i).__PROMISE_SYMBOL__;
218
244
  }));
219
245
  }
220
- get(model) {
246
+ get(modelOrTag) {
221
247
  const { state } = this;
222
- return state[(0, import_phecda_core2.getTag)(model)];
248
+ const tag = typeof modelOrTag === "function" ? (0, import_phecda_core2.getTag)(modelOrTag) : modelOrTag;
249
+ return state[tag];
223
250
  }
224
- has(model) {
251
+ getModel(tag) {
225
252
  const { state } = this;
226
- return (0, import_phecda_core2.getTag)(model) in state;
253
+ return this.modelMap.get(state[tag]);
227
254
  }
228
255
  reset(model) {
229
256
  const { state } = this;
230
257
  const tag = (0, import_phecda_core2.getTag)(model);
231
- if (!(tag in state))
232
- return this.init(model);
258
+ if (!(tag in state)) return this.init(model);
259
+ this.emit("Reset", {
260
+ tag
261
+ });
233
262
  const instance = this.init(model);
234
263
  const newInstance = new model();
235
264
  Object.assign(instance, newInstance);
236
265
  for (const key in instance) {
237
- if (!(key in newInstance))
238
- delete instance[key];
266
+ if (!(key in newInstance)) delete instance[key];
239
267
  }
240
268
  }
241
269
  async unmount(modelOrTag) {
242
270
  const tag = typeof modelOrTag === "function" ? (0, import_phecda_core2.getTag)(modelOrTag) : modelOrTag;
271
+ if (!this.has(tag)) return;
272
+ this.emit("Unmount", {
273
+ tag
274
+ });
243
275
  const { state } = this;
244
- await (0, import_phecda_core2.invokeHandler)("unmount", state[tag]);
276
+ await (0, import_phecda_core2.invokeHandler)("unmount", this.get(tag));
245
277
  delete state[tag];
246
278
  }
247
279
  async unmountAll() {
248
280
  const { state } = this;
249
281
  return Promise.all(Object.keys(state).map((tag) => this.unmount(tag)));
250
282
  }
251
- ismount(modelOrTag) {
283
+ has(modelOrTag) {
252
284
  const { state } = this;
253
285
  const tag = typeof modelOrTag === "function" ? (0, import_phecda_core2.getTag)(modelOrTag) : modelOrTag;
254
- if (tag in state)
255
- return true;
286
+ if (tag in state) return true;
256
287
  return false;
257
288
  }
258
289
  serialize() {
259
290
  const { state } = this;
260
291
  return JSON.stringify(state, (_key, value) => {
261
- if (this.modelMap.has(value))
262
- return null;
292
+ if (this.modelMap.has(value)) return null;
263
293
  });
264
294
  }
265
295
  load(str) {
266
- this.origin = JSON.parse(str);
296
+ const state = JSON.parse(str);
297
+ this.emit("Load", {
298
+ data: state
299
+ });
300
+ for (const tag in state) {
301
+ if (tag in this.state) Object.assign(this.state[tag], state[tag]);
302
+ else this.memory[tag] = state[tag];
303
+ }
304
+ }
305
+ emit(type, event) {
306
+ this.emitter.emit(type, event);
307
+ }
308
+ on(type, handler) {
309
+ this.emitter.on(type, handler);
267
310
  }
268
311
  };
269
- __name(WebPhecda, "WebPhecda");
270
312
 
271
313
  // src/base.ts
272
314
  var import_phecda_core3 = require("phecda-core");
273
315
  function _ts_decorate(decorators, target, key, desc) {
274
316
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
275
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
276
- r = Reflect.decorate(decorators, target, key, desc);
277
- else
278
- for (var i = decorators.length - 1; i >= 0; i--)
279
- if (d = decorators[i])
280
- r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
317
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
318
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
281
319
  return c > 3 && r && Object.defineProperty(target, key, r), r;
282
320
  }
283
321
  __name(_ts_decorate, "_ts_decorate");
284
322
  function _ts_metadata(k, v) {
285
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
286
- return Reflect.metadata(k, v);
323
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
287
324
  }
288
325
  __name(_ts_metadata, "_ts_metadata");
289
326
  var Base = class {
327
+ static {
328
+ __name(this, "Base");
329
+ }
330
+ __UNMOUNT_SYMBOL__ = [];
331
+ __PROMISE_SYMBOL__;
290
332
  constructor() {
291
333
  }
292
334
  get tag() {
293
335
  return (0, import_phecda_core3.getTag)(this);
294
336
  }
295
337
  then(cb, reject) {
296
- return this._promise.then(cb, reject);
338
+ return this.__PROMISE_SYMBOL__.then(cb, reject);
297
339
  }
298
340
  on(type, handler) {
299
341
  emitter.on(type, handler);
@@ -305,7 +347,6 @@ var Base = class {
305
347
  off(type, handler) {
306
348
  emitter.off(type, handler);
307
349
  }
308
- __UNMOUNT_SYMBOL__ = [];
309
350
  onUnmount(cb) {
310
351
  this.__UNMOUNT_SYMBOL__.push(cb);
311
352
  }
@@ -313,7 +354,6 @@ var Base = class {
313
354
  return Promise.all(this.__UNMOUNT_SYMBOL__.map((fn) => fn()));
314
355
  }
315
356
  };
316
- __name(Base, "Base");
317
357
  _ts_decorate([
318
358
  import_phecda_core3.Unmount,
319
359
  _ts_metadata("design:type", Function),
@@ -326,8 +366,10 @@ _ts_decorate([
326
366
  WebPhecda,
327
367
  bindMethod,
328
368
  defaultWebInject,
369
+ delDefaultPhecda,
329
370
  emitter,
330
371
  getDefaultPhecda,
372
+ phecdaNamespace,
331
373
  setDefaultPhecda,
332
374
  wait,
333
375
  ...require("phecda-core")
package/dist/index.mjs CHANGED
@@ -34,8 +34,7 @@ function defaultWebInject() {
34
34
  instance[key] = data;
35
35
  } else {
36
36
  for (const i in data) {
37
- if (i)
38
- instance[i] = data[i];
37
+ if (i) instance[i] = data[i];
39
38
  }
40
39
  }
41
40
  }
@@ -51,6 +50,7 @@ __name(defaultWebInject, "defaultWebInject");
51
50
  // src/core.ts
52
51
  import { get, getTag, invokeHandler } from "phecda-core";
53
52
  import "reflect-metadata";
53
+ import mitt2 from "mitt";
54
54
 
55
55
  // src/utils.ts
56
56
  function isObject(o) {
@@ -59,8 +59,7 @@ function isObject(o) {
59
59
  __name(isObject, "isObject");
60
60
  function deepMerge(target, patchToApply) {
61
61
  for (const key in patchToApply) {
62
- if (!patchToApply.hasOwnProperty(key))
63
- continue;
62
+ if (!patchToApply.hasOwnProperty(key)) continue;
64
63
  const subPatch = patchToApply[key];
65
64
  const targetValue = target[key];
66
65
  if (isObject(targetValue) && isObject(subPatch) && target.hasOwnProperty(key)) {
@@ -75,31 +74,34 @@ __name(deepMerge, "deepMerge");
75
74
 
76
75
  // src/core.ts
77
76
  function wait(...instances) {
78
- return Promise.all(instances.map((i) => i._promise));
77
+ return Promise.all(instances.map((i) => i.__PROMISE_SYMBOL__));
79
78
  }
80
79
  __name(wait, "wait");
81
80
  function getParamtypes(Model, key) {
82
81
  return Reflect.getMetadata("design:paramtypes", Model, key);
83
82
  }
84
83
  __name(getParamtypes, "getParamtypes");
85
- var defaultPhecda;
86
- function setDefaultPhecda(phecda) {
87
- defaultPhecda = phecda;
84
+ var phecdaNamespace = /* @__PURE__ */ new Map();
85
+ function setDefaultPhecda(namespace, phecda) {
86
+ phecdaNamespace.set(namespace, phecda);
88
87
  }
89
88
  __name(setDefaultPhecda, "setDefaultPhecda");
90
- function getDefaultPhecda() {
91
- return defaultPhecda;
89
+ function getDefaultPhecda(namespace) {
90
+ return phecdaNamespace.get(namespace);
92
91
  }
93
92
  __name(getDefaultPhecda, "getDefaultPhecda");
93
+ function delDefaultPhecda(namespace) {
94
+ return phecdaNamespace.delete(namespace);
95
+ }
96
+ __name(delDefaultPhecda, "delDefaultPhecda");
94
97
  var bindCache = /* @__PURE__ */ new WeakMap();
95
- function bindMethod(instance) {
98
+ function bindMethod(instance, wrapper) {
96
99
  if (!bindCache.has(instance)) {
97
100
  const cache = /* @__PURE__ */ new WeakMap();
98
101
  bindCache.set(instance, new Proxy(instance, {
99
102
  get(target, p) {
100
103
  if (typeof target[p] === "function" && !target[p].toString().startsWith("(")) {
101
- if (!cache.has(target[p]))
102
- cache.set(target[p], target[p].bind(target));
104
+ if (!cache.has(target[p])) cache.set(target[p], wrapper ? wrapper(target, p) : target[p].bind(target));
103
105
  return cache.get(target[p]);
104
106
  }
105
107
  return target[p];
@@ -110,52 +112,72 @@ function bindMethod(instance) {
110
112
  }
111
113
  __name(bindMethod, "bindMethod");
112
114
  var WebPhecda = class {
115
+ static {
116
+ __name(this, "WebPhecda");
117
+ }
118
+ namespace;
113
119
  parseModule;
114
- origin;
120
+ /**
121
+ * for ssr or manual inject
122
+ */
123
+ memory;
115
124
  state;
116
125
  modelMap;
117
- constructor(parseModule) {
126
+ emitter;
127
+ constructor(namespace, parseModule) {
128
+ this.namespace = namespace;
118
129
  this.parseModule = parseModule;
119
- this.origin = {};
130
+ this.memory = {};
120
131
  this.state = {};
121
132
  this.modelMap = /* @__PURE__ */ new WeakMap();
133
+ this.emitter = mitt2();
122
134
  if (typeof window !== "undefined") {
123
135
  defaultWebInject();
124
- setDefaultPhecda(this);
136
+ setDefaultPhecda(namespace, this);
125
137
  }
126
138
  }
127
- // Initialize a module that has not been created yet, and return it directly if it is cached.
139
+ /**
140
+ * Initialize a module that has not been created yet, and return it directly if it is cached.
141
+ */
128
142
  init(model) {
129
143
  const tag = getTag(model);
130
144
  const initModel = /* @__PURE__ */ __name(() => {
131
145
  const paramtypes = getParamtypes(model);
132
146
  let instance2;
147
+ this.emit("Instantiate", {
148
+ tag
149
+ });
133
150
  if (paramtypes) {
134
151
  const paramtypesInstances = [];
135
- for (const i in paramtypes)
136
- paramtypesInstances[i] = this.init(paramtypes[i]);
152
+ for (const i in paramtypes) paramtypesInstances[i] = this.init(paramtypes[i]);
137
153
  instance2 = this.parseModule(new model(...paramtypesInstances));
138
154
  } else {
139
155
  instance2 = this.parseModule(new model());
140
156
  }
141
- if (tag in this.origin) {
142
- Object.assign(instance2, this.origin[tag]);
143
- delete this.origin[tag];
157
+ if (tag in this.memory) Object.assign(instance2, this.memory[tag]);
158
+ if (typeof window !== "undefined") {
159
+ this.emit("Initialize", {
160
+ tag
161
+ });
162
+ instance2.__PROMISE_SYMBOL__ = invokeHandler("init", instance2);
144
163
  }
145
- if (typeof window !== "undefined")
146
- instance2._promise = invokeHandler("init", instance2);
147
164
  return instance2;
148
165
  }, "initModel");
149
166
  const { state, modelMap: map } = this;
150
- if (get(model.prototype, "isolate"))
151
- return initModel();
167
+ if (get(model.prototype, "isolate")) return initModel();
152
168
  if (tag in state) {
153
169
  if (process.env.NODE_ENV === "development") {
154
- if (map.get(state[tag]) === model)
155
- return state[tag];
170
+ if (map.get(state[tag]) === model) return state[tag];
171
+ else this.emit("Hmr", {
172
+ tag
173
+ });
156
174
  } else {
157
- if (map.get(state[tag]) !== model)
175
+ if (map.get(state[tag]) !== model) {
176
+ this.emit("Synonym", {
177
+ tag
178
+ });
158
179
  console.warn(`Synonym model: Module taged "${String(tag)}" has been loaded before, so won't load Module "${model.name}"`);
180
+ }
159
181
  return state[tag];
160
182
  }
161
183
  }
@@ -167,93 +189,111 @@ var WebPhecda = class {
167
189
  patch(model, data) {
168
190
  const tag = getTag(model);
169
191
  const { state } = this;
192
+ this.emit("Patch", {
193
+ tag,
194
+ data
195
+ });
170
196
  deepMerge(state[tag], data);
171
197
  }
172
198
  wait(...modelOrTag) {
173
- const { state } = this;
174
199
  return Promise.all(modelOrTag.map((i) => {
175
- if (typeof i === "function")
176
- i = getTag(i);
177
- return state[i]._promise;
200
+ if (typeof i === "function") i = getTag(i);
201
+ return this.get(i).__PROMISE_SYMBOL__;
178
202
  }));
179
203
  }
180
- get(model) {
204
+ get(modelOrTag) {
181
205
  const { state } = this;
182
- return state[getTag(model)];
206
+ const tag = typeof modelOrTag === "function" ? getTag(modelOrTag) : modelOrTag;
207
+ return state[tag];
183
208
  }
184
- has(model) {
209
+ getModel(tag) {
185
210
  const { state } = this;
186
- return getTag(model) in state;
211
+ return this.modelMap.get(state[tag]);
187
212
  }
188
213
  reset(model) {
189
214
  const { state } = this;
190
215
  const tag = getTag(model);
191
- if (!(tag in state))
192
- return this.init(model);
216
+ if (!(tag in state)) return this.init(model);
217
+ this.emit("Reset", {
218
+ tag
219
+ });
193
220
  const instance = this.init(model);
194
221
  const newInstance = new model();
195
222
  Object.assign(instance, newInstance);
196
223
  for (const key in instance) {
197
- if (!(key in newInstance))
198
- delete instance[key];
224
+ if (!(key in newInstance)) delete instance[key];
199
225
  }
200
226
  }
201
227
  async unmount(modelOrTag) {
202
228
  const tag = typeof modelOrTag === "function" ? getTag(modelOrTag) : modelOrTag;
229
+ if (!this.has(tag)) return;
230
+ this.emit("Unmount", {
231
+ tag
232
+ });
203
233
  const { state } = this;
204
- await invokeHandler("unmount", state[tag]);
234
+ await invokeHandler("unmount", this.get(tag));
205
235
  delete state[tag];
206
236
  }
207
237
  async unmountAll() {
208
238
  const { state } = this;
209
239
  return Promise.all(Object.keys(state).map((tag) => this.unmount(tag)));
210
240
  }
211
- ismount(modelOrTag) {
241
+ has(modelOrTag) {
212
242
  const { state } = this;
213
243
  const tag = typeof modelOrTag === "function" ? getTag(modelOrTag) : modelOrTag;
214
- if (tag in state)
215
- return true;
244
+ if (tag in state) return true;
216
245
  return false;
217
246
  }
218
247
  serialize() {
219
248
  const { state } = this;
220
249
  return JSON.stringify(state, (_key, value) => {
221
- if (this.modelMap.has(value))
222
- return null;
250
+ if (this.modelMap.has(value)) return null;
223
251
  });
224
252
  }
225
253
  load(str) {
226
- this.origin = JSON.parse(str);
254
+ const state = JSON.parse(str);
255
+ this.emit("Load", {
256
+ data: state
257
+ });
258
+ for (const tag in state) {
259
+ if (tag in this.state) Object.assign(this.state[tag], state[tag]);
260
+ else this.memory[tag] = state[tag];
261
+ }
262
+ }
263
+ emit(type, event) {
264
+ this.emitter.emit(type, event);
265
+ }
266
+ on(type, handler) {
267
+ this.emitter.on(type, handler);
227
268
  }
228
269
  };
229
- __name(WebPhecda, "WebPhecda");
230
270
 
231
271
  // src/base.ts
232
272
  import { Unmount, getTag as getTag2 } from "phecda-core";
233
273
  function _ts_decorate(decorators, target, key, desc) {
234
274
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
235
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
236
- r = Reflect.decorate(decorators, target, key, desc);
237
- else
238
- for (var i = decorators.length - 1; i >= 0; i--)
239
- if (d = decorators[i])
240
- r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
275
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
276
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
241
277
  return c > 3 && r && Object.defineProperty(target, key, r), r;
242
278
  }
243
279
  __name(_ts_decorate, "_ts_decorate");
244
280
  function _ts_metadata(k, v) {
245
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
246
- return Reflect.metadata(k, v);
281
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
247
282
  }
248
283
  __name(_ts_metadata, "_ts_metadata");
249
284
  var Base = class {
285
+ static {
286
+ __name(this, "Base");
287
+ }
288
+ __UNMOUNT_SYMBOL__ = [];
289
+ __PROMISE_SYMBOL__;
250
290
  constructor() {
251
291
  }
252
292
  get tag() {
253
293
  return getTag2(this);
254
294
  }
255
295
  then(cb, reject) {
256
- return this._promise.then(cb, reject);
296
+ return this.__PROMISE_SYMBOL__.then(cb, reject);
257
297
  }
258
298
  on(type, handler) {
259
299
  emitter.on(type, handler);
@@ -265,7 +305,6 @@ var Base = class {
265
305
  off(type, handler) {
266
306
  emitter.off(type, handler);
267
307
  }
268
- __UNMOUNT_SYMBOL__ = [];
269
308
  onUnmount(cb) {
270
309
  this.__UNMOUNT_SYMBOL__.push(cb);
271
310
  }
@@ -273,7 +312,6 @@ var Base = class {
273
312
  return Promise.all(this.__UNMOUNT_SYMBOL__.map((fn) => fn()));
274
313
  }
275
314
  };
276
- __name(Base, "Base");
277
315
  _ts_decorate([
278
316
  Unmount,
279
317
  _ts_metadata("design:type", Function),
@@ -285,8 +323,10 @@ export {
285
323
  WebPhecda,
286
324
  bindMethod,
287
325
  defaultWebInject,
326
+ delDefaultPhecda,
288
327
  emitter,
289
328
  getDefaultPhecda,
329
+ phecdaNamespace,
290
330
  setDefaultPhecda,
291
331
  wait
292
332
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "phecda-web",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
4
4
  "description": "using proxy, provide phecda function to web app",
5
5
  "author": "fgsreally",
6
6
  "license": "MIT",
@@ -18,10 +18,10 @@
18
18
  "dependencies": {
19
19
  "mitt": "^3.0.0",
20
20
  "reflect-metadata": "^0.1.13",
21
- "phecda-core": "3.0.0"
21
+ "phecda-core": "3.0.1"
22
22
  },
23
23
  "devDependencies": {
24
- "tsup": "^6.5.0"
24
+ "tsup": "^8.1.0"
25
25
  },
26
26
  "scripts": {
27
27
  "build": "tsup",