kiru 0.44.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +7 -0
- package/README.md +5 -0
- package/package.json +81 -0
- package/src/appContext.ts +186 -0
- package/src/cloneVNode.ts +14 -0
- package/src/constants.ts +146 -0
- package/src/context.ts +56 -0
- package/src/dom.ts +712 -0
- package/src/element.ts +54 -0
- package/src/env.ts +6 -0
- package/src/error.ts +85 -0
- package/src/flags.ts +15 -0
- package/src/form/index.ts +662 -0
- package/src/form/types.ts +261 -0
- package/src/form/utils.ts +19 -0
- package/src/generateId.ts +19 -0
- package/src/globalContext.ts +161 -0
- package/src/globals.ts +21 -0
- package/src/hmr.ts +178 -0
- package/src/hooks/index.ts +14 -0
- package/src/hooks/useAsync.ts +136 -0
- package/src/hooks/useCallback.ts +31 -0
- package/src/hooks/useContext.ts +79 -0
- package/src/hooks/useEffect.ts +44 -0
- package/src/hooks/useEffectEvent.ts +24 -0
- package/src/hooks/useId.ts +42 -0
- package/src/hooks/useLayoutEffect.ts +47 -0
- package/src/hooks/useMemo.ts +33 -0
- package/src/hooks/useReducer.ts +50 -0
- package/src/hooks/useRef.ts +40 -0
- package/src/hooks/useState.ts +62 -0
- package/src/hooks/useSyncExternalStore.ts +59 -0
- package/src/hooks/useViewTransition.ts +26 -0
- package/src/hooks/utils.ts +259 -0
- package/src/hydration.ts +67 -0
- package/src/index.ts +61 -0
- package/src/jsx.ts +11 -0
- package/src/lazy.ts +238 -0
- package/src/memo.ts +48 -0
- package/src/portal.ts +43 -0
- package/src/profiling.ts +105 -0
- package/src/props.ts +36 -0
- package/src/reconciler.ts +531 -0
- package/src/renderToString.ts +91 -0
- package/src/router/index.ts +2 -0
- package/src/router/route.ts +51 -0
- package/src/router/router.ts +275 -0
- package/src/router/routerUtils.ts +49 -0
- package/src/scheduler.ts +522 -0
- package/src/signals/base.ts +237 -0
- package/src/signals/computed.ts +139 -0
- package/src/signals/effect.ts +60 -0
- package/src/signals/globals.ts +11 -0
- package/src/signals/index.ts +12 -0
- package/src/signals/jsx.ts +45 -0
- package/src/signals/types.ts +10 -0
- package/src/signals/utils.ts +12 -0
- package/src/signals/watch.ts +151 -0
- package/src/ssr/client.ts +29 -0
- package/src/ssr/hydrationBoundary.ts +63 -0
- package/src/ssr/index.ts +1 -0
- package/src/ssr/server.ts +124 -0
- package/src/store.ts +241 -0
- package/src/swr.ts +360 -0
- package/src/transition.ts +80 -0
- package/src/types.dom.ts +1250 -0
- package/src/types.ts +209 -0
- package/src/types.utils.ts +39 -0
- package/src/utils.ts +581 -0
- package/src/warning.ts +9 -0
package/src/element.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { $FRAGMENT, $MEMO } from "./constants.js"
|
|
2
|
+
import { isMemoFn } from "./memo.js"
|
|
3
|
+
import { isValidElementKeyProp, isValidElementRefProp } from "./props.js"
|
|
4
|
+
|
|
5
|
+
export function createElement<T extends Kaioken.VNode["type"]>(
|
|
6
|
+
type: T,
|
|
7
|
+
props: null | Record<string, unknown> = null,
|
|
8
|
+
...children: unknown[]
|
|
9
|
+
): Kaioken.VNode {
|
|
10
|
+
if ((type as any) === Fragment) {
|
|
11
|
+
return Fragment({ children: children as any, ...props })
|
|
12
|
+
}
|
|
13
|
+
const node: Kaioken.VNode = {
|
|
14
|
+
type,
|
|
15
|
+
flags: 0,
|
|
16
|
+
index: 0,
|
|
17
|
+
depth: 0,
|
|
18
|
+
props: {},
|
|
19
|
+
parent: null,
|
|
20
|
+
sibling: null,
|
|
21
|
+
child: null,
|
|
22
|
+
prev: null,
|
|
23
|
+
deletions: null,
|
|
24
|
+
}
|
|
25
|
+
if (isMemoFn(type)) {
|
|
26
|
+
node.isMemoized = true
|
|
27
|
+
node.arePropsEqual = type[$MEMO].arePropsEqual
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (props !== null) {
|
|
31
|
+
const { key, ref, ...rest } = props
|
|
32
|
+
if (isValidElementKeyProp(key)) node.props.key = key.toString()
|
|
33
|
+
if (isValidElementRefProp(ref)) node.props.ref = ref
|
|
34
|
+
Object.assign(node.props, rest)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const _children =
|
|
38
|
+
children.length === 1 ? children[0] : children.length > 1 ? children : null
|
|
39
|
+
if (_children !== null) {
|
|
40
|
+
node.props.children = _children
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return node
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function Fragment({
|
|
47
|
+
children,
|
|
48
|
+
key,
|
|
49
|
+
}: {
|
|
50
|
+
children: JSX.Children
|
|
51
|
+
key?: JSX.ElementKey
|
|
52
|
+
}): Kaioken.VNode {
|
|
53
|
+
return createElement($FRAGMENT, key ? { key } : null, children)
|
|
54
|
+
}
|
package/src/env.ts
ADDED
package/src/error.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { $KAIOKEN_ERROR } from "./constants.js"
|
|
2
|
+
import { __DEV__ } from "./env.js"
|
|
3
|
+
import { findParent, noop } from "./utils.js"
|
|
4
|
+
|
|
5
|
+
type KaiokenErrorOptions =
|
|
6
|
+
| string
|
|
7
|
+
| {
|
|
8
|
+
message: string
|
|
9
|
+
/** Used to indicate that the error is fatal and should crash the application */
|
|
10
|
+
fatal?: boolean
|
|
11
|
+
/** Used to generate custom node stack */
|
|
12
|
+
vNode?: Kaioken.VNode
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class KaiokenError extends Error {
|
|
16
|
+
[$KAIOKEN_ERROR] = true
|
|
17
|
+
/** Indicates whether the error is fatal and should crash the application */
|
|
18
|
+
fatal?: boolean
|
|
19
|
+
/** Present if vNode is provided */
|
|
20
|
+
customNodeStack?: string
|
|
21
|
+
constructor(optionsOrMessage: KaiokenErrorOptions) {
|
|
22
|
+
const message =
|
|
23
|
+
typeof optionsOrMessage === "string"
|
|
24
|
+
? optionsOrMessage
|
|
25
|
+
: optionsOrMessage.message
|
|
26
|
+
super(message)
|
|
27
|
+
if (typeof optionsOrMessage !== "string") {
|
|
28
|
+
if (__DEV__) {
|
|
29
|
+
if (optionsOrMessage?.vNode) {
|
|
30
|
+
this.customNodeStack = captureErrorStack(optionsOrMessage.vNode)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
this.fatal = optionsOrMessage?.fatal
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
static isKaiokenError(error: unknown): error is KaiokenError {
|
|
38
|
+
return (
|
|
39
|
+
error instanceof Error && (error as KaiokenError)[$KAIOKEN_ERROR] === true
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function captureErrorStack(vNode: Kaioken.VNode) {
|
|
45
|
+
let n = vNode
|
|
46
|
+
let componentFns: string[] = []
|
|
47
|
+
while (n) {
|
|
48
|
+
if (!n.parent) break // skip root node
|
|
49
|
+
if (typeof n.type === "function") {
|
|
50
|
+
componentFns.push(getComponentErrorDisplayText(n.type))
|
|
51
|
+
} else if (typeof n.type === "string") {
|
|
52
|
+
componentFns.push(n.type)
|
|
53
|
+
}
|
|
54
|
+
n = n.parent
|
|
55
|
+
}
|
|
56
|
+
const componentNode = (
|
|
57
|
+
typeof vNode.type === "function"
|
|
58
|
+
? vNode
|
|
59
|
+
: findParent(vNode, (n) => typeof n.type === "function")
|
|
60
|
+
) as (Kaioken.VNode & { type: Function }) | null
|
|
61
|
+
return `The above error occurred in the <${getFunctionName(
|
|
62
|
+
componentNode?.type || noop
|
|
63
|
+
)}> component:
|
|
64
|
+
|
|
65
|
+
${componentFns.map((x) => ` at ${x}`).join("\n")}\n`
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function getComponentErrorDisplayText(fn: Function) {
|
|
69
|
+
let str = getFunctionName(fn)
|
|
70
|
+
if (__DEV__) {
|
|
71
|
+
const fileLink = getComponentFileLink(fn)
|
|
72
|
+
if (fileLink) {
|
|
73
|
+
str = `${str} (${fileLink})`
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return str
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function getFunctionName(fn: Function) {
|
|
80
|
+
return (fn as any).displayName ?? (fn.name || "Anonymous Function")
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function getComponentFileLink(fn: Function) {
|
|
84
|
+
return fn.toString().match(/\/\/ \[kaioken_devtools\]:(.*)/)?.[1] ?? null
|
|
85
|
+
}
|
package/src/flags.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export const flags = {
|
|
2
|
+
// Set the flag at position `n` (0-based index)
|
|
3
|
+
set(field: number, flag: number): number {
|
|
4
|
+
return (field |= 1 << flag)
|
|
5
|
+
},
|
|
6
|
+
// Check if the flag at position `n` is set (true) or not (false)
|
|
7
|
+
get(field: number, n: number): boolean {
|
|
8
|
+
return (field & (1 << n)) !== 0
|
|
9
|
+
},
|
|
10
|
+
// Unset all flags between `start` and `end`
|
|
11
|
+
unsetRange(field: number, start: number, end: number): number {
|
|
12
|
+
const mask = ((1 << (end - start + 1)) - 1) << start
|
|
13
|
+
return field & ~mask
|
|
14
|
+
},
|
|
15
|
+
} as const
|