feat: a new "island" based architechture (#97)
This commit is contained in:
+3
-3
@@ -1,4 +1,4 @@
|
||||
export * from "https://esm.sh/preact@10.5.14";
|
||||
export * from "https://esm.sh/preact@10.6.6";
|
||||
export {
|
||||
useCallback,
|
||||
useContext,
|
||||
@@ -11,7 +11,7 @@ export {
|
||||
useReducer,
|
||||
useRef,
|
||||
useState,
|
||||
} from "https://esm.sh/preact@10.5.14/hooks";
|
||||
} from "https://esm.sh/preact@10.6.6/hooks";
|
||||
export type {
|
||||
CreateHandle,
|
||||
EffectCallback,
|
||||
@@ -20,4 +20,4 @@ export type {
|
||||
Reducer,
|
||||
Ref,
|
||||
StateUpdater,
|
||||
} from "https://esm.sh/preact@10.5.14/hooks";
|
||||
} from "https://esm.sh/preact@10.6.6/hooks";
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
import { createContext, useContext } from "./deps.ts";
|
||||
import { IS_BROWSER } from "./utils.ts";
|
||||
|
||||
export const DATA_CONTEXT = createContext(new Map<string, unknown>());
|
||||
|
||||
export type Fetcher<T> = (key: string) => T | Promise<T>;
|
||||
|
||||
/**
|
||||
* The `useData` hook can be used to fetch some data during JIT rendering.
|
||||
* On the client the hook will return the data that was computed during JIT
|
||||
* rendering. If `useData` is called on the client with a different key than
|
||||
* during JIT rendering, an error will occur.
|
||||
*
|
||||
* The fetcher can be asynchronous. The render will be suspended while waiting
|
||||
* for the asynchronous data to be ready. Once the promise is ready we rerender.
|
||||
* Because of the suspense it is possible that a page or component can be
|
||||
* rendered more than once during a JIT render.
|
||||
*
|
||||
* ```tsx
|
||||
* export default function App() {
|
||||
* const { latest, versions } = useData("https://cdn.deno.land/std/meta/versions.json", fetcher);
|
||||
* return <p>The Deno standard library has {versions.length} versions, with {latest} being the most recent.</p>
|
||||
* }
|
||||
*
|
||||
* async function fetcher(url: string) {
|
||||
* const resp = await fetch(url);
|
||||
* return resp.json();
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @param key A unique identifier for this data.
|
||||
* @param fetcher The function used to fetch data based on the above key.
|
||||
* @returns The the data that the promise returned from `fetcher` resolves to.
|
||||
*/
|
||||
export function useData<T>(key: string, fetcher: Fetcher<T>): T {
|
||||
const DATA_CACHE = useContext(DATA_CONTEXT);
|
||||
|
||||
// If there is no data or pending promise for this key in the data cache yet,
|
||||
// call the fetcher, store it into the cache, and possibly suspend.
|
||||
if (!DATA_CACHE.has(key)) {
|
||||
if (IS_BROWSER) {
|
||||
throw new Error(
|
||||
`'useData' can not fetch new data on the client. The JIT render did not generate data for key '${key}'.`,
|
||||
);
|
||||
}
|
||||
const data = fetcher(key);
|
||||
if (data instanceof Promise) {
|
||||
data.then((data) => {
|
||||
DATA_CACHE.set(key, data);
|
||||
});
|
||||
}
|
||||
DATA_CACHE.set(key, data);
|
||||
}
|
||||
|
||||
const data = DATA_CACHE.get(key);
|
||||
// If the data is a promise, suspend the component.
|
||||
if (data instanceof Promise) throw data;
|
||||
// else return the data.
|
||||
return data as T;
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
import {
|
||||
ComponentChild,
|
||||
ComponentChildren,
|
||||
createContext,
|
||||
Fragment,
|
||||
h,
|
||||
useContext,
|
||||
} from "./deps.ts";
|
||||
import { IS_BROWSER } from "./utils.ts";
|
||||
|
||||
export const SUSPENSE_CONTEXT = createContext<ComponentChildren[]>([]);
|
||||
|
||||
export interface SuspenseProps {
|
||||
fallback: ComponentChild;
|
||||
children: ComponentChildren;
|
||||
}
|
||||
|
||||
export function Suspense(props: SuspenseProps) {
|
||||
if (IS_BROWSER) {
|
||||
throw new Error(`'<Suspense>' is not supported on the client yet.`);
|
||||
}
|
||||
|
||||
const suspenseData = useContext(SUSPENSE_CONTEXT);
|
||||
const id = suspenseData.push(props.children);
|
||||
|
||||
return h(
|
||||
Fragment,
|
||||
null,
|
||||
h("template", { id: `S:${id}` }),
|
||||
props.fallback,
|
||||
h("template", { id: `E:${id}` }),
|
||||
);
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
import { ComponentType } from "./deps.ts";
|
||||
|
||||
export interface PageProps<
|
||||
T extends Record<string, unknown> = Record<string, unknown>,
|
||||
> {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
export interface PageProps<T = any> {
|
||||
/** The URL of the request that resulted in this page being rendered. */
|
||||
url: URL;
|
||||
|
||||
@@ -21,10 +20,10 @@ export interface PageProps<
|
||||
params: Record<string, string>;
|
||||
|
||||
/**
|
||||
* Additional data passed into `HandlerContext.render`. Not present by
|
||||
* default.
|
||||
* Additional data passed into `HandlerContext.render`. Defaults to
|
||||
* `undefined`.
|
||||
*/
|
||||
renderData?: T;
|
||||
data: T;
|
||||
}
|
||||
|
||||
export interface AppProps {
|
||||
|
||||
Reference in New Issue
Block a user