From 3b4f3c872d0c48f17941c6392e22d63c5f60919d Mon Sep 17 00:00:00 2001 From: atellmer Date: Fri, 25 Nov 2022 04:06:14 -0800 Subject: [PATCH] types --- packages/core/.gitignore | 3 +- packages/core/.prettierignore | 1 + packages/core/package.json | 8 +- packages/core/tsconfig.json | 12 +++ packages/core/types/batch/batch.d.ts | 4 + packages/core/types/batch/index.d.ts | 1 + packages/core/types/component/component.d.ts | 36 +++++++++ packages/core/types/component/index.d.ts | 2 + packages/core/types/component/types.d.ts | 28 +++++++ packages/core/types/constants.d.ts | 10 +++ packages/core/types/context/context.d.ts | 3 + packages/core/types/context/index.d.ts | 2 + packages/core/types/context/types.d.ts | 15 ++++ packages/core/types/element/element.d.ts | 7 ++ packages/core/types/element/index.d.ts | 1 + packages/core/types/fiber/fiber.d.ts | 45 +++++++++++ packages/core/types/fiber/index.d.ts | 2 + packages/core/types/fiber/types.d.ts | 20 +++++ packages/core/types/fragment/fragment.d.ts | 18 +++++ packages/core/types/fragment/index.d.ts | 1 + packages/core/types/helpers/index.d.ts | 39 +++++++++ packages/core/types/index.d.ts | 39 +++++++++ packages/core/types/lazy/index.d.ts | 1 + packages/core/types/lazy/lazy.d.ts | 25 ++++++ packages/core/types/memo/index.d.ts | 1 + packages/core/types/memo/memo.d.ts | 10 +++ packages/core/types/platform/index.d.ts | 2 + packages/core/types/platform/platform.d.ts | 2 + packages/core/types/platform/types.d.ts | 19 +++++ packages/core/types/ref/index.d.ts | 2 + packages/core/types/ref/ref.d.ts | 7 ++ packages/core/types/ref/types.d.ts | 5 ++ packages/core/types/scope/index.d.ts | 1 + packages/core/types/scope/scope.d.ts | 80 +++++++++++++++++++ packages/core/types/shared/index.d.ts | 1 + packages/core/types/shared/types.d.ts | 10 +++ packages/core/types/suspense/index.d.ts | 1 + packages/core/types/suspense/suspense.d.ts | 28 +++++++ packages/core/types/unmount/index.d.ts | 1 + packages/core/types/unmount/unmount.d.ts | 4 + packages/core/types/use-callback/index.d.ts | 1 + .../core/types/use-callback/use-callback.d.ts | 2 + packages/core/types/use-context/index.d.ts | 1 + .../core/types/use-context/use-context.d.ts | 3 + .../core/types/use-deferred-value/index.d.ts | 1 + .../use-deferred-value.d.ts | 5 ++ packages/core/types/use-effect/index.d.ts | 1 + packages/core/types/use-effect/types.d.ts | 2 + .../core/types/use-effect/use-effect.d.ts | 15 ++++ packages/core/types/use-error/index.d.ts | 1 + packages/core/types/use-error/use-error.d.ts | 2 + packages/core/types/use-event/index.d.ts | 1 + packages/core/types/use-event/use-event.d.ts | 2 + .../types/use-imperative-handle/index.d.ts | 1 + .../use-imperative-handle.d.ts | 3 + .../core/types/use-layout-effect/index.d.ts | 1 + .../use-layout-effect/use-layout-effect.d.ts | 6 ++ packages/core/types/use-memo/index.d.ts | 1 + packages/core/types/use-memo/use-memo.d.ts | 2 + packages/core/types/use-reducer/index.d.ts | 2 + packages/core/types/use-reducer/types.d.ts | 4 + .../core/types/use-reducer/use-reducer.d.ts | 7 ++ packages/core/types/use-ref/index.d.ts | 1 + packages/core/types/use-ref/use-ref.d.ts | 3 + packages/core/types/use-state/index.d.ts | 1 + packages/core/types/use-state/use-state.d.ts | 7 ++ packages/core/types/use-update/index.d.ts | 1 + .../core/types/use-update/use-update.d.ts | 3 + packages/core/types/view/index.d.ts | 2 + packages/core/types/view/types.d.ts | 11 +++ packages/core/types/view/view.d.ts | 57 +++++++++++++ packages/core/types/walk/index.d.ts | 1 + packages/core/types/walk/walk.d.ts | 13 +++ 73 files changed, 657 insertions(+), 4 deletions(-) create mode 100644 packages/core/.prettierignore create mode 100644 packages/core/tsconfig.json create mode 100644 packages/core/types/batch/batch.d.ts create mode 100644 packages/core/types/batch/index.d.ts create mode 100644 packages/core/types/component/component.d.ts create mode 100644 packages/core/types/component/index.d.ts create mode 100644 packages/core/types/component/types.d.ts create mode 100644 packages/core/types/constants.d.ts create mode 100644 packages/core/types/context/context.d.ts create mode 100644 packages/core/types/context/index.d.ts create mode 100644 packages/core/types/context/types.d.ts create mode 100644 packages/core/types/element/element.d.ts create mode 100644 packages/core/types/element/index.d.ts create mode 100644 packages/core/types/fiber/fiber.d.ts create mode 100644 packages/core/types/fiber/index.d.ts create mode 100644 packages/core/types/fiber/types.d.ts create mode 100644 packages/core/types/fragment/fragment.d.ts create mode 100644 packages/core/types/fragment/index.d.ts create mode 100644 packages/core/types/helpers/index.d.ts create mode 100644 packages/core/types/index.d.ts create mode 100644 packages/core/types/lazy/index.d.ts create mode 100644 packages/core/types/lazy/lazy.d.ts create mode 100644 packages/core/types/memo/index.d.ts create mode 100644 packages/core/types/memo/memo.d.ts create mode 100644 packages/core/types/platform/index.d.ts create mode 100644 packages/core/types/platform/platform.d.ts create mode 100644 packages/core/types/platform/types.d.ts create mode 100644 packages/core/types/ref/index.d.ts create mode 100644 packages/core/types/ref/ref.d.ts create mode 100644 packages/core/types/ref/types.d.ts create mode 100644 packages/core/types/scope/index.d.ts create mode 100644 packages/core/types/scope/scope.d.ts create mode 100644 packages/core/types/shared/index.d.ts create mode 100644 packages/core/types/shared/types.d.ts create mode 100644 packages/core/types/suspense/index.d.ts create mode 100644 packages/core/types/suspense/suspense.d.ts create mode 100644 packages/core/types/unmount/index.d.ts create mode 100644 packages/core/types/unmount/unmount.d.ts create mode 100644 packages/core/types/use-callback/index.d.ts create mode 100644 packages/core/types/use-callback/use-callback.d.ts create mode 100644 packages/core/types/use-context/index.d.ts create mode 100644 packages/core/types/use-context/use-context.d.ts create mode 100644 packages/core/types/use-deferred-value/index.d.ts create mode 100644 packages/core/types/use-deferred-value/use-deferred-value.d.ts create mode 100644 packages/core/types/use-effect/index.d.ts create mode 100644 packages/core/types/use-effect/types.d.ts create mode 100644 packages/core/types/use-effect/use-effect.d.ts create mode 100644 packages/core/types/use-error/index.d.ts create mode 100644 packages/core/types/use-error/use-error.d.ts create mode 100644 packages/core/types/use-event/index.d.ts create mode 100644 packages/core/types/use-event/use-event.d.ts create mode 100644 packages/core/types/use-imperative-handle/index.d.ts create mode 100644 packages/core/types/use-imperative-handle/use-imperative-handle.d.ts create mode 100644 packages/core/types/use-layout-effect/index.d.ts create mode 100644 packages/core/types/use-layout-effect/use-layout-effect.d.ts create mode 100644 packages/core/types/use-memo/index.d.ts create mode 100644 packages/core/types/use-memo/use-memo.d.ts create mode 100644 packages/core/types/use-reducer/index.d.ts create mode 100644 packages/core/types/use-reducer/types.d.ts create mode 100644 packages/core/types/use-reducer/use-reducer.d.ts create mode 100644 packages/core/types/use-ref/index.d.ts create mode 100644 packages/core/types/use-ref/use-ref.d.ts create mode 100644 packages/core/types/use-state/index.d.ts create mode 100644 packages/core/types/use-state/use-state.d.ts create mode 100644 packages/core/types/use-update/index.d.ts create mode 100644 packages/core/types/use-update/use-update.d.ts create mode 100644 packages/core/types/view/index.d.ts create mode 100644 packages/core/types/view/types.d.ts create mode 100644 packages/core/types/view/view.d.ts create mode 100644 packages/core/types/walk/index.d.ts create mode 100644 packages/core/types/walk/walk.d.ts diff --git a/packages/core/.gitignore b/packages/core/.gitignore index 3f0466aa..1beeb3ed 100644 --- a/packages/core/.gitignore +++ b/packages/core/.gitignore @@ -4,4 +4,5 @@ npm-debug.log .DS_Store Thumbs.db -yarn-error.log \ No newline at end of file +yarn-error.log +/umd diff --git a/packages/core/.prettierignore b/packages/core/.prettierignore new file mode 100644 index 00000000..e890a457 --- /dev/null +++ b/packages/core/.prettierignore @@ -0,0 +1 @@ +/umd \ No newline at end of file diff --git a/packages/core/package.json b/packages/core/package.json index 430cbab8..289a918d 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -5,11 +5,13 @@ "author": "AlexPlex", "license": "MIT", "main": "./index.js", - "typings": "./index.d.ts", + "typings": "./types/index.d.ts", "scripts": { - "build": "npm run build:dev && npm run build:prod", + "build": "npm run build:types && npm run build:dev && npm run build:prod && npm run format", "build:dev": "webpack --config webpack.config --env development", - "build:prod": "webpack --config webpack.config --env production" + "build:prod": "webpack --config webpack.config --env production", + "build:types": "npx tsc -d --emitDeclarationOnly --skipLibCheck --declarationDir types", + "format": "npx prettier --write ./**/*.{js,ts}" }, "repository": { "type": "git", diff --git a/packages/core/tsconfig.json b/packages/core/tsconfig.json new file mode 100644 index 00000000..5c4cba79 --- /dev/null +++ b/packages/core/tsconfig.json @@ -0,0 +1,12 @@ +{ + "extends": "../../tsconfig", + "compilerOptions": { + "baseUrl": ".", + }, + "include": [ + "./**/*.ts" + ], + "exclude": [ + "src/**/*.spec.ts" + ] +} \ No newline at end of file diff --git a/packages/core/types/batch/batch.d.ts b/packages/core/types/batch/batch.d.ts new file mode 100644 index 00000000..67fd6a72 --- /dev/null +++ b/packages/core/types/batch/batch.d.ts @@ -0,0 +1,4 @@ +import { type Fiber } from '../fiber'; +declare function batch(callback: () => void): void; +declare function runBatch(fiber: Fiber, callback: () => void): void; +export { batch, runBatch }; diff --git a/packages/core/types/batch/index.d.ts b/packages/core/types/batch/index.d.ts new file mode 100644 index 00000000..2aa4c11a --- /dev/null +++ b/packages/core/types/batch/index.d.ts @@ -0,0 +1 @@ +export * from './batch'; diff --git a/packages/core/types/component/component.d.ts b/packages/core/types/component/component.d.ts new file mode 100644 index 00000000..78ba04e3 --- /dev/null +++ b/packages/core/types/component/component.d.ts @@ -0,0 +1,36 @@ +import type { DarkElementKey, DarkElementInstance } from '../shared'; +import type { Ref } from '../ref'; +import type { CreateElement, ComponentOptions, StandardComponentProps, SlotProps } from './types'; +declare class ComponentFactory

{ + type: CreateElement

; + token: Symbol; + props: P; + ref: Ref; + displayName: string; + children: Array; + shouldUpdate?: (props: P, nextProps: P) => boolean; + constructor(options: ComponentFactory

); +} +declare function createComponent( + createElement: CreateElement

, + options?: ComponentOptions

, +): ( + props?: P & + import('./types').KeyProps & + Readonly<{ + slot?: import('../shared').DarkElement; + }> & + import('./types').RefProps, + ref?: Ref, +) => ComponentFactory< + P & + import('./types').KeyProps & + Readonly<{ + slot?: import('../shared').DarkElement; + }> & + import('./types').RefProps, + any +>; +declare const detectIsComponentFactory: (factory: unknown) => factory is ComponentFactory; +declare const getComponentFactoryKey: (factory: ComponentFactory) => DarkElementKey; +export { ComponentFactory, createComponent, detectIsComponentFactory, getComponentFactoryKey }; diff --git a/packages/core/types/component/index.d.ts b/packages/core/types/component/index.d.ts new file mode 100644 index 00000000..c86c484a --- /dev/null +++ b/packages/core/types/component/index.d.ts @@ -0,0 +1,2 @@ +export * from './component'; +export * from './types'; diff --git a/packages/core/types/component/types.d.ts b/packages/core/types/component/types.d.ts new file mode 100644 index 00000000..242f2428 --- /dev/null +++ b/packages/core/types/component/types.d.ts @@ -0,0 +1,28 @@ +import type { ComponentFactory } from './component'; +import type { DarkElementKey, DarkElement } from '../shared'; +import type { Ref } from '../ref'; +export declare type ComponentOptions

= Readonly<{ + displayName?: string; + defaultProps?: Partial

; + token?: Symbol; + shouldUpdate?: (props: P, nextProps: P) => boolean; +}>; +export declare type StandardComponentProps = KeyProps & SlotProps & RefProps; +export declare type KeyProps = { + key?: DarkElementKey; +}; +export declare type SlotProps = Readonly<{ + slot?: T; +}>; +export declare type RefProps = { + ref?: Ref; +}; +export declare type Component = any, R = any> = ( + props: T, + ref?: Ref, +) => ComponentFactory; +export declare type ComponentFactoryReturnType = DarkElement; +export declare type CreateElement

= ( + props: P & Pick, + ref?: Ref, +) => ComponentFactoryReturnType; diff --git a/packages/core/types/constants.d.ts b/packages/core/types/constants.d.ts new file mode 100644 index 00000000..e3f0bcfc --- /dev/null +++ b/packages/core/types/constants.d.ts @@ -0,0 +1,10 @@ +export declare const ROOT = 'root'; +export declare const EMPTY_NODE = 'dark:matter'; +export declare const ATTR_KEY = 'key'; +export declare const ATTR_REF = 'ref'; +export declare const PARTIAL_UPDATE = 'partial-update'; +export declare enum TaskPriority { + HIGH = 2, + NORMAL = 1, + LOW = 0, +} diff --git a/packages/core/types/context/context.d.ts b/packages/core/types/context/context.d.ts new file mode 100644 index 00000000..0e69e26d --- /dev/null +++ b/packages/core/types/context/context.d.ts @@ -0,0 +1,3 @@ +import type { Context } from './types'; +declare function createContext(defaultValue: T): Context; +export { createContext }; diff --git a/packages/core/types/context/index.d.ts b/packages/core/types/context/index.d.ts new file mode 100644 index 00000000..58ab7be8 --- /dev/null +++ b/packages/core/types/context/index.d.ts @@ -0,0 +1,2 @@ +export * from './context'; +export * from './types'; diff --git a/packages/core/types/context/types.d.ts b/packages/core/types/context/types.d.ts new file mode 100644 index 00000000..1bd83a8f --- /dev/null +++ b/packages/core/types/context/types.d.ts @@ -0,0 +1,15 @@ +import type { Component, SlotProps } from '../component'; +import type { DarkElement } from '../shared'; +export declare type ContexProviderProps = { + value: T; +} & SlotProps; +export declare type Context = { + Provider: Component>; + Consumer: Component DarkElement>>; + displayName?: string; + defaultValue: T; +}; +export declare type ContextProviderValue = { + value: T; + subscribers: Array<(value: T) => void>; +}; diff --git a/packages/core/types/element/element.d.ts b/packages/core/types/element/element.d.ts new file mode 100644 index 00000000..6371a0eb --- /dev/null +++ b/packages/core/types/element/element.d.ts @@ -0,0 +1,7 @@ +import { type TagVirtualNodeFactory } from '../view'; +declare function createElement( + tag: string | Function, + props: any, + ...children: Array +): TagVirtualNodeFactory | Function | null; +export { createElement }; diff --git a/packages/core/types/element/index.d.ts b/packages/core/types/element/index.d.ts new file mode 100644 index 00000000..ef9f88ec --- /dev/null +++ b/packages/core/types/element/index.d.ts @@ -0,0 +1 @@ +export * from './element'; diff --git a/packages/core/types/fiber/fiber.d.ts b/packages/core/types/fiber/fiber.d.ts new file mode 100644 index 00000000..52767301 --- /dev/null +++ b/packages/core/types/fiber/fiber.d.ts @@ -0,0 +1,45 @@ +import { type ComponentFactory } from '../component'; +import { type TagVirtualNode } from '../view'; +import type { Context, ContextProviderValue } from '../context'; +import type { DarkElementInstance } from '../shared'; +import { type NativeElement, type Hook, EffectTag } from './types'; +declare class Fiber { + nativeElement: N; + parent: Fiber; + child: Fiber; + nextSibling: Fiber; + alternate: Fiber; + effectTag: EffectTag; + instance: DarkElementInstance; + hook: Hook; + shadow: Fiber; + provider: Map; + transposition: boolean; + mountedToHost: boolean; + portalHost: boolean; + effectHost: boolean; + layoutEffectHost: boolean; + childrenCount: number; + marker: string; + isUsed: boolean; + idx: number; + batched: Array<() => void>; + catchException: (error: Error) => void; + constructor(options: Partial>); + markPortalHost(): void; + markEffectHost(): void; + markLayoutEffectHost(): void; + markMountedToHost(): void; + setError(error: Error): void; +} +declare function workLoop(): boolean; +declare function hasChildrenProp(element: DarkElementInstance): element is TagVirtualNode | ComponentFactory; +declare function createHook(): Hook; +declare type CreateUpdateCallbackOptions = { + rootId: number; + fiber: Fiber; + forceStart?: boolean; + onStart: () => void; +}; +declare function createUpdateCallback(options: CreateUpdateCallbackOptions): () => void; +export { Fiber, workLoop, createHook, hasChildrenProp, createUpdateCallback }; diff --git a/packages/core/types/fiber/index.d.ts b/packages/core/types/fiber/index.d.ts new file mode 100644 index 00000000..20f97726 --- /dev/null +++ b/packages/core/types/fiber/index.d.ts @@ -0,0 +1,2 @@ +export * from './fiber'; +export * from './types'; diff --git a/packages/core/types/fiber/types.d.ts b/packages/core/types/fiber/types.d.ts new file mode 100644 index 00000000..1173c1ab --- /dev/null +++ b/packages/core/types/fiber/types.d.ts @@ -0,0 +1,20 @@ +export declare enum EffectTag { + CREATE = 'CREATE', + UPDATE = 'UPDATE', + DELETE = 'DELETE', + SKIP = 'SKIP', +} +export declare type NativeElement = unknown; +export declare type HookValue = { + token?: Symbol; + deps: Array; + value: T; +}; +export declare type Hook = { + idx: number; + values: Array; +}; +export declare const cloneTagMap: { + CREATE: boolean; + SKIP: boolean; +}; diff --git a/packages/core/types/fragment/fragment.d.ts b/packages/core/types/fragment/fragment.d.ts new file mode 100644 index 00000000..ac53003e --- /dev/null +++ b/packages/core/types/fragment/fragment.d.ts @@ -0,0 +1,18 @@ +import { type KeyProps } from '../component'; +declare const Fragment: ( + props?: KeyProps & + Readonly<{ + slot?: import('..').DarkElement; + }> & + import('../component').RefProps, + ref?: import('..').Ref, +) => import('../component/component').ComponentFactory< + KeyProps & + Readonly<{ + slot?: import('..').DarkElement; + }> & + import('../component').RefProps, + any +>; +declare const detectIsFragment: (factory: unknown) => boolean; +export { Fragment, detectIsFragment }; diff --git a/packages/core/types/fragment/index.d.ts b/packages/core/types/fragment/index.d.ts new file mode 100644 index 00000000..adae427e --- /dev/null +++ b/packages/core/types/fragment/index.d.ts @@ -0,0 +1 @@ +export * from './fragment'; diff --git a/packages/core/types/helpers/index.d.ts b/packages/core/types/helpers/index.d.ts new file mode 100644 index 00000000..bc5533c6 --- /dev/null +++ b/packages/core/types/helpers/index.d.ts @@ -0,0 +1,39 @@ +import type { NestedArray } from '../shared'; +declare const detectIsFunction: (o: any) => o is Function; +declare const detectIsUndefined: (o: any) => o is undefined; +declare const detectIsNumber: (o: any) => o is number; +declare const detectIsString: (o: any) => o is string; +declare const detectIsObject: (o: any) => o is object; +declare const detectIsBoolean: (o: any) => o is boolean; +declare const detectIsArray: (o: any) => o is any[]; +declare const detectIsNull: (o: any) => o is null; +declare const detectIsEmpty: (o: any) => boolean; +declare function error(str: string): void; +declare function flatten(source: Array>): Array; +declare function getTime(): number; +declare function keyBy( + list: Array, + fn: (o: T) => string | number, + value?: boolean, +): Record; +declare function fromEnd(source: Array, count: number): T[]; +declare const dummyFn: () => void; +declare function detectIsDepsDifferent(deps: Array, prevDeps: Array): boolean; +export { + detectIsFunction, + detectIsUndefined, + detectIsNumber, + detectIsString, + detectIsObject, + detectIsBoolean, + detectIsArray, + detectIsNull, + detectIsEmpty, + error, + flatten, + getTime, + keyBy, + fromEnd, + dummyFn, + detectIsDepsDifferent, +}; diff --git a/packages/core/types/index.d.ts b/packages/core/types/index.d.ts new file mode 100644 index 00000000..9bab00c9 --- /dev/null +++ b/packages/core/types/index.d.ts @@ -0,0 +1,39 @@ +export * from './component'; +export * from './context'; +export { createElement as h } from './element'; +export * from './fiber'; +export * from './fragment'; +export * from './platform'; +export * from './helpers'; +export * from './lazy'; +export * from './memo'; +export * from './ref'; +export * from './scope'; +export * from './shared'; +export * from './suspense'; +export * from './use-callback'; +export * from './use-context'; +export * from './use-deferred-value'; +export { useEffect } from './use-effect'; +export * from './use-error'; +export * from './use-event'; +export * from './use-imperative-handle'; +export { useLayoutEffect } from './use-layout-effect'; +export * from './use-memo'; +export * from './use-reducer'; +export * from './use-ref'; +export * from './use-state'; +export * from './use-update'; +export * from './view'; +export * from './constants'; +export { walkFiber } from './walk'; +export { unmountRoot } from './unmount'; +export { batch } from './batch'; +export declare const version: string; +declare global { + namespace JSX { + interface ElementChildrenAttribute { + slot: {}; + } + } +} diff --git a/packages/core/types/lazy/index.d.ts b/packages/core/types/lazy/index.d.ts new file mode 100644 index 00000000..5bd21616 --- /dev/null +++ b/packages/core/types/lazy/index.d.ts @@ -0,0 +1 @@ +export * from './lazy'; diff --git a/packages/core/types/lazy/lazy.d.ts b/packages/core/types/lazy/lazy.d.ts new file mode 100644 index 00000000..97d0a9cd --- /dev/null +++ b/packages/core/types/lazy/lazy.d.ts @@ -0,0 +1,25 @@ +import { type Component } from '../component'; +declare function lazy( + dynamic: () => Promise<{ + default: Component

; + }>, +): ({ + ref, + ...rest +}: P & + import('../component').KeyProps & + Readonly<{ + slot?: import('..').DarkElement; + }> & + import('../component').RefProps & + import('../component').RefProps) => import('../component/component').ComponentFactory< + P & + import('../component').KeyProps & + Readonly<{ + slot?: import('..').DarkElement; + }> & + import('../component').RefProps, + R +>; +declare const detectIsLazy: (factory: unknown) => boolean; +export { lazy, detectIsLazy }; diff --git a/packages/core/types/memo/index.d.ts b/packages/core/types/memo/index.d.ts new file mode 100644 index 00000000..ba556f3f --- /dev/null +++ b/packages/core/types/memo/index.d.ts @@ -0,0 +1 @@ +export * from './memo'; diff --git a/packages/core/types/memo/memo.d.ts b/packages/core/types/memo/memo.d.ts new file mode 100644 index 00000000..b9c2e868 --- /dev/null +++ b/packages/core/types/memo/memo.d.ts @@ -0,0 +1,10 @@ +import { type Component, type ComponentFactory, type StandardComponentProps, type SlotProps } from '../component'; +import { type Ref } from '../ref'; +declare type ShouldUpdate = (props: T, nextProps: T) => boolean; +declare const $$memo: unique symbol; +declare const detectIsMemo: (factory: unknown) => boolean; +declare function memo( + component: (props: T, ref?: Ref) => ComponentFactory, + shouldUpdate?: ShouldUpdate, +): Component; +export { $$memo, memo, detectIsMemo }; diff --git a/packages/core/types/platform/index.d.ts b/packages/core/types/platform/index.d.ts new file mode 100644 index 00000000..774d4536 --- /dev/null +++ b/packages/core/types/platform/index.d.ts @@ -0,0 +1,2 @@ +export * from './platform'; +export * from './types'; diff --git a/packages/core/types/platform/platform.d.ts b/packages/core/types/platform/platform.d.ts new file mode 100644 index 00000000..e4ffd65e --- /dev/null +++ b/packages/core/types/platform/platform.d.ts @@ -0,0 +1,2 @@ +import type { Platform } from './types'; +export declare const platform: Platform; diff --git a/packages/core/types/platform/types.d.ts b/packages/core/types/platform/types.d.ts new file mode 100644 index 00000000..c6279434 --- /dev/null +++ b/packages/core/types/platform/types.d.ts @@ -0,0 +1,19 @@ +import { type Fiber } from '../fiber'; +import { type TaskPriority } from '../constants'; +import { type ComponentFactory } from '../component'; +import { type VirtualNode } from '../view'; +export declare type Platform = { + createNativeElement: (vNode: VirtualNode) => N; + requestAnimationFrame: typeof requestAnimationFrame; + scheduleCallback: (callback: () => void, options?: ScheduleCallbackOptions) => void; + shouldYeildToHost: () => boolean; + applyCommit: (fiber: Fiber) => void; + finishCommitWork: () => void; + detectIsPortal: (factory: ComponentFactory) => boolean; + unmountPortal: (fiber: Fiber) => void; +}; +export declare type ScheduleCallbackOptions = { + priority?: TaskPriority; + timeoutMs?: number; + forceSync?: boolean; +}; diff --git a/packages/core/types/ref/index.d.ts b/packages/core/types/ref/index.d.ts new file mode 100644 index 00000000..d01905b9 --- /dev/null +++ b/packages/core/types/ref/index.d.ts @@ -0,0 +1,2 @@ +export * from './ref'; +export * from './types'; diff --git a/packages/core/types/ref/ref.d.ts b/packages/core/types/ref/ref.d.ts new file mode 100644 index 00000000..bcbb739f --- /dev/null +++ b/packages/core/types/ref/ref.d.ts @@ -0,0 +1,7 @@ +import type { Component, RefProps, ComponentFactory } from '../component'; +import type { MutableRef } from './types'; +declare function forwardRef( + component: Component, +): ({ ref, ...rest }: P & RefProps) => ComponentFactory; +declare const detectIsMutableRef: (ref: unknown) => ref is MutableRef; +export { forwardRef, detectIsMutableRef }; diff --git a/packages/core/types/ref/types.d.ts b/packages/core/types/ref/types.d.ts new file mode 100644 index 00000000..2ddc5d66 --- /dev/null +++ b/packages/core/types/ref/types.d.ts @@ -0,0 +1,5 @@ +export declare type MutableRef = { + current: T; +}; +export declare type FunctionRef = (ref: T) => void; +export declare type Ref = MutableRef | FunctionRef; diff --git a/packages/core/types/scope/index.d.ts b/packages/core/types/scope/index.d.ts new file mode 100644 index 00000000..4f041b0c --- /dev/null +++ b/packages/core/types/scope/index.d.ts @@ -0,0 +1 @@ +export * from './scope'; diff --git a/packages/core/types/scope/scope.d.ts b/packages/core/types/scope/scope.d.ts new file mode 100644 index 00000000..f621955d --- /dev/null +++ b/packages/core/types/scope/scope.d.ts @@ -0,0 +1,80 @@ +import type { Fiber } from '../fiber'; +declare const rootStore: { + set: (id: number) => void; + remove: (id: number) => boolean; +}; +declare const getRootId: () => number; +declare const wipRootStore: { + get: () => Fiber; + set: (fiber: Fiber) => Fiber; +}; +declare const currentRootStore: { + get: (id?: number) => Fiber; + set: (fiber: Fiber) => Fiber; +}; +declare const nextUnitOfWorkStore: { + get: () => Fiber; + set: (fiber: Fiber) => Fiber; +}; +declare const currentFiberStore: { + get: () => Fiber; + set: (fiber: Fiber) => Fiber; +}; +declare const eventsStore: { + get: () => Map>; + addUnsubscriber: (fn: () => void) => number; + unsubscribe: (id: number) => void; +}; +declare const deletionsStore: { + get: () => Fiber[]; + set: (deletions: Array) => Fiber[]; +}; +declare const fiberMountStore: { + reset: () => void; + getIndex: () => number; + jumpToChild: () => void; + jumpToParent: () => void; + jumpToSibling: () => void; + deepWalking: { + get: () => boolean; + set: (value: boolean) => boolean; + }; +}; +declare const effectsStore: { + get: () => (() => void)[]; + reset: () => any[]; + add: (effect: () => void) => number; +}; +declare const layoutEffectsStore: { + get: () => (() => void)[]; + reset: () => any[]; + add: (effect: () => void) => number; +}; +declare const isLayoutEffectsZone: { + get: () => boolean; + set: (value: boolean) => boolean; +}; +declare const isUpdateHookZone: { + get: () => boolean; + set: (value: boolean) => boolean; +}; +declare const isBatchZone: { + get: () => boolean; + set: (value: boolean) => boolean; +}; +export { + getRootId, + rootStore, + wipRootStore, + currentRootStore, + nextUnitOfWorkStore, + currentFiberStore, + eventsStore, + deletionsStore, + fiberMountStore, + effectsStore, + layoutEffectsStore, + isLayoutEffectsZone, + isUpdateHookZone, + isBatchZone, +}; diff --git a/packages/core/types/shared/index.d.ts b/packages/core/types/shared/index.d.ts new file mode 100644 index 00000000..fcb073fe --- /dev/null +++ b/packages/core/types/shared/index.d.ts @@ -0,0 +1 @@ +export * from './types'; diff --git a/packages/core/types/shared/types.d.ts b/packages/core/types/shared/types.d.ts new file mode 100644 index 00000000..f18a5723 --- /dev/null +++ b/packages/core/types/shared/types.d.ts @@ -0,0 +1,10 @@ +import type { ComponentFactory } from '../component'; +import type { VirtualNode, VirtualNodeFactory } from '../view'; +export declare type DarkElement = NestedArray< + ComponentFactory | VirtualNode | RenderProps | Nullable | string | number +>; +export declare type Nullable = null | false | undefined; +export declare type NestedArray = T | Array>; +export declare type RenderProps = (...args: Array) => DarkElement; +export declare type DarkElementKey = string | number; +export declare type DarkElementInstance = VirtualNode | VirtualNodeFactory | ComponentFactory; diff --git a/packages/core/types/suspense/index.d.ts b/packages/core/types/suspense/index.d.ts new file mode 100644 index 00000000..8bc8e344 --- /dev/null +++ b/packages/core/types/suspense/index.d.ts @@ -0,0 +1 @@ +export * from './suspense'; diff --git a/packages/core/types/suspense/suspense.d.ts b/packages/core/types/suspense/suspense.d.ts new file mode 100644 index 00000000..ca1de0ee --- /dev/null +++ b/packages/core/types/suspense/suspense.d.ts @@ -0,0 +1,28 @@ +import type { DarkElement } from '../shared'; +declare type SuspenseProps = { + fallback: DarkElement; +}; +declare type SuspenseContextValue = { + fallback: DarkElement; + isLoaded: boolean; + trigger: () => void; +}; +declare const SuspenseContext: import('../context').Context; +declare const Suspense: ( + props?: SuspenseProps & + import('../component').KeyProps & + Readonly<{ + slot?: DarkElement; + }> & + import('../component').RefProps, + ref?: import('..').Ref, +) => import('../component/component').ComponentFactory< + SuspenseProps & + import('../component').KeyProps & + Readonly<{ + slot?: DarkElement; + }> & + import('../component').RefProps, + any +>; +export { SuspenseContext, Suspense }; diff --git a/packages/core/types/unmount/index.d.ts b/packages/core/types/unmount/index.d.ts new file mode 100644 index 00000000..ef7fd2e0 --- /dev/null +++ b/packages/core/types/unmount/index.d.ts @@ -0,0 +1 @@ +export * from './unmount'; diff --git a/packages/core/types/unmount/unmount.d.ts b/packages/core/types/unmount/unmount.d.ts new file mode 100644 index 00000000..f107cf60 --- /dev/null +++ b/packages/core/types/unmount/unmount.d.ts @@ -0,0 +1,4 @@ +import { type Fiber } from '../fiber'; +declare function unmountFiber(fiber: Fiber): void; +declare function unmountRoot(rootId: number, onComplete: () => void): void; +export { unmountFiber, unmountRoot }; diff --git a/packages/core/types/use-callback/index.d.ts b/packages/core/types/use-callback/index.d.ts new file mode 100644 index 00000000..87e7d397 --- /dev/null +++ b/packages/core/types/use-callback/index.d.ts @@ -0,0 +1 @@ +export * from './use-callback'; diff --git a/packages/core/types/use-callback/use-callback.d.ts b/packages/core/types/use-callback/use-callback.d.ts new file mode 100644 index 00000000..f3a24cd6 --- /dev/null +++ b/packages/core/types/use-callback/use-callback.d.ts @@ -0,0 +1,2 @@ +declare function useCallback(callback: T, deps: Array): T; +export { useCallback }; diff --git a/packages/core/types/use-context/index.d.ts b/packages/core/types/use-context/index.d.ts new file mode 100644 index 00000000..433e4ba4 --- /dev/null +++ b/packages/core/types/use-context/index.d.ts @@ -0,0 +1 @@ +export * from './use-context'; diff --git a/packages/core/types/use-context/use-context.d.ts b/packages/core/types/use-context/use-context.d.ts new file mode 100644 index 00000000..4615bd36 --- /dev/null +++ b/packages/core/types/use-context/use-context.d.ts @@ -0,0 +1,3 @@ +import type { Context } from '../context'; +declare function useContext(context: Context): T; +export { useContext }; diff --git a/packages/core/types/use-deferred-value/index.d.ts b/packages/core/types/use-deferred-value/index.d.ts new file mode 100644 index 00000000..fb39ead1 --- /dev/null +++ b/packages/core/types/use-deferred-value/index.d.ts @@ -0,0 +1 @@ +export * from './use-deferred-value'; diff --git a/packages/core/types/use-deferred-value/use-deferred-value.d.ts b/packages/core/types/use-deferred-value/use-deferred-value.d.ts new file mode 100644 index 00000000..60086fbc --- /dev/null +++ b/packages/core/types/use-deferred-value/use-deferred-value.d.ts @@ -0,0 +1,5 @@ +declare type UseDeferredValueOprions = { + timeoutMs: number; +}; +declare function useDeferredValue(value: T, options?: UseDeferredValueOprions): T; +export { useDeferredValue }; diff --git a/packages/core/types/use-effect/index.d.ts b/packages/core/types/use-effect/index.d.ts new file mode 100644 index 00000000..20dba820 --- /dev/null +++ b/packages/core/types/use-effect/index.d.ts @@ -0,0 +1 @@ +export * from './use-effect'; diff --git a/packages/core/types/use-effect/types.d.ts b/packages/core/types/use-effect/types.d.ts new file mode 100644 index 00000000..c58c8dbd --- /dev/null +++ b/packages/core/types/use-effect/types.d.ts @@ -0,0 +1,2 @@ +export declare type DropEffect = void | (() => void); +export declare type Effect = () => DropEffect; diff --git a/packages/core/types/use-effect/use-effect.d.ts b/packages/core/types/use-effect/use-effect.d.ts new file mode 100644 index 00000000..d77ac60b --- /dev/null +++ b/packages/core/types/use-effect/use-effect.d.ts @@ -0,0 +1,15 @@ +import { effectsStore } from '../scope'; +import type { Fiber, Hook, HookValue } from '../fiber'; +import type { Effect, DropEffect } from './types'; +declare const useEffect: (effect: Effect, deps?: Array) => void, + hasEffects: (fiber: Fiber) => boolean, + dropEffects: (hook: Hook>) => void; +declare function createEffect( + token: Symbol, + store: typeof effectsStore, +): { + useEffect: (effect: Effect, deps?: Array) => void; + hasEffects: (fiber: Fiber) => boolean; + dropEffects: (hook: Hook>) => void; +}; +export { useEffect, hasEffects, dropEffects, createEffect }; diff --git a/packages/core/types/use-error/index.d.ts b/packages/core/types/use-error/index.d.ts new file mode 100644 index 00000000..dc350f5e --- /dev/null +++ b/packages/core/types/use-error/index.d.ts @@ -0,0 +1 @@ +export * from './use-error'; diff --git a/packages/core/types/use-error/use-error.d.ts b/packages/core/types/use-error/use-error.d.ts new file mode 100644 index 00000000..b920b174 --- /dev/null +++ b/packages/core/types/use-error/use-error.d.ts @@ -0,0 +1,2 @@ +declare function useError(): Error | null; +export { useError }; diff --git a/packages/core/types/use-event/index.d.ts b/packages/core/types/use-event/index.d.ts new file mode 100644 index 00000000..0323ed08 --- /dev/null +++ b/packages/core/types/use-event/index.d.ts @@ -0,0 +1 @@ +export * from './use-event'; diff --git a/packages/core/types/use-event/use-event.d.ts b/packages/core/types/use-event/use-event.d.ts new file mode 100644 index 00000000..0e947a48 --- /dev/null +++ b/packages/core/types/use-event/use-event.d.ts @@ -0,0 +1,2 @@ +declare function useEvent any>(fn: T): T; +export { useEvent }; diff --git a/packages/core/types/use-imperative-handle/index.d.ts b/packages/core/types/use-imperative-handle/index.d.ts new file mode 100644 index 00000000..8e9a22ed --- /dev/null +++ b/packages/core/types/use-imperative-handle/index.d.ts @@ -0,0 +1 @@ +export * from './use-imperative-handle'; diff --git a/packages/core/types/use-imperative-handle/use-imperative-handle.d.ts b/packages/core/types/use-imperative-handle/use-imperative-handle.d.ts new file mode 100644 index 00000000..c554b64d --- /dev/null +++ b/packages/core/types/use-imperative-handle/use-imperative-handle.d.ts @@ -0,0 +1,3 @@ +import type { MutableRef } from '../ref'; +declare function useImperativeHandle(ref: MutableRef, createHandle: () => T, deps: Array): void; +export { useImperativeHandle }; diff --git a/packages/core/types/use-layout-effect/index.d.ts b/packages/core/types/use-layout-effect/index.d.ts new file mode 100644 index 00000000..96ba8986 --- /dev/null +++ b/packages/core/types/use-layout-effect/index.d.ts @@ -0,0 +1 @@ +export * from './use-layout-effect'; diff --git a/packages/core/types/use-layout-effect/use-layout-effect.d.ts b/packages/core/types/use-layout-effect/use-layout-effect.d.ts new file mode 100644 index 00000000..2b78becd --- /dev/null +++ b/packages/core/types/use-layout-effect/use-layout-effect.d.ts @@ -0,0 +1,6 @@ +declare const useLayoutEffect: (effect: import('../use-effect/types').Effect, deps?: any[]) => void, + hasLayoutEffects: (fiber: import('../fiber/fiber').Fiber) => boolean, + dropLayoutEffects: ( + hook: import('..').Hook>, + ) => void; +export { useLayoutEffect, hasLayoutEffects, dropLayoutEffects }; diff --git a/packages/core/types/use-memo/index.d.ts b/packages/core/types/use-memo/index.d.ts new file mode 100644 index 00000000..5b982991 --- /dev/null +++ b/packages/core/types/use-memo/index.d.ts @@ -0,0 +1 @@ +export * from './use-memo'; diff --git a/packages/core/types/use-memo/use-memo.d.ts b/packages/core/types/use-memo/use-memo.d.ts new file mode 100644 index 00000000..2d02e1ac --- /dev/null +++ b/packages/core/types/use-memo/use-memo.d.ts @@ -0,0 +1,2 @@ +declare function useMemo(getValue: () => T, deps: Array): T; +export { useMemo }; diff --git a/packages/core/types/use-reducer/index.d.ts b/packages/core/types/use-reducer/index.d.ts new file mode 100644 index 00000000..4604e2ec --- /dev/null +++ b/packages/core/types/use-reducer/index.d.ts @@ -0,0 +1,2 @@ +export * from './use-reducer'; +export type { Reducer } from './types'; diff --git a/packages/core/types/use-reducer/types.d.ts b/packages/core/types/use-reducer/types.d.ts new file mode 100644 index 00000000..9baa5c7f --- /dev/null +++ b/packages/core/types/use-reducer/types.d.ts @@ -0,0 +1,4 @@ +export declare type Reducer = (prevState: S, action: A) => S; +export declare type ReducerState = R extends Reducer ? S : never; +export declare type ReducerAction = R extends Reducer ? A : never; +export declare type Dispatch = (value: A) => void; diff --git a/packages/core/types/use-reducer/use-reducer.d.ts b/packages/core/types/use-reducer/use-reducer.d.ts new file mode 100644 index 00000000..290e9008 --- /dev/null +++ b/packages/core/types/use-reducer/use-reducer.d.ts @@ -0,0 +1,7 @@ +import type { Reducer, Dispatch, ReducerAction, ReducerState } from './types'; +declare function useReducer( + reducer: R, + initialState: ReducerState, + initializer?: (state: ReducerState) => ReducerState, +): [ReducerState, Dispatch>]; +export { useReducer }; diff --git a/packages/core/types/use-ref/index.d.ts b/packages/core/types/use-ref/index.d.ts new file mode 100644 index 00000000..1a14cecf --- /dev/null +++ b/packages/core/types/use-ref/index.d.ts @@ -0,0 +1 @@ +export * from './use-ref'; diff --git a/packages/core/types/use-ref/use-ref.d.ts b/packages/core/types/use-ref/use-ref.d.ts new file mode 100644 index 00000000..ca6d7497 --- /dev/null +++ b/packages/core/types/use-ref/use-ref.d.ts @@ -0,0 +1,3 @@ +import type { MutableRef } from '../ref'; +declare function useRef(initialValue?: T): MutableRef; +export { useRef }; diff --git a/packages/core/types/use-state/index.d.ts b/packages/core/types/use-state/index.d.ts new file mode 100644 index 00000000..d6ed522e --- /dev/null +++ b/packages/core/types/use-state/index.d.ts @@ -0,0 +1 @@ +export * from './use-state'; diff --git a/packages/core/types/use-state/use-state.d.ts b/packages/core/types/use-state/use-state.d.ts new file mode 100644 index 00000000..78d573bb --- /dev/null +++ b/packages/core/types/use-state/use-state.d.ts @@ -0,0 +1,7 @@ +import { type ScheduleCallbackOptions } from '../platform'; +declare type Value = T | ((prevValue: T) => T); +declare function useState( + initialValue: T, + options?: ScheduleCallbackOptions, +): [T, (value: Value) => void]; +export { useState }; diff --git a/packages/core/types/use-update/index.d.ts b/packages/core/types/use-update/index.d.ts new file mode 100644 index 00000000..4ca6738e --- /dev/null +++ b/packages/core/types/use-update/index.d.ts @@ -0,0 +1 @@ +export * from './use-update'; diff --git a/packages/core/types/use-update/use-update.d.ts b/packages/core/types/use-update/use-update.d.ts new file mode 100644 index 00000000..0b9d3427 --- /dev/null +++ b/packages/core/types/use-update/use-update.d.ts @@ -0,0 +1,3 @@ +import { type ScheduleCallbackOptions } from '../platform'; +declare function useUpdate(options?: ScheduleCallbackOptions): (onStart?: () => void) => void; +export { useUpdate }; diff --git a/packages/core/types/view/index.d.ts b/packages/core/types/view/index.d.ts new file mode 100644 index 00000000..83f4dd6f --- /dev/null +++ b/packages/core/types/view/index.d.ts @@ -0,0 +1,2 @@ +export * from './view'; +export * from './types'; diff --git a/packages/core/types/view/types.d.ts b/packages/core/types/view/types.d.ts new file mode 100644 index 00000000..1f993917 --- /dev/null +++ b/packages/core/types/view/types.d.ts @@ -0,0 +1,11 @@ +export declare type ViewDef = { + as: string; + slot?: any; + isVoid?: boolean; + [prop: string]: any; +}; +export declare enum NodeType { + TAG = 'TAG', + TEXT = 'TEXT', + COMMENT = 'COMMENT', +} diff --git a/packages/core/types/view/view.d.ts b/packages/core/types/view/view.d.ts new file mode 100644 index 00000000..09b40151 --- /dev/null +++ b/packages/core/types/view/view.d.ts @@ -0,0 +1,57 @@ +import type { DarkElementKey } from '../shared'; +import type { ComponentFactory, StandardComponentProps } from '../component'; +import { NodeType, type ViewDef } from './types'; +export declare type VirtualNodeFactory = () => VirtualNode; +export declare type TagVirtualNodeFactory = () => TagVirtualNode; +export declare type TextVirtualNodeFactory = () => TextVirtualNode; +export declare type CommentVirtualNodeFactory = () => CommentVirtualNode; +declare class VirtualNode { + type: NodeType; + constructor(options: Partial); +} +declare class TagVirtualNode extends VirtualNode { + type: NodeType; + name: string; + isVoid: boolean; + attrs: Record; + children: Array; + constructor(options: Partial); +} +declare class TextVirtualNode extends VirtualNode { + type: NodeType; + value: string; + constructor(text: string); +} +declare class CommentVirtualNode extends VirtualNode { + type: NodeType; + value: string; + constructor(text: string); +} +declare const detectIsVirtualNode: (vNode: unknown) => vNode is VirtualNode; +declare const detectIsTagVirtualNode: (vNode: unknown) => vNode is TagVirtualNode; +declare const detectIsCommentVirtualNode: (vNode: unknown) => vNode is CommentVirtualNode; +declare const detectIsTextVirtualNode: (vNode: unknown) => vNode is TextVirtualNode; +declare const detectIsEmptyVirtualNode: (vNode: unknown) => boolean; +declare function getVirtualNodeKey(vNode: TagVirtualNode): DarkElementKey | null; +declare const createEmptyVirtualNode: () => CommentVirtualNode; +declare const detectIsVirtualNodeFactory: (factory: unknown) => factory is VirtualNodeFactory; +declare function View(def: ViewDef): TagVirtualNodeFactory; +declare function Text(source: string | StandardComponentProps['slot']): string | TextVirtualNode; +declare function Comment(text: string): CommentVirtualNodeFactory; +export { + VirtualNode, + TagVirtualNode, + TextVirtualNode, + CommentVirtualNode, + detectIsVirtualNode, + detectIsTagVirtualNode, + detectIsCommentVirtualNode, + detectIsTextVirtualNode, + detectIsEmptyVirtualNode, + getVirtualNodeKey, + createEmptyVirtualNode, + detectIsVirtualNodeFactory, + View, + Text, + Comment, +}; diff --git a/packages/core/types/walk/index.d.ts b/packages/core/types/walk/index.d.ts new file mode 100644 index 00000000..c6de808b --- /dev/null +++ b/packages/core/types/walk/index.d.ts @@ -0,0 +1 @@ +export * from './walk'; diff --git a/packages/core/types/walk/walk.d.ts b/packages/core/types/walk/walk.d.ts new file mode 100644 index 00000000..1ff00cc0 --- /dev/null +++ b/packages/core/types/walk/walk.d.ts @@ -0,0 +1,13 @@ +import { type Fiber } from '../fiber'; +declare type WalkFiberOptions = { + fiber: Fiber; + onLoop: (options: OnLoopOptions) => void; +}; +declare type OnLoopOptions = { + nextFiber: Fiber; + isReturn: boolean; + resetIsDeepWalking: () => void; + stop: () => void; +}; +declare function walkFiber(options: WalkFiberOptions): void; +export { walkFiber };