Skip to content

Commit

Permalink
improve type declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
huang2002 committed Jan 31, 2019
1 parent c565bcd commit 594cade
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 33 deletions.
8 changes: 4 additions & 4 deletions src/core/HNode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Store, createStore, PartialHandlers, StoreType, StoreHandlers } from "./Store";
import { Store, createStore, StoreType, StoreHandlers, HandlerMap } from "./Store";
import { _document, _isArray, _Infinity, _Map, _entries, _createTextNode } from "../utils/refCache";
import { toArr, isHNode } from "../utils/helpers";
import { handleProp } from "./handleProp";
Expand Down Expand Up @@ -31,12 +31,12 @@ export type HCallback<P extends object = any, S extends Store = Store, C extends
(this: HNode<P, S, C>, props: HProps<P>, store: S, context: C) => T;

export type EffectCallback<P extends object = any, S extends Store = Store, C extends Store = Store> =
HCallback<P, S, C, HCallback<P, S, C, void> | void>;
HCallback<P, S, C, (() => void) | void>;

export interface HDesc<P extends object = any, S extends Store = Store, C extends Store = Store> {
defaultProps?: Partial<P>;
defaultStore?: Partial<StoreType<S>>;
storeHandlers?: PartialHandlers<StoreHandlers<S>, S>;
storeHandlers?: Partial<HandlerMap<StoreType<S>, StoreHandlers<S>>>;
effects?: EffectCallback<P, S, C>[];
state?: Array<keyof StoreType<S>>;
context?: Array<keyof StoreType<C>>;
Expand All @@ -59,7 +59,7 @@ export interface HNode<P extends object = EleProps, S extends Store = Store, C e
nodes?: Node[];
active: boolean;
evMap?: EventMap;
eff?: HCallback<P, S, C, void>[];
eff?: (() => void)[];
err?: unknown;
}

Expand Down
7 changes: 4 additions & 3 deletions src/core/HUI.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HProps as _HProps, HDesc as _HDesc, HNode as _HNode, HCallback as _HCallback, EffectCallback as _EffectCallback } from "./HNode";
import { registry, define, HType as _HType } from "./registry";
import { _assign, _Infinity, _requestAnimationFrame } from "../utils/refCache";
import { Store as _Store, createStore, HandlerMap as _HandlerMap, Setter as _Setter, SetterRecord as _SetterRecord, StoreType as _StoreType, StoreHandlers as _StoreHandlers, PartialHandlers as _PartialHandlers, EmptyStore as _EmptyStore } from "./Store";
import { Store as _Store, createStore, HandlerMap as _HandlerMap, Setter as _Setter, SetterRecord as _SetterRecord, StoreType as _StoreType, StoreHandlers as _StoreHandlers, EmptyStore as _EmptyStore } from "./Store";
import { RenderOptions as _RenderOptions, renderToDOM } from "./render";
import { propHandlers, EleProps as _EleProps, PropHandler as _PropHandler, RefCallback as _RefCallback, AttributeMap as _AttributeMap } from "./propHandlers";
import { EventRecord as _EventRecord, EventMap as _EventMap } from "./events";
Expand Down Expand Up @@ -50,8 +50,9 @@ export namespace HUI {

export type HType<P extends object = any, S extends _Store = _Store, C extends _Store = _Store> = _HType<P, S, C>;

export type Store<T extends object = any, H extends _HandlerMap = any> = _Store<T, H>;
export type HandlerMap<T extends object = any> = _HandlerMap<T>;
export type Store<T extends object = any, H extends object = any> = _Store<T, H>;
export type StoreHandlers<S extends _Store = _Store> = _StoreHandlers<S>;
export type HandlerMap<T extends object = any, H extends object = any> = _HandlerMap<T, H>;
export type Setter<T = unknown> = _Setter<T>;
export type SetterRecord<T = unknown> = _SetterRecord<T>;
export type EmptyStore = _EmptyStore;
Expand Down
43 changes: 24 additions & 19 deletions src/core/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,33 @@ import { SpliceArgs } from "../utils/helpers";

type AssertArray<T> = T extends any[] ? T : never;
type MapOf<T> = Map<keyof T, Pick<T, keyof T>>;
type InjectThis<F extends (...args: any[]) => any, T> = (this: T, ...args: Parameters<F>) => ReturnType<F>;

export type Setter<T = unknown> = (value: T) => void;
export type SetterRecord<T = unknown> = [Setter<T> | undefined, Setter<T> | undefined];

export interface HandlerMap<T extends object = any> {
[name: string]: (this: Store<T, HandlerMap<T>>, ...args: any[]) => any;
}

export type PartialHandlers<H extends HandlerMap, T> = { [K in keyof H]?: InjectThis<H[K], T> | null };
export type HandlerMap<T extends object =any, H extends object = any> = {
[K in keyof H]: H[K] extends (this: Store<T, H>, ...args: infer A) => any ? (
H[K] extends (this: Store<T, H>, ...args: A) => infer R ? (
(this: Store<T, H>, ...args: A) => R
) : (
never
)
) : (
never
);
};

export type StoreType<S> = S extends Store<infer T> ? T : never;
export type StoreHandlers<S> = S extends Store<any, infer H> ? H : never;
export type StoreType<S extends Store> = S extends Store<infer T> ? T : never;
export type StoreHandlers<S extends Store> = S extends Store<any, infer H> ? H : never;

export type EmptyStore = Store<{}>;
export type EmptyStore = Store<{}, {}>;

export interface Store<T extends object = any, H extends HandlerMap<T> = any> {
export interface Store<T extends object = any, H extends object = any> {

valueMap: MapOf<T>;
bindingMap: Map<keyof T, HNode<any>[]>;
setterMap: Map<keyof T, SetterRecord<any>>;
handlerMap: MapOf<H>;
handlerMap: MapOf<HandlerMap<T, H>>;

bind(hNode: HNode<any>, subscriptions: Array<keyof T>): this;

Expand All @@ -46,21 +51,21 @@ export interface Store<T extends object = any, H extends HandlerMap<T> = any> {
splice<K extends keyof T>(key: K, start: number, deleteCount?: number): this;
splice<K extends keyof T>(key: K, start: number, deleteCount: number, ...items: AssertArray<T[K]>): this;

handle<N extends keyof H>(name: N, handler?: InjectThis<H[N], this> | null): this;
handleSome(handlers: PartialHandlers<H, this>): this;
getHandler<N extends keyof H>(name: N): InjectThis<H[N], this> | undefined;
trigger<N extends keyof H>(name: N, ...args: Parameters<H[N]>): ReturnType<H[N]> | undefined;
handle<N extends keyof HandlerMap<T, H>>(name: N, handler?: HandlerMap<T, H>[N]): this;
handleSome(handlers: Partial<HandlerMap<T, H>>): this;
getHandler<N extends keyof HandlerMap<T, H>>(name: N): HandlerMap<T, H>[N] | undefined;
trigger<N extends keyof HandlerMap<T, H>>(name: N, ...args: Parameters<HandlerMap<T, H>[N]>): ReturnType<HandlerMap<T, H>[N]> | undefined;

}

export const createStore = function crtSto<
T extends object = any, H extends HandlerMap<T> = any
T extends object = any, H extends object = any
>(): Store<T, H> {

const valueMap = new _Map<keyof T, any>(),
bindingMap = new _Map<keyof T, HNode<any>[]>(),
setterMap = new _Map<keyof T, SetterRecord<any>>(),
handlerMap = new _Map<keyof H, any>();
handlerMap = new _Map<keyof HandlerMap<T, H>, any>();

const store: Store<T, H> = {

Expand Down Expand Up @@ -179,13 +184,13 @@ export const createStore = function crtSto<

handleSome: function s_handleSome(handlers) {
_entries(handlers).forEach(pair => {
store.handle(pair[0], pair[1]);
store.handle(pair[0] as keyof H, pair[1]);
});
return this;
},

getHandler: function s_getHandler(name) {
return handlerMap.get(name) as any;
return handlerMap.get(name);
},

trigger: function s_trigger(name, ...args) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/initComponent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HNode, HCallback, HProps } from "./HNode";
import { HNode, HProps } from "./HNode";
import { Store } from "./Store";
import { supply } from "../utils/helpers";
import { _Boolean } from "../utils/refCache";
Expand Down Expand Up @@ -33,7 +33,7 @@ export const initComponent = function initCom<P extends object = any, S extends
}

if (effects) {
hNode.eff = effects.map(effect => effect.call(hNode, props, store, ctx!)).filter(_Boolean) as HCallback<P, S, C, void>[];
hNode.eff = effects.map(effect => effect.call(hNode, props, store, ctx!)).filter(_Boolean) as (() => void)[];
}

};
4 changes: 2 additions & 2 deletions src/core/render.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { toNodeArr, HNode } from "./HNode";
import { _document, _assign, _from } from "../utils/refCache";
import { createStore, Store, PartialHandlers, StoreType, StoreHandlers } from "./Store";
import { createStore, Store, StoreType, StoreHandlers, HandlerMap } from "./Store";
import { toFrag } from "../utils/helpers";
import { DeferCallback, reqTick, willTick } from "../ticker/ticker";

Expand All @@ -10,7 +10,7 @@ export interface RenderOptions<C extends Store = Store> {
clear?: boolean;
context?: C;
defaultContext?: Partial<StoreType<C>>;
contextHandlers?: PartialHandlers<StoreHandlers<C>, C>;
contextHandlers?: Partial<HandlerMap<StoreType<C>, StoreHandlers<C>>>;
parent?: Node;
owner?: HNode<any>;
sync?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/clear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const clear = function clr(hNode: HNode<any>) {

if (hNode.eff) {
hNode.eff.forEach(effCanceller => {
effCanceller.call(hNode, props, sto!, ctx!);
effCanceller();
});
}

Expand Down
2 changes: 1 addition & 1 deletion test/src/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ interface DialogStore {
on: boolean;
}

type DialogStoreHandlers = {
interface DialogStoreHandlers {
toggle: () => void;
}

Expand Down
2 changes: 1 addition & 1 deletion test/src/Timer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface TimerStore {
time: number;
}

type TimerStoreHandlers = {
interface TimerStoreHandlers {
setInterval: (interval: number) => any;
}

Expand Down

0 comments on commit 594cade

Please sign in to comment.