import React, {useEffect, useState, useCallback, useMemo, useContext, ReactNode} from 'react' class PromiseWrapper { private callback: () => Promise private promise: Promise | null = null private _isFulfilled: boolean = false private _isResolved: boolean = false private _isRejected: boolean = false private _value: any private _error: any constructor(callback: () => Promise) { this.callback = callback } public get isFulfilled() { return this._isFulfilled } public get isResolved() { return this._isResolved } public get isRejected() { return this._isRejected } public get value() { return this._value } public get error() { return this._error } public invokePromise() { if (this.promise) return this.promise this.promise = this.callback() .then(value => { this._isFulfilled = true this._isResolved = true this._value = value.default return this._value }) .catch(err => { this._isFulfilled = true this._isRejected = true this._error = err return this._error }) return this.promise } } interface LazyCaptureContextType { notifyRender(path: string): void } export const LazyCaptureContext = React.createContext(null) export interface LazyCaptureProps { readonly rendered: string[] readonly children?: ReactNode } export function LazyCapture(props: LazyCaptureProps) { const notifyRender = useCallback((path: string) => { props.rendered.push(path) }, []) const context = useMemo(() => ({notifyRender}), []) return {props.children} } const lazyPromiseRegistry: {[key: string]: PromiseWrapper} = {} export function lazy(path: string, callback: () => Promise<{default: React.ComponentType}>) { if (lazyPromiseRegistry[path]) { console.warn(`Path "${path}" is already registered as a lazy component.`) } lazyPromiseRegistry[path] = new PromiseWrapper(callback) return () => { const [LazyComponent, setLazyComponent] = useState() const captureContext = useContext(LazyCaptureContext) const wrapper = lazyPromiseRegistry[path] if (captureContext) { captureContext.notifyRender(path) } useEffect(() => { if (!wrapper.isFulfilled) { wrapper.invokePromise().then(value => { setLazyComponent(() => value) }) } }) if (LazyComponent) { return } if (wrapper.isFulfilled) { return } if (typeof window === 'undefined') { throw new Error( `Path ${path} wasn't loaded on the server, did you forget to call "preloadAllLazyComponents"?` ) } return
Loading...
} } export async function preloadAllLazyComponents() { function arePromisesFullfilled() { return Object.entries(lazyPromiseRegistry).find(([_key, wrapper]) => !wrapper.isFulfilled) } while (arePromisesFullfilled()) { const promises = Object.entries(lazyPromiseRegistry).map(([_key, wrapper]) => wrapper.invokePromise() ) await Promise.all(promises) } } export async function preloadLazyComponents(keys: string[]): Promise { function arePromisesFullfilled() { return Object.entries(lazyPromiseRegistry).find( ([key, wrapper]) => keys.includes(key) && !wrapper.isFulfilled ) } while (arePromisesFullfilled()) { const promises = Object.entries(lazyPromiseRegistry) .filter(([key]) => keys.includes(key)) .map(([_key, wrapper]) => wrapper.invokePromise()) await Promise.all(promises) } }