Skip to content

Commit

Permalink
types
Browse files Browse the repository at this point in the history
  • Loading branch information
atellmer committed Nov 25, 2022
1 parent 5e51599 commit 3b4f3c8
Show file tree
Hide file tree
Showing 73 changed files with 657 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/core/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
npm-debug.log
.DS_Store
Thumbs.db
yarn-error.log
yarn-error.log
/umd
1 change: 1 addition & 0 deletions packages/core/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/umd
8 changes: 5 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 12 additions & 0 deletions packages/core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "../../tsconfig",
"compilerOptions": {
"baseUrl": ".",
},
"include": [
"./**/*.ts"
],
"exclude": [
"src/**/*.spec.ts"
]
}
4 changes: 4 additions & 0 deletions packages/core/types/batch/batch.d.ts
Original file line number Diff line number Diff line change
@@ -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 };
1 change: 1 addition & 0 deletions packages/core/types/batch/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './batch';
36 changes: 36 additions & 0 deletions packages/core/types/component/component.d.ts
Original file line number Diff line number Diff line change
@@ -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<P extends StandardComponentProps = any, R = any> {
type: CreateElement<P>;
token: Symbol;
props: P;
ref: Ref<R>;
displayName: string;
children: Array<DarkElementInstance>;
shouldUpdate?: (props: P, nextProps: P) => boolean;
constructor(options: ComponentFactory<P>);
}
declare function createComponent<P, R = any>(
createElement: CreateElement<P & SlotProps, R>,
options?: ComponentOptions<P>,
): (
props?: P &
import('./types').KeyProps &
Readonly<{
slot?: import('../shared').DarkElement;
}> &
import('./types').RefProps<unknown>,
ref?: Ref<R>,
) => ComponentFactory<
P &
import('./types').KeyProps &
Readonly<{
slot?: import('../shared').DarkElement;
}> &
import('./types').RefProps<unknown>,
any
>;
declare const detectIsComponentFactory: (factory: unknown) => factory is ComponentFactory<any, any>;
declare const getComponentFactoryKey: (factory: ComponentFactory) => DarkElementKey;
export { ComponentFactory, createComponent, detectIsComponentFactory, getComponentFactoryKey };
2 changes: 2 additions & 0 deletions packages/core/types/component/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './component';
export * from './types';
28 changes: 28 additions & 0 deletions packages/core/types/component/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { ComponentFactory } from './component';
import type { DarkElementKey, DarkElement } from '../shared';
import type { Ref } from '../ref';
export declare type ComponentOptions<P extends StandardComponentProps> = Readonly<{
displayName?: string;
defaultProps?: Partial<P>;
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<T = DarkElement> = Readonly<{
slot?: T;
}>;
export declare type RefProps<T = unknown> = {
ref?: Ref<T>;
};
export declare type Component<T extends Pick<StandardComponentProps, 'slot'> = any, R = any> = (
props: T,
ref?: Ref<R>,
) => ComponentFactory;
export declare type ComponentFactoryReturnType = DarkElement;
export declare type CreateElement<P extends StandardComponentProps, R = any> = (
props: P & Pick<StandardComponentProps, 'slot'>,
ref?: Ref<R>,
) => ComponentFactoryReturnType;
10 changes: 10 additions & 0 deletions packages/core/types/constants.d.ts
Original file line number Diff line number Diff line change
@@ -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,
}
3 changes: 3 additions & 0 deletions packages/core/types/context/context.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { Context } from './types';
declare function createContext<T>(defaultValue: T): Context<T>;
export { createContext };
2 changes: 2 additions & 0 deletions packages/core/types/context/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './context';
export * from './types';
15 changes: 15 additions & 0 deletions packages/core/types/context/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Component, SlotProps } from '../component';
import type { DarkElement } from '../shared';
export declare type ContexProviderProps<T> = {
value: T;
} & SlotProps;
export declare type Context<T = unknown> = {
Provider: Component<ContexProviderProps<T>>;
Consumer: Component<SlotProps<(value: T) => DarkElement>>;
displayName?: string;
defaultValue: T;
};
export declare type ContextProviderValue<T = unknown> = {
value: T;
subscribers: Array<(value: T) => void>;
};
7 changes: 7 additions & 0 deletions packages/core/types/element/element.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { type TagVirtualNodeFactory } from '../view';
declare function createElement(
tag: string | Function,
props: any,
...children: Array<any>
): TagVirtualNodeFactory | Function | null;
export { createElement };
1 change: 1 addition & 0 deletions packages/core/types/element/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './element';
45 changes: 45 additions & 0 deletions packages/core/types/fiber/fiber.d.ts
Original file line number Diff line number Diff line change
@@ -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<N = NativeElement> {
nativeElement: N;
parent: Fiber<N>;
child: Fiber<N>;
nextSibling: Fiber<N>;
alternate: Fiber<N>;
effectTag: EffectTag;
instance: DarkElementInstance;
hook: Hook;
shadow: Fiber<N>;
provider: Map<Context, ContextProviderValue>;
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<Fiber<N>>);
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 };
2 changes: 2 additions & 0 deletions packages/core/types/fiber/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './fiber';
export * from './types';
20 changes: 20 additions & 0 deletions packages/core/types/fiber/types.d.ts
Original file line number Diff line number Diff line change
@@ -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<T = any> = {
token?: Symbol;
deps: Array<any>;
value: T;
};
export declare type Hook<T = any> = {
idx: number;
values: Array<T>;
};
export declare const cloneTagMap: {
CREATE: boolean;
SKIP: boolean;
};
18 changes: 18 additions & 0 deletions packages/core/types/fragment/fragment.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { type KeyProps } from '../component';
declare const Fragment: (
props?: KeyProps &
Readonly<{
slot?: import('..').DarkElement;
}> &
import('../component').RefProps<unknown>,
ref?: import('..').Ref<any>,
) => import('../component/component').ComponentFactory<
KeyProps &
Readonly<{
slot?: import('..').DarkElement;
}> &
import('../component').RefProps<unknown>,
any
>;
declare const detectIsFragment: (factory: unknown) => boolean;
export { Fragment, detectIsFragment };
1 change: 1 addition & 0 deletions packages/core/types/fragment/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './fragment';
39 changes: 39 additions & 0 deletions packages/core/types/helpers/index.d.ts
Original file line number Diff line number Diff line change
@@ -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<T = any>(source: Array<NestedArray<T>>): Array<T>;
declare function getTime(): number;
declare function keyBy<T = any>(
list: Array<T>,
fn: (o: T) => string | number,
value?: boolean,
): Record<string | number, T | boolean>;
declare function fromEnd<T>(source: Array<T>, count: number): T[];
declare const dummyFn: () => void;
declare function detectIsDepsDifferent(deps: Array<unknown>, prevDeps: Array<unknown>): boolean;
export {
detectIsFunction,
detectIsUndefined,
detectIsNumber,
detectIsString,
detectIsObject,
detectIsBoolean,
detectIsArray,
detectIsNull,
detectIsEmpty,
error,
flatten,
getTime,
keyBy,
fromEnd,
dummyFn,
detectIsDepsDifferent,
};
39 changes: 39 additions & 0 deletions packages/core/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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: {};
}
}
}
1 change: 1 addition & 0 deletions packages/core/types/lazy/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lazy';
25 changes: 25 additions & 0 deletions packages/core/types/lazy/lazy.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { type Component } from '../component';
declare function lazy<P, R = unknown>(
dynamic: () => Promise<{
default: Component<P>;
}>,
): ({
ref,
...rest
}: P &
import('../component').KeyProps &
Readonly<{
slot?: import('..').DarkElement;
}> &
import('../component').RefProps<unknown> &
import('../component').RefProps<R>) => import('../component/component').ComponentFactory<
P &
import('../component').KeyProps &
Readonly<{
slot?: import('..').DarkElement;
}> &
import('../component').RefProps<unknown>,
R
>;
declare const detectIsLazy: (factory: unknown) => boolean;
export { lazy, detectIsLazy };
1 change: 1 addition & 0 deletions packages/core/types/memo/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './memo';
10 changes: 10 additions & 0 deletions packages/core/types/memo/memo.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { type Component, type ComponentFactory, type StandardComponentProps, type SlotProps } from '../component';
import { type Ref } from '../ref';
declare type ShouldUpdate<T> = (props: T, nextProps: T) => boolean;
declare const $$memo: unique symbol;
declare const detectIsMemo: (factory: unknown) => boolean;
declare function memo<T>(
component: (props: T, ref?: Ref) => ComponentFactory<T>,
shouldUpdate?: ShouldUpdate<T & SlotProps>,
): Component<T & StandardComponentProps>;
export { $$memo, memo, detectIsMemo };
2 changes: 2 additions & 0 deletions packages/core/types/platform/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './platform';
export * from './types';
2 changes: 2 additions & 0 deletions packages/core/types/platform/platform.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import type { Platform } from './types';
export declare const platform: Platform;
19 changes: 19 additions & 0 deletions packages/core/types/platform/types.d.ts
Original file line number Diff line number Diff line change
@@ -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: <N>(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;
};
Loading

0 comments on commit 3b4f3c8

Please sign in to comment.