@taicode/common-web 3.7.7 → 3.7.8
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/output/components/alert-dialog.d.ts +20 -0
- package/output/components/alert-dialog.d.ts.map +1 -0
- package/output/components/alert-dialog.js +65 -0
- package/output/components/alert.d.ts +11 -0
- package/output/components/alert.d.ts.map +1 -0
- package/output/components/alert.js +38 -0
- package/output/components/avatar.d.ts +13 -0
- package/output/components/avatar.d.ts.map +1 -0
- package/output/components/avatar.js +40 -0
- package/output/components/badge.d.ts +11 -0
- package/output/components/badge.d.ts.map +1 -0
- package/output/components/badge.js +39 -0
- package/output/components/button.d.ts +12 -0
- package/output/components/button.d.ts.map +1 -0
- package/output/components/button.js +48 -0
- package/output/components/calendar.d.ts +13 -0
- package/output/components/calendar.d.ts.map +1 -0
- package/output/components/calendar.js +57 -0
- package/output/components/card.d.ts +13 -0
- package/output/components/card.d.ts.map +1 -0
- package/output/components/card.js +43 -0
- package/output/components/chart.d.ts +56 -0
- package/output/components/chart.d.ts.map +1 -0
- package/output/components/chart.js +127 -0
- package/output/components/checkbox.d.ts +5 -0
- package/output/components/checkbox.d.ts.map +1 -0
- package/output/components/checkbox.js +21 -0
- package/output/components/command.d.ts +20 -0
- package/output/components/command.d.ts.map +1 -0
- package/output/components/command.js +55 -0
- package/output/components/dialog.d.ts +20 -0
- package/output/components/dialog.d.ts.map +1 -0
- package/output/components/dialog.js +58 -0
- package/output/components/direction.d.ts +9 -0
- package/output/components/direction.d.ts.map +1 -0
- package/output/components/direction.js +10 -0
- package/output/components/drawer.d.ts +14 -0
- package/output/components/drawer.d.ts.map +1 -0
- package/output/components/drawer.js +56 -0
- package/output/components/dropdown-menu.d.ts +29 -0
- package/output/components/dropdown-menu.d.ts.map +1 -0
- package/output/components/dropdown-menu.js +78 -0
- package/output/components/field.d.ts +39 -0
- package/output/components/field.d.ts.map +1 -0
- package/output/components/field.js +102 -0
- package/output/components/footer/index.d.ts +27 -0
- package/output/components/footer/index.d.ts.map +1 -0
- package/output/components/footer/index.js +17 -0
- package/output/components/form.d.ts +26 -0
- package/output/components/form.d.ts.map +1 -0
- package/output/components/form.js +69 -0
- package/output/components/index.d.ts +46 -0
- package/output/components/index.d.ts.map +1 -0
- package/output/components/index.js +45 -0
- package/output/components/input-group.d.ts +17 -0
- package/output/components/input-group.d.ts.map +1 -0
- package/output/components/input-group.js +75 -0
- package/output/components/input.d.ts +4 -0
- package/output/components/input.d.ts.map +1 -0
- package/output/components/input.js +19 -0
- package/output/components/item.d.ts +25 -0
- package/output/components/item.d.ts.map +1 -0
- package/output/components/item.js +89 -0
- package/output/components/json-view/index.d.ts +11 -0
- package/output/components/json-view/index.d.ts.map +1 -0
- package/output/components/json-view/index.js +52 -0
- package/output/components/json-view/lazy/index.d.ts +18 -0
- package/output/components/json-view/lazy/index.d.ts.map +1 -0
- package/output/components/json-view/lazy/index.js +22 -0
- package/output/components/kbd.d.ts +4 -0
- package/output/components/kbd.d.ts.map +1 -0
- package/output/components/kbd.js +22 -0
- package/output/components/label.d.ts +5 -0
- package/output/components/label.d.ts.map +1 -0
- package/output/components/label.js +20 -0
- package/output/components/list-actions/index.d.ts +23 -0
- package/output/components/list-actions/index.d.ts.map +1 -0
- package/output/components/list-actions/index.js +15 -0
- package/output/components/loading-button/index.d.ts +44 -0
- package/output/components/loading-button/index.d.ts.map +1 -0
- package/output/components/loading-button/index.js +75 -0
- package/output/components/loading-button/index.test.d.ts +2 -0
- package/output/components/loading-button/index.test.d.ts.map +1 -0
- package/output/components/loading-button/index.test.js +203 -0
- package/output/components/native-select.d.ts +9 -0
- package/output/components/native-select.d.ts.map +1 -0
- package/output/components/native-select.js +28 -0
- package/output/components/page-back-button/index.d.ts +8 -0
- package/output/components/page-back-button/index.d.ts.map +1 -0
- package/output/components/page-back-button/index.js +7 -0
- package/output/components/page-state.d.ts +19 -0
- package/output/components/page-state.d.ts.map +1 -0
- package/output/components/page-state.js +13 -0
- package/output/components/pagination/index.d.ts +34 -0
- package/output/components/pagination/index.d.ts.map +1 -0
- package/output/components/pagination/index.js +75 -0
- package/output/components/popover.d.ts +11 -0
- package/output/components/popover.d.ts.map +1 -0
- package/output/components/popover.js +44 -0
- package/output/components/progress.d.ts +5 -0
- package/output/components/progress.d.ts.map +1 -0
- package/output/components/progress.js +21 -0
- package/output/components/radio-group.d.ts +6 -0
- package/output/components/radio-group.d.ts.map +1 -0
- package/output/components/radio-group.js +25 -0
- package/output/components/route-pending-indicator/index.d.ts +20 -0
- package/output/components/route-pending-indicator/index.d.ts.map +1 -0
- package/output/components/route-pending-indicator/index.js +17 -0
- package/output/components/scroll-area.d.ts +6 -0
- package/output/components/scroll-area.d.ts.map +1 -0
- package/output/components/scroll-area.js +24 -0
- package/output/components/searchable-select/index.d.ts +41 -0
- package/output/components/searchable-select/index.d.ts.map +1 -0
- package/output/components/searchable-select/index.js +46 -0
- package/output/components/select.d.ts +17 -0
- package/output/components/select.d.ts.map +1 -0
- package/output/components/select.js +58 -0
- package/output/components/separator.d.ts +5 -0
- package/output/components/separator.d.ts.map +1 -0
- package/output/components/separator.js +20 -0
- package/output/components/sheet.d.ts +16 -0
- package/output/components/sheet.d.ts.map +1 -0
- package/output/components/sheet.js +58 -0
- package/output/components/sidebar.d.ts +78 -0
- package/output/components/sidebar.d.ts.map +1 -0
- package/output/components/sidebar.js +233 -0
- package/output/components/size-provider/index.d.ts +2 -0
- package/output/components/size-provider/index.d.ts.map +1 -0
- package/output/components/size-provider/index.js +1 -0
- package/output/components/size-provider/size-provider.d.ts +9 -0
- package/output/components/size-provider/size-provider.d.ts.map +1 -0
- package/output/components/size-provider/size-provider.js +29 -0
- package/output/components/skeleton.d.ts +3 -0
- package/output/components/skeleton.d.ts.map +1 -0
- package/output/components/skeleton.js +18 -0
- package/output/components/sonner.d.ts +8 -0
- package/output/components/sonner.d.ts.map +1 -0
- package/output/components/sonner.js +34 -0
- package/output/components/spinner.d.ts +3 -0
- package/output/components/spinner.d.ts.map +1 -0
- package/output/components/spinner.js +19 -0
- package/output/components/switch.d.ts +8 -0
- package/output/components/switch.d.ts.map +1 -0
- package/output/components/switch.js +20 -0
- package/output/components/table.d.ts +11 -0
- package/output/components/table.d.ts.map +1 -0
- package/output/components/table.js +47 -0
- package/output/components/tabs.d.ts +12 -0
- package/output/components/tabs.d.ts.map +1 -0
- package/output/components/tabs.js +45 -0
- package/output/components/textarea.d.ts +4 -0
- package/output/components/textarea.d.ts.map +1 -0
- package/output/components/textarea.js +19 -0
- package/output/components/theme-switcher.d.ts +6 -0
- package/output/components/theme-switcher.d.ts.map +1 -0
- package/output/components/theme-switcher.js +19 -0
- package/output/components/toaster/index.d.ts +16 -0
- package/output/components/toaster/index.d.ts.map +1 -0
- package/output/components/toaster/index.js +70 -0
- package/output/components/toggle-group.d.ts +12 -0
- package/output/components/toggle-group.d.ts.map +1 -0
- package/output/components/toggle-group.js +38 -0
- package/output/components/toggle.d.ts +10 -0
- package/output/components/toggle.d.ts.map +1 -0
- package/output/components/toggle.js +38 -0
- package/output/components/tooltip.d.ts +8 -0
- package/output/components/tooltip.d.ts.map +1 -0
- package/output/components/tooltip.js +33 -0
- package/output/features/confirm-dialog/index.d.ts +41 -0
- package/output/features/confirm-dialog/index.d.ts.map +1 -0
- package/output/features/confirm-dialog/index.js +19 -0
- package/output/features/input-confirm/index.d.ts +26 -0
- package/output/features/input-confirm/index.d.ts.map +1 -0
- package/output/features/input-confirm/index.js +59 -0
- package/output/features/signin/context.d.ts +14 -0
- package/output/features/signin/context.d.ts.map +1 -0
- package/output/features/signin/context.js +13 -0
- package/output/features/signin/index.d.ts +58 -0
- package/output/features/signin/index.d.ts.map +1 -0
- package/output/features/signin/index.js +137 -0
- package/output/features/signin/service.d.ts +92 -0
- package/output/features/signin/service.d.ts.map +1 -0
- package/output/features/signin/service.js +519 -0
- package/output/features/signin/service.test.d.ts +2 -0
- package/output/features/signin/service.test.d.ts.map +1 -0
- package/output/features/signin/service.test.js +211 -0
- package/output/features/signin/types.d.ts +63 -0
- package/output/features/signin/types.d.ts.map +1 -0
- package/output/features/signin/types.js +1 -0
- package/output/hooks/use-loading/index.d.ts +6 -0
- package/output/hooks/use-loading/index.d.ts.map +1 -0
- package/output/hooks/use-loading/index.js +14 -0
- package/output/hooks/use-loading/index.test.d.ts +2 -0
- package/output/hooks/use-loading/index.test.d.ts.map +1 -0
- package/output/hooks/use-loading/index.test.js +24 -0
- package/output/hooks/use-mobile/index.d.ts +2 -0
- package/output/hooks/use-mobile/index.d.ts.map +1 -0
- package/output/hooks/use-mobile/index.js +16 -0
- package/output/hooks/use-observer/index.d.ts +2 -0
- package/output/hooks/use-observer/index.d.ts.map +1 -0
- package/output/hooks/use-observer/index.js +1 -0
- package/output/hooks/use-observer/use-observer.d.ts +3 -0
- package/output/hooks/use-observer/use-observer.d.ts.map +1 -0
- package/output/hooks/use-observer/use-observer.js +16 -0
- package/output/hooks/use-observer/use-observer.test.d.ts +2 -0
- package/output/hooks/use-observer/use-observer.test.d.ts.map +1 -0
- package/output/hooks/use-observer/use-observer.test.js +134 -0
- package/output/hooks/use-offline/index.d.ts +6 -0
- package/output/hooks/use-offline/index.d.ts.map +1 -0
- package/output/hooks/use-offline/index.js +19 -0
- package/output/layout/app/index.d.ts +34 -0
- package/output/layout/app/index.d.ts.map +1 -0
- package/output/layout/app/index.js +34 -0
- package/output/layout/dashboard/index.d.ts +76 -0
- package/output/layout/dashboard/index.d.ts.map +1 -0
- package/output/layout/dashboard/index.js +84 -0
- package/output/layout/index.d.ts +3 -0
- package/output/layout/index.d.ts.map +1 -0
- package/output/layout/index.js +2 -0
- package/output/services/cache-api/cache-api.d.ts +23 -0
- package/output/services/cache-api/cache-api.d.ts.map +1 -0
- package/output/services/cache-api/cache-api.js +145 -0
- package/output/services/cache-api/cache-api.test.d.ts +2 -0
- package/output/services/cache-api/cache-api.test.d.ts.map +1 -0
- package/output/services/cache-api/cache-api.test.js +610 -0
- package/output/services/cache-api/index.d.ts +2 -0
- package/output/services/cache-api/index.d.ts.map +1 -0
- package/output/services/cache-api/index.js +1 -0
- package/output/services/cached-fetch/index.d.ts +50 -0
- package/output/services/cached-fetch/index.d.ts.map +1 -0
- package/output/services/cached-fetch/index.js +196 -0
- package/output/services/service/index.d.ts +2 -0
- package/output/services/service/index.d.ts.map +1 -0
- package/output/services/service/index.js +1 -0
- package/output/services/service/service.d.ts +135 -0
- package/output/services/service/service.d.ts.map +1 -0
- package/output/services/service/service.js +276 -0
- package/output/services/service/service.test.d.ts +2 -0
- package/output/services/service/service.test.d.ts.map +1 -0
- package/output/services/service/service.test.js +1399 -0
- package/output/services/side-cache/index.d.ts +2 -0
- package/output/services/side-cache/index.d.ts.map +1 -0
- package/output/services/side-cache/index.js +1 -0
- package/output/services/side-cache/side-cache.d.ts +11 -0
- package/output/services/side-cache/side-cache.d.ts.map +1 -0
- package/output/services/side-cache/side-cache.js +161 -0
- package/output/services/side-cache/side-cache.test.d.ts +2 -0
- package/output/services/side-cache/side-cache.test.d.ts.map +1 -0
- package/output/services/side-cache/side-cache.test.js +271 -0
- package/output/utils/avatar/index.d.ts +28 -0
- package/output/utils/avatar/index.d.ts.map +1 -0
- package/output/utils/avatar/index.js +35 -0
- package/output/utils/delay/index.d.ts +2 -0
- package/output/utils/delay/index.d.ts.map +1 -0
- package/output/utils/delay/index.js +5 -0
- package/output/utils/form/index.d.ts +3 -0
- package/output/utils/form/index.d.ts.map +1 -0
- package/output/utils/form/index.js +1 -0
- package/output/utils/merge-class-names/index.d.ts +3 -0
- package/output/utils/merge-class-names/index.d.ts.map +1 -0
- package/output/utils/merge-class-names/index.js +5 -0
- package/output/utils/merge-class-names/index.test.d.ts +2 -0
- package/output/utils/merge-class-names/index.test.d.ts.map +1 -0
- package/output/utils/merge-class-names/index.test.js +7 -0
- package/output/utils/performance/index.d.ts +82 -0
- package/output/utils/performance/index.d.ts.map +1 -0
- package/output/utils/performance/index.js +129 -0
- package/output/utils/redirect/index.d.ts +6 -0
- package/output/utils/redirect/index.d.ts.map +1 -0
- package/output/utils/redirect/index.js +32 -0
- package/package.json +2 -2
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
2
|
+
var useValue = arguments.length > 2;
|
|
3
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
4
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
5
|
+
}
|
|
6
|
+
return useValue ? value : void 0;
|
|
7
|
+
};
|
|
8
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
9
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
10
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
11
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
12
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
13
|
+
var _, done = false;
|
|
14
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
15
|
+
var context = {};
|
|
16
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
17
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
18
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
19
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
20
|
+
if (kind === "accessor") {
|
|
21
|
+
if (result === void 0) continue;
|
|
22
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
23
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
24
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
25
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
26
|
+
}
|
|
27
|
+
else if (_ = accept(result)) {
|
|
28
|
+
if (kind === "field") initializers.unshift(_);
|
|
29
|
+
else descriptor[key] = _;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
33
|
+
done = true;
|
|
34
|
+
};
|
|
35
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
36
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
37
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
38
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
39
|
+
};
|
|
40
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
41
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
42
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
43
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
44
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
45
|
+
};
|
|
46
|
+
import { action, observable, runInAction } from 'mobx';
|
|
47
|
+
import { Service } from '@taicode/common-base/service';
|
|
48
|
+
/**
|
|
49
|
+
* 带 TTL 缓存和 in-flight 请求去重的数据获取基类
|
|
50
|
+
*
|
|
51
|
+
* 子类通过实现 `fetch` 方法定义数据获取逻辑,基类负责:
|
|
52
|
+
* - 基于 cache key 的 TTL 缓存
|
|
53
|
+
* - 相同 key 的并发请求去重
|
|
54
|
+
* - observable 状态管理
|
|
55
|
+
*
|
|
56
|
+
* @typeParam TData - 缓存的数据类型
|
|
57
|
+
* @typeParam TParams - 查询参数类型(用于生成 cache key)
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* class UserService extends CachedFetchService<User[], QueryParams> {
|
|
62
|
+
* protected async fetch(params: QueryParams) {
|
|
63
|
+
* const result = await api.queryUsers(params)
|
|
64
|
+
* return result.data.list
|
|
65
|
+
* }
|
|
66
|
+
* }
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
let CachedFetchService = (() => {
|
|
70
|
+
var _a, _CachedFetchService_inited_accessor_storage, _CachedFetchService_loading_accessor_storage, _CachedFetchService_data_accessor_storage;
|
|
71
|
+
var _b;
|
|
72
|
+
let _classSuper = Service;
|
|
73
|
+
let _instanceExtraInitializers = [];
|
|
74
|
+
let _inited_decorators;
|
|
75
|
+
let _inited_initializers = [];
|
|
76
|
+
let _inited_extraInitializers = [];
|
|
77
|
+
let _loading_decorators;
|
|
78
|
+
let _loading_initializers = [];
|
|
79
|
+
let _loading_extraInitializers = [];
|
|
80
|
+
let _data_decorators;
|
|
81
|
+
let _data_initializers = [];
|
|
82
|
+
let _data_extraInitializers = [];
|
|
83
|
+
let _query_decorators;
|
|
84
|
+
let _clearCache_decorators;
|
|
85
|
+
return _a = class CachedFetchService extends _classSuper {
|
|
86
|
+
constructor() {
|
|
87
|
+
super(...arguments);
|
|
88
|
+
this.cacheTTLMs = (__runInitializers(this, _instanceExtraInitializers), 5000);
|
|
89
|
+
_CachedFetchService_inited_accessor_storage.set(this, __runInitializers(this, _inited_initializers, false));
|
|
90
|
+
_CachedFetchService_loading_accessor_storage.set(this, (__runInitializers(this, _inited_extraInitializers), __runInitializers(this, _loading_initializers, false)));
|
|
91
|
+
_CachedFetchService_data_accessor_storage.set(this, (__runInitializers(this, _loading_extraInitializers), __runInitializers(this, _data_initializers, void 0)));
|
|
92
|
+
this.cache = (__runInitializers(this, _data_extraInitializers), new Map());
|
|
93
|
+
this.inflight = new Map();
|
|
94
|
+
this.lastKey = null;
|
|
95
|
+
}
|
|
96
|
+
get inited() { return __classPrivateFieldGet(this, _CachedFetchService_inited_accessor_storage, "f"); }
|
|
97
|
+
set inited(value) { __classPrivateFieldSet(this, _CachedFetchService_inited_accessor_storage, value, "f"); }
|
|
98
|
+
get loading() { return __classPrivateFieldGet(this, _CachedFetchService_loading_accessor_storage, "f"); }
|
|
99
|
+
set loading(value) { __classPrivateFieldSet(this, _CachedFetchService_loading_accessor_storage, value, "f"); }
|
|
100
|
+
get data() { return __classPrivateFieldGet(this, _CachedFetchService_data_accessor_storage, "f"); }
|
|
101
|
+
set data(value) { __classPrivateFieldSet(this, _CachedFetchService_data_accessor_storage, value, "f"); }
|
|
102
|
+
/**
|
|
103
|
+
* 生成缓存 key,默认使用 JSON.stringify
|
|
104
|
+
*/
|
|
105
|
+
cacheKey(params) {
|
|
106
|
+
var _b;
|
|
107
|
+
return (_b = JSON.stringify(params)) !== null && _b !== void 0 ? _b : 'undefined';
|
|
108
|
+
}
|
|
109
|
+
now() {
|
|
110
|
+
return Date.now();
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* 执行查询(带缓存和去重)
|
|
114
|
+
*/
|
|
115
|
+
async query(params) {
|
|
116
|
+
const key = this.cacheKey(params);
|
|
117
|
+
// 相同 key 且缓存有效
|
|
118
|
+
if (this.lastKey === key) {
|
|
119
|
+
const cached = this.cache.get(key);
|
|
120
|
+
if (cached && cached.expiresAt > this.now()) {
|
|
121
|
+
runInAction(() => {
|
|
122
|
+
this.data = cached.data;
|
|
123
|
+
});
|
|
124
|
+
return cached.data;
|
|
125
|
+
}
|
|
126
|
+
// 相同 key 的请求正在进行中
|
|
127
|
+
const pending = this.inflight.get(key);
|
|
128
|
+
if (pending) {
|
|
129
|
+
const result = await pending;
|
|
130
|
+
runInAction(() => {
|
|
131
|
+
this.data = result;
|
|
132
|
+
});
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
this.lastKey = key;
|
|
137
|
+
runInAction(() => {
|
|
138
|
+
this.loading = true;
|
|
139
|
+
});
|
|
140
|
+
const request = this.fetch(params)
|
|
141
|
+
.then((result) => {
|
|
142
|
+
const cached = {
|
|
143
|
+
data: result,
|
|
144
|
+
expiresAt: this.now() + this.cacheTTLMs,
|
|
145
|
+
};
|
|
146
|
+
this.cache.set(key, cached);
|
|
147
|
+
runInAction(() => {
|
|
148
|
+
this.data = result;
|
|
149
|
+
});
|
|
150
|
+
return result;
|
|
151
|
+
})
|
|
152
|
+
.finally(() => {
|
|
153
|
+
this.inflight.delete(key);
|
|
154
|
+
runInAction(() => {
|
|
155
|
+
this.loading = false;
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
this.inflight.set(key, request);
|
|
159
|
+
return request;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* 清除缓存
|
|
163
|
+
*/
|
|
164
|
+
clearCache() {
|
|
165
|
+
this.cache.clear();
|
|
166
|
+
this.inflight.clear();
|
|
167
|
+
this.lastKey = null;
|
|
168
|
+
}
|
|
169
|
+
async init() {
|
|
170
|
+
runInAction(() => {
|
|
171
|
+
this.inited = true;
|
|
172
|
+
});
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
_CachedFetchService_inited_accessor_storage = new WeakMap(),
|
|
177
|
+
_CachedFetchService_loading_accessor_storage = new WeakMap(),
|
|
178
|
+
_CachedFetchService_data_accessor_storage = new WeakMap(),
|
|
179
|
+
(() => {
|
|
180
|
+
var _b;
|
|
181
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create((_b = _classSuper[Symbol.metadata]) !== null && _b !== void 0 ? _b : null) : void 0;
|
|
182
|
+
_inited_decorators = [observable];
|
|
183
|
+
_loading_decorators = [observable];
|
|
184
|
+
_data_decorators = [(_b = observable).ref.bind(_b)];
|
|
185
|
+
_query_decorators = [action];
|
|
186
|
+
_clearCache_decorators = [action];
|
|
187
|
+
__esDecorate(_a, null, _inited_decorators, { kind: "accessor", name: "inited", static: false, private: false, access: { has: obj => "inited" in obj, get: obj => obj.inited, set: (obj, value) => { obj.inited = value; } }, metadata: _metadata }, _inited_initializers, _inited_extraInitializers);
|
|
188
|
+
__esDecorate(_a, null, _loading_decorators, { kind: "accessor", name: "loading", static: false, private: false, access: { has: obj => "loading" in obj, get: obj => obj.loading, set: (obj, value) => { obj.loading = value; } }, metadata: _metadata }, _loading_initializers, _loading_extraInitializers);
|
|
189
|
+
__esDecorate(_a, null, _data_decorators, { kind: "accessor", name: "data", static: false, private: false, access: { has: obj => "data" in obj, get: obj => obj.data, set: (obj, value) => { obj.data = value; } }, metadata: _metadata }, _data_initializers, _data_extraInitializers);
|
|
190
|
+
__esDecorate(_a, null, _query_decorators, { kind: "method", name: "query", static: false, private: false, access: { has: obj => "query" in obj, get: obj => obj.query }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
191
|
+
__esDecorate(_a, null, _clearCache_decorators, { kind: "method", name: "clearCache", static: false, private: false, access: { has: obj => "clearCache" in obj, get: obj => obj.clearCache }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
192
|
+
if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
193
|
+
})(),
|
|
194
|
+
_a;
|
|
195
|
+
})();
|
|
196
|
+
export { CachedFetchService };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../source/services/service/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useService, useLocalService, ServiceProvider } from './service.js';
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React 服务层集成工具
|
|
3
|
+
*
|
|
4
|
+
* 本模块提供了在 React 应用中集成服务和状态管理(Mobx)的工具函数和组件。
|
|
5
|
+
* 主要功能包括:
|
|
6
|
+
* - 通过 useService Hook 使用全局注册的服务,并自动响应状态变化
|
|
7
|
+
* - 通过 useLocalService Hook 创建局部服务实例,避免全局状态污染
|
|
8
|
+
* - 通过 ServiceProvider 组件提供服务注册表上下文
|
|
9
|
+
* - 自动初始化服务实例
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```tsx
|
|
13
|
+
* // 1. 定义服务
|
|
14
|
+
* class UserService extends Service {
|
|
15
|
+
* @observable users: User[] = []
|
|
16
|
+
* async init() { return true }
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* // 2. 在根组件提供服务
|
|
20
|
+
* function App() {
|
|
21
|
+
* return (
|
|
22
|
+
* <ServiceProvider services={[UserService]}>
|
|
23
|
+
* <UserList />
|
|
24
|
+
* </ServiceProvider>
|
|
25
|
+
* )
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* // 3. 在组件中使用服务
|
|
29
|
+
* function UserList() {
|
|
30
|
+
* const users = useService(UserService, s => s.users)
|
|
31
|
+
* return <div>{users.map(user => <div key={user.id}>{user.name}</div>)}</div>
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
import React from 'react';
|
|
36
|
+
/**
|
|
37
|
+
* 服务类构造函数类型
|
|
38
|
+
*/
|
|
39
|
+
type ServiceClass<T = unknown> = new (...args: any[]) => T;
|
|
40
|
+
/**
|
|
41
|
+
* 服务提供者条目:
|
|
42
|
+
* - 服务类:由 ServiceProvider 惰性实例化为单例
|
|
43
|
+
* - 值注入:{ provide, useValue } 直接复用已有实例
|
|
44
|
+
*/
|
|
45
|
+
type ServiceEntry<T = unknown> = ServiceClass<T> | {
|
|
46
|
+
provide: ServiceClass<T>;
|
|
47
|
+
useValue: T;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* 轻量级服务注册表,替代 DI 容器。
|
|
51
|
+
* 以服务类为键,按需创建并缓存单例实例,支持父子级联查找。
|
|
52
|
+
*/
|
|
53
|
+
declare class ServiceRegistry {
|
|
54
|
+
private readonly values;
|
|
55
|
+
private readonly singletons;
|
|
56
|
+
private readonly parent?;
|
|
57
|
+
constructor(parent?: ServiceRegistry);
|
|
58
|
+
/** 注册值注入的服务实例 */
|
|
59
|
+
setValue(provide: ServiceClass, value: unknown): void;
|
|
60
|
+
/** 在当前及父级注册表中查找已存在的实例 */
|
|
61
|
+
private find;
|
|
62
|
+
/** 获取服务实例;若不存在则惰性创建并缓存单例 */
|
|
63
|
+
get<T>(ServiceClass: ServiceClass<T>): T;
|
|
64
|
+
/** 创建全新的局部实例(不受父级影响),并缓存到当前注册表 */
|
|
65
|
+
createLocal<T>(ServiceClass: ServiceClass<T>): T;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* 使用服务 Hook
|
|
69
|
+
*
|
|
70
|
+
* 从服务注册表中获取指定的服务实例,支持可选的 selector 函数来选择需要的数据。
|
|
71
|
+
* 当选择的数据发生变化时,组件会自动重新渲染。
|
|
72
|
+
*
|
|
73
|
+
* 特性:
|
|
74
|
+
* - 从注册表获取单例服务实例
|
|
75
|
+
* - 支持 Mobx 响应式更新
|
|
76
|
+
* - 支持可选 selector,不传时监听整个服务的所有变化
|
|
77
|
+
*
|
|
78
|
+
* @template T 服务类型
|
|
79
|
+
* @template U 选择器返回的数据类型
|
|
80
|
+
* @param target 服务类,用于从注册表中获取服务实例
|
|
81
|
+
* @param selector 可选的选择器函数。如果不传入,返回服务实例并监听所有 observable 属性变化
|
|
82
|
+
* @returns 如果传入了 selector,返回选择器的结果;否则返回服务实例
|
|
83
|
+
*
|
|
84
|
+
* @throws {Error} 当组件不在 ServiceProvider 内部时抛出错误
|
|
85
|
+
*/
|
|
86
|
+
export declare function useService<T>(target: ServiceClass<T>): T;
|
|
87
|
+
export declare function useService<T, U>(target: ServiceClass<T>, selector: (service: T) => U): U;
|
|
88
|
+
/**
|
|
89
|
+
* 使用局部服务实例 Hook
|
|
90
|
+
*
|
|
91
|
+
* 创建指定服务的局部实例,避免全局状态污染。
|
|
92
|
+
* 局部实例在 Hook 的整个生命周期内保持引用稳定,支持自动初始化和响应式更新。
|
|
93
|
+
*
|
|
94
|
+
* 特性:
|
|
95
|
+
* - 创建局部服务实例,避免全局状态污染
|
|
96
|
+
* - 在组件重渲染时保持实例引用稳定
|
|
97
|
+
* - 自动初始化实现了 Service 接口的服务
|
|
98
|
+
* - 支持 Mobx 响应式更新
|
|
99
|
+
* - 支持可选 selector,不传时监听整个服务的所有变化
|
|
100
|
+
*
|
|
101
|
+
* @template T 服务类型
|
|
102
|
+
* @template U 选择器返回的数据类型
|
|
103
|
+
* @param ServiceClass 服务构造函数/类
|
|
104
|
+
* @param selector 可选的选择器函数。如果不传入,返回服务实例并监听所有 observable 属性变化
|
|
105
|
+
* @returns 如果传入了 selector,返回选择器的结果;否则返回服务实例
|
|
106
|
+
*
|
|
107
|
+
* @throws {Error} 当组件不在 ServiceProvider 内部时抛出错误
|
|
108
|
+
*/
|
|
109
|
+
export declare function useLocalService<T>(ServiceClass: ServiceClass<T>): T;
|
|
110
|
+
export declare function useLocalService<T, U>(ServiceClass: ServiceClass<T>, selector: (service: T) => U): U;
|
|
111
|
+
/**
|
|
112
|
+
* ServiceProvider 组件的属性接口
|
|
113
|
+
*/
|
|
114
|
+
interface ServiceProviderProps {
|
|
115
|
+
/**
|
|
116
|
+
* 要注册到注册表中的服务条目
|
|
117
|
+
* 支持两种方式:
|
|
118
|
+
* 1. 类注入:直接传入服务类,ServiceProvider 会惰性实例化并自动初始化
|
|
119
|
+
* 2. 值注入:传入 { provide, useValue },直接复用已有实例
|
|
120
|
+
*/
|
|
121
|
+
services: ServiceEntry[];
|
|
122
|
+
/** 子组件 */
|
|
123
|
+
children: React.ReactNode;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* 服务提供者组件
|
|
127
|
+
*
|
|
128
|
+
* 为其子组件树提供服务注册表上下文,自动复用父级注册表并创建子注册表。
|
|
129
|
+
* 支持容器层级结构,子注册表可以覆盖父级的服务。
|
|
130
|
+
*
|
|
131
|
+
* @param props 组件属性
|
|
132
|
+
*/
|
|
133
|
+
export declare function ServiceProvider(props: ServiceProviderProps): React.FunctionComponentElement<React.ProviderProps<ServiceRegistry | undefined>>;
|
|
134
|
+
export {};
|
|
135
|
+
//# sourceMappingURL=service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../../../source/services/service/service.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAIH,OAAO,KAA0E,MAAM,OAAO,CAAA;AAE9F;;GAEG;AACH,KAAK,YAAY,CAAC,CAAC,GAAG,OAAO,IAAI,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAA;AAE1D;;;;GAIG;AACH,KAAK,YAAY,CAAC,CAAC,GAAG,OAAO,IAAI,YAAY,CAAC,CAAC,CAAC,GAAG;IAAE,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC;IAAC,QAAQ,EAAE,CAAC,CAAA;CAAE,CAAA;AAW5F;;;GAGG;AACH,cAAM,eAAe;IACnB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAmC;IAC1D,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAmC;IAC9D,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAiB;gBAE7B,MAAM,CAAC,EAAE,eAAe;IAIpC,iBAAiB;IACjB,QAAQ,CAAC,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE,OAAO;IAI9C,yBAAyB;IACzB,OAAO,CAAC,IAAI;IAQZ,4BAA4B;IAC5B,GAAG,CAAC,CAAC,EAAE,YAAY,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC;IAQxC,kCAAkC;IAClC,WAAW,CAAC,CAAC,EAAE,YAAY,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC;CAKjD;AAsDD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;AACzD,wBAAgB,UAAU,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,OAAO,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;AAsCzF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAAE,YAAY,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;AACpE,wBAAgB,eAAe,CAAC,CAAC,EAAE,CAAC,EAAE,YAAY,EAAE,YAAY,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,OAAO,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;AAqEpG;;GAEG;AACH,UAAU,oBAAoB;IAC5B;;;;;OAKG;IACH,QAAQ,EAAE,YAAY,EAAE,CAAA;IACxB,UAAU;IACV,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAA;CAC1B;AAED;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,oBAAoB,oFAyB1D"}
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React 服务层集成工具
|
|
3
|
+
*
|
|
4
|
+
* 本模块提供了在 React 应用中集成服务和状态管理(Mobx)的工具函数和组件。
|
|
5
|
+
* 主要功能包括:
|
|
6
|
+
* - 通过 useService Hook 使用全局注册的服务,并自动响应状态变化
|
|
7
|
+
* - 通过 useLocalService Hook 创建局部服务实例,避免全局状态污染
|
|
8
|
+
* - 通过 ServiceProvider 组件提供服务注册表上下文
|
|
9
|
+
* - 自动初始化服务实例
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```tsx
|
|
13
|
+
* // 1. 定义服务
|
|
14
|
+
* class UserService extends Service {
|
|
15
|
+
* @observable users: User[] = []
|
|
16
|
+
* async init() { return true }
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* // 2. 在根组件提供服务
|
|
20
|
+
* function App() {
|
|
21
|
+
* return (
|
|
22
|
+
* <ServiceProvider services={[UserService]}>
|
|
23
|
+
* <UserList />
|
|
24
|
+
* </ServiceProvider>
|
|
25
|
+
* )
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* // 3. 在组件中使用服务
|
|
29
|
+
* function UserList() {
|
|
30
|
+
* const users = useService(UserService, s => s.users)
|
|
31
|
+
* return <div>{users.map(user => <div key={user.id}>{user.name}</div>)}</div>
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
import { reaction, runInAction } from 'mobx';
|
|
36
|
+
import { Service } from '@taicode/common-base';
|
|
37
|
+
import React, { useContext, useEffect, useMemo, useState, useRef, createElement } from 'react';
|
|
38
|
+
const INIT_RETURN_ERROR_MESSAGE = 'Service init must return a boolean value.';
|
|
39
|
+
class InvalidInitReturnError extends Error {
|
|
40
|
+
constructor() {
|
|
41
|
+
super(INIT_RETURN_ERROR_MESSAGE);
|
|
42
|
+
this.name = 'InvalidInitReturnError';
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* 轻量级服务注册表,替代 DI 容器。
|
|
47
|
+
* 以服务类为键,按需创建并缓存单例实例,支持父子级联查找。
|
|
48
|
+
*/
|
|
49
|
+
class ServiceRegistry {
|
|
50
|
+
constructor(parent) {
|
|
51
|
+
this.values = new Map();
|
|
52
|
+
this.singletons = new Map();
|
|
53
|
+
this.parent = parent;
|
|
54
|
+
}
|
|
55
|
+
/** 注册值注入的服务实例 */
|
|
56
|
+
setValue(provide, value) {
|
|
57
|
+
this.values.set(provide, value);
|
|
58
|
+
}
|
|
59
|
+
/** 在当前及父级注册表中查找已存在的实例 */
|
|
60
|
+
find(provide) {
|
|
61
|
+
var _a;
|
|
62
|
+
const value = this.values.get(provide);
|
|
63
|
+
if (value !== undefined)
|
|
64
|
+
return value;
|
|
65
|
+
const singleton = this.singletons.get(provide);
|
|
66
|
+
if (singleton !== undefined)
|
|
67
|
+
return singleton;
|
|
68
|
+
return (_a = this.parent) === null || _a === void 0 ? void 0 : _a.find(provide);
|
|
69
|
+
}
|
|
70
|
+
/** 获取服务实例;若不存在则惰性创建并缓存单例 */
|
|
71
|
+
get(ServiceClass) {
|
|
72
|
+
const found = this.find(ServiceClass);
|
|
73
|
+
if (found !== undefined)
|
|
74
|
+
return found;
|
|
75
|
+
const instance = new ServiceClass();
|
|
76
|
+
this.singletons.set(ServiceClass, instance);
|
|
77
|
+
return instance;
|
|
78
|
+
}
|
|
79
|
+
/** 创建全新的局部实例(不受父级影响),并缓存到当前注册表 */
|
|
80
|
+
createLocal(ServiceClass) {
|
|
81
|
+
const instance = new ServiceClass();
|
|
82
|
+
this.singletons.set(ServiceClass, instance);
|
|
83
|
+
return instance;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* React Context 用于传递服务注册表实例
|
|
88
|
+
* 通过上下文在组件树中共享服务实例
|
|
89
|
+
*/
|
|
90
|
+
const ctx = React.createContext(undefined);
|
|
91
|
+
function setServiceInited(target, value) {
|
|
92
|
+
runInAction(() => {
|
|
93
|
+
if (target != null && typeof target === 'object') {
|
|
94
|
+
target.inited = value;
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
function applyInitResult(target, result) {
|
|
99
|
+
if (typeof result === 'boolean') {
|
|
100
|
+
setServiceInited(target, result);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
setServiceInited(target, false);
|
|
104
|
+
throw new InvalidInitReturnError();
|
|
105
|
+
}
|
|
106
|
+
async function initializeServiceInstance(target) {
|
|
107
|
+
if (target == null ||
|
|
108
|
+
typeof target !== 'object' ||
|
|
109
|
+
!('init' in target) ||
|
|
110
|
+
typeof target.init !== 'function') {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const service = target;
|
|
114
|
+
if (service.inited === true) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
const result = await service.init();
|
|
119
|
+
applyInitResult(service, result);
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
if (error instanceof InvalidInitReturnError) {
|
|
123
|
+
throw error;
|
|
124
|
+
}
|
|
125
|
+
setServiceInited(service, false);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
export function useService(target, selector) {
|
|
129
|
+
const registry = useContext(ctx);
|
|
130
|
+
const [, refresh] = useState({});
|
|
131
|
+
useEffect(() => {
|
|
132
|
+
if (registry == null)
|
|
133
|
+
return;
|
|
134
|
+
const service = registry.get(target);
|
|
135
|
+
const dispose = reaction(() => {
|
|
136
|
+
if (!selector) {
|
|
137
|
+
return service;
|
|
138
|
+
}
|
|
139
|
+
const result = selector(service);
|
|
140
|
+
if (result === service) {
|
|
141
|
+
return service;
|
|
142
|
+
}
|
|
143
|
+
return result;
|
|
144
|
+
}, () => refresh({}));
|
|
145
|
+
return dispose;
|
|
146
|
+
}, [registry, selector, target]);
|
|
147
|
+
if (registry == null) {
|
|
148
|
+
throw new Error('Must be a child of ServiceProvider.');
|
|
149
|
+
}
|
|
150
|
+
const service = registry.get(target);
|
|
151
|
+
return selector ? selector(service) : service;
|
|
152
|
+
}
|
|
153
|
+
export function useLocalService(ServiceClass, selector) {
|
|
154
|
+
const parentRegistry = useContext(ctx);
|
|
155
|
+
const serviceInstanceRef = useRef(null);
|
|
156
|
+
const [, refresh] = useState({});
|
|
157
|
+
const [initError, setInitError] = useState(null);
|
|
158
|
+
if (parentRegistry == null) {
|
|
159
|
+
throw new Error('Must be a child of ServiceProvider.');
|
|
160
|
+
}
|
|
161
|
+
if (serviceInstanceRef.current == null) {
|
|
162
|
+
const localRegistry = new ServiceRegistry(parentRegistry);
|
|
163
|
+
serviceInstanceRef.current = localRegistry.createLocal(ServiceClass);
|
|
164
|
+
}
|
|
165
|
+
useEffect(() => {
|
|
166
|
+
if (parentRegistry == null)
|
|
167
|
+
return;
|
|
168
|
+
const service = serviceInstanceRef.current;
|
|
169
|
+
const dispose = reaction(() => {
|
|
170
|
+
if (!selector) {
|
|
171
|
+
return service;
|
|
172
|
+
}
|
|
173
|
+
const result = selector(service);
|
|
174
|
+
if (result === service) {
|
|
175
|
+
return service;
|
|
176
|
+
}
|
|
177
|
+
return result;
|
|
178
|
+
}, () => refresh({}));
|
|
179
|
+
return dispose;
|
|
180
|
+
}, [parentRegistry, selector, ServiceClass]);
|
|
181
|
+
useEffect(() => {
|
|
182
|
+
if (parentRegistry == null)
|
|
183
|
+
return;
|
|
184
|
+
let disposed = false;
|
|
185
|
+
setInitError(prev => (prev === null ? prev : null));
|
|
186
|
+
const service = serviceInstanceRef.current;
|
|
187
|
+
initializeServiceInstance(service).catch((error) => {
|
|
188
|
+
if (error instanceof InvalidInitReturnError && !disposed) {
|
|
189
|
+
setInitError(error);
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
return () => {
|
|
193
|
+
disposed = true;
|
|
194
|
+
};
|
|
195
|
+
}, [parentRegistry, ServiceClass]);
|
|
196
|
+
if (initError) {
|
|
197
|
+
throw initError;
|
|
198
|
+
}
|
|
199
|
+
return selector ? selector(serviceInstanceRef.current) : serviceInstanceRef.current;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* 服务提供者组件
|
|
203
|
+
*
|
|
204
|
+
* 为其子组件树提供服务注册表上下文,自动复用父级注册表并创建子注册表。
|
|
205
|
+
* 支持容器层级结构,子注册表可以覆盖父级的服务。
|
|
206
|
+
*
|
|
207
|
+
* @param props 组件属性
|
|
208
|
+
*/
|
|
209
|
+
export function ServiceProvider(props) {
|
|
210
|
+
const parentRegistry = useContext(ctx);
|
|
211
|
+
const { children, services } = props;
|
|
212
|
+
const registry = useMemo(() => {
|
|
213
|
+
const currentRegistry = new ServiceRegistry(parentRegistry);
|
|
214
|
+
services.forEach(service => {
|
|
215
|
+
if (service != null && typeof service === 'object' && 'provide' in service && 'useValue' in service) {
|
|
216
|
+
currentRegistry.setValue(service.provide, service.useValue);
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
return currentRegistry;
|
|
220
|
+
}, [parentRegistry, services]);
|
|
221
|
+
const init = createElement(InitService, { services, children });
|
|
222
|
+
return createElement(ctx.Provider, { value: registry, children: registry ? init : null });
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* 服务初始化组件
|
|
226
|
+
*
|
|
227
|
+
* 在组件挂载时自动初始化实现了 Service 接口的类注入服务实例。
|
|
228
|
+
*
|
|
229
|
+
* 初始化流程:
|
|
230
|
+
* 1. 跳过值注入的服务(它们应该在传入前已经初始化)
|
|
231
|
+
* 2. 从注册表获取服务实例(仅类注入的服务)
|
|
232
|
+
* 3. 检查服务是否实现了 Service 接口(有 init 方法)
|
|
233
|
+
* 4. 检查服务是否已经初始化(inited 属性)
|
|
234
|
+
* 5. 如果未初始化,则调用 init 方法并更新状态
|
|
235
|
+
*
|
|
236
|
+
* @param props 组件属性,与 ServiceProviderProps 相同
|
|
237
|
+
*/
|
|
238
|
+
function InitService(props) {
|
|
239
|
+
const { services, children } = props;
|
|
240
|
+
const registry = useContext(ctx);
|
|
241
|
+
const [initError, setInitError] = useState(null);
|
|
242
|
+
useEffect(() => {
|
|
243
|
+
if (registry == null)
|
|
244
|
+
return;
|
|
245
|
+
if (services.length == 0)
|
|
246
|
+
return;
|
|
247
|
+
let disposed = false;
|
|
248
|
+
setInitError(prev => (prev === null ? prev : null));
|
|
249
|
+
const initPromises = services.map(async (service) => {
|
|
250
|
+
if (service != null && typeof service === 'object' && 'provide' in service && 'useValue' in service) {
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
const instance = registry.get(service);
|
|
254
|
+
if (instance != null && typeof instance === 'object') {
|
|
255
|
+
const serviceInstance = instance;
|
|
256
|
+
if (typeof serviceInstance.init === 'function') {
|
|
257
|
+
if (serviceInstance.inited !== true) {
|
|
258
|
+
await initializeServiceInstance(serviceInstance).catch((error) => {
|
|
259
|
+
if (error instanceof InvalidInitReturnError && !disposed) {
|
|
260
|
+
setInitError(error);
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
void Promise.all(initPromises);
|
|
268
|
+
return () => {
|
|
269
|
+
disposed = true;
|
|
270
|
+
};
|
|
271
|
+
}, [registry, services]);
|
|
272
|
+
if (initError) {
|
|
273
|
+
throw initError;
|
|
274
|
+
}
|
|
275
|
+
return children;
|
|
276
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.test.d.ts","sourceRoot":"","sources":["../../../source/services/service/service.test.tsx"],"names":[],"mappings":""}
|