@xfe-repo/web-micro 1.1.5 → 1.1.7

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.d.ts CHANGED
@@ -9,13 +9,13 @@ type MicroAppProps = {
9
9
  url: string;
10
10
  keepAlive?: boolean;
11
11
  className?: string;
12
+ data?: Record<string, any>;
12
13
  loadingFallback?: React.ReactNode;
13
14
  errorFallback?: React.ReactNode;
14
15
  };
15
16
  declare const MicroAppRoute: React.MemoExoticComponent<(props: MicroAppProps) => react_jsx_runtime.JSX.Element>;
16
17
 
17
18
  declare enum MicroAppEventEnum {
18
- SET_GLOBAL_STATE = "setGlobalState",
19
19
  DISPATCH_ACTION = "dispatchAction"
20
20
  }
21
21
  type MicroAppActionMetaType = {
@@ -30,24 +30,18 @@ type MicroAppActionType = {
30
30
  meta?: MicroAppActionMetaType;
31
31
  };
32
32
  type MicroAppDispatchListener = (action: MicroAppActionType) => any | Promise<any>;
33
- type MicroAppGlobalStateType = Record<string, string | number | boolean>;
34
33
  type MicroAppGlobalDataType = {
35
34
  eventType: MicroAppEventEnum;
36
- globalState?: MicroAppGlobalStateType;
37
35
  action?: MicroAppActionType;
38
36
  };
39
37
  type MicroAppEventCenterConfig = {
40
38
  name: string;
41
39
  isMain?: boolean;
42
- onGlobalStateChange?: (globalState: any) => void;
43
40
  };
44
41
  declare class MicroAppEventCenter {
45
42
  constructor(config: MicroAppEventCenterConfig);
46
43
  private readonly config;
47
44
  private readonly dispatchListeners;
48
- private _globalState;
49
- set globalState(globalState: MicroAppGlobalStateType);
50
- get globalState(): MicroAppGlobalStateType;
51
45
  private listener;
52
46
  private emitDispatchListener;
53
47
  dispatch(target: string, action: MicroAppActionType): Promise<any>;
@@ -57,4 +51,4 @@ declare class MicroAppEventCenter {
57
51
 
58
52
  declare const microApp: _micro_zoe_micro_app.MicroApp;
59
53
 
60
- export { type MicroAppActionMetaType, type MicroAppActionType, type MicroAppDispatchListener, MicroAppEventCenter, type MicroAppEventCenterConfig, MicroAppEventEnum, type MicroAppGlobalDataType, type MicroAppGlobalStateType, type MicroAppProps, MicroAppRoute, microApp };
54
+ export { type MicroAppActionMetaType, type MicroAppActionType, type MicroAppDispatchListener, MicroAppEventCenter, type MicroAppEventCenterConfig, MicroAppEventEnum, type MicroAppGlobalDataType, type MicroAppProps, MicroAppRoute, microApp };
package/dist/index.js CHANGED
@@ -304,7 +304,7 @@ var import_web_router = require("@xfe-repo/web-router");
304
304
  var import_micro_app = __toESM(require("@micro-zoe/micro-app"));
305
305
  var import_jsx_custom_event = __toESM(require("@micro-zoe/micro-app/polyfill/jsx-custom-event.js"));
306
306
  var MicroAppRoute = (0, import_react.memo)(function(props) {
307
- var name = props.name, baseRoute = props.baseRoute, url = props.url, keepAlive = props.keepAlive, className = props.className, loadingFallback = props.loadingFallback, errorFallback = props.errorFallback;
307
+ var name = props.name, baseRoute = props.baseRoute, url = props.url, keepAlive = props.keepAlive, className = props.className, data = props.data, loadingFallback = props.loadingFallback, errorFallback = props.errorFallback;
308
308
  var location = (0, import_web_router.useLocation)();
309
309
  var defaultPage = location.pathname + location.search;
310
310
  var _ref = _sliced_to_array((0, import_react.useState)(false), 2), microAppIsShow = _ref[0], setMicroAppIsShow = _ref[1];
@@ -340,6 +340,7 @@ var MicroAppRoute = (0, import_react.memo)(function(props) {
340
340
  baseroute: baseRoute ? baseRoute : "/".concat(name),
341
341
  "keep-alive": keepAlive,
342
342
  "default-page": defaultPage,
343
+ data: data,
343
344
  onMounted: handleMicroAppMounted,
344
345
  onAftershow: keepAlive ? handleMicroAppAfterShow : void 0,
345
346
  onError: handleMicroAppError
@@ -347,7 +348,6 @@ var MicroAppRoute = (0, import_react.memo)(function(props) {
347
348
  });
348
349
  // src/index.ts
349
350
  var MicroAppEventEnum = /* @__PURE__ */ function(MicroAppEventEnum2) {
350
- MicroAppEventEnum2["SET_GLOBAL_STATE"] = "setGlobalState";
351
351
  MicroAppEventEnum2["DISPATCH_ACTION"] = "dispatchAction";
352
352
  return MicroAppEventEnum2;
353
353
  }(MicroAppEventEnum || {});
@@ -356,7 +356,6 @@ var MicroAppEventCenter = /*#__PURE__*/ function() {
356
356
  var _this = this;
357
357
  _class_call_check(this, MicroAppEventCenter);
358
358
  this.dispatchListeners = {};
359
- this._globalState = {};
360
359
  this.config = config;
361
360
  if (config.isMain) window.microGlobalEventCenter = new import_micro_app2.EventCenterForMicroApp("global");
362
361
  if (!window.microGlobalEventCenter) throw new Error("microGlobalEventCenter is not defined, please check if the main app is loaded first");
@@ -365,34 +364,12 @@ var MicroAppEventCenter = /*#__PURE__*/ function() {
365
364
  });
366
365
  }
367
366
  _create_class(MicroAppEventCenter, [
368
- {
369
- key: "globalState",
370
- get: function get() {
371
- return this._globalState;
372
- },
373
- set: function set(globalState) {
374
- var name = this.config.name;
375
- var newGlobalState = _object_spread({}, globalState);
376
- var globalData = {
377
- eventType: "setGlobalState" /* SET_GLOBAL_STATE */ ,
378
- globalState: newGlobalState
379
- };
380
- if (!import_env.isProduction) console.log("MicroAppMessage [".concat(name, "]: setGlobalData"), globalData);
381
- this._globalState = newGlobalState;
382
- window.microGlobalEventCenter.forceSetGlobalData(globalData);
383
- }
384
- },
385
367
  {
386
368
  // 监听函数
387
369
  key: "listener",
388
370
  value: function listener(globalData) {
389
- var onGlobalStateChange = this.config.onGlobalStateChange;
390
- var eventType = globalData.eventType, action = globalData.action, globalState = globalData.globalState;
371
+ var eventType = globalData.eventType, action = globalData.action;
391
372
  switch(eventType){
392
- case "setGlobalState" /* SET_GLOBAL_STATE */ :
393
- this._globalState = globalState || {};
394
- onGlobalStateChange === null || onGlobalStateChange === void 0 ? void 0 : onGlobalStateChange(globalState);
395
- break;
396
373
  case "dispatchAction" /* DISPATCH_ACTION */ :
397
374
  this.emitDispatchListener(action);
398
375
  break;
package/package.json CHANGED
@@ -1,28 +1,16 @@
1
1
  {
2
2
  "name": "@xfe-repo/web-micro",
3
- "version": "1.1.5",
3
+ "version": "1.1.7",
4
4
  "sideEffects": false,
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
- "exports": {
8
- ".": {
9
- "import": {
10
- "types": "./dist/index.d.mts",
11
- "default": "./dist/index.mjs"
12
- },
13
- "require": {
14
- "types": "./dist/index.d.ts",
15
- "default": "./dist/index.js"
16
- }
17
- }
18
- },
19
7
  "files": [
20
8
  "dist"
21
9
  ],
22
10
  "dependencies": {
23
11
  "@micro-zoe/micro-app": "1.0.0-rc.13",
24
- "@xfe-repo/web-utils": "1.1.3",
25
- "@xfe-repo/web-router": "1.1.5"
12
+ "@xfe-repo/web-router": "1.1.5",
13
+ "@xfe-repo/web-utils": "1.1.4"
26
14
  },
27
15
  "devDependencies": {
28
16
  "@types/node": "^20.16.5",
package/dist/index.d.mts DELETED
@@ -1,60 +0,0 @@
1
- import * as _micro_zoe_micro_app from '@micro-zoe/micro-app';
2
- export * from '@micro-zoe/micro-app';
3
- import * as react_jsx_runtime from 'react/jsx-runtime';
4
- import React from 'react';
5
-
6
- type MicroAppProps = {
7
- name: string;
8
- baseRoute?: string;
9
- url: string;
10
- keepAlive?: boolean;
11
- className?: string;
12
- loadingFallback?: React.ReactNode;
13
- errorFallback?: React.ReactNode;
14
- };
15
- declare const MicroAppRoute: React.MemoExoticComponent<(props: MicroAppProps) => react_jsx_runtime.JSX.Element>;
16
-
17
- declare enum MicroAppEventEnum {
18
- SET_GLOBAL_STATE = "setGlobalState",
19
- DISPATCH_ACTION = "dispatchAction"
20
- }
21
- type MicroAppActionMetaType = {
22
- source: string;
23
- target: string;
24
- transId: string;
25
- error?: any;
26
- };
27
- type MicroAppActionType = {
28
- type: string;
29
- payload?: any;
30
- meta?: MicroAppActionMetaType;
31
- };
32
- type MicroAppDispatchListener = (action: MicroAppActionType) => any | Promise<any>;
33
- type MicroAppGlobalStateType = Record<string, string | number | boolean>;
34
- type MicroAppGlobalDataType = {
35
- eventType: MicroAppEventEnum;
36
- globalState?: MicroAppGlobalStateType;
37
- action?: MicroAppActionType;
38
- };
39
- type MicroAppEventCenterConfig = {
40
- name: string;
41
- isMain?: boolean;
42
- onGlobalStateChange?: (globalState: any) => void;
43
- };
44
- declare class MicroAppEventCenter {
45
- constructor(config: MicroAppEventCenterConfig);
46
- private readonly config;
47
- private readonly dispatchListeners;
48
- private _globalState;
49
- set globalState(globalState: MicroAppGlobalStateType);
50
- get globalState(): MicroAppGlobalStateType;
51
- private listener;
52
- private emitDispatchListener;
53
- dispatch(target: string, action: MicroAppActionType): Promise<any>;
54
- addDispatchListener(type: string, listener: MicroAppDispatchListener): void;
55
- removeDispatchListener(type: string, listener?: MicroAppDispatchListener): boolean | undefined;
56
- }
57
-
58
- declare const microApp: _micro_zoe_micro_app.MicroApp;
59
-
60
- export { type MicroAppActionMetaType, type MicroAppActionType, type MicroAppDispatchListener, MicroAppEventCenter, type MicroAppEventCenterConfig, MicroAppEventEnum, type MicroAppGlobalDataType, type MicroAppGlobalStateType, type MicroAppProps, MicroAppRoute, microApp };
package/dist/index.mjs DELETED
@@ -1,474 +0,0 @@
1
- // src/index.ts
2
- function _array_like_to_array(arr, len) {
3
- if (len == null || len > arr.length) len = arr.length;
4
- for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
5
- return arr2;
6
- }
7
- function _array_with_holes(arr) {
8
- if (Array.isArray(arr)) return arr;
9
- }
10
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
11
- try {
12
- var info = gen[key](arg);
13
- var value = info.value;
14
- } catch (error) {
15
- reject(error);
16
- return;
17
- }
18
- if (info.done) {
19
- resolve(value);
20
- } else {
21
- Promise.resolve(value).then(_next, _throw);
22
- }
23
- }
24
- function _async_to_generator(fn) {
25
- return function() {
26
- var self = this, args = arguments;
27
- return new Promise(function(resolve, reject) {
28
- var gen = fn.apply(self, args);
29
- function _next(value) {
30
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
31
- }
32
- function _throw(err) {
33
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
34
- }
35
- _next(undefined);
36
- });
37
- };
38
- }
39
- function _class_call_check(instance, Constructor) {
40
- if (!(instance instanceof Constructor)) {
41
- throw new TypeError("Cannot call a class as a function");
42
- }
43
- }
44
- function _defineProperties(target, props) {
45
- for(var i = 0; i < props.length; i++){
46
- var descriptor = props[i];
47
- descriptor.enumerable = descriptor.enumerable || false;
48
- descriptor.configurable = true;
49
- if ("value" in descriptor) descriptor.writable = true;
50
- Object.defineProperty(target, descriptor.key, descriptor);
51
- }
52
- }
53
- function _create_class(Constructor, protoProps, staticProps) {
54
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
55
- if (staticProps) _defineProperties(Constructor, staticProps);
56
- return Constructor;
57
- }
58
- function _define_property(obj, key, value) {
59
- if (key in obj) {
60
- Object.defineProperty(obj, key, {
61
- value: value,
62
- enumerable: true,
63
- configurable: true,
64
- writable: true
65
- });
66
- } else {
67
- obj[key] = value;
68
- }
69
- return obj;
70
- }
71
- function _iterable_to_array_limit(arr, i) {
72
- var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
73
- if (_i == null) return;
74
- var _arr = [];
75
- var _n = true;
76
- var _d = false;
77
- var _s, _e;
78
- try {
79
- for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
80
- _arr.push(_s.value);
81
- if (i && _arr.length === i) break;
82
- }
83
- } catch (err) {
84
- _d = true;
85
- _e = err;
86
- } finally{
87
- try {
88
- if (!_n && _i["return"] != null) _i["return"]();
89
- } finally{
90
- if (_d) throw _e;
91
- }
92
- }
93
- return _arr;
94
- }
95
- function _non_iterable_rest() {
96
- throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
97
- }
98
- function _object_spread(target) {
99
- for(var i = 1; i < arguments.length; i++){
100
- var source = arguments[i] != null ? arguments[i] : {};
101
- var ownKeys = Object.keys(source);
102
- if (typeof Object.getOwnPropertySymbols === "function") {
103
- ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
104
- return Object.getOwnPropertyDescriptor(source, sym).enumerable;
105
- }));
106
- }
107
- ownKeys.forEach(function(key) {
108
- _define_property(target, key, source[key]);
109
- });
110
- }
111
- return target;
112
- }
113
- function _sliced_to_array(arr, i) {
114
- return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
115
- }
116
- function _unsupported_iterable_to_array(o, minLen) {
117
- if (!o) return;
118
- if (typeof o === "string") return _array_like_to_array(o, minLen);
119
- var n = Object.prototype.toString.call(o).slice(8, -1);
120
- if (n === "Object" && o.constructor) n = o.constructor.name;
121
- if (n === "Map" || n === "Set") return Array.from(n);
122
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
123
- }
124
- function _ts_generator(thisArg, body) {
125
- var f, y, t, g, _ = {
126
- label: 0,
127
- sent: function() {
128
- if (t[0] & 1) throw t[1];
129
- return t[1];
130
- },
131
- trys: [],
132
- ops: []
133
- };
134
- return g = {
135
- next: verb(0),
136
- "throw": verb(1),
137
- "return": verb(2)
138
- }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
139
- return this;
140
- }), g;
141
- function verb(n) {
142
- return function(v) {
143
- return step([
144
- n,
145
- v
146
- ]);
147
- };
148
- }
149
- function step(op) {
150
- if (f) throw new TypeError("Generator is already executing.");
151
- while(_)try {
152
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
153
- if (y = 0, t) op = [
154
- op[0] & 2,
155
- t.value
156
- ];
157
- switch(op[0]){
158
- case 0:
159
- case 1:
160
- t = op;
161
- break;
162
- case 4:
163
- _.label++;
164
- return {
165
- value: op[1],
166
- done: false
167
- };
168
- case 5:
169
- _.label++;
170
- y = op[1];
171
- op = [
172
- 0
173
- ];
174
- continue;
175
- case 7:
176
- op = _.ops.pop();
177
- _.trys.pop();
178
- continue;
179
- default:
180
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
181
- _ = 0;
182
- continue;
183
- }
184
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
185
- _.label = op[1];
186
- break;
187
- }
188
- if (op[0] === 6 && _.label < t[1]) {
189
- _.label = t[1];
190
- t = op;
191
- break;
192
- }
193
- if (t && _.label < t[2]) {
194
- _.label = t[2];
195
- _.ops.push(op);
196
- break;
197
- }
198
- if (t[2]) _.ops.pop();
199
- _.trys.pop();
200
- continue;
201
- }
202
- op = body.call(thisArg, _);
203
- } catch (e) {
204
- op = [
205
- 6,
206
- e
207
- ];
208
- y = 0;
209
- } finally{
210
- f = t = 0;
211
- }
212
- if (op[0] & 5) throw op[1];
213
- return {
214
- value: op[0] ? op[1] : void 0,
215
- done: true
216
- };
217
- }
218
- }
219
- import originMicroApp, { EventCenterForMicroApp } from "@micro-zoe/micro-app";
220
- import { isProduction } from "@xfe-repo/web-utils/env";
221
- import { uuid } from "@xfe-repo/web-utils/tools";
222
- export * from "@micro-zoe/micro-app";
223
- // src/MicroAppRoute.tsx
224
- import { memo, useCallback, useState } from "react";
225
- import { useLocation } from "@xfe-repo/web-router";
226
- import microApp from "@micro-zoe/micro-app";
227
- import jsxCustomEvent from "@micro-zoe/micro-app/polyfill/jsx-custom-event.js";
228
- var MicroAppRoute = memo(function(props) {
229
- var name = props.name, baseRoute = props.baseRoute, url = props.url, keepAlive = props.keepAlive, className = props.className, loadingFallback = props.loadingFallback, errorFallback = props.errorFallback;
230
- var location = useLocation();
231
- var defaultPage = location.pathname + location.search;
232
- var _useState = _sliced_to_array(useState(false), 2), microAppIsShow = _useState[0], setMicroAppIsShow = _useState[1];
233
- var _useState1 = _sliced_to_array(useState(false), 2), microAppIsError = _useState1[0], setMicroAppIsError = _useState1[1];
234
- var handleMicroAppMounted = useCallback(function() {
235
- setMicroAppIsShow(true);
236
- }, []);
237
- var handleMicroAppAfterShow = useCallback(function() {
238
- var path = location.pathname + location.search;
239
- microApp.router.push({
240
- name: "dashboard",
241
- path: path,
242
- replace: true
243
- }).catch(function(e) {
244
- return e;
245
- });
246
- setMicroAppIsShow(true);
247
- }, [
248
- location
249
- ]);
250
- var handleMicroAppError = useCallback(function() {
251
- setMicroAppIsError(true);
252
- }, []);
253
- return /* @__PURE__ */ jsxCustomEvent("div", {
254
- className: className
255
- }, microAppIsError && errorFallback, !microAppIsShow && !microAppIsError && loadingFallback, /* @__PURE__ */ jsxCustomEvent("micro-app", {
256
- style: {
257
- display: microAppIsShow ? "block" : "none"
258
- },
259
- url: url,
260
- name: name,
261
- "router-mode": "native",
262
- baseroute: baseRoute ? baseRoute : "/".concat(name),
263
- "keep-alive": keepAlive,
264
- "default-page": defaultPage,
265
- onMounted: handleMicroAppMounted,
266
- onAftershow: keepAlive ? handleMicroAppAfterShow : void 0,
267
- onError: handleMicroAppError
268
- }));
269
- });
270
- // src/index.ts
271
- var MicroAppEventEnum = /* @__PURE__ */ function(MicroAppEventEnum2) {
272
- MicroAppEventEnum2["SET_GLOBAL_STATE"] = "setGlobalState";
273
- MicroAppEventEnum2["DISPATCH_ACTION"] = "dispatchAction";
274
- return MicroAppEventEnum2;
275
- }(MicroAppEventEnum || {});
276
- var MicroAppEventCenter = /*#__PURE__*/ function() {
277
- "use strict";
278
- function MicroAppEventCenter(config) {
279
- var _this = this;
280
- _class_call_check(this, MicroAppEventCenter);
281
- this.dispatchListeners = {};
282
- this._globalState = {};
283
- this.config = config;
284
- if (config.isMain) window.microGlobalEventCenter = new EventCenterForMicroApp("global");
285
- if (!window.microGlobalEventCenter) throw new Error("microGlobalEventCenter is not defined, please check if the main app is loaded first");
286
- window.microGlobalEventCenter.addGlobalDataListener(function(globalData) {
287
- return _this.listener(globalData);
288
- });
289
- }
290
- _create_class(MicroAppEventCenter, [
291
- {
292
- key: "globalState",
293
- get: function get() {
294
- return this._globalState;
295
- },
296
- set: function set(globalState) {
297
- var name = this.config.name;
298
- var newGlobalState = _object_spread({}, globalState);
299
- var globalData = {
300
- eventType: "setGlobalState" /* SET_GLOBAL_STATE */ ,
301
- globalState: newGlobalState
302
- };
303
- if (!isProduction) console.log("MicroAppMessage [".concat(name, "]: setGlobalData"), globalData);
304
- this._globalState = newGlobalState;
305
- window.microGlobalEventCenter.forceSetGlobalData(globalData);
306
- }
307
- },
308
- {
309
- // 监听函数
310
- key: "listener",
311
- value: function listener(globalData) {
312
- var onGlobalStateChange = this.config.onGlobalStateChange;
313
- var eventType = globalData.eventType, action = globalData.action, globalState = globalData.globalState;
314
- switch(eventType){
315
- case "setGlobalState" /* SET_GLOBAL_STATE */ :
316
- this._globalState = globalState || {};
317
- onGlobalStateChange === null || onGlobalStateChange === void 0 ? void 0 : onGlobalStateChange(globalState);
318
- break;
319
- case "dispatchAction" /* DISPATCH_ACTION */ :
320
- this.emitDispatchListener(action);
321
- break;
322
- }
323
- }
324
- },
325
- {
326
- // 触发事件
327
- key: "emitDispatchListener",
328
- value: function emitDispatchListener(action) {
329
- var _this_dispatchListeners_type;
330
- var name = this.config.name;
331
- if (!action) return console.error("MicroAppMessage [".concat(name, "]: 未知消息类型 缺少action"));
332
- var type = action.type, meta = action.meta;
333
- if (!meta) return console.error("MicroAppMessage [".concat(name, "]: 未知消息类型 缺少meta"), action);
334
- var source = meta.source, target = meta.target, transId = meta.transId;
335
- if (source === name) return;
336
- if (target !== "all" && target !== name) return;
337
- if (!((_this_dispatchListeners_type = this.dispatchListeners[type]) === null || _this_dispatchListeners_type === void 0 ? void 0 : _this_dispatchListeners_type.length)) return;
338
- var actionResponseType = "".concat(type, "___").concat(transId, "___response");
339
- var _this = this;
340
- this.dispatchListeners[type].forEach(function() {
341
- var _ref = _async_to_generator(function(listener) {
342
- var responseAction, error, isBroadcast, isResponse;
343
- return _ts_generator(this, function(_state) {
344
- switch(_state.label){
345
- case 0:
346
- responseAction = {
347
- type: actionResponseType,
348
- meta: {
349
- source: name,
350
- target: source,
351
- transId: transId
352
- }
353
- };
354
- _state.label = 1;
355
- case 1:
356
- _state.trys.push([
357
- 1,
358
- 3,
359
- ,
360
- 4
361
- ]);
362
- return [
363
- 4,
364
- listener(action)
365
- ];
366
- case 2:
367
- responseAction.payload = _state.sent();
368
- return [
369
- 3,
370
- 4
371
- ];
372
- case 3:
373
- error = _state.sent();
374
- if (responseAction.meta) responseAction.meta.error = error;
375
- return [
376
- 3,
377
- 4
378
- ];
379
- case 4:
380
- isBroadcast = target === "all";
381
- isResponse = type.endsWith("___response");
382
- if (isBroadcast || isResponse) return [
383
- 2
384
- ];
385
- return [
386
- 4,
387
- _this.dispatch(source, responseAction)
388
- ];
389
- case 5:
390
- _state.sent();
391
- return [
392
- 2
393
- ];
394
- }
395
- });
396
- });
397
- return function(listener) {
398
- return _ref.apply(this, arguments);
399
- };
400
- }());
401
- }
402
- },
403
- {
404
- key: "dispatch",
405
- value: // 远程调用
406
- function dispatch(target, action) {
407
- var _this = this;
408
- return _async_to_generator(function() {
409
- var name, type, payload, meta, transId, newAction, globalData, isBroadcast, isResponse, actionResponseType, actionResponsePromise;
410
- return _ts_generator(this, function(_state) {
411
- name = _this.config.name;
412
- type = action.type, payload = action.payload, meta = action.meta;
413
- transId = (meta === null || meta === void 0 ? void 0 : meta.transId) || uuid();
414
- newAction = {
415
- type: type,
416
- payload: payload,
417
- meta: _object_spread({
418
- source: name,
419
- target: target,
420
- transId: transId
421
- }, meta)
422
- };
423
- globalData = {
424
- eventType: "dispatchAction" /* DISPATCH_ACTION */ ,
425
- action: newAction
426
- };
427
- if (!isProduction) console.log("MicroAppMessage [".concat(name, "]: dispatch ").concat(type, " to [").concat(target, "]"), globalData);
428
- isBroadcast = target === "all";
429
- isResponse = type.endsWith("___response");
430
- if (isBroadcast || isResponse) return [
431
- 2,
432
- window.microGlobalEventCenter.forceSetGlobalData(globalData)
433
- ];
434
- actionResponseType = "".concat(type, "___").concat(transId, "___response");
435
- actionResponsePromise = new Promise(function(resolve, reject) {
436
- var listener = function(responseAction) {
437
- var payload2 = responseAction.payload, meta2 = responseAction.meta;
438
- (meta2 === null || meta2 === void 0 ? void 0 : meta2.error) ? reject(meta2.error) : resolve(payload2);
439
- _this.removeDispatchListener(actionResponseType, listener);
440
- };
441
- _this.addDispatchListener(actionResponseType, listener);
442
- });
443
- window.microGlobalEventCenter.forceSetGlobalData(globalData);
444
- return [
445
- 2,
446
- actionResponsePromise
447
- ];
448
- });
449
- })();
450
- }
451
- },
452
- {
453
- // 添加监听
454
- key: "addDispatchListener",
455
- value: function addDispatchListener(type, listener) {
456
- if (!this.dispatchListeners[type]) this.dispatchListeners[type] = [];
457
- this.dispatchListeners[type].push(listener);
458
- }
459
- },
460
- {
461
- // 移除监听 如果不传listener则移除所有监听
462
- key: "removeDispatchListener",
463
- value: function removeDispatchListener(type, listener) {
464
- if (!this.dispatchListeners[type]) return;
465
- if (!listener) return delete this.dispatchListeners[type];
466
- var index = this.dispatchListeners[type].indexOf(listener);
467
- if (index !== -1) this.dispatchListeners[type].splice(index, 1);
468
- }
469
- }
470
- ]);
471
- return MicroAppEventCenter;
472
- }();
473
- var microApp2 = originMicroApp;
474
- export { MicroAppEventCenter, MicroAppEventEnum, MicroAppRoute, microApp2 as microApp };