From c565bcdcb7b17309f51c960284acad13d8564a47 Mon Sep 17 00:00:00 2001 From: hhh <1340545944@qq.com> Date: Thu, 31 Jan 2019 09:20:53 +0800 Subject: [PATCH] add effects API --- src/core/HNode.ts | 14 +++++++++--- src/core/HUI.ts | 4 +++- src/core/initComponent.ts | 17 +++++++++------ src/ext/Portal.ts | 45 ++++++++++++++++++++++----------------- src/utils/clear.ts | 20 ++++++++++++----- test/src/Timer.tsx | 16 ++++++++------ 6 files changed, 75 insertions(+), 41 deletions(-) diff --git a/src/core/HNode.ts b/src/core/HNode.ts index 6469300..22422aa 100644 --- a/src/core/HNode.ts +++ b/src/core/HNode.ts @@ -27,15 +27,22 @@ export type HProps
= { children: unknown[]; }); +export type HCallback
= + (this: HNode
, props: HProps
, store: S, context: C) => T; + +export type EffectCallback
= + HCallback
| void>; + export interface HDesc
{ defaultProps?: Partial
;
defaultStore?: Partial [];
state?: Array , props: HProps , store: S, context: C) => void;
- render: (this: HNode , props: HProps , store: S, context: C) => unknown;
- clear?: (this: HNode , props: HProps , store: S, context: C) => void;
+ init?: HCallback ;
+ render: HCallback ;
+ clear?: HCallback ;
catch?: (this: HNode , err: any, props: HProps , store: S, context: C) => unknown;
}
@@ -52,6 +59,7 @@ export interface HNode [];
err?: unknown;
}
diff --git a/src/core/HUI.ts b/src/core/HUI.ts
index b4fabe9..d9ac800 100644
--- a/src/core/HUI.ts
+++ b/src/core/HUI.ts
@@ -1,4 +1,4 @@
-import { HProps as _HProps, HDesc as _HDesc, HNode as _HNode } from "./HNode";
+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";
@@ -43,6 +43,8 @@ HUI.cmp = compare;
export namespace HUI {
export type HProps = _HProps ;
+ export type HCallback = _HCallback ;
+ export type EffectCallback = _EffectCallback ;
export type HDesc = _HDesc ;
export type HNode = _HNode ;
diff --git a/src/core/initComponent.ts b/src/core/initComponent.ts
index 9aff5e9..53e1ee4 100644
--- a/src/core/initComponent.ts
+++ b/src/core/initComponent.ts
@@ -1,15 +1,16 @@
-import { HNode } from "./HNode";
+import { HNode, HCallback, HProps } from "./HNode";
import { Store } from "./Store";
import { supply } from "../utils/helpers";
+import { _Boolean } from "../utils/refCache";
-export const initComponent = function initCom(hNode: HNode (hNode: HNode , store: S) {
const { props, ctx } = hNode,
desc = hNode.desc!,
- { defaultProps, defaultStore, storeHandlers, state, context, init } = desc;
+ { defaultProps, defaultStore, storeHandlers, state, context, init, effects } = desc;
if (defaultProps) {
- supply(props, defaultProps);
+ supply(props, defaultProps as Partial [];
+ }
+
};
\ No newline at end of file
diff --git a/src/ext/Portal.ts b/src/ext/Portal.ts
index 369dbcf..ed9712b 100644
--- a/src/ext/Portal.ts
+++ b/src/ext/Portal.ts
@@ -19,32 +19,39 @@ export interface PortalStore {
export const Portal = define