MCPcopy
hub / github.com/gregberge/loadable-components / loadable

Function loadable

packages/component/src/createLoadable.js:47–358  ·  view source on GitHub ↗
(loadableConstructor, options = {})

Source from the content-addressed store, hash-verified

45 onLoad,
46}) {
47 function loadable(loadableConstructor, options = {}) {
48 const ctor = resolveConstructor(loadableConstructor)
49 const cache = {}
50
51 /**
52 * Cachekey represents the component to be loaded
53 * if key changes - component has to be reloaded
54 * @param props
55 * @returns {null|Component}
56 */
57 function getCacheKey(props) {
58 if (options.cacheKey) {
59 return options.cacheKey(props)
60 }
61 if (ctor.resolve) {
62 return ctor.resolve(props)
63 }
64 return 'static'
65 }
66
67 /**
68 * Resolves loaded `module` to a specific `Component
69 * @param module
70 * @param props
71 * @param Loadable
72 * @returns Component
73 */
74 function resolve(module, props, Loadable) {
75 const Component = options.resolveComponent
76 ? options.resolveComponent(module, props)
77 : defaultResolveComponent(module)
78
79 // FIXME: suppressed due to https://github.com/gregberge/loadable-components/issues/990
80 // if (options.resolveComponent && !ReactIs.isValidElementType(Component)) {
81 // throw new Error(
82 // `resolveComponent returned something that is not a React component!`,
83 // )
84 // }
85 hoistNonReactStatics(Loadable, Component, {
86 preload: true,
87 })
88 return Component
89 }
90
91 const cachedLoad = props => {
92 const cacheKey = getCacheKey(props)
93 let promise = cache[cacheKey]
94
95 if (!promise || promise.status === STATUS_REJECTED) {
96 promise = ctor.requireAsync(props)
97 promise.status = STATUS_PENDING
98
99 cache[cacheKey] = promise
100
101 promise.then(
102 () => {
103 promise.status = STATUS_RESOLVED
104 },

Callers 12

loadable.test.jsFile · 0.85
lazyFunction · 0.85
App.tsxFile · 0.85
index.jsFile · 0.85
App.jsFile · 0.85
App.jsFile · 0.85
index.jsFile · 0.85
App.jsFile · 0.85
App.jsFile · 0.85
App.jsFile · 0.85

Calls 3

resolveConstructorFunction · 0.85
withChunkExtractorFunction · 0.85
cachedLoadFunction · 0.85

Tested by

no test coverage detected