-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor code to transformer and update example for better rerender b…
…ehaviour
- Loading branch information
Showing
9 changed files
with
166 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import React from 'react'; | ||
|
||
export default function Tier2() { | ||
return <div>Tier 2</div>; | ||
export default function Tier2({enabled}: {enabled: boolean}) { | ||
if (enabled) return <div>Tier 2</div>; | ||
throw new Error('Enabled prop missing'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,45 @@ | ||
import React, {FC} from 'react'; | ||
import React, { | ||
ComponentType, | ||
ForwardRefExoticComponent, | ||
PropsWithoutRef, | ||
RefAttributes, | ||
forwardRef, | ||
} from 'react'; | ||
|
||
let loaderMap = new WeakMap<DeferredImport<any>, Promise<any>>(); | ||
let componentMap = new WeakMap<DeferredImport<any>, any>(); | ||
export function deferredLoadComponent<P extends {[k: string]: any} | undefined>( | ||
resource: DeferredImport<{default: ComponentType<P>}>, | ||
): ForwardRefExoticComponent< | ||
PropsWithoutRef<P> & RefAttributes<ComponentType<P>> | ||
> { | ||
// Create a deferred component map in the global context, so we can reuse the components everywhere | ||
if (!globalThis.deferredComponentMap) { | ||
globalThis.deferredComponentMap = new WeakMap<DeferredImport<any>, any>(); | ||
} | ||
|
||
export function deferredLoadComponent<T>( | ||
resource: DeferredImport<T extends {default: any} ? T : never>, | ||
): FC { | ||
if (!loaderMap.has(resource)) { | ||
loaderMap.set( | ||
resource, | ||
new Promise(resolve => { | ||
resource.onReady(component => { | ||
componentMap.set(resource, component); | ||
resolve(component); | ||
}); | ||
}), | ||
); | ||
if (globalThis.deferredComponentMap.has(resource)) { | ||
return globalThis.deferredComponentMap.get(resource); | ||
} | ||
|
||
return function WrappedComponent(props) { | ||
const Component = componentMap.get(resource); | ||
let Component: ComponentType | undefined; | ||
const loader = new Promise(resolve => { | ||
resource.onReady(loaded => { | ||
Component = loaded; | ||
resolve(loaded); | ||
}); | ||
}); | ||
|
||
const wrapper = forwardRef<ComponentType<P>, P>(function DeferredComponent( | ||
props, | ||
ref, | ||
) { | ||
if (Component) { | ||
return <Component {...props} />; | ||
return <Component {...props} ref={ref} />; | ||
} else { | ||
throw ( | ||
loaderMap.get(resource) ?? new Error(`Loader map did not have resource`) | ||
); | ||
throw loader; | ||
} | ||
}; | ||
}); | ||
|
||
// Store in weakmap so we only have one instance | ||
globalThis.deferredComponentMap.set(resource, wrapper); | ||
return wrapper; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export function load(loader) { | ||
var listeners = new Set(); | ||
var resolved = false; | ||
if (loader instanceof Promise) { | ||
loader.then(mod => { | ||
resolved = true; | ||
for (let listener of listeners) { | ||
listener?.(mod.default); | ||
} | ||
}); | ||
} else { | ||
resolved = true; | ||
} | ||
return { | ||
onReady: listener => { | ||
if (resolved) { | ||
listener(loader.default); | ||
} else { | ||
listeners.add(listener); | ||
} | ||
}, | ||
}; | ||
} |