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

feat: throw if aborted while rendering section #713

Merged
merged 5 commits into from
Jul 19, 2024
Merged
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
3 changes: 2 additions & 1 deletion blocks/section.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ export interface SectionModule<
ReturnType<ComponentFunc<TLoaderProps | TActionProps>>,
PreactComponent
> {
LoadingFallback?: ComponentType;
// Fix this ComponentType<any>
LoadingFallback?: ComponentType<any>;
ErrorFallback?: ComponentType<{ error?: Error }>;
loader?: PropsLoader<TConfig, TLoaderProps>;
action?: PropsLoader<TConfig, TActionProps>;
Expand Down
28 changes: 25 additions & 3 deletions components/section.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Component, type ComponentType, createContext, Fragment } from "preact";
import { useContext } from "preact/hooks";
import type { HttpContext } from "../blocks/handler.ts";
import type { RequestState } from "../blocks/utils.tsx";
import { Context } from "../deco.ts";
import { Context, RequestContext } from "../deco.ts";
import { Murmurhash3 } from "../deps.ts";
import type { ComponentFunc } from "../engine/block.ts";
import type { FieldResolver } from "../engine/core/resolver.ts";
Expand All @@ -17,7 +17,7 @@ export interface SectionContext extends HttpContext<RequestState> {
renderSalt?: string;
device: Device;
framework: "fresh" | "htmx";
deploymentId?: string
deploymentId?: string;
}

export const SectionContext = createContext<SectionContext | undefined>(
Expand Down Expand Up @@ -146,6 +146,11 @@ export const withSection = <TProps,>(
};
let device: Device | null = null;

/**
* Get the signal created during the request;
*/
const signal = RequestContext.signal;

return {
props,
Component: (props: TProps) => {
Expand All @@ -162,6 +167,12 @@ export const withSection = <TProps,>(
const id = `${idPrefix}-${renderSalt}`; // all children of the same parent will have the same renderSalt, but different renderCount
renderCount = ++renderCount % MAX_RENDER_COUNT;

const Throw = () => {
// If the signal from request is aborted, then throw
signal?.throwIfAborted();
return null;
};

return (
<SectionContext.Provider
value={{
Expand All @@ -187,7 +198,17 @@ export const withSection = <TProps,>(
loading={() => (
<binding.LoadingFallback id={id}>
{/* @ts-ignore difficult typing this */}
<LoadingFallback {...props} />
<LoadingFallback
{...new Proxy<Partial<TProps>>(props, {
get: (value: Partial<TProps>, prop) => {
try {
return Reflect.get(value, prop);
} catch (_) {
return undefined;
}
},
})}
/>
</binding.LoadingFallback>
)}
error={({ error }) => (
Expand All @@ -204,6 +225,7 @@ export const withSection = <TProps,>(
)
)}
>
{<Throw />}
<ComponentFunc {...props} />
</ErrorBoundary>
</section>
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"preact": "https://esm.sh/[email protected]?pin=102",
"partytown/": "https://deno.land/x/[email protected]/",
"std/": "https://deno.land/[email protected]/",
"$fresh/": "https://denopkg.com/denoland/[email protected].5/",
"$fresh/": "https://denopkg.com/denoland/[email protected].8/",
"deco/": "./"
},
"compilerOptions": {
Expand Down
Loading