Skip to content

Commit

Permalink
improve some internals
Browse files Browse the repository at this point in the history
  • Loading branch information
huang2002 committed Jan 22, 2019
1 parent 0b17fc6 commit 304eea4
Show file tree
Hide file tree
Showing 19 changed files with 135 additions and 147 deletions.
5 changes: 4 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"presets": [
[
"@babel/preset-env"
"@babel/preset-env",
{
"loose": true
}
]
]
}
26 changes: 13 additions & 13 deletions src/core/HNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ export interface HDesc<P extends object = any, S extends object = any, C extends
}

export interface HNode<P extends object = EleProps, S extends object = any, C extends object = any, SH extends HandlerMap<S> = any, CH extends HandlerMap<C> = any> {
isHNode: true;
isHN: true;
type: unknown;
desc?: HDesc<P, S, C, SH, CH>;
props: HProps<P>;
store?: Store<S, SH>;
context?: Store<C, CH>;
sto?: Store<S, SH>;
ctx?: Store<C, CH>;
owner?: HNode<any>;
ownerNode?: Node;
output?: unknown[];
ownNode?: Node;
out?: unknown[];
nodes?: Node[];
active: boolean;
events?: EventMap;
error?: unknown;
evMap?: EventMap;
err?: unknown;
}

export const toNodeArr = function toNodes(
Expand All @@ -69,9 +69,9 @@ export const toNodeArr = function toNodes(

const { type, desc } = src;

src.ownerNode = ownerNode;
src.ownNode = ownerNode;
src.owner = owner;
src.context = context;
src.ctx = context;

if (desc) {

Expand All @@ -84,7 +84,7 @@ export const toNodeArr = function toNodes(
initComponent(src, store);

return src.nodes = toNodeArr(
src.output = toArr(
src.out = toArr(
desc.render.call(src, src.props, store, context)
).flat(_Infinity),
context,
Expand All @@ -96,7 +96,7 @@ export const toNodeArr = function toNodes(

if (desc.catch) {
return src.nodes = toNodeArr(
src.output = toArr(
src.out = toArr(
desc.catch.call(src, err, src.props, store, context)
).flat(_Infinity),
context,
Expand All @@ -115,7 +115,7 @@ export const toNodeArr = function toNodes(

const { props } = src;

src.events = new _Map();
src.evMap = new _Map();

const element = props.xmlns ?
_document.createElementNS(props.xmlns, type as string) :
Expand All @@ -125,7 +125,7 @@ export const toNodeArr = function toNodes(
handleProp(element, src, pair[0], pair[1]);
});

src.output = [];
src.out = [];
src.nodes = [element];

return [element];
Expand Down
16 changes: 8 additions & 8 deletions src/core/HUI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import { _assign, _Infinity, _requestAnimationFrame } from "../utils/refCache";
import { createStore } from "./Store";
import { renderToDOM } from "./render";
import { propHandlers, EleProps } from "./propHandlers";
import { portalSymbol } from "../ext/Portal";
import { Portal } from "../ext/Portal";
import { defer } from "../ticker/ticker";
import { cmp } from "../utils/cmp";
import { fragmentSymbol } from "../ext/Fragment";
import { compare } from "../utils/cmp";
import { Fragment } from "../ext/Fragment";
import { noCmpProps } from "../ticker/patch";

export const HUI = <P extends object = EleProps, S extends object = any, C extends object = any>(
type: HType<P, S, C> | string, props?: P | null, ...children: unknown[]
): HNode<P, S, C> => ({
isHNode: true,
isHN: true,
type,
desc: registry.get(type),
props: _assign({ children: children.flat(_Infinity) }, props) as HProps<P>,
Expand All @@ -30,11 +30,11 @@ HUI.noCmpProps = noCmpProps;

HUI.render = renderToDOM;

HUI.tick = function (callback: () => void) { _requestAnimationFrame(callback); };
HUI.tick = function tick(callback: () => void) { _requestAnimationFrame(callback); };
HUI.frameLimit = 15;
HUI.defer = defer;

HUI.Portal = portalSymbol;
HUI.Fragment = fragmentSymbol;
HUI.Portal = Portal;
HUI.Fragment = Fragment;

HUI.cmp = cmp;
HUI.cmp = compare;
34 changes: 18 additions & 16 deletions src/core/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ export interface Store<T extends object = any, H extends HandlerMap<T> = any> {

}

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

const valueMap = new _Map<keyof T, any>(),
bindingMap = new _Map<keyof T, HNode<any>[]>(),
Expand All @@ -62,7 +64,7 @@ export const createStore = function <T extends object = any, H extends HandlerMa
setterMap,
handlerMap,

bind: function store_bind(hNode, subscriptions) {
bind: function s_bind(hNode, subscriptions) {
subscriptions.forEach(key => {
if (bindingMap.has(key)) {
bindingMap.get(key)!.push(hNode);
Expand All @@ -73,11 +75,11 @@ export const createStore = function <T extends object = any, H extends HandlerMa
return this;
},

get: function store_get(key) {
get: function s_get(key) {
return valueMap.get(key);
},

set: function store_set(key, value, force) {
set: function s_set(key, value, force) {

if (force || !HUI.cmp(value, store.get(key))) {

Expand All @@ -97,7 +99,7 @@ export const createStore = function <T extends object = any, H extends HandlerMa

},

setter: function store_setter(key, force) {
setter: function s_setter(key, force) {

const index = +!force;

Expand Down Expand Up @@ -128,40 +130,40 @@ export const createStore = function <T extends object = any, H extends HandlerMa

},

setSome: function store_setSome(pairs, force) {
setSome: function s_setSome(pairs, force) {
_entries(pairs).forEach(pair => {
store.set(pair[0] as keyof T, pair[1], force);
});
return this;
},

toggle: function store_toggle(key) {
toggle: function s_toggle(key) {
return store.set(key, !store.get(key) as any);
},

inc: function store_inc(key, addition = 1) {
inc: function s_inc(key, addition = 1) {
return store.set(key, store.get(key) + addition);
},

push: function store_push(key, ...items) {
push: function s_push(key, ...items) {
return store.set(key, (store.get(key) as unknown as any[]).concat(items) as any);
},

unshift: function store_unshift(key, ...items) {
unshift: function s_unshift(key, ...items) {
return store.set(key, items.concat(store.get(key)) as any);
},

slice: function store_slice(key, start, end) {
slice: function s_slice(key, start, end) {
return store.set(key, (store.get(key) as unknown as any[]).slice(start, end) as any);
},

splice: function store_splice(key: keyof T, start: number, deleteCount: number, ...items: any[]) {
splice: function s_splice(key: keyof T, start: number, deleteCount: number, ...items: any[]) {
const arr = (store.get(key) as unknown as any[]).slice();
_splice.apply(arr, [start, deleteCount].concat(items) as SpliceArgs);
return store.set(key, arr as any);
},

handle: function store_handle(name, handler) {
handle: function s_handle(name, handler) {
if (handler) {
handlerMap.set(name, handler.bind(store));
} else {
Expand All @@ -170,18 +172,18 @@ export const createStore = function <T extends object = any, H extends HandlerMa
return this;
},

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

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

trigger: function store_trigger(name, ...args) {
trigger: function s_trigger(name, ...args) {
const handler = handlerMap.get(name);
if (handler) {
return handler.apply(store, args);
Expand Down
12 changes: 3 additions & 9 deletions src/core/events.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
export type EventRecord = [string, EventListener, boolean | AddEventListenerOptions];
export type EventMap = Map<string, EventRecord>;

export const LISTENER_PREFIX = 'on';

const CAPTURE = 'Captrue',
NONPASSIVE = 'Nonpassive',
ONCE = 'Once';

export const listen = function (element: Element, event: string, listener: EventListener): EventRecord {

let capture = false,
passive = true,
once = false;

for (let i = 0; i < 3; i++) {
if (event.endsWith(CAPTURE)) {
if (event.endsWith('Capture')) {
capture = true;
event = event.slice(0, -7);
} else if (event.endsWith(NONPASSIVE)) {
} else if (event.endsWith('Nonpassive')) {
passive = false;
event = event.slice(0, -10);
} else if (event.endsWith(ONCE)) {
} else if (event.endsWith('Once')) {
once = true;
event = event.slice(0, -4);
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/handleError.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HNode } from "./HNode";
import { mark } from "../ticker/ticker";

export const handleError = function (err: unknown, hNode: HNode<any>) {
export const handleError = function handleErr(err: unknown, hNode: HNode<any>) {

let { owner } = hNode,
desc;
Expand All @@ -12,7 +12,7 @@ export const handleError = function (err: unknown, hNode: HNode<any>) {
owner = owner.owner;
}

owner.error = err;
owner.err = err;

mark(owner);

Expand Down
10 changes: 5 additions & 5 deletions src/core/handleProp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { propHandlers } from "./propHandlers";
import { listen, LISTENER_PREFIX } from "./events";
import { listen } from "./events";
import { HNode } from "./HNode";

export const handleProp = function (
Expand All @@ -14,16 +14,16 @@ export const handleProp = function (

} else {

if (key.startsWith(LISTENER_PREFIX)) {
if (key.startsWith('on')) {

const { events } = hNode,
record = events!.get(key);
const { evMap } = hNode,
record = evMap!.get(key);

if (record) {
element.removeEventListener(record[0], record[1], record[2]);
}

events!.set(key, listen(element, key.slice(2), newValue as EventListener));
evMap!.set(key, listen(element, key.slice(2), newValue as EventListener));

} else if (key in element) {

Expand Down
12 changes: 6 additions & 6 deletions src/core/initComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@ import { HNode } from "./HNode";
import { Store } from "./Store";
import { supply } from "../utils/helpers";

export const initComponent = function (hNode: HNode<any>, store: Store) {
export const initComponent = function initCom(hNode: HNode<any>, store: Store) {

const { props, context: ctxStore } = hNode,
const { props, ctx } = hNode,
desc = hNode.desc!,
{ defaultProps, defaultStore, storeHandlers, state, context, init } = desc;

if (defaultProps) {
supply(props, defaultProps);
}

hNode.store = store;
hNode.sto = store;
if (defaultStore) {
store.setSome(defaultStore);
}
if (state) {
hNode.store.bind(hNode, state);
hNode.sto.bind(hNode, state);
}
if (storeHandlers) {
store.handleSome(storeHandlers);
}

if (context) {
ctxStore!.bind(hNode, context);
ctx!.bind(hNode, context);
}

if (init) {
init.call(hNode, props, store, ctxStore!);
init.call(hNode, props, store, ctx!);
}

};
Loading

0 comments on commit 304eea4

Please sign in to comment.