-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-module.js
More file actions
129 lines (110 loc) · 2.78 KB
/
Copy pathuse-module.js
File metadata and controls
129 lines (110 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import React, {
useReducer,
useEffect,
useMemo,
createContext,
useContext,
} from "react";
import PropTypes from "prop-types";
import invariant from "tiny-invariant";
import {
IS_MODULE,
IS_MODULE_CACHE,
MODULE_CACHE_FETCH,
MODULE_PENDING,
} from "./constants";
export default function useModule(module) {
invariant(
module === null || (typeof module === "object" && module[IS_MODULE]),
"expected module to be an object returned by `createModule` or null",
);
const cache = useModuleCache();
const [state, dispatch] = useReducer(reducer, { module, cache }, initializer);
if (module !== state.module || cache !== state.cache) {
dispatch({
type: ACTION_UPDATE,
module,
cache,
});
}
useEffect(() => {
if (state.entry && state.entry.status === MODULE_PENDING) {
const unlisten = state.entry.listen(nextEntry => {
dispatch({
type: ACTION_RESOLVE,
nextEntry,
prevEntry: state.entry,
});
});
return unlisten;
}
}, [state.entry]);
return useMemo(() => {
if (!state.entry || state.entry.status === MODULE_PENDING) {
// filtering out extra properties
return {
status: MODULE_PENDING,
};
} else {
return state.entry;
}
}, [state.entry]);
}
function initializer({ cache, module }) {
const entry = module ? cache[MODULE_CACHE_FETCH](module) : null;
return {
cache,
module,
entry,
};
}
const ACTION_UPDATE = Symbol("ACTION_UPDATE");
const ACTION_RESOLVE = Symbol("ACTION_RESOLVE");
function reducer(state, action) {
switch (action.type) {
case ACTION_UPDATE: {
const { cache, module } = action;
return initializer({ cache, module });
}
case ACTION_RESOLVE: {
const { nextEntry, prevEntry } = action;
const { cache, module, entry: currentEntry } = state;
if (prevEntry === currentEntry) {
return {
cache,
module,
entry: nextEntry,
};
} else {
return state;
}
}
default: {
invariant(false, `invalid action type ${action.type}`);
}
}
}
const ModuleCacheContext = createContext(null);
export function ModuleCacheProvider({ cache, children }) {
invariant(
cache && typeof cache === "object" && cache[IS_MODULE_CACHE],
"expected cache to be an object returned by `create*ModuleCache`",
);
return (
<ModuleCacheContext.Provider value={cache}>
{children}
</ModuleCacheContext.Provider>
);
}
ModuleCacheProvider.propTypes = {
cache: PropTypes.object.isRequired,
children: PropTypes.node,
};
function useModuleCache() {
const cache = useContext(ModuleCacheContext);
invariant(
cache,
"`useModule` can only be used inside a <ModuleCacheProvider/>",
);
return cache;
}