@xfe-repo/web-micro 1.1.3

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 ADDED
@@ -0,0 +1,3 @@
1
+ # `@xfe-repo/web-register`
2
+
3
+ web register库
@@ -0,0 +1,59 @@
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
+ onGlobalStateChange?: (globalState: any) => void;
42
+ };
43
+ declare class MicroAppEventCenter {
44
+ constructor(config: MicroAppEventCenterConfig);
45
+ private readonly config;
46
+ private readonly dispatchListeners;
47
+ private _globalState;
48
+ set globalState(globalState: MicroAppGlobalStateType);
49
+ get globalState(): MicroAppGlobalStateType;
50
+ private listener;
51
+ private emitDispatchListener;
52
+ dispatch(target: string, action: MicroAppActionType): Promise<any>;
53
+ addDispatchListener(type: string, listener: MicroAppDispatchListener): void;
54
+ removeDispatchListener(type: string, listener?: MicroAppDispatchListener): boolean | undefined;
55
+ }
56
+
57
+ declare const microApp: _micro_zoe_micro_app.MicroApp;
58
+
59
+ export { type MicroAppActionMetaType, type MicroAppActionType, type MicroAppDispatchListener, MicroAppEventCenter, type MicroAppEventCenterConfig, MicroAppEventEnum, type MicroAppGlobalDataType, type MicroAppGlobalStateType, type MicroAppProps, MicroAppRoute, microApp };
@@ -0,0 +1,59 @@
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
+ onGlobalStateChange?: (globalState: any) => void;
42
+ };
43
+ declare class MicroAppEventCenter {
44
+ constructor(config: MicroAppEventCenterConfig);
45
+ private readonly config;
46
+ private readonly dispatchListeners;
47
+ private _globalState;
48
+ set globalState(globalState: MicroAppGlobalStateType);
49
+ get globalState(): MicroAppGlobalStateType;
50
+ private listener;
51
+ private emitDispatchListener;
52
+ dispatch(target: string, action: MicroAppActionType): Promise<any>;
53
+ addDispatchListener(type: string, listener: MicroAppDispatchListener): void;
54
+ removeDispatchListener(type: string, listener?: MicroAppDispatchListener): boolean | undefined;
55
+ }
56
+
57
+ declare const microApp: _micro_zoe_micro_app.MicroApp;
58
+
59
+ export { type MicroAppActionMetaType, type MicroAppActionType, type MicroAppDispatchListener, MicroAppEventCenter, type MicroAppEventCenterConfig, MicroAppEventEnum, type MicroAppGlobalDataType, type MicroAppGlobalStateType, type MicroAppProps, MicroAppRoute, microApp };
package/dist/index.js ADDED
@@ -0,0 +1,556 @@
1
+ "use strict";
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
+ var __create = Object.create;
220
+ var __defProp = Object.defineProperty;
221
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
222
+ var __getOwnPropNames = Object.getOwnPropertyNames;
223
+ var __getProtoOf = Object.getPrototypeOf;
224
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
225
+ var __export = function(target, all) {
226
+ for(var name in all)__defProp(target, name, {
227
+ get: all[name],
228
+ enumerable: true
229
+ });
230
+ };
231
+ var __copyProps = function(to, from, except, desc) {
232
+ if (from && typeof from === "object" || typeof from === "function") {
233
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
234
+ try {
235
+ var _loop = function() {
236
+ var key = _step.value;
237
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
238
+ get: function() {
239
+ return from[key];
240
+ },
241
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
242
+ });
243
+ };
244
+ for(var _iterator = __getOwnPropNames(from)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop();
245
+ } catch (err) {
246
+ _didIteratorError = true;
247
+ _iteratorError = err;
248
+ } finally{
249
+ try {
250
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
251
+ _iterator.return();
252
+ }
253
+ } finally{
254
+ if (_didIteratorError) {
255
+ throw _iteratorError;
256
+ }
257
+ }
258
+ }
259
+ }
260
+ return to;
261
+ };
262
+ var __reExport = function(target, mod, secondTarget) {
263
+ return __copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default");
264
+ };
265
+ var __toESM = function(mod, isNodeMode, target) {
266
+ return target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(// If the importer is in node compatibility mode or this is not an ESM
267
+ // file that has been converted to a CommonJS file using a Babel-
268
+ // compatible transform (i.e. "__esModule" has not been set), then set
269
+ // "default" to the CommonJS "module.exports" for node compatibility.
270
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
271
+ value: mod,
272
+ enumerable: true
273
+ }) : target, mod);
274
+ };
275
+ var __toCommonJS = function(mod) {
276
+ return __copyProps(__defProp({}, "__esModule", {
277
+ value: true
278
+ }), mod);
279
+ };
280
+ // src/index.ts
281
+ var src_exports = {};
282
+ __export(src_exports, {
283
+ MicroAppEventCenter: function() {
284
+ return MicroAppEventCenter;
285
+ },
286
+ MicroAppEventEnum: function() {
287
+ return MicroAppEventEnum;
288
+ },
289
+ MicroAppRoute: function() {
290
+ return MicroAppRoute;
291
+ },
292
+ microApp: function() {
293
+ return microApp2;
294
+ }
295
+ });
296
+ module.exports = __toCommonJS(src_exports);
297
+ var import_micro_app2 = __toESM(require("@micro-zoe/micro-app"));
298
+ var import_env = require("@xfe-repo/web-utils/env");
299
+ var import_tools = require("@xfe-repo/web-utils/tools");
300
+ __reExport(src_exports, require("@micro-zoe/micro-app"), module.exports);
301
+ // src/MicroAppRoute.tsx
302
+ var import_react = require("react");
303
+ var import_web_router = require("@xfe-repo/web-router");
304
+ var import_micro_app = __toESM(require("@micro-zoe/micro-app"));
305
+ var import_jsx_custom_event = __toESM(require("@micro-zoe/micro-app/polyfill/jsx-custom-event.js"));
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;
308
+ var location = (0, import_web_router.useLocation)();
309
+ var defaultPage = location.pathname + location.search;
310
+ var _ref = _sliced_to_array((0, import_react.useState)(false), 2), microAppIsShow = _ref[0], setMicroAppIsShow = _ref[1];
311
+ var _ref1 = _sliced_to_array((0, import_react.useState)(false), 2), microAppIsError = _ref1[0], setMicroAppIsError = _ref1[1];
312
+ var handleMicroAppMounted = (0, import_react.useCallback)(function() {
313
+ setMicroAppIsShow(true);
314
+ }, []);
315
+ var handleMicroAppAfterShow = (0, import_react.useCallback)(function() {
316
+ var path = location.pathname + location.search;
317
+ import_micro_app.default.router.push({
318
+ name: "dashboard",
319
+ path: path,
320
+ replace: true
321
+ }).catch(function(e) {
322
+ return e;
323
+ });
324
+ setMicroAppIsShow(true);
325
+ }, [
326
+ location
327
+ ]);
328
+ var handleMicroAppError = (0, import_react.useCallback)(function() {
329
+ setMicroAppIsError(true);
330
+ }, []);
331
+ return /* @__PURE__ */ (0, import_jsx_custom_event.default)("div", {
332
+ className: className
333
+ }, microAppIsError && errorFallback, !microAppIsShow && loadingFallback, /* @__PURE__ */ (0, import_jsx_custom_event.default)("micro-app", {
334
+ style: {
335
+ display: microAppIsShow ? "block" : "none"
336
+ },
337
+ url: url,
338
+ name: name,
339
+ "router-mode": "native",
340
+ baseroute: baseRoute ? baseRoute : "/".concat(name),
341
+ "keep-alive": keepAlive,
342
+ "default-page": defaultPage,
343
+ onMounted: handleMicroAppMounted,
344
+ onAftershow: keepAlive ? handleMicroAppAfterShow : void 0,
345
+ onError: handleMicroAppError
346
+ }));
347
+ });
348
+ // src/index.ts
349
+ window.microGlobalEventCenter = new import_micro_app2.EventCenterForMicroApp("global");
350
+ var MicroAppEventEnum = /* @__PURE__ */ function(MicroAppEventEnum2) {
351
+ MicroAppEventEnum2["SET_GLOBAL_STATE"] = "setGlobalState";
352
+ MicroAppEventEnum2["DISPATCH_ACTION"] = "dispatchAction";
353
+ return MicroAppEventEnum2;
354
+ }(MicroAppEventEnum || {});
355
+ var MicroAppEventCenter = /*#__PURE__*/ function() {
356
+ function MicroAppEventCenter(config) {
357
+ var _this = this;
358
+ _class_call_check(this, MicroAppEventCenter);
359
+ this.dispatchListeners = {};
360
+ this._globalState = {};
361
+ this.config = config;
362
+ window.microGlobalEventCenter.addGlobalDataListener(function(globalData) {
363
+ return _this.listener(globalData);
364
+ });
365
+ }
366
+ _create_class(MicroAppEventCenter, [
367
+ {
368
+ key: "globalState",
369
+ get: function get() {
370
+ return this._globalState;
371
+ },
372
+ set: function set(globalState) {
373
+ var name = this.config.name;
374
+ var newGlobalState = _object_spread({}, globalState);
375
+ var globalData = {
376
+ eventType: "setGlobalState" /* SET_GLOBAL_STATE */ ,
377
+ globalState: newGlobalState
378
+ };
379
+ if (!import_env.isProduction) console.log("MicroAppMessage [".concat(name, "]: setGlobalData"), globalData);
380
+ this._globalState = newGlobalState;
381
+ window.microGlobalEventCenter.forceSetGlobalData(globalData);
382
+ }
383
+ },
384
+ {
385
+ // 监听函数
386
+ key: "listener",
387
+ value: function listener(globalData) {
388
+ var onGlobalStateChange = this.config.onGlobalStateChange;
389
+ var eventType = globalData.eventType, action = globalData.action, globalState = globalData.globalState;
390
+ switch(eventType){
391
+ case "setGlobalState" /* SET_GLOBAL_STATE */ :
392
+ this._globalState = globalState || {};
393
+ onGlobalStateChange === null || onGlobalStateChange === void 0 ? void 0 : onGlobalStateChange(globalState);
394
+ break;
395
+ case "dispatchAction" /* DISPATCH_ACTION */ :
396
+ this.emitDispatchListener(action);
397
+ break;
398
+ }
399
+ }
400
+ },
401
+ {
402
+ // 触发事件
403
+ key: "emitDispatchListener",
404
+ value: function emitDispatchListener(action) {
405
+ var _this_dispatchListeners_type;
406
+ var name = this.config.name;
407
+ if (!action) return console.error("MicroAppMessage [".concat(name, "]: 未知消息类型 缺少action"));
408
+ var type = action.type, meta = action.meta;
409
+ if (!meta) return console.error("MicroAppMessage [".concat(name, "]: 未知消息类型 缺少meta"), action);
410
+ var source = meta.source, target = meta.target, transId = meta.transId;
411
+ if (source === name) return;
412
+ if (target !== "all" && target !== name) return;
413
+ if (!((_this_dispatchListeners_type = this.dispatchListeners[type]) === null || _this_dispatchListeners_type === void 0 ? void 0 : _this_dispatchListeners_type.length)) return;
414
+ var actionResponseType = "".concat(type, "___").concat(transId, "___response");
415
+ var _this = this;
416
+ this.dispatchListeners[type].forEach(function() {
417
+ var _ref = _async_to_generator(function(listener) {
418
+ var responseAction, error, isBroadcast, isResponse;
419
+ return _ts_generator(this, function(_state) {
420
+ switch(_state.label){
421
+ case 0:
422
+ responseAction = {
423
+ type: actionResponseType,
424
+ meta: {
425
+ source: name,
426
+ target: source,
427
+ transId: transId
428
+ }
429
+ };
430
+ _state.label = 1;
431
+ case 1:
432
+ _state.trys.push([
433
+ 1,
434
+ 3,
435
+ ,
436
+ 4
437
+ ]);
438
+ return [
439
+ 4,
440
+ listener(action)
441
+ ];
442
+ case 2:
443
+ responseAction.payload = _state.sent();
444
+ return [
445
+ 3,
446
+ 4
447
+ ];
448
+ case 3:
449
+ error = _state.sent();
450
+ if (responseAction.meta) responseAction.meta.error = error;
451
+ return [
452
+ 3,
453
+ 4
454
+ ];
455
+ case 4:
456
+ isBroadcast = target === "all";
457
+ isResponse = type.endsWith("___response");
458
+ if (isBroadcast || isResponse) return [
459
+ 2
460
+ ];
461
+ return [
462
+ 4,
463
+ _this.dispatch(source, responseAction)
464
+ ];
465
+ case 5:
466
+ _state.sent();
467
+ return [
468
+ 2
469
+ ];
470
+ }
471
+ });
472
+ });
473
+ return function(listener) {
474
+ return _ref.apply(this, arguments);
475
+ };
476
+ }());
477
+ }
478
+ },
479
+ {
480
+ key: "dispatch",
481
+ value: // 远程调用
482
+ function dispatch(target, action) {
483
+ var _this = this;
484
+ return _async_to_generator(function() {
485
+ var name, type, payload, meta, transId, newAction, globalData, isBroadcast, isResponse, actionResponseType, actionResponsePromise;
486
+ return _ts_generator(this, function(_state) {
487
+ name = _this.config.name;
488
+ type = action.type, payload = action.payload, meta = action.meta;
489
+ transId = (meta === null || meta === void 0 ? void 0 : meta.transId) || (0, import_tools.uuid)();
490
+ newAction = {
491
+ type: type,
492
+ payload: payload,
493
+ meta: _object_spread({
494
+ source: name,
495
+ target: target,
496
+ transId: transId
497
+ }, meta)
498
+ };
499
+ globalData = {
500
+ eventType: "dispatchAction" /* DISPATCH_ACTION */ ,
501
+ action: newAction
502
+ };
503
+ if (!import_env.isProduction) console.log("MicroAppMessage [".concat(name, "]: dispatch ").concat(type, " to [").concat(target, "]"), globalData);
504
+ isBroadcast = target === "all";
505
+ isResponse = type.endsWith("___response");
506
+ if (isBroadcast || isResponse) return [
507
+ 2,
508
+ window.microGlobalEventCenter.forceSetGlobalData(globalData)
509
+ ];
510
+ actionResponseType = "".concat(type, "___").concat(transId, "___response");
511
+ actionResponsePromise = new Promise(function(resolve, reject) {
512
+ var listener = function(responseAction) {
513
+ var payload2 = responseAction.payload, meta2 = responseAction.meta;
514
+ (meta2 === null || meta2 === void 0 ? void 0 : meta2.error) ? reject(meta2.error) : resolve(payload2);
515
+ _this.removeDispatchListener(actionResponseType, listener);
516
+ };
517
+ _this.addDispatchListener(actionResponseType, listener);
518
+ });
519
+ window.microGlobalEventCenter.forceSetGlobalData(globalData);
520
+ return [
521
+ 2,
522
+ actionResponsePromise
523
+ ];
524
+ });
525
+ })();
526
+ }
527
+ },
528
+ {
529
+ // 添加监听
530
+ key: "addDispatchListener",
531
+ value: function addDispatchListener(type, listener) {
532
+ if (!this.dispatchListeners[type]) this.dispatchListeners[type] = [];
533
+ this.dispatchListeners[type].push(listener);
534
+ }
535
+ },
536
+ {
537
+ // 移除监听 如果不传listener则移除所有监听
538
+ key: "removeDispatchListener",
539
+ value: function removeDispatchListener(type, listener) {
540
+ if (!this.dispatchListeners[type]) return;
541
+ if (!listener) return delete this.dispatchListeners[type];
542
+ var index = this.dispatchListeners[type].indexOf(listener);
543
+ if (index !== -1) this.dispatchListeners[type].splice(index, 1);
544
+ }
545
+ }
546
+ ]);
547
+ return MicroAppEventCenter;
548
+ }();
549
+ var microApp2 = import_micro_app2.default;
550
+ // Annotate the CommonJS export names for ESM import in node:
551
+ 0 && (module.exports = _object_spread({
552
+ MicroAppEventCenter: MicroAppEventCenter,
553
+ MicroAppEventEnum: MicroAppEventEnum,
554
+ MicroAppRoute: MicroAppRoute,
555
+ microApp: microApp
556
+ }, require("@micro-zoe/micro-app")));
package/dist/index.mjs ADDED
@@ -0,0 +1,473 @@
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 && 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
+ window.microGlobalEventCenter = new EventCenterForMicroApp("global");
272
+ var MicroAppEventEnum = /* @__PURE__ */ function(MicroAppEventEnum2) {
273
+ MicroAppEventEnum2["SET_GLOBAL_STATE"] = "setGlobalState";
274
+ MicroAppEventEnum2["DISPATCH_ACTION"] = "dispatchAction";
275
+ return MicroAppEventEnum2;
276
+ }(MicroAppEventEnum || {});
277
+ var MicroAppEventCenter = /*#__PURE__*/ function() {
278
+ "use strict";
279
+ function MicroAppEventCenter(config) {
280
+ var _this = this;
281
+ _class_call_check(this, MicroAppEventCenter);
282
+ this.dispatchListeners = {};
283
+ this._globalState = {};
284
+ this.config = config;
285
+ window.microGlobalEventCenter.addGlobalDataListener(function(globalData) {
286
+ return _this.listener(globalData);
287
+ });
288
+ }
289
+ _create_class(MicroAppEventCenter, [
290
+ {
291
+ key: "globalState",
292
+ get: function get() {
293
+ return this._globalState;
294
+ },
295
+ set: function set(globalState) {
296
+ var name = this.config.name;
297
+ var newGlobalState = _object_spread({}, globalState);
298
+ var globalData = {
299
+ eventType: "setGlobalState" /* SET_GLOBAL_STATE */ ,
300
+ globalState: newGlobalState
301
+ };
302
+ if (!isProduction) console.log("MicroAppMessage [".concat(name, "]: setGlobalData"), globalData);
303
+ this._globalState = newGlobalState;
304
+ window.microGlobalEventCenter.forceSetGlobalData(globalData);
305
+ }
306
+ },
307
+ {
308
+ // 监听函数
309
+ key: "listener",
310
+ value: function listener(globalData) {
311
+ var onGlobalStateChange = this.config.onGlobalStateChange;
312
+ var eventType = globalData.eventType, action = globalData.action, globalState = globalData.globalState;
313
+ switch(eventType){
314
+ case "setGlobalState" /* SET_GLOBAL_STATE */ :
315
+ this._globalState = globalState || {};
316
+ onGlobalStateChange === null || onGlobalStateChange === void 0 ? void 0 : onGlobalStateChange(globalState);
317
+ break;
318
+ case "dispatchAction" /* DISPATCH_ACTION */ :
319
+ this.emitDispatchListener(action);
320
+ break;
321
+ }
322
+ }
323
+ },
324
+ {
325
+ // 触发事件
326
+ key: "emitDispatchListener",
327
+ value: function emitDispatchListener(action) {
328
+ var _this_dispatchListeners_type;
329
+ var name = this.config.name;
330
+ if (!action) return console.error("MicroAppMessage [".concat(name, "]: 未知消息类型 缺少action"));
331
+ var type = action.type, meta = action.meta;
332
+ if (!meta) return console.error("MicroAppMessage [".concat(name, "]: 未知消息类型 缺少meta"), action);
333
+ var source = meta.source, target = meta.target, transId = meta.transId;
334
+ if (source === name) return;
335
+ if (target !== "all" && target !== name) return;
336
+ if (!((_this_dispatchListeners_type = this.dispatchListeners[type]) === null || _this_dispatchListeners_type === void 0 ? void 0 : _this_dispatchListeners_type.length)) return;
337
+ var actionResponseType = "".concat(type, "___").concat(transId, "___response");
338
+ var _this = this;
339
+ this.dispatchListeners[type].forEach(function() {
340
+ var _ref = _async_to_generator(function(listener) {
341
+ var responseAction, error, isBroadcast, isResponse;
342
+ return _ts_generator(this, function(_state) {
343
+ switch(_state.label){
344
+ case 0:
345
+ responseAction = {
346
+ type: actionResponseType,
347
+ meta: {
348
+ source: name,
349
+ target: source,
350
+ transId: transId
351
+ }
352
+ };
353
+ _state.label = 1;
354
+ case 1:
355
+ _state.trys.push([
356
+ 1,
357
+ 3,
358
+ ,
359
+ 4
360
+ ]);
361
+ return [
362
+ 4,
363
+ listener(action)
364
+ ];
365
+ case 2:
366
+ responseAction.payload = _state.sent();
367
+ return [
368
+ 3,
369
+ 4
370
+ ];
371
+ case 3:
372
+ error = _state.sent();
373
+ if (responseAction.meta) responseAction.meta.error = error;
374
+ return [
375
+ 3,
376
+ 4
377
+ ];
378
+ case 4:
379
+ isBroadcast = target === "all";
380
+ isResponse = type.endsWith("___response");
381
+ if (isBroadcast || isResponse) return [
382
+ 2
383
+ ];
384
+ return [
385
+ 4,
386
+ _this.dispatch(source, responseAction)
387
+ ];
388
+ case 5:
389
+ _state.sent();
390
+ return [
391
+ 2
392
+ ];
393
+ }
394
+ });
395
+ });
396
+ return function(listener) {
397
+ return _ref.apply(this, arguments);
398
+ };
399
+ }());
400
+ }
401
+ },
402
+ {
403
+ key: "dispatch",
404
+ value: // 远程调用
405
+ function dispatch(target, action) {
406
+ var _this = this;
407
+ return _async_to_generator(function() {
408
+ var name, type, payload, meta, transId, newAction, globalData, isBroadcast, isResponse, actionResponseType, actionResponsePromise;
409
+ return _ts_generator(this, function(_state) {
410
+ name = _this.config.name;
411
+ type = action.type, payload = action.payload, meta = action.meta;
412
+ transId = (meta === null || meta === void 0 ? void 0 : meta.transId) || uuid();
413
+ newAction = {
414
+ type: type,
415
+ payload: payload,
416
+ meta: _object_spread({
417
+ source: name,
418
+ target: target,
419
+ transId: transId
420
+ }, meta)
421
+ };
422
+ globalData = {
423
+ eventType: "dispatchAction" /* DISPATCH_ACTION */ ,
424
+ action: newAction
425
+ };
426
+ if (!isProduction) console.log("MicroAppMessage [".concat(name, "]: dispatch ").concat(type, " to [").concat(target, "]"), globalData);
427
+ isBroadcast = target === "all";
428
+ isResponse = type.endsWith("___response");
429
+ if (isBroadcast || isResponse) return [
430
+ 2,
431
+ window.microGlobalEventCenter.forceSetGlobalData(globalData)
432
+ ];
433
+ actionResponseType = "".concat(type, "___").concat(transId, "___response");
434
+ actionResponsePromise = new Promise(function(resolve, reject) {
435
+ var listener = function(responseAction) {
436
+ var payload2 = responseAction.payload, meta2 = responseAction.meta;
437
+ (meta2 === null || meta2 === void 0 ? void 0 : meta2.error) ? reject(meta2.error) : resolve(payload2);
438
+ _this.removeDispatchListener(actionResponseType, listener);
439
+ };
440
+ _this.addDispatchListener(actionResponseType, listener);
441
+ });
442
+ window.microGlobalEventCenter.forceSetGlobalData(globalData);
443
+ return [
444
+ 2,
445
+ actionResponsePromise
446
+ ];
447
+ });
448
+ })();
449
+ }
450
+ },
451
+ {
452
+ // 添加监听
453
+ key: "addDispatchListener",
454
+ value: function addDispatchListener(type, listener) {
455
+ if (!this.dispatchListeners[type]) this.dispatchListeners[type] = [];
456
+ this.dispatchListeners[type].push(listener);
457
+ }
458
+ },
459
+ {
460
+ // 移除监听 如果不传listener则移除所有监听
461
+ key: "removeDispatchListener",
462
+ value: function removeDispatchListener(type, listener) {
463
+ if (!this.dispatchListeners[type]) return;
464
+ if (!listener) return delete this.dispatchListeners[type];
465
+ var index = this.dispatchListeners[type].indexOf(listener);
466
+ if (index !== -1) this.dispatchListeners[type].splice(index, 1);
467
+ }
468
+ }
469
+ ]);
470
+ return MicroAppEventCenter;
471
+ }();
472
+ var microApp2 = originMicroApp;
473
+ export { MicroAppEventCenter, MicroAppEventEnum, MicroAppRoute, microApp2 as microApp };
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@xfe-repo/web-micro",
3
+ "version": "1.1.3",
4
+ "sideEffects": false,
5
+ "module": "dist/index.js",
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
+ "files": [
20
+ "dist"
21
+ ],
22
+ "dependencies": {
23
+ "@micro-zoe/micro-app": "1.0.0-rc.13",
24
+ "@xfe-repo/web-router": "1.1.5",
25
+ "@xfe-repo/web-utils": "1.1.3"
26
+ },
27
+ "devDependencies": {
28
+ "@types/node": "^20.16.5",
29
+ "@types/react": "^18",
30
+ "@xfe-repo/eslint-config": "0.0.4",
31
+ "@xfe-repo/typescript-config": "0.0.6"
32
+ },
33
+ "scripts": {
34
+ "build": "tsup",
35
+ "dev": "tsup --watch",
36
+ "lint": "eslint \"src/**/*.ts*\"",
37
+ "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
38
+ }
39
+ }