Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: refactor loadable #232

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/loadable/assignImportedComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Promised } from '../types';
import { assignMetaData } from './metadata';
import { done } from './pending';
import { LOADABLE_SIGNATURE } from './registry';
import { toLoadable } from './toLoadable';
import {getLoadable} from "./loadable";

type ImportedDefinition = [Promised<any>, string, string, boolean];

Expand All @@ -15,7 +15,7 @@ export const assignImportedComponents = (set: ImportedDefinition[]) => {

set.forEach((imported) => {
const allowAutoLoad = !(imported[3] || !settings.fileFilter(imported[2]));
const loadable = toLoadable(imported[0], allowAutoLoad);
const loadable = getLoadable(imported[0], allowAutoLoad);
assignMetaData(loadable.mark, loadable, imported[1], imported[2]);
});

Expand Down
4 changes: 2 additions & 2 deletions src/loadable/loadable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function executeLoadable(importFunction: DefaultImport<any> | Loadable<an
* @internal
* @param importFunction
*/
export function getLoadable<T>(importFunction: DefaultImport<T> | Loadable<T>): Loadable<T> {
export function getLoadable<T>(importFunction: DefaultImport<T> | Loadable<T>, autoImport: boolean = true): Loadable<T> {
if ('resolution' in importFunction) {
return importFunction;
}
Expand Down Expand Up @@ -77,7 +77,7 @@ export function getLoadable<T>(importFunction: DefaultImport<T> | Loadable<T>):
});
}

const loadable = toLoadable(importFunction as any);
const loadable = toLoadable(importFunction as any, autoImport);
LOADABLE_WEAK_SIGNATURE.set(importFunction, loadable);

return loadable as any;
Expand Down
2 changes: 2 additions & 0 deletions src/loadable/marks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export const rehydrateMarks = (marks?: string[]): Promise<unknown> => {

const usedMarks = new Set<string>();

console.log({rehydratedMarks,LOADABLE_MARKS })

LOADABLE_MARKS.forEach(({ mark, loadable }) => {
if (markerOverlap(mark, rehydratedMarks)) {
mark.forEach((m) => usedMarks.add(m));
Expand Down
1 change: 1 addition & 0 deletions src/loadable/pending.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const isItReady = (): boolean => readyFlag;
* waits for all necessary imports to be fulfilled
*/
export const done = (): Promise<void> => {
console.log('pending:', pending.length);
if (pending.length) {
readyFlag = false;

Expand Down
5 changes: 5 additions & 0 deletions src/loadable/toLoadable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export interface InnerLoadable<T> extends Loadable<T> {
_probeChanges(): Promise<boolean>;
}

/**
* @deprecated to be used only inside {@link getLoadable}
* @internal
*/
export function toLoadable<T>(firstImportFunction: Promised<T>, autoImport = true): Loadable<T> {
let importFunction = firstImportFunction;
const loadImportedComponent = (): Promise<T> =>
Expand Down Expand Up @@ -116,6 +120,7 @@ export function toLoadable<T>(firstImportFunction: Promised<T>, autoImport = tru

load() {
if (!this.promise) {
console.log('loading', importFunction);
const promise = (this.promise = loadImportedComponent().then(
(payload) => {
this.done = true;
Expand Down