Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit dae4f0c

Browse files
committed
Remove withDeno hoc
1 parent a2db49a commit dae4f0c

File tree

4 files changed

+24
-48
lines changed

4 files changed

+24
-48
lines changed

framework/react/context.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import { createContext, ReactNode } from 'https://esm.sh/[email protected]'
22
import type { RouterURL } from '../../types.ts'
33
import { createBlankRouterURL } from '../core/routing.ts'
44
import { createNamedContext } from './helper.ts'
5-
import type { RendererStorage } from './renderer.ts'
5+
import type { RendererStore } from './renderer.ts'
66

77
export const RouterContext = createNamedContext<RouterURL>(createBlankRouterURL(), 'RouterContext')
88

9-
export const SSRContext = createContext<RendererStorage>({
9+
export const FallbackContext = createNamedContext<{ to: ReactNode }>({
10+
to: null
11+
}, 'FallbackContext')
12+
13+
export const SSRContext = createContext<RendererStore>({
1014
headElements: new Map(),
11-
scripts: new Map(),
1215
inlineStyles: new Map(),
13-
})
14-
15-
export const FallbackContext = createContext<{ to: ReactNode }>({
16-
to: null
16+
scripts: new Map(),
1717
})

framework/react/hoc.ts

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import {
99
} from 'https://esm.sh/[email protected]'
1010
import { FallbackContext } from './context.ts'
1111
import { isLikelyReactComponent } from './helper.ts'
12-
import { useDeno, useRouter } from './hooks.ts'
12+
import { useRouter } from './hooks.ts'
1313

1414
/**
15-
* `withRouter` allows you to use `useRouter` hook with class component.
15+
* `withRouter` injects the prop as current `RouterURL` of page routing.
1616
*
1717
* ```tsx
1818
* class MyComponent extends React.Component {
@@ -31,31 +31,7 @@ export function withRouter<P>(Component: ComponentType<P>) {
3131
}
3232

3333
/**
34-
* `withDeno` allows you to use `useDeno` hook with class component.
35-
*
36-
* ```tsx
37-
* class MyComponent extends React.Component {
38-
* render() {
39-
* return <p>{this.props.version}</p>
40-
* }
41-
* }
42-
* export default withDeno(() => ({ version: Deno.version.deno }))(MyComponent)
43-
* ```
44-
*
45-
* @param {Function} callback - hook callback.
46-
* @param {number} revalidate - revalidate duration in seconds.
47-
*/
48-
export function withDeno<T>(callback: () => (T | Promise<T>), revalidate?: number) {
49-
return function <P extends T>(Component: ComponentType<P>): ComponentType<Exclude<P, keyof T>> {
50-
return function WithDeno(props: Exclude<P, keyof T>) {
51-
const deno = useDeno<T>(callback, revalidate)
52-
return createElement(Component, { ...props, ...deno })
53-
}
54-
}
55-
}
56-
57-
/**
58-
* `dynamic` allows you to load a component asynchronously with SSR support.
34+
* `dynamic` loads a component asynchronously that is ignored at build time(SSR).
5935
*
6036
* ```jsx
6137
* const MyLogo = dynamic(() => import('~/components/logo.tsx'))
@@ -67,8 +43,6 @@ export function withDeno<T>(callback: () => (T | Promise<T>), revalidate?: numbe
6743
* )
6844
* }
6945
* ```
70-
*
71-
* @param {Function} factory - dynamic loading factory.
7246
*/
7347
export function dynamic<T extends ComponentType<any>>(
7448
factory: () => Promise<{ default: T }>

framework/react/hooks.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { inDeno } from './helper.ts'
77
export class AsyncUseDenoError extends Error { }
88

99
/**
10-
* `useRouter` allows you to use `RouterURL` obeject of routing
10+
* `useRouter` returns current `RouterURL` of page routing.
1111
*
1212
* ```tsx
1313
* export default function App() {
@@ -21,7 +21,7 @@ export function useRouter(): RouterURL {
2121
}
2222

2323
/**
24-
* `useDeno` allows you to use Deno runtime in build time(SSR).
24+
* `useDeno` hacks in Deno runtime at build time(SSR).
2525
*
2626
* ```tsx
2727
* export default function App() {
@@ -33,11 +33,12 @@ export function useRouter(): RouterURL {
3333
export function useDeno<T = any>(callback: () => (T | Promise<T>), options?: { key?: string | number, revalidate?: number }): T {
3434
const { key, revalidate } = options || {}
3535
const uuid = arguments[2] // generated by compiler
36+
const router = useRouter()
3637
const id = useMemo(() => uuid + (key ? '-' + key : ''), [key])
37-
const url = useRouter().toString()
3838

3939
return useMemo(() => {
4040
const store = globalThis as any
41+
const url = router.toString()
4142
const dataUrl = 'pagedata://' + url
4243
const expires = typeof revalidate === 'number' && !isNaN(revalidate) ? Date.now() + revalidate * 1000 : 0
4344

@@ -62,5 +63,5 @@ export function useDeno<T = any>(callback: () => (T | Promise<T>), options?: { k
6263

6364
const data = store[dataUrl + '#' + id]
6465
return data ? data.value ?? null : null
65-
}, [id, url])
66+
}, [id, router])
6667
}

framework/react/renderer.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import { AsyncUseDenoError } from './hooks.ts'
1010
import { isLikelyReactComponent } from './helper.ts'
1111
import { createPageProps } from './pageprops.ts'
1212

13-
export type RendererStorage = {
13+
export type RendererStore = {
1414
headElements: Map<string, { type: string, props: Record<string, any> }>
15-
scripts: Map<string, { props: Record<string, any> }>
1615
inlineStyles: Map<string, string>
16+
scripts: Map<string, { props: Record<string, any> }>
1717
}
1818

1919
export async function render(
@@ -29,10 +29,10 @@ export async function render(
2929
scripts: [],
3030
data: null,
3131
}
32-
const rendererStorage: RendererStorage = {
32+
const rendererStore: RendererStore = {
3333
headElements: new Map(),
34-
scripts: new Map(),
3534
inlineStyles: new Map(),
35+
scripts: new Map(),
3636
}
3737
const dataUrl = 'pagedata://' + url.toString()
3838
const asyncCalls: Array<[string, number, Promise<any>]> = []
@@ -87,9 +87,10 @@ export async function render(
8787
data[id] = { value, expires }
8888
})
8989
}
90+
Object.keys(rendererStore).forEach(key => rendererStore[key as keyof typeof rendererStore].clear())
9091
ret.body = renderToString(createElement(
9192
SSRContext.Provider,
92-
{ value: rendererStorage },
93+
{ value: rendererStore },
9394
createElement(
9495
RouterContext.Provider,
9596
{ value: url },
@@ -111,7 +112,7 @@ export async function render(
111112
}
112113

113114
// insert head child tags
114-
rendererStorage.headElements.forEach(({ type, props }) => {
115+
rendererStore.headElements.forEach(({ type, props }) => {
115116
const { children, ...rest } = props
116117
if (type === 'title') {
117118
if (util.isNEString(children)) {
@@ -134,7 +135,7 @@ export async function render(
134135
})
135136

136137
// insert script tags
137-
rendererStorage.scripts.forEach(({ props }) => {
138+
rendererStore.scripts.forEach(({ props }) => {
138139
const { children, dangerouslySetInnerHTML, ...attrs } = props
139140
if (dangerouslySetInnerHTML && util.isNEString(dangerouslySetInnerHTML.__html)) {
140141
ret.scripts.push({ ...attrs, innerText: dangerouslySetInnerHTML.__html })
@@ -155,7 +156,7 @@ export async function render(
155156
ret.head.push(`<link rel="stylesheet" href=${JSON.stringify(href)} data-module-id=${JSON.stringify(url)} ssr />`)
156157
}
157158
})
158-
for (const [url, css] of rendererStorage.inlineStyles.entries()) {
159+
for (const [url, css] of rendererStore.inlineStyles.entries()) {
159160
ret.head.push(`<style type="text/css" data-module-id=${JSON.stringify(url)} ssr>${css}</style>`)
160161
}
161162

0 commit comments

Comments
 (0)