From 594cadef9cbd9f748a66c2b9249c5df1d9b1bbf4 Mon Sep 17 00:00:00 2001
From: hhh <1340545944@qq.com>
Date: Thu, 31 Jan 2019 10:12:33 +0800
Subject: [PATCH] improve type declarations
---
src/core/HNode.ts | 8 ++++----
src/core/HUI.ts | 7 ++++---
src/core/Store.ts | 43 ++++++++++++++++++++++-----------------
src/core/initComponent.ts | 4 ++--
src/core/render.ts | 4 ++--
src/utils/clear.ts | 2 +-
test/src/Dialog.tsx | 2 +-
test/src/Timer.tsx | 2 +-
8 files changed, 39 insertions(+), 33 deletions(-)
diff --git a/src/core/HNode.ts b/src/core/HNode.ts
index 22422aa..2b7910a 100644
--- a/src/core/HNode.ts
+++ b/src/core/HNode.ts
@@ -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";
@@ -31,12 +31,12 @@ export type HCallback
, props: HProps
, store: S, context: C) => T;
export type EffectCallback
=
- HCallback
| void>;
+ HCallback
void) | void>;
export interface HDesc
{
defaultProps?: Partial
;
defaultStore?: Partial>;
- storeHandlers?: PartialHandlers, S>;
+ storeHandlers?: Partial, StoreHandlers>>;
effects?: EffectCallback[];
state?: Array>;
context?: Array>;
@@ -59,7 +59,7 @@ export interface HNode[];
+ eff?: (() => void)[];
err?: unknown;
}
diff --git a/src/core/HUI.ts b/src/core/HUI.ts
index d9ac800..fa0c859 100644
--- a/src/core/HUI.ts
+++ b/src/core/HUI.ts
@@ -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";
@@ -50,8 +50,9 @@ export namespace HUI {
export type HType
= _HType
;
- export type Store = _Store;
- export type HandlerMap = _HandlerMap;
+ export type Store = _Store;
+ export type StoreHandlers = _StoreHandlers;
+ export type HandlerMap = _HandlerMap;
export type Setter = _Setter;
export type SetterRecord = _SetterRecord;
export type EmptyStore = _EmptyStore;
diff --git a/src/core/Store.ts b/src/core/Store.ts
index 1f7f93a..e0c951c 100644
--- a/src/core/Store.ts
+++ b/src/core/Store.ts
@@ -6,28 +6,33 @@ import { SpliceArgs } from "../utils/helpers";
type AssertArray = T extends any[] ? T : never;
type MapOf = Map>;
-type InjectThis any, T> = (this: T, ...args: Parameters) => ReturnType;
export type Setter = (value: T) => void;
export type SetterRecord = [Setter | undefined, Setter | undefined];
-export interface HandlerMap {
- [name: string]: (this: Store>, ...args: any[]) => any;
-}
-
-export type PartialHandlers = { [K in keyof H]?: InjectThis | null };
+export type HandlerMap = {
+ [K in keyof H]: H[K] extends (this: Store, ...args: infer A) => any ? (
+ H[K] extends (this: Store, ...args: A) => infer R ? (
+ (this: Store, ...args: A) => R
+ ) : (
+ never
+ )
+ ) : (
+ never
+ );
+};
-export type StoreType = S extends Store ? T : never;
-export type StoreHandlers = S extends Store ? H : never;
+export type StoreType = S extends Store ? T : never;
+export type StoreHandlers = S extends Store ? H : never;
-export type EmptyStore = Store<{}>;
+export type EmptyStore = Store<{}, {}>;
-export interface Store = any> {
+export interface Store {
valueMap: MapOf;
bindingMap: Map[]>;
setterMap: Map>;
- handlerMap: MapOf;
+ handlerMap: MapOf>;
bind(hNode: HNode, subscriptions: Array): this;
@@ -46,21 +51,21 @@ export interface Store = any> {
splice(key: K, start: number, deleteCount?: number): this;
splice(key: K, start: number, deleteCount: number, ...items: AssertArray): this;
- handle(name: N, handler?: InjectThis | null): this;
- handleSome(handlers: PartialHandlers): this;
- getHandler(name: N): InjectThis | undefined;
- trigger(name: N, ...args: Parameters): ReturnType | undefined;
+ handle>(name: N, handler?: HandlerMap[N]): this;
+ handleSome(handlers: Partial>): this;
+ getHandler>(name: N): HandlerMap[N] | undefined;
+ trigger>(name: N, ...args: Parameters[N]>): ReturnType[N]> | undefined;
}
export const createStore = function crtSto<
- T extends object = any, H extends HandlerMap = any
+ T extends object = any, H extends object = any
>(): Store {
const valueMap = new _Map(),
bindingMap = new _Map[]>(),
setterMap = new _Map>(),
- handlerMap = new _Map();
+ handlerMap = new _Map, any>();
const store: Store = {
@@ -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) {
diff --git a/src/core/initComponent.ts b/src/core/initComponent.ts
index 53e1ee4..6ca0594 100644
--- a/src/core/initComponent.ts
+++ b/src/core/initComponent.ts
@@ -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";
@@ -33,7 +33,7 @@ export const initComponent = function initCom effect.call(hNode, props, store, ctx!)).filter(_Boolean) as HCallback
[];
+ hNode.eff = effects.map(effect => effect.call(hNode, props, store, ctx!)).filter(_Boolean) as (() => void)[];
}
};
\ No newline at end of file
diff --git a/src/core/render.ts b/src/core/render.ts
index ac87902..32cacfa 100644
--- a/src/core/render.ts
+++ b/src/core/render.ts
@@ -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";
@@ -10,7 +10,7 @@ export interface RenderOptions {
clear?: boolean;
context?: C;
defaultContext?: Partial>;
- contextHandlers?: PartialHandlers, C>;
+ contextHandlers?: Partial, StoreHandlers>>;
parent?: Node;
owner?: HNode;
sync?: boolean;
diff --git a/src/utils/clear.ts b/src/utils/clear.ts
index 5ddfbe8..1a2d062 100644
--- a/src/utils/clear.ts
+++ b/src/utils/clear.ts
@@ -17,7 +17,7 @@ export const clear = function clr(hNode: HNode) {
if (hNode.eff) {
hNode.eff.forEach(effCanceller => {
- effCanceller.call(hNode, props, sto!, ctx!);
+ effCanceller();
});
}
diff --git a/test/src/Dialog.tsx b/test/src/Dialog.tsx
index 3cdd3c4..9607fcd 100644
--- a/test/src/Dialog.tsx
+++ b/test/src/Dialog.tsx
@@ -2,7 +2,7 @@ interface DialogStore {
on: boolean;
}
-type DialogStoreHandlers = {
+interface DialogStoreHandlers {
toggle: () => void;
}
diff --git a/test/src/Timer.tsx b/test/src/Timer.tsx
index 89bf584..744e59c 100644
--- a/test/src/Timer.tsx
+++ b/test/src/Timer.tsx
@@ -6,7 +6,7 @@ interface TimerStore {
time: number;
}
-type TimerStoreHandlers = {
+interface TimerStoreHandlers {
setInterval: (interval: number) => any;
}