context.ts
This file
Path
src/jsx/context.ts
54 lines · 1.6 KiB · TypeScript
Last changed by
fix(jsx): preserve context when using await before html helper (#4662)Kai Gritunbefore adoptioncea7b7b
History of this fileTake it
The walk stopped after 200 commits: lines older than 793ffda point at it.
chore: enable `skipLibCheck` to resolve TypeScript compilation issues (#4342)Yusuke Wadabefore adoption793ffda
lines 1–26why, one step back1import { raw } from '../helper/html'2import type { HtmlEscapedString } from '../utils/html'3import { JSXFragmentNode } from './base'4import { DOM_RENDERER } from './constants'5import { createContextProviderFunction } from './dom/context'6import type { FC, PropsWithChildren } from './'78export interface Context<T> extends FC<PropsWithChildren<{ value: T }>> {9 values: T[]10 Provider: FC<PropsWithChildren<{ value: T }>>11}1213export const globalContexts: Context<unknown>[] = []1415export const createContext = <T>(defaultValue: T): Context<T> => {16 const values = [defaultValue]17 const context: Context<T> = ((props): HtmlEscapedString | Promise<HtmlEscapedString> => {18 values.push(props.value)19 let string20 try {21 string = props.children22 ? (Array.isArray(props.children)23 ? new JSXFragmentNode('', {}, props.children)24 : props.children25 ).toString()26 : ''fix(jsx): preserve context when using await before html helper (#4662)Kai Gritunbefore adoptioncea7b7b
line 27why, one step back27 } catch (e) {chore: enable `skipLibCheck` to resolve TypeScript compilation issues (#4342)Yusuke Wadabefore adoption793ffda
line 28why, one step back28 values.pop()fix(jsx): preserve context when using await before html helper (#4662)Kai Gritunbefore adoptioncea7b7b
line 29why, one step back29 throw echore: enable `skipLibCheck` to resolve TypeScript compilation issues (#4342)Yusuke Wadabefore adoption793ffda
lines 30–32why, one step back30 }3132 if (string instanceof Promise) {fix(jsx): preserve context when using await before html helper (#4662)Kai Gritunbefore adoptioncea7b7b
lines 33–35why, one step back33 return string34 .finally(() => values.pop())35 .then((resString) => raw(resString, (resString as HtmlEscapedString).callbacks))chore: enable `skipLibCheck` to resolve TypeScript compilation issues (#4342)Yusuke Wadabefore adoption793ffda
line 36why, one step back36 } else {fix(jsx): preserve context when using await before html helper (#4662)Kai Gritunbefore adoptioncea7b7b
line 37why, one step back37 values.pop()chore: enable `skipLibCheck` to resolve TypeScript compilation issues (#4342)Yusuke Wadabefore adoption793ffda
lines 38–54why, one step back38 return raw(string)39 }40 }) as Context<T>41 context.values = values42 context.Provider = context4344 // eslint-disable-next-line @typescript-eslint/no-explicit-any45 ;(context as any)[DOM_RENDERER] = createContextProviderFunction(values)4647 globalContexts.push(context as Context<unknown>)4849 return context50}5152export const useContext = <T>(context: Context<T>): T => {53 return context.values.at(-1) as T54}