react-simple-loadable 2.0.1 → 2.0.2
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/loadable.d.ts +8 -3
- package/loadable.d.ts.map +1 -1
- package/loadable.js +17 -27
- package/package.json +1 -1
package/loadable.d.ts
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
import { ComponentType } from 'react';
|
|
2
|
-
export type
|
|
3
|
-
|
|
2
|
+
export type LoadComponentFn<P = Record<string, any>> = () => Promise<ComponentType<P>>;
|
|
3
|
+
export type LoadableMixin = {
|
|
4
|
+
preload(): void;
|
|
5
|
+
};
|
|
6
|
+
export type LoadableComponent<P = Record<string, any>> = ComponentType<P> & LoadableMixin;
|
|
7
|
+
export interface LoadableConfig {
|
|
4
8
|
loader?: ComponentType<any>;
|
|
5
9
|
/**
|
|
6
10
|
* Component which renders with the lazy component
|
|
7
11
|
*/
|
|
8
12
|
extra?: ComponentType<any>;
|
|
9
13
|
}
|
|
10
|
-
|
|
14
|
+
type LoadableComponent$<P> = LoadableComponent<P>;
|
|
15
|
+
export declare function loadable<P = any>(loadFn: LoadComponentFn<P>, loaderOrConfig?: null | undefined | LoadableConfig['loader'] | LoadableConfig): LoadableComponent$<P>;
|
|
11
16
|
export {};
|
|
12
17
|
//# sourceMappingURL=loadable.d.ts.map
|
package/loadable.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loadable.d.ts","sourceRoot":"","sources":["../src/loadable.tsx"],"names":[],"mappings":"AAIA,OAAO,EAAa,aAAa,EAAE,MAAM,OAAO,CAAC;AAGjD,MAAM,MAAM,
|
|
1
|
+
{"version":3,"file":"loadable.d.ts","sourceRoot":"","sources":["../src/loadable.tsx"],"names":[],"mappings":"AAIA,OAAO,EAAa,aAAa,EAAE,MAAM,OAAO,CAAC;AAGjD,MAAM,MAAM,eAAe,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,IAAI,MAAM,OAAO,CAClE,aAAa,CAAC,CAAC,CAAC,CACjB,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG;IAC1B,OAAO,IAAI,IAAI,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,iBAAiB,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,IAAI,aAAa,CAAC,CAAC,CAAC,GACvE,aAAa,CAAC;AAIhB,MAAM,WAAW,cAAc;IAC7B,MAAM,CAAC,EAAE,aAAa,CAAC,GAAG,CAAC,CAAC;IAC5B;;OAEG;IACH,KAAK,CAAC,EAAE,aAAa,CAAC,GAAG,CAAC,CAAC;CAC5B;AAyHD,KAAK,kBAAkB,CAAC,CAAC,IAAI,iBAAiB,CAAC,CAAC,CAAC,CAAC;AAElD,wBAAgB,QAAQ,CAAC,CAAC,GAAG,GAAG,EAC9B,MAAM,EAAE,eAAe,CAAC,CAAC,CAAC,EAC1B,cAAc,CAAC,EAAE,IAAI,GAAG,SAAS,GAAG,cAAc,CAAC,QAAQ,CAAC,GAAG,cAAc,GAC5E,kBAAkB,CAAC,CAAC,CAAC,CAqBvB"}
|
package/loadable.js
CHANGED
|
@@ -6,81 +6,72 @@ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-run
|
|
|
6
6
|
import { Component } from 'react';
|
|
7
7
|
const DefaultLoader = () => null;
|
|
8
8
|
const loadingStates = new WeakMap();
|
|
9
|
-
const
|
|
9
|
+
const getLoadingState = (loadFn, modify) => {
|
|
10
10
|
if (!loadingStates.has(loadFn)) {
|
|
11
11
|
loadingStates.set(loadFn, {
|
|
12
12
|
error: null,
|
|
13
13
|
loading: true,
|
|
14
14
|
});
|
|
15
15
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const updatedState = { ...loadingState, ...
|
|
16
|
+
const loadingState = loadingStates.get(loadFn);
|
|
17
|
+
if (modify == null) {
|
|
18
|
+
return loadingState;
|
|
19
|
+
}
|
|
20
|
+
const updatedState = { ...loadingState, ...modify };
|
|
21
21
|
loadingStates.set(loadFn, updatedState);
|
|
22
22
|
return updatedState;
|
|
23
23
|
};
|
|
24
24
|
const load = (loadFn) => {
|
|
25
|
-
const state =
|
|
25
|
+
const state = getLoadingState(loadFn);
|
|
26
26
|
if (state.result) {
|
|
27
27
|
return state;
|
|
28
28
|
}
|
|
29
|
-
|
|
29
|
+
getLoadingState(loadFn, { loading: true, error: null });
|
|
30
30
|
state.loading = true;
|
|
31
31
|
state.error = null;
|
|
32
32
|
state.promise = loadFn()
|
|
33
33
|
.then((result) => {
|
|
34
34
|
state.loading = false;
|
|
35
35
|
state.result = result;
|
|
36
|
-
|
|
36
|
+
getLoadingState(loadFn, state);
|
|
37
37
|
return result;
|
|
38
38
|
})
|
|
39
39
|
.catch((error) => {
|
|
40
40
|
state.loading = false;
|
|
41
41
|
state.error = error;
|
|
42
|
-
|
|
42
|
+
getLoadingState(loadFn, state);
|
|
43
43
|
throw error;
|
|
44
44
|
});
|
|
45
|
-
|
|
45
|
+
getLoadingState(loadFn, state);
|
|
46
46
|
return state;
|
|
47
47
|
};
|
|
48
48
|
class LoadableComponentBase extends Component {
|
|
49
49
|
config;
|
|
50
|
-
constructor(config,
|
|
50
|
+
constructor(config, props) {
|
|
51
51
|
super(props);
|
|
52
52
|
this.config = config;
|
|
53
|
-
this.state =
|
|
53
|
+
this.state = getLoadingState(config.loadFn);
|
|
54
54
|
}
|
|
55
55
|
static loadFn = undefined;
|
|
56
56
|
static preload() {
|
|
57
57
|
load(this.loadFn);
|
|
58
58
|
}
|
|
59
59
|
UNSAFE_componentWillMount() {
|
|
60
|
-
this.loadModule();
|
|
61
|
-
}
|
|
62
|
-
loadModule() {
|
|
63
60
|
if (!this.state.loading) {
|
|
64
61
|
return;
|
|
65
62
|
}
|
|
66
63
|
const { promise } = load(this.config.loadFn);
|
|
67
|
-
this.
|
|
64
|
+
this.setState(getLoadingState(this.config.loadFn));
|
|
68
65
|
promise
|
|
69
66
|
.then(() => {
|
|
70
|
-
this.
|
|
67
|
+
this.setState(getLoadingState(this.config.loadFn));
|
|
71
68
|
return null;
|
|
72
69
|
})
|
|
73
70
|
.catch(() => {
|
|
74
|
-
this.
|
|
71
|
+
this.setState(getLoadingState(this.config.loadFn));
|
|
75
72
|
return null;
|
|
76
73
|
});
|
|
77
74
|
}
|
|
78
|
-
syncLoadingState() {
|
|
79
|
-
this.setState(getOrCreateLoadingState(this.config.loadFn));
|
|
80
|
-
}
|
|
81
|
-
updateLoadingState(update) {
|
|
82
|
-
this.setState(updateLoadingState(this.config.loadFn, update));
|
|
83
|
-
}
|
|
84
75
|
render() {
|
|
85
76
|
const { loader } = this.config;
|
|
86
77
|
const Loader = loader || DefaultLoader;
|
|
@@ -97,7 +88,6 @@ class LoadableComponentBase extends Component {
|
|
|
97
88
|
}
|
|
98
89
|
}
|
|
99
90
|
}
|
|
100
|
-
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
|
101
91
|
export function loadable(loadFn, loaderOrConfig) {
|
|
102
92
|
const config = typeof loaderOrConfig === 'object'
|
|
103
93
|
? loaderOrConfig
|
|
@@ -108,7 +98,7 @@ export function loadable(loadFn, loaderOrConfig) {
|
|
|
108
98
|
super({
|
|
109
99
|
loadFn,
|
|
110
100
|
...config,
|
|
111
|
-
},
|
|
101
|
+
}, props);
|
|
112
102
|
}
|
|
113
103
|
}
|
|
114
104
|
return LoadableComponent;
|