Skip to content

Commit

Permalink
Use state loader instead of manually mounting middleware state (#264)
Browse files Browse the repository at this point in the history
* Use state loader for middleware

Signed-off-by: Marcos Candeia <[email protected]>

* Fix router

Signed-off-by: Marcos Candeia <[email protected]>

---------

Signed-off-by: Marcos Candeia <[email protected]>
  • Loading branch information
mcandeia authored May 30, 2023
1 parent 5e1a913 commit 6ca45f3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
9 changes: 9 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions engine/releases/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,13 @@ function mapGlobalToAccount(
}
c[middlewareConfig] = {
...middleware,
[state]: {
[state]: [
...middleware[state],
[name]: wellKnownAccount ? { __resolveType: name } : globalSection.props,
},
{
key: name,
value: wellKnownAccount ? { __resolveType: name } : globalSection.props,
},
],
};
return c;
}
Expand Down Expand Up @@ -237,10 +240,8 @@ const baseEntrypoint = Object.freeze({
__resolveType: "$live/flags/everyone.ts",
},
[middlewareConfig]: {
__resolveType: "resolve",
[state]: {
__resolveType: "resolve",
},
__resolveType: "$live/loaders/state.ts",
[state]: [],
},
[ENTRYPOINT]: {
audiences: [
Expand Down Expand Up @@ -411,7 +412,7 @@ const pagesToConfig = (
flags: Pick<Flag, "key" | "data" | "name">[],
ns: string,
) => {
const configs = p.sort((pageA, pageB) =>
const { [globalSections]: _, ...configs } = p.sort((pageA, pageB) =>
pageA.state === "global" ? -1 : pageB.state === "global" ? 1 : 0
) // process global first
.reduce(pageToConfig(ns), structuredClone(baseEntrypoint));
Expand Down
2 changes: 1 addition & 1 deletion handlers/routesSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const createUrlPatternFromHref = (href: string) => {

export const router = (
routes: Route[],
hrefRoutes: Record<string, Resolvable<Handler>>,
hrefRoutes: Record<string, Resolvable<Handler>> = {},
configs?: ResolveOptions,
flags?: Map<string, CookiedFlag>,
): Handler => {
Expand Down
21 changes: 15 additions & 6 deletions loaders/state.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Resolvable } from "../engine/core/resolver.ts";
import { LoaderContext } from "../mod.ts";
import { MiddlewareConfig } from "../routes/_middleware.ts";

export interface StateProp {
Expand All @@ -13,13 +14,21 @@ export interface Props {
* @title Shared application State Loader.
* @description Set the application state using resolvables.
*/
export default function StateLoader(
export default async function StateLoader(
{ state }: Props,
): MiddlewareConfig {
_req: Request,
{ get }: LoaderContext,
): Promise<MiddlewareConfig> {
const mState: Promise<[string, Resolvable]>[] = [];

for (const { key, value } of state) {
const resolved = get(value).then((resolved) =>
[key, resolved] as [string, Resolvable]
);
mState.push(resolved);
}

return {
state: state.reduce((acc, st) => {
acc[st.key] = st.value;
return acc;
}, {} as Record<string, Resolvable>),
state: Object.fromEntries(await Promise.all(mState)),
};
}

0 comments on commit 6ca45f3

Please sign in to comment.