Skip to content

Commit

Permalink
fix: expose short typescript helpers for useImported
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed Aug 23, 2020
1 parent 7b0cbf2 commit ef7da10
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/entrypoints/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getMarkedChunks, getMarkedFileNames } from '../loadable/markerMapper';
import { drainHydrateMarks, printDrainHydrateMarks, rehydrateMarks, waitForMarks } from '../loadable/marks';
import { done as whenComponentsReady } from '../loadable/pending';
import { addPreloader } from '../loadable/preloaders';
import { DefaultImport } from '../types';
import { ImportedComponent } from '../ui/Component';
import { ImportedComponent as ComponentLoader } from '../ui/Component';
import { ImportedStream } from '../ui/context';
Expand Down Expand Up @@ -45,5 +46,6 @@ export {
clearImportedCache,
ImportedConfiguration,
configure,
DefaultImport,
};
export default imported;
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export type DefaultComponent<P> = ComponentType<P> | DefaultImportedComponent<P>
export type DefaultComponentImport<T> = () => Promise<DefaultComponent<T>>;

export type Defaultable<P> = P | Default<P>;
/**
* standard "importer" accepted by the package.
* Could be {default:T} or T
*/
export type DefaultImport<T> = () => Promise<Defaultable<T>>;

export interface MarkMeta {
Expand Down
23 changes: 22 additions & 1 deletion src/ui/useImported.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,27 @@ export function useLoadable<T>(loadable: Loadable<T>, options: HookOptions = {})
);
}

/**
* short version of {@link useImported}
* @param {Function} importer - an function with `import` inside it
*
* @return {Object}
* - imported: if non empty - the data is loaded
* - error: if non empty - there is an error
* - loading: if true - then it's still loading
* - loadable: the under laying reference
* - retry: retry if case of failure
*
* @example
* const { imported: Imported, loadable } = useImported(importer);
* if (Imported) {
* // yep, it's imported
* return <Imported {...children} />;
* }
* // else throw resolution
* throw loadable.resolution;
*/
export function useImported<T>(importer: DefaultImport<T> | Loadable<T>): ImportedShape<T>;
/**
* The code splitting hook
* @param {Function} importer - an function with `import` inside it
Expand All @@ -118,7 +139,7 @@ export function useLoadable<T>(loadable: Loadable<T>, options: HookOptions = {})
* - loadable: the under laying reference
* - retry: retry if case of failure
*
* @see if you dont need precise control consider {@link useLazy}
* @see if you dont need precise control consider(and loading Components) {@link useLazy}
*
* @example
* const { imported: Imported, loadable } = useImported(importer);
Expand Down

0 comments on commit ef7da10

Please sign in to comment.