@xfe-repo/web-register 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/dist/index.d.mts +62 -0
- package/dist/index.d.ts +62 -0
- package/dist/index.js +676 -0
- package/dist/index.mjs +140 -0
- package/package.json +38 -0
package/README.md
ADDED
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { ComponentType } from 'react';
|
|
2
|
+
|
|
3
|
+
declare class ContextStorageClass<ExtendContext = null> implements ContextStorage<ExtendContext> {
|
|
4
|
+
constructor(context: RegisterContext<ExtendContext>);
|
|
5
|
+
private storage;
|
|
6
|
+
get context(): RegisterContext<ExtendContext>;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
declare const createRegister: <ExtendConfig = null, ExtendContext = null, P = any>(options?: CreateOptions<ExtendConfig, ExtendContext, P> | undefined) => Register<ExtendConfig, ExtendContext, P>;
|
|
10
|
+
declare const register: Register<null, null, any>;
|
|
11
|
+
|
|
12
|
+
type CreateOptions<ExtendConfig = null, ExtendContext = null, P = any> = {
|
|
13
|
+
plugins?: RegisterPlugin<ExtendConfig, ExtendContext>[];
|
|
14
|
+
onError?: Function;
|
|
15
|
+
LoadFullback?: ComponentType<any> | null;
|
|
16
|
+
ErrorFullback?: ComponentType<any> | null;
|
|
17
|
+
renderTitle?: (name: string, context: RegisterContext<ExtendContext, P>) => string;
|
|
18
|
+
};
|
|
19
|
+
type ORegisterConfig = {
|
|
20
|
+
name: string;
|
|
21
|
+
onLoad?: (context: RegisterContext) => Promise<any>;
|
|
22
|
+
onLeave?: (context: ORegisterContext) => Promise<boolean>;
|
|
23
|
+
};
|
|
24
|
+
type ORegisterContext<P = {}, Q = {}, R = {}> = {
|
|
25
|
+
payload: Readonly<P>;
|
|
26
|
+
query: Q;
|
|
27
|
+
result?: R;
|
|
28
|
+
error?: Error;
|
|
29
|
+
isPreload?: boolean;
|
|
30
|
+
};
|
|
31
|
+
type RegisterConfig<ExtendConfig = null> = ExtendConfig extends null ? ORegisterConfig & CreateOptions : ORegisterConfig & CreateOptions & ExtendConfig;
|
|
32
|
+
type RegisterContext<ExtendContext = null, P = any, Q = any, R = any> = ExtendContext extends null ? ORegisterContext<P, Q, R> : ORegisterContext<P, Q, R> & ExtendContext;
|
|
33
|
+
type LoadParams<Q = any> = {
|
|
34
|
+
isPreload?: boolean;
|
|
35
|
+
query: Q;
|
|
36
|
+
};
|
|
37
|
+
type RegisterComponentStatic<ExtendContext = null, P = any> = {
|
|
38
|
+
config?: RegisterConfig;
|
|
39
|
+
emitOnLoad?: <Q, R>(params?: LoadParams<Q>) => Promise<RegisterContext<ExtendContext, P, Q, R>>;
|
|
40
|
+
emitOnLeave?: () => Promise<boolean>;
|
|
41
|
+
};
|
|
42
|
+
type RegisterComponentType<P = any> = ComponentType<P> & RegisterComponentStatic;
|
|
43
|
+
type RegisterPlugin<ExtendConfig = null, ExtendContext = null> = {
|
|
44
|
+
registerHook?: (config: RegisterConfig<ExtendConfig>, WrappedComponent: RegisterComponentType) => ComponentType<any> | void;
|
|
45
|
+
loadHook?: (config: RegisterConfig<ExtendConfig>, context: RegisterContext<ExtendContext>) => Promise<any>;
|
|
46
|
+
leaveHook?: (config: RegisterConfig<ExtendConfig>, context: RegisterContext<ExtendContext>) => Promise<boolean>;
|
|
47
|
+
};
|
|
48
|
+
type ContextStorage<ExtendContext = null, P = any, Q = any, R = any> = {
|
|
49
|
+
context: RegisterContext<ExtendContext, P, Q, R>;
|
|
50
|
+
};
|
|
51
|
+
type SetContextStorage<ExtendContext = null, P = any> = <Q = any, R = any>(contextStorage: ContextStorage<ExtendContext, P, Q, R>) => void;
|
|
52
|
+
type GetContext<ExtendContext = null, P = any> = <Q = any, R = any>() => RegisterContext<ExtendContext, P, Q, R>;
|
|
53
|
+
interface Register<ExtendConfig = null, ExtendContext = null, P = any> {
|
|
54
|
+
(config: RegisterConfig<ExtendConfig>): (WrappedComponent: any) => any;
|
|
55
|
+
setContextStorage: SetContextStorage<ExtendContext, P>;
|
|
56
|
+
getContext: GetContext<ExtendContext, P>;
|
|
57
|
+
}
|
|
58
|
+
type WithRegisterContext<ExtendContext = null, P = any, Q = any, R = any> = {
|
|
59
|
+
context: RegisterContext<ExtendContext, P, Q, R>;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export { type ContextStorage, ContextStorageClass, type CreateOptions, type GetContext, type LoadParams, type ORegisterConfig, type ORegisterContext, type Register, type RegisterComponentStatic, type RegisterComponentType, type RegisterConfig, type RegisterContext, type RegisterPlugin, type SetContextStorage, type WithRegisterContext, createRegister, register as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { ComponentType } from 'react';
|
|
2
|
+
|
|
3
|
+
declare class ContextStorageClass<ExtendContext = null> implements ContextStorage<ExtendContext> {
|
|
4
|
+
constructor(context: RegisterContext<ExtendContext>);
|
|
5
|
+
private storage;
|
|
6
|
+
get context(): RegisterContext<ExtendContext>;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
declare const createRegister: <ExtendConfig = null, ExtendContext = null, P = any>(options?: CreateOptions<ExtendConfig, ExtendContext, P> | undefined) => Register<ExtendConfig, ExtendContext, P>;
|
|
10
|
+
declare const register: Register<null, null, any>;
|
|
11
|
+
|
|
12
|
+
type CreateOptions<ExtendConfig = null, ExtendContext = null, P = any> = {
|
|
13
|
+
plugins?: RegisterPlugin<ExtendConfig, ExtendContext>[];
|
|
14
|
+
onError?: Function;
|
|
15
|
+
LoadFullback?: ComponentType<any> | null;
|
|
16
|
+
ErrorFullback?: ComponentType<any> | null;
|
|
17
|
+
renderTitle?: (name: string, context: RegisterContext<ExtendContext, P>) => string;
|
|
18
|
+
};
|
|
19
|
+
type ORegisterConfig = {
|
|
20
|
+
name: string;
|
|
21
|
+
onLoad?: (context: RegisterContext) => Promise<any>;
|
|
22
|
+
onLeave?: (context: ORegisterContext) => Promise<boolean>;
|
|
23
|
+
};
|
|
24
|
+
type ORegisterContext<P = {}, Q = {}, R = {}> = {
|
|
25
|
+
payload: Readonly<P>;
|
|
26
|
+
query: Q;
|
|
27
|
+
result?: R;
|
|
28
|
+
error?: Error;
|
|
29
|
+
isPreload?: boolean;
|
|
30
|
+
};
|
|
31
|
+
type RegisterConfig<ExtendConfig = null> = ExtendConfig extends null ? ORegisterConfig & CreateOptions : ORegisterConfig & CreateOptions & ExtendConfig;
|
|
32
|
+
type RegisterContext<ExtendContext = null, P = any, Q = any, R = any> = ExtendContext extends null ? ORegisterContext<P, Q, R> : ORegisterContext<P, Q, R> & ExtendContext;
|
|
33
|
+
type LoadParams<Q = any> = {
|
|
34
|
+
isPreload?: boolean;
|
|
35
|
+
query: Q;
|
|
36
|
+
};
|
|
37
|
+
type RegisterComponentStatic<ExtendContext = null, P = any> = {
|
|
38
|
+
config?: RegisterConfig;
|
|
39
|
+
emitOnLoad?: <Q, R>(params?: LoadParams<Q>) => Promise<RegisterContext<ExtendContext, P, Q, R>>;
|
|
40
|
+
emitOnLeave?: () => Promise<boolean>;
|
|
41
|
+
};
|
|
42
|
+
type RegisterComponentType<P = any> = ComponentType<P> & RegisterComponentStatic;
|
|
43
|
+
type RegisterPlugin<ExtendConfig = null, ExtendContext = null> = {
|
|
44
|
+
registerHook?: (config: RegisterConfig<ExtendConfig>, WrappedComponent: RegisterComponentType) => ComponentType<any> | void;
|
|
45
|
+
loadHook?: (config: RegisterConfig<ExtendConfig>, context: RegisterContext<ExtendContext>) => Promise<any>;
|
|
46
|
+
leaveHook?: (config: RegisterConfig<ExtendConfig>, context: RegisterContext<ExtendContext>) => Promise<boolean>;
|
|
47
|
+
};
|
|
48
|
+
type ContextStorage<ExtendContext = null, P = any, Q = any, R = any> = {
|
|
49
|
+
context: RegisterContext<ExtendContext, P, Q, R>;
|
|
50
|
+
};
|
|
51
|
+
type SetContextStorage<ExtendContext = null, P = any> = <Q = any, R = any>(contextStorage: ContextStorage<ExtendContext, P, Q, R>) => void;
|
|
52
|
+
type GetContext<ExtendContext = null, P = any> = <Q = any, R = any>() => RegisterContext<ExtendContext, P, Q, R>;
|
|
53
|
+
interface Register<ExtendConfig = null, ExtendContext = null, P = any> {
|
|
54
|
+
(config: RegisterConfig<ExtendConfig>): (WrappedComponent: any) => any;
|
|
55
|
+
setContextStorage: SetContextStorage<ExtendContext, P>;
|
|
56
|
+
getContext: GetContext<ExtendContext, P>;
|
|
57
|
+
}
|
|
58
|
+
type WithRegisterContext<ExtendContext = null, P = any, Q = any, R = any> = {
|
|
59
|
+
context: RegisterContext<ExtendContext, P, Q, R>;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export { type ContextStorage, ContextStorageClass, type CreateOptions, type GetContext, type LoadParams, type ORegisterConfig, type ORegisterContext, type Register, type RegisterComponentStatic, type RegisterComponentType, type RegisterConfig, type RegisterContext, type RegisterPlugin, type SetContextStorage, type WithRegisterContext, createRegister, register as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,676 @@
|
|
|
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 _array_without_holes(arr) {
|
|
11
|
+
if (Array.isArray(arr)) return _array_like_to_array(arr);
|
|
12
|
+
}
|
|
13
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
14
|
+
try {
|
|
15
|
+
var info = gen[key](arg);
|
|
16
|
+
var value = info.value;
|
|
17
|
+
} catch (error) {
|
|
18
|
+
reject(error);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
if (info.done) {
|
|
22
|
+
resolve(value);
|
|
23
|
+
} else {
|
|
24
|
+
Promise.resolve(value).then(_next, _throw);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function _async_to_generator(fn) {
|
|
28
|
+
return function() {
|
|
29
|
+
var self = this, args = arguments;
|
|
30
|
+
return new Promise(function(resolve, reject) {
|
|
31
|
+
var gen = fn.apply(self, args);
|
|
32
|
+
function _next(value) {
|
|
33
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
34
|
+
}
|
|
35
|
+
function _throw(err) {
|
|
36
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
37
|
+
}
|
|
38
|
+
_next(undefined);
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function _class_call_check(instance, Constructor) {
|
|
43
|
+
if (!(instance instanceof Constructor)) {
|
|
44
|
+
throw new TypeError("Cannot call a class as a function");
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function _defineProperties(target, props) {
|
|
48
|
+
for(var i = 0; i < props.length; i++){
|
|
49
|
+
var descriptor = props[i];
|
|
50
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
51
|
+
descriptor.configurable = true;
|
|
52
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
53
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function _create_class(Constructor, protoProps, staticProps) {
|
|
57
|
+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
58
|
+
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
59
|
+
return Constructor;
|
|
60
|
+
}
|
|
61
|
+
function _define_property(obj, key, value) {
|
|
62
|
+
if (key in obj) {
|
|
63
|
+
Object.defineProperty(obj, key, {
|
|
64
|
+
value: value,
|
|
65
|
+
enumerable: true,
|
|
66
|
+
configurable: true,
|
|
67
|
+
writable: true
|
|
68
|
+
});
|
|
69
|
+
} else {
|
|
70
|
+
obj[key] = value;
|
|
71
|
+
}
|
|
72
|
+
return obj;
|
|
73
|
+
}
|
|
74
|
+
function _instanceof(left, right) {
|
|
75
|
+
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
76
|
+
return !!right[Symbol.hasInstance](left);
|
|
77
|
+
} else {
|
|
78
|
+
return left instanceof right;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function _iterable_to_array(iter) {
|
|
82
|
+
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
|
|
83
|
+
}
|
|
84
|
+
function _iterable_to_array_limit(arr, i) {
|
|
85
|
+
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
|
|
86
|
+
if (_i == null) return;
|
|
87
|
+
var _arr = [];
|
|
88
|
+
var _n = true;
|
|
89
|
+
var _d = false;
|
|
90
|
+
var _s, _e;
|
|
91
|
+
try {
|
|
92
|
+
for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
|
|
93
|
+
_arr.push(_s.value);
|
|
94
|
+
if (i && _arr.length === i) break;
|
|
95
|
+
}
|
|
96
|
+
} catch (err) {
|
|
97
|
+
_d = true;
|
|
98
|
+
_e = err;
|
|
99
|
+
} finally{
|
|
100
|
+
try {
|
|
101
|
+
if (!_n && _i["return"] != null) _i["return"]();
|
|
102
|
+
} finally{
|
|
103
|
+
if (_d) throw _e;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return _arr;
|
|
107
|
+
}
|
|
108
|
+
function _non_iterable_rest() {
|
|
109
|
+
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
110
|
+
}
|
|
111
|
+
function _non_iterable_spread() {
|
|
112
|
+
throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
113
|
+
}
|
|
114
|
+
function _object_spread(target) {
|
|
115
|
+
for(var i = 1; i < arguments.length; i++){
|
|
116
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
117
|
+
var ownKeys = Object.keys(source);
|
|
118
|
+
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
119
|
+
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
120
|
+
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
121
|
+
}));
|
|
122
|
+
}
|
|
123
|
+
ownKeys.forEach(function(key) {
|
|
124
|
+
_define_property(target, key, source[key]);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
return target;
|
|
128
|
+
}
|
|
129
|
+
function ownKeys(object, enumerableOnly) {
|
|
130
|
+
var keys = Object.keys(object);
|
|
131
|
+
if (Object.getOwnPropertySymbols) {
|
|
132
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
|
133
|
+
if (enumerableOnly) {
|
|
134
|
+
symbols = symbols.filter(function(sym) {
|
|
135
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
keys.push.apply(keys, symbols);
|
|
139
|
+
}
|
|
140
|
+
return keys;
|
|
141
|
+
}
|
|
142
|
+
function _object_spread_props(target, source) {
|
|
143
|
+
source = source != null ? source : {};
|
|
144
|
+
if (Object.getOwnPropertyDescriptors) {
|
|
145
|
+
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
146
|
+
} else {
|
|
147
|
+
ownKeys(Object(source)).forEach(function(key) {
|
|
148
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
return target;
|
|
152
|
+
}
|
|
153
|
+
function _sliced_to_array(arr, i) {
|
|
154
|
+
return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
|
|
155
|
+
}
|
|
156
|
+
function _to_consumable_array(arr) {
|
|
157
|
+
return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
|
|
158
|
+
}
|
|
159
|
+
function _unsupported_iterable_to_array(o, minLen) {
|
|
160
|
+
if (!o) return;
|
|
161
|
+
if (typeof o === "string") return _array_like_to_array(o, minLen);
|
|
162
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
163
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
164
|
+
if (n === "Map" || n === "Set") return Array.from(n);
|
|
165
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
|
|
166
|
+
}
|
|
167
|
+
function _ts_generator(thisArg, body) {
|
|
168
|
+
var f, y, t, g, _ = {
|
|
169
|
+
label: 0,
|
|
170
|
+
sent: function() {
|
|
171
|
+
if (t[0] & 1) throw t[1];
|
|
172
|
+
return t[1];
|
|
173
|
+
},
|
|
174
|
+
trys: [],
|
|
175
|
+
ops: []
|
|
176
|
+
};
|
|
177
|
+
return g = {
|
|
178
|
+
next: verb(0),
|
|
179
|
+
"throw": verb(1),
|
|
180
|
+
"return": verb(2)
|
|
181
|
+
}, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
182
|
+
return this;
|
|
183
|
+
}), g;
|
|
184
|
+
function verb(n) {
|
|
185
|
+
return function(v) {
|
|
186
|
+
return step([
|
|
187
|
+
n,
|
|
188
|
+
v
|
|
189
|
+
]);
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
function step(op) {
|
|
193
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
194
|
+
while(_)try {
|
|
195
|
+
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;
|
|
196
|
+
if (y = 0, t) op = [
|
|
197
|
+
op[0] & 2,
|
|
198
|
+
t.value
|
|
199
|
+
];
|
|
200
|
+
switch(op[0]){
|
|
201
|
+
case 0:
|
|
202
|
+
case 1:
|
|
203
|
+
t = op;
|
|
204
|
+
break;
|
|
205
|
+
case 4:
|
|
206
|
+
_.label++;
|
|
207
|
+
return {
|
|
208
|
+
value: op[1],
|
|
209
|
+
done: false
|
|
210
|
+
};
|
|
211
|
+
case 5:
|
|
212
|
+
_.label++;
|
|
213
|
+
y = op[1];
|
|
214
|
+
op = [
|
|
215
|
+
0
|
|
216
|
+
];
|
|
217
|
+
continue;
|
|
218
|
+
case 7:
|
|
219
|
+
op = _.ops.pop();
|
|
220
|
+
_.trys.pop();
|
|
221
|
+
continue;
|
|
222
|
+
default:
|
|
223
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
224
|
+
_ = 0;
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
228
|
+
_.label = op[1];
|
|
229
|
+
break;
|
|
230
|
+
}
|
|
231
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
232
|
+
_.label = t[1];
|
|
233
|
+
t = op;
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
if (t && _.label < t[2]) {
|
|
237
|
+
_.label = t[2];
|
|
238
|
+
_.ops.push(op);
|
|
239
|
+
break;
|
|
240
|
+
}
|
|
241
|
+
if (t[2]) _.ops.pop();
|
|
242
|
+
_.trys.pop();
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
op = body.call(thisArg, _);
|
|
246
|
+
} catch (e) {
|
|
247
|
+
op = [
|
|
248
|
+
6,
|
|
249
|
+
e
|
|
250
|
+
];
|
|
251
|
+
y = 0;
|
|
252
|
+
} finally{
|
|
253
|
+
f = t = 0;
|
|
254
|
+
}
|
|
255
|
+
if (op[0] & 5) throw op[1];
|
|
256
|
+
return {
|
|
257
|
+
value: op[0] ? op[1] : void 0,
|
|
258
|
+
done: true
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
var __create = Object.create;
|
|
263
|
+
var __defProp = Object.defineProperty;
|
|
264
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
265
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
266
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
267
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
268
|
+
var __export = function(target, all) {
|
|
269
|
+
for(var name in all)__defProp(target, name, {
|
|
270
|
+
get: all[name],
|
|
271
|
+
enumerable: true
|
|
272
|
+
});
|
|
273
|
+
};
|
|
274
|
+
var __copyProps = function(to, from, except, desc) {
|
|
275
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
276
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
277
|
+
try {
|
|
278
|
+
var _loop = function() {
|
|
279
|
+
var key = _step.value;
|
|
280
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
281
|
+
get: function() {
|
|
282
|
+
return from[key];
|
|
283
|
+
},
|
|
284
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
285
|
+
});
|
|
286
|
+
};
|
|
287
|
+
for(var _iterator = __getOwnPropNames(from)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop();
|
|
288
|
+
} catch (err) {
|
|
289
|
+
_didIteratorError = true;
|
|
290
|
+
_iteratorError = err;
|
|
291
|
+
} finally{
|
|
292
|
+
try {
|
|
293
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
294
|
+
_iterator.return();
|
|
295
|
+
}
|
|
296
|
+
} finally{
|
|
297
|
+
if (_didIteratorError) {
|
|
298
|
+
throw _iteratorError;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
return to;
|
|
304
|
+
};
|
|
305
|
+
var __toESM = function(mod, isNodeMode, target) {
|
|
306
|
+
return target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(// If the importer is in node compatibility mode or this is not an ESM
|
|
307
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
308
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
309
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
310
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
311
|
+
value: mod,
|
|
312
|
+
enumerable: true
|
|
313
|
+
}) : target, mod);
|
|
314
|
+
};
|
|
315
|
+
var __toCommonJS = function(mod) {
|
|
316
|
+
return __copyProps(__defProp({}, "__esModule", {
|
|
317
|
+
value: true
|
|
318
|
+
}), mod);
|
|
319
|
+
};
|
|
320
|
+
// src/index.ts
|
|
321
|
+
var src_exports = {};
|
|
322
|
+
__export(src_exports, {
|
|
323
|
+
ContextStorageClass: function() {
|
|
324
|
+
return ContextStorageClass;
|
|
325
|
+
},
|
|
326
|
+
createRegister: function() {
|
|
327
|
+
return createRegister;
|
|
328
|
+
},
|
|
329
|
+
default: function() {
|
|
330
|
+
return src_default;
|
|
331
|
+
}
|
|
332
|
+
});
|
|
333
|
+
module.exports = __toCommonJS(src_exports);
|
|
334
|
+
var import_hoist_non_react_statics = __toESM(require("hoist-non-react-statics"));
|
|
335
|
+
// src/Register.tsx
|
|
336
|
+
var import_react = require("react");
|
|
337
|
+
var import_react_helmet = require("react-helmet");
|
|
338
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
339
|
+
var createRegisterComponent = function(config, getContext, WrappedComponent) {
|
|
340
|
+
var RegisterComponent = (0, import_react.memo)(function(props) {
|
|
341
|
+
var context = getContext();
|
|
342
|
+
var isPreload = context.isPreload, query = context.query;
|
|
343
|
+
var name = config.name, ErrorFullback = config.ErrorFullback, LoadFullback = config.LoadFullback, renderTitle = config.renderTitle;
|
|
344
|
+
var _ref = _sliced_to_array((0, import_react.useState)(!isPreload), 2), needLoad = _ref[0], setNeedLoad = _ref[1];
|
|
345
|
+
var _ref1 = _sliced_to_array((0, import_react.useState)(), 2), loadError = _ref1[0], setLoadError = _ref1[1];
|
|
346
|
+
var isLeave = false;
|
|
347
|
+
var emitLoad = (0, import_react.useCallback)(/*#__PURE__*/ _async_to_generator(function() {
|
|
348
|
+
var _WrappedComponent_emitOnLoad, error;
|
|
349
|
+
return _ts_generator(this, function(_state) {
|
|
350
|
+
switch(_state.label){
|
|
351
|
+
case 0:
|
|
352
|
+
if (!needLoad || loadError) return [
|
|
353
|
+
2
|
|
354
|
+
];
|
|
355
|
+
return [
|
|
356
|
+
4,
|
|
357
|
+
(_WrappedComponent_emitOnLoad = WrappedComponent.emitOnLoad) === null || _WrappedComponent_emitOnLoad === void 0 ? void 0 : _WrappedComponent_emitOnLoad.call(WrappedComponent, {
|
|
358
|
+
query: query
|
|
359
|
+
})
|
|
360
|
+
];
|
|
361
|
+
case 1:
|
|
362
|
+
error = (_state.sent() || {}).error;
|
|
363
|
+
if (isLeave) return [
|
|
364
|
+
2
|
|
365
|
+
];
|
|
366
|
+
ErrorFullback && setLoadError(error);
|
|
367
|
+
LoadFullback && setNeedLoad(false);
|
|
368
|
+
return [
|
|
369
|
+
2
|
|
370
|
+
];
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
}), [
|
|
374
|
+
needLoad,
|
|
375
|
+
loadError
|
|
376
|
+
]);
|
|
377
|
+
void emitLoad();
|
|
378
|
+
if (isPreload) delete context.isPreload;
|
|
379
|
+
(0, import_react.useEffect)(function() {
|
|
380
|
+
return function() {
|
|
381
|
+
var _WrappedComponent_emitOnLeave;
|
|
382
|
+
isLeave = true;
|
|
383
|
+
(_WrappedComponent_emitOnLeave = WrappedComponent.emitOnLeave) === null || _WrappedComponent_emitOnLeave === void 0 ? void 0 : _WrappedComponent_emitOnLeave.call(WrappedComponent);
|
|
384
|
+
};
|
|
385
|
+
}, []);
|
|
386
|
+
var content = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(WrappedComponent, _object_spread_props(_object_spread({}, props), {
|
|
387
|
+
context: context
|
|
388
|
+
}));
|
|
389
|
+
if (loadError && ErrorFullback) content = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ErrorFullback, {});
|
|
390
|
+
if (needLoad && LoadFullback) content = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(LoadFullback, {});
|
|
391
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react.Fragment, {
|
|
392
|
+
children: [
|
|
393
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_helmet.Helmet, {
|
|
394
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("title", {
|
|
395
|
+
children: (renderTitle === null || renderTitle === void 0 ? void 0 : renderTitle(name, context)) || name
|
|
396
|
+
})
|
|
397
|
+
}),
|
|
398
|
+
content
|
|
399
|
+
]
|
|
400
|
+
});
|
|
401
|
+
});
|
|
402
|
+
var componentName = WrappedComponent.displayName || WrappedComponent.name || "Component";
|
|
403
|
+
RegisterComponent.displayName = "Register(".concat(componentName, ")");
|
|
404
|
+
return RegisterComponent;
|
|
405
|
+
};
|
|
406
|
+
// src/contextStorage/client.ts
|
|
407
|
+
var ContextStorageClass = /*#__PURE__*/ function() {
|
|
408
|
+
function ContextStorageClass(context) {
|
|
409
|
+
_class_call_check(this, ContextStorageClass);
|
|
410
|
+
this.storage = {
|
|
411
|
+
context: context
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
_create_class(ContextStorageClass, [
|
|
415
|
+
{
|
|
416
|
+
key: "context",
|
|
417
|
+
get: function get() {
|
|
418
|
+
var context = this.storage.context;
|
|
419
|
+
if (!context) throw new Error("Can not find context in ContextStorage");
|
|
420
|
+
return this.storage.context;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
]);
|
|
424
|
+
return ContextStorageClass;
|
|
425
|
+
}();
|
|
426
|
+
// src/index.ts
|
|
427
|
+
var createRegister = function(options) {
|
|
428
|
+
var _contextStorage;
|
|
429
|
+
var _emitOnLoad = function() {
|
|
430
|
+
var _ref = _async_to_generator(function(config, getContext, params) {
|
|
431
|
+
var onLoad, onError, _config_plugins, plugins, context, finalResult, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, loadHook, result, err, result1, e;
|
|
432
|
+
return _ts_generator(this, function(_state) {
|
|
433
|
+
switch(_state.label){
|
|
434
|
+
case 0:
|
|
435
|
+
onLoad = config.onLoad, onError = config.onError, _config_plugins = config.plugins, plugins = _config_plugins === void 0 ? [] : _config_plugins;
|
|
436
|
+
context = getContext();
|
|
437
|
+
if (params === null || params === void 0 ? void 0 : params.isPreload) {
|
|
438
|
+
context.isPreload = true;
|
|
439
|
+
}
|
|
440
|
+
context.query = (params === null || params === void 0 ? void 0 : params.query) || {};
|
|
441
|
+
delete context.error;
|
|
442
|
+
finalResult = {};
|
|
443
|
+
_state.label = 1;
|
|
444
|
+
case 1:
|
|
445
|
+
_state.trys.push([
|
|
446
|
+
1,
|
|
447
|
+
12,
|
|
448
|
+
,
|
|
449
|
+
13
|
|
450
|
+
]);
|
|
451
|
+
_iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
452
|
+
_state.label = 2;
|
|
453
|
+
case 2:
|
|
454
|
+
_state.trys.push([
|
|
455
|
+
2,
|
|
456
|
+
7,
|
|
457
|
+
8,
|
|
458
|
+
9
|
|
459
|
+
]);
|
|
460
|
+
_iterator = plugins[Symbol.iterator]();
|
|
461
|
+
_state.label = 3;
|
|
462
|
+
case 3:
|
|
463
|
+
if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
|
|
464
|
+
3,
|
|
465
|
+
6
|
|
466
|
+
];
|
|
467
|
+
loadHook = _step.value.loadHook;
|
|
468
|
+
return [
|
|
469
|
+
4,
|
|
470
|
+
loadHook === null || loadHook === void 0 ? void 0 : loadHook(config, context)
|
|
471
|
+
];
|
|
472
|
+
case 4:
|
|
473
|
+
result = _state.sent();
|
|
474
|
+
finalResult = _object_spread({}, finalResult, result);
|
|
475
|
+
_state.label = 5;
|
|
476
|
+
case 5:
|
|
477
|
+
_iteratorNormalCompletion = true;
|
|
478
|
+
return [
|
|
479
|
+
3,
|
|
480
|
+
3
|
|
481
|
+
];
|
|
482
|
+
case 6:
|
|
483
|
+
return [
|
|
484
|
+
3,
|
|
485
|
+
9
|
|
486
|
+
];
|
|
487
|
+
case 7:
|
|
488
|
+
err = _state.sent();
|
|
489
|
+
_didIteratorError = true;
|
|
490
|
+
_iteratorError = err;
|
|
491
|
+
return [
|
|
492
|
+
3,
|
|
493
|
+
9
|
|
494
|
+
];
|
|
495
|
+
case 8:
|
|
496
|
+
try {
|
|
497
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
498
|
+
_iterator.return();
|
|
499
|
+
}
|
|
500
|
+
} finally{
|
|
501
|
+
if (_didIteratorError) {
|
|
502
|
+
throw _iteratorError;
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
return [
|
|
506
|
+
7
|
|
507
|
+
];
|
|
508
|
+
case 9:
|
|
509
|
+
if (!onLoad) return [
|
|
510
|
+
3,
|
|
511
|
+
11
|
|
512
|
+
];
|
|
513
|
+
return [
|
|
514
|
+
4,
|
|
515
|
+
onLoad(context)
|
|
516
|
+
];
|
|
517
|
+
case 10:
|
|
518
|
+
result1 = _state.sent();
|
|
519
|
+
finalResult = _object_spread({}, finalResult, result1);
|
|
520
|
+
_state.label = 11;
|
|
521
|
+
case 11:
|
|
522
|
+
context.result = finalResult;
|
|
523
|
+
return [
|
|
524
|
+
3,
|
|
525
|
+
13
|
|
526
|
+
];
|
|
527
|
+
case 12:
|
|
528
|
+
e = _state.sent();
|
|
529
|
+
context.error = _instanceof(e, Error) ? e : new Error("register onLoad error");
|
|
530
|
+
onError ? onError(e) : console.error(e);
|
|
531
|
+
return [
|
|
532
|
+
3,
|
|
533
|
+
13
|
|
534
|
+
];
|
|
535
|
+
case 13:
|
|
536
|
+
return [
|
|
537
|
+
2,
|
|
538
|
+
context
|
|
539
|
+
];
|
|
540
|
+
}
|
|
541
|
+
});
|
|
542
|
+
});
|
|
543
|
+
return function _emitOnLoad(config, getContext, params) {
|
|
544
|
+
return _ref.apply(this, arguments);
|
|
545
|
+
};
|
|
546
|
+
}();
|
|
547
|
+
var _emitOnLeave = function() {
|
|
548
|
+
var _ref = _async_to_generator(function(config, getContext) {
|
|
549
|
+
var onLeave, onError, _config_plugins, plugins, context, canLeave, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, leaveHook, e;
|
|
550
|
+
return _ts_generator(this, function(_state) {
|
|
551
|
+
switch(_state.label){
|
|
552
|
+
case 0:
|
|
553
|
+
onLeave = config.onLeave, onError = config.onError, _config_plugins = config.plugins, plugins = _config_plugins === void 0 ? [] : _config_plugins;
|
|
554
|
+
context = getContext();
|
|
555
|
+
canLeave = true;
|
|
556
|
+
_state.label = 1;
|
|
557
|
+
case 1:
|
|
558
|
+
_state.trys.push([
|
|
559
|
+
1,
|
|
560
|
+
4,
|
|
561
|
+
,
|
|
562
|
+
5
|
|
563
|
+
]);
|
|
564
|
+
_iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
565
|
+
try {
|
|
566
|
+
for(_iterator = plugins[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
567
|
+
leaveHook = _step.value.leaveHook;
|
|
568
|
+
canLeave = !!(leaveHook === null || leaveHook === void 0 ? void 0 : leaveHook(config, context));
|
|
569
|
+
}
|
|
570
|
+
} catch (err) {
|
|
571
|
+
_didIteratorError = true;
|
|
572
|
+
_iteratorError = err;
|
|
573
|
+
} finally{
|
|
574
|
+
try {
|
|
575
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
576
|
+
_iterator.return();
|
|
577
|
+
}
|
|
578
|
+
} finally{
|
|
579
|
+
if (_didIteratorError) {
|
|
580
|
+
throw _iteratorError;
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
if (!onLeave) return [
|
|
585
|
+
3,
|
|
586
|
+
3
|
|
587
|
+
];
|
|
588
|
+
return [
|
|
589
|
+
4,
|
|
590
|
+
onLeave(context)
|
|
591
|
+
];
|
|
592
|
+
case 2:
|
|
593
|
+
canLeave = _state.sent();
|
|
594
|
+
_state.label = 3;
|
|
595
|
+
case 3:
|
|
596
|
+
return [
|
|
597
|
+
3,
|
|
598
|
+
5
|
|
599
|
+
];
|
|
600
|
+
case 4:
|
|
601
|
+
e = _state.sent();
|
|
602
|
+
onError ? onError(e) : console.error(e);
|
|
603
|
+
return [
|
|
604
|
+
3,
|
|
605
|
+
5
|
|
606
|
+
];
|
|
607
|
+
case 5:
|
|
608
|
+
return [
|
|
609
|
+
2,
|
|
610
|
+
canLeave
|
|
611
|
+
];
|
|
612
|
+
}
|
|
613
|
+
});
|
|
614
|
+
});
|
|
615
|
+
return function _emitOnLeave(config, getContext) {
|
|
616
|
+
return _ref.apply(this, arguments);
|
|
617
|
+
};
|
|
618
|
+
}();
|
|
619
|
+
var register2 = function(config) {
|
|
620
|
+
return function(WrappedComponent) {
|
|
621
|
+
var finalPlugin = _to_consumable_array((options === null || options === void 0 ? void 0 : options.plugins) || []).concat(_to_consumable_array((config === null || config === void 0 ? void 0 : config.plugins) || []));
|
|
622
|
+
config = _object_spread_props(_object_spread({}, options, config), {
|
|
623
|
+
plugins: finalPlugin
|
|
624
|
+
});
|
|
625
|
+
var TempRegisterComponent = WrappedComponent;
|
|
626
|
+
TempRegisterComponent.config = config;
|
|
627
|
+
TempRegisterComponent.emitOnLoad = function(params) {
|
|
628
|
+
return _emitOnLoad(config, register2.getContext, params);
|
|
629
|
+
};
|
|
630
|
+
TempRegisterComponent.emitOnLeave = function() {
|
|
631
|
+
return _emitOnLeave(config, register2.getContext);
|
|
632
|
+
};
|
|
633
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
634
|
+
try {
|
|
635
|
+
for(var _iterator = finalPlugin[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
636
|
+
var registerHook = _step.value.registerHook;
|
|
637
|
+
if (!registerHook) continue;
|
|
638
|
+
var ExtendWrappedComponent = registerHook(config, TempRegisterComponent);
|
|
639
|
+
if (!ExtendWrappedComponent) continue;
|
|
640
|
+
(0, import_hoist_non_react_statics.default)(ExtendWrappedComponent, TempRegisterComponent);
|
|
641
|
+
TempRegisterComponent = ExtendWrappedComponent;
|
|
642
|
+
}
|
|
643
|
+
} catch (err) {
|
|
644
|
+
_didIteratorError = true;
|
|
645
|
+
_iteratorError = err;
|
|
646
|
+
} finally{
|
|
647
|
+
try {
|
|
648
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
649
|
+
_iterator.return();
|
|
650
|
+
}
|
|
651
|
+
} finally{
|
|
652
|
+
if (_didIteratorError) {
|
|
653
|
+
throw _iteratorError;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
var FinalRegisterComponent = createRegisterComponent(config, register2.getContext, TempRegisterComponent);
|
|
658
|
+
(0, import_hoist_non_react_statics.default)(FinalRegisterComponent, TempRegisterComponent);
|
|
659
|
+
return FinalRegisterComponent;
|
|
660
|
+
};
|
|
661
|
+
};
|
|
662
|
+
register2.setContextStorage = function(contextStorage) {
|
|
663
|
+
_contextStorage = contextStorage;
|
|
664
|
+
};
|
|
665
|
+
register2.getContext = function() {
|
|
666
|
+
return _contextStorage.context;
|
|
667
|
+
};
|
|
668
|
+
return register2;
|
|
669
|
+
};
|
|
670
|
+
var register = createRegister();
|
|
671
|
+
var src_default = register;
|
|
672
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
673
|
+
0 && (module.exports = {
|
|
674
|
+
ContextStorageClass: ContextStorageClass,
|
|
675
|
+
createRegister: createRegister
|
|
676
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import hoistNonReactStatic from "hoist-non-react-statics";
|
|
3
|
+
|
|
4
|
+
// src/Register.tsx
|
|
5
|
+
import { memo, Fragment, useEffect, useState, useCallback } from "react";
|
|
6
|
+
import { Helmet } from "react-helmet";
|
|
7
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
8
|
+
var createRegisterComponent = (config, getContext, WrappedComponent) => {
|
|
9
|
+
const RegisterComponent = memo((props) => {
|
|
10
|
+
const context = getContext();
|
|
11
|
+
const { isPreload, query } = context;
|
|
12
|
+
const { name, ErrorFullback, LoadFullback, renderTitle } = config;
|
|
13
|
+
const [needLoad, setNeedLoad] = useState(!isPreload);
|
|
14
|
+
const [loadError, setLoadError] = useState();
|
|
15
|
+
let isLeave = false;
|
|
16
|
+
const emitLoad = useCallback(async () => {
|
|
17
|
+
if (!needLoad || loadError)
|
|
18
|
+
return;
|
|
19
|
+
const { error } = await WrappedComponent.emitOnLoad?.({ query }) || {};
|
|
20
|
+
if (isLeave)
|
|
21
|
+
return;
|
|
22
|
+
ErrorFullback && setLoadError(error);
|
|
23
|
+
LoadFullback && setNeedLoad(false);
|
|
24
|
+
}, [needLoad, loadError]);
|
|
25
|
+
void emitLoad();
|
|
26
|
+
if (isPreload)
|
|
27
|
+
delete context.isPreload;
|
|
28
|
+
useEffect(
|
|
29
|
+
() => () => {
|
|
30
|
+
isLeave = true;
|
|
31
|
+
WrappedComponent.emitOnLeave?.();
|
|
32
|
+
},
|
|
33
|
+
[]
|
|
34
|
+
);
|
|
35
|
+
let content = /* @__PURE__ */ jsx(WrappedComponent, { ...props, context });
|
|
36
|
+
if (loadError && ErrorFullback)
|
|
37
|
+
content = /* @__PURE__ */ jsx(ErrorFullback, {});
|
|
38
|
+
if (needLoad && LoadFullback)
|
|
39
|
+
content = /* @__PURE__ */ jsx(LoadFullback, {});
|
|
40
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
41
|
+
/* @__PURE__ */ jsx(Helmet, { children: /* @__PURE__ */ jsx("title", { children: renderTitle?.(name, context) || name }) }),
|
|
42
|
+
content
|
|
43
|
+
] });
|
|
44
|
+
});
|
|
45
|
+
const componentName = WrappedComponent.displayName || WrappedComponent.name || "Component";
|
|
46
|
+
RegisterComponent.displayName = `Register(${componentName})`;
|
|
47
|
+
return RegisterComponent;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// src/contextStorage/client.ts
|
|
51
|
+
var ContextStorageClass = class {
|
|
52
|
+
constructor(context) {
|
|
53
|
+
this.storage = { context };
|
|
54
|
+
}
|
|
55
|
+
get context() {
|
|
56
|
+
const context = this.storage.context;
|
|
57
|
+
if (!context)
|
|
58
|
+
throw new Error("Can not find context in ContextStorage");
|
|
59
|
+
return this.storage.context;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// src/index.ts
|
|
64
|
+
var createRegister = (options) => {
|
|
65
|
+
let _contextStorage;
|
|
66
|
+
const _emitOnLoad = async (config, getContext, params) => {
|
|
67
|
+
const { onLoad, onError, plugins = [] } = config;
|
|
68
|
+
const context = getContext();
|
|
69
|
+
if (params?.isPreload) {
|
|
70
|
+
context.isPreload = true;
|
|
71
|
+
}
|
|
72
|
+
context.query = params?.query || {};
|
|
73
|
+
delete context.error;
|
|
74
|
+
let finalResult = {};
|
|
75
|
+
try {
|
|
76
|
+
for (const { loadHook } of plugins) {
|
|
77
|
+
const result = await loadHook?.(config, context);
|
|
78
|
+
finalResult = { ...finalResult, ...result };
|
|
79
|
+
}
|
|
80
|
+
if (onLoad) {
|
|
81
|
+
const result = await onLoad(context);
|
|
82
|
+
finalResult = { ...finalResult, ...result };
|
|
83
|
+
}
|
|
84
|
+
context.result = finalResult;
|
|
85
|
+
} catch (e) {
|
|
86
|
+
context.error = e instanceof Error ? e : new Error("register onLoad error");
|
|
87
|
+
onError ? onError(e) : console.error(e);
|
|
88
|
+
}
|
|
89
|
+
return context;
|
|
90
|
+
};
|
|
91
|
+
const _emitOnLeave = async (config, getContext) => {
|
|
92
|
+
const { onLeave, onError, plugins = [] } = config;
|
|
93
|
+
const context = getContext();
|
|
94
|
+
let canLeave = true;
|
|
95
|
+
try {
|
|
96
|
+
for (const { leaveHook } of plugins) {
|
|
97
|
+
canLeave = !!leaveHook?.(config, context);
|
|
98
|
+
}
|
|
99
|
+
if (onLeave)
|
|
100
|
+
canLeave = await onLeave(context);
|
|
101
|
+
} catch (e) {
|
|
102
|
+
onError ? onError(e) : console.error(e);
|
|
103
|
+
}
|
|
104
|
+
return canLeave;
|
|
105
|
+
};
|
|
106
|
+
const register2 = (config) => (WrappedComponent) => {
|
|
107
|
+
const finalPlugin = [...options?.plugins || [], ...config?.plugins || []];
|
|
108
|
+
config = { ...options, ...config, plugins: finalPlugin };
|
|
109
|
+
let TempRegisterComponent = WrappedComponent;
|
|
110
|
+
TempRegisterComponent.config = config;
|
|
111
|
+
TempRegisterComponent.emitOnLoad = (params) => _emitOnLoad(config, register2.getContext, params);
|
|
112
|
+
TempRegisterComponent.emitOnLeave = () => _emitOnLeave(config, register2.getContext);
|
|
113
|
+
for (const { registerHook } of finalPlugin) {
|
|
114
|
+
if (!registerHook)
|
|
115
|
+
continue;
|
|
116
|
+
const ExtendWrappedComponent = registerHook(config, TempRegisterComponent);
|
|
117
|
+
if (!ExtendWrappedComponent)
|
|
118
|
+
continue;
|
|
119
|
+
hoistNonReactStatic(ExtendWrappedComponent, TempRegisterComponent);
|
|
120
|
+
TempRegisterComponent = ExtendWrappedComponent;
|
|
121
|
+
}
|
|
122
|
+
const FinalRegisterComponent = createRegisterComponent(config, register2.getContext, TempRegisterComponent);
|
|
123
|
+
hoistNonReactStatic(FinalRegisterComponent, TempRegisterComponent);
|
|
124
|
+
return FinalRegisterComponent;
|
|
125
|
+
};
|
|
126
|
+
register2.setContextStorage = (contextStorage) => {
|
|
127
|
+
_contextStorage = contextStorage;
|
|
128
|
+
};
|
|
129
|
+
register2.getContext = () => {
|
|
130
|
+
return _contextStorage.context;
|
|
131
|
+
};
|
|
132
|
+
return register2;
|
|
133
|
+
};
|
|
134
|
+
var register = createRegister();
|
|
135
|
+
var src_default = register;
|
|
136
|
+
export {
|
|
137
|
+
ContextStorageClass,
|
|
138
|
+
createRegister,
|
|
139
|
+
src_default as default
|
|
140
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@xfe-repo/web-register",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"sideEffects": false,
|
|
5
|
+
"exports": {
|
|
6
|
+
".": {
|
|
7
|
+
"import": "./dist/index.mjs",
|
|
8
|
+
"require": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"hoist-non-react-statics": "^3.3.2",
|
|
17
|
+
"react-helmet": "^6.1.0"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/hoist-non-react-statics": "^3.3.5",
|
|
21
|
+
"@types/node": "^20.10.4",
|
|
22
|
+
"@types/qs": "^6.9.10",
|
|
23
|
+
"@types/react": "^18.2.0",
|
|
24
|
+
"@types/react-helmet": "^6.1.11",
|
|
25
|
+
"eslint": "^8.53.0",
|
|
26
|
+
"react": "^18.2.0",
|
|
27
|
+
"tsup": "^8.0.1",
|
|
28
|
+
"typescript": "^5.2.2",
|
|
29
|
+
"@xfe-repo/typescript-config": "0.0.0",
|
|
30
|
+
"@xfe-repo/eslint-config": "0.0.0"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsup",
|
|
34
|
+
"dev": "tsup --watch",
|
|
35
|
+
"lint": "eslint \"src/**/*.ts*\"",
|
|
36
|
+
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
|
|
37
|
+
}
|
|
38
|
+
}
|