Skip to content

Commit 1903536

Browse files
committed
add lazy import to replace solidjs lazy fixes #81
1 parent f4424c6 commit 1903536

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/primitives/LazyImport.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import {
2+
createSignal,
3+
createResource,
4+
createMemo,
5+
untrack,
6+
Component,
7+
JSX,
8+
sharedConfig,
9+
} from 'solid-js';
10+
11+
// lazy load a function component asynchronously
12+
export function lazy<T extends Component<any>>(
13+
fn: () => Promise<{ default: T }>,
14+
): T & { preload: () => Promise<{ default: T }> } {
15+
let comp: () => T | undefined;
16+
let p: Promise<{ default: T }> | undefined;
17+
const wrap: T & { preload?: () => void } = ((props: any) => {
18+
const ctx = sharedConfig.context;
19+
if (ctx) {
20+
const [s, set] = createSignal<T>();
21+
sharedConfig.count || (sharedConfig.count = 0);
22+
sharedConfig.count++;
23+
(p || (p = fn())).then((mod) => {
24+
!sharedConfig.done && (sharedConfig.context = ctx);
25+
sharedConfig.count!--;
26+
set(() => mod.default);
27+
sharedConfig.context = undefined;
28+
});
29+
comp = s;
30+
} else if (!comp) {
31+
const [s] = createResource<T>(() =>
32+
(p || (p = fn())).then((mod) => mod.default),
33+
);
34+
comp = s;
35+
}
36+
let Comp: T | undefined;
37+
return createMemo(() =>
38+
(Comp = comp())
39+
? untrack(() => {
40+
if (!ctx || sharedConfig.done) return Comp!(props);
41+
const c = sharedConfig.context;
42+
sharedConfig.context = ctx;
43+
const r = Comp!(props);
44+
sharedConfig.context = c;
45+
return r;
46+
})
47+
: null,
48+
) as unknown as JSX.Element;
49+
}) as T;
50+
wrap.preload = () =>
51+
p || ((p = fn()).then((mod) => (comp = () => mod.default)), p);
52+
return wrap as T & { preload: () => Promise<{ default: T }> };
53+
}

src/primitives/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export * from './createInfiniteItems.js';
44
export * from './useMouse.js';
55
export * from './portal.jsx';
66
export * from './Lazy.jsx';
7+
export * from './LazyImport.js';
78
export * from './Image.jsx';
89
export * from './Visible.jsx';
910
export * from './router.js';

0 commit comments

Comments
 (0)