generated from LankyMoose/pnpm-monorepo-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib - initial implementation of "lazy" components
- Loading branch information
1 parent
84d70c3
commit 690c842
Showing
6 changed files
with
72 additions
and
5 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
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,35 @@ | ||
import { createElement } from "./element.js" | ||
import { useRequestUpdate } from "./hooks/utils.js" | ||
|
||
const lazyCache = new WeakMap<() => Promise<Kaioken.FC>, Kaioken.FC | null>() | ||
|
||
type LazyComponentProps<T extends Kaioken.FC> = Kaioken.InferProps<T> & { | ||
fallback?: JSX.Element | ||
} | ||
|
||
export function lazy<T extends Kaioken.FC>( | ||
componentPromise: () => Promise<T> | ||
): Kaioken.FC<LazyComponentProps<T>> { | ||
function LazyComponent(props: LazyComponentProps<T>) { | ||
const { fallback = null, ...rest } = props | ||
const requestUpdate = useRequestUpdate() | ||
if (!lazyCache.has(componentPromise)) { | ||
lazyCache.set(componentPromise, null) | ||
componentPromise().then((component) => { | ||
lazyCache.set(componentPromise, component) | ||
requestUpdate() | ||
}) | ||
return fallback | ||
} | ||
|
||
const component: Kaioken.FC<LazyComponentProps<T>> | null = | ||
lazyCache.get(componentPromise)! | ||
if (component === null) { | ||
componentPromise().then(requestUpdate) | ||
return fallback | ||
} | ||
return createElement(component, rest) | ||
} | ||
LazyComponent.displayName = "Kaioken.lazy" | ||
return LazyComponent | ||
} |
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,26 @@ | ||
import { lazy, signal } from "kaioken" | ||
|
||
const sleep = async (ms: number) => | ||
new Promise((resolve) => setTimeout(resolve, ms)) | ||
|
||
function MiniCounter() { | ||
const count = signal(0) | ||
return <button onclick={() => count.value++}>Increment {count}</button> | ||
} | ||
|
||
const LazyCounter = lazy(() => { | ||
console.log("loading Counter") | ||
return sleep(1000).then(() => | ||
import("./Counter").then(({ Counter }) => Counter) | ||
) | ||
}) | ||
|
||
export function LazyDemo() { | ||
return ( | ||
<div className="flex flex-col"> | ||
<h1>Lazy Demo</h1> | ||
<LazyCounter fallback={<MiniCounter />}>sdsd</LazyCounter> | ||
<LazyCounter fallback={"TEST"}>sdsd</LazyCounter> | ||
</div> | ||
) | ||
} |