Skip to content
Myr

context.ts

This file

Path

src/jsx/dom/context.ts

52 lines · 1.4 KiB · TypeScript

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than f0b094d point at it.

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 1–52why, one step back
1import type { Child } from '../base'
2import { DOM_ERROR_HANDLER } from '../constants'3import type { Context } from '../context'4import { globalContexts } from '../context'5import { setInternalTagFlag } from './utils'67export const createContextProviderFunction =8 <T>(values: T[]): Function =>9 ({ value, children }: { value: T; children: Child[] }) => {10 if (!children) {11 return undefined12 }1314 // eslint-disable-next-line @typescript-eslint/no-explicit-any15 const props: { children: any } = {16 children: [17 {18 tag: setInternalTagFlag(() => {19 values.push(value)20 }),21 props: {},22 },23 ],24 }25 if (Array.isArray(children)) {26 props.children.push(...children.flat())27 } else {28 props.children.push(children)29 }30 props.children.push({31 tag: setInternalTagFlag(() => {32 values.pop()33 }),34 props: {},35 })36 const res = { tag: '', props, type: '' }37 // eslint-disable-next-line @typescript-eslint/no-explicit-any38 ;(res as any)[DOM_ERROR_HANDLER] = (err: unknown) => {39 values.pop()40 throw err41 }42 return res43 }4445export const createContext = <T>(defaultValue: T): Context<T> => {46 const values = [defaultValue]47 const context: Context<T> = createContextProviderFunction(values) as Context<T>48 context.values = values49 context.Provider = context50 globalContexts.push(context as Context<unknown>)51 return context52}