-
Notifications
You must be signed in to change notification settings - Fork 6
/
register.ts
38 lines (31 loc) · 1.14 KB
/
register.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { ComponentRegistration, LazyNNArgument, LinkedRenderStore } from "link-lib";
import React, { ExoticComponent } from "react";
import { link } from "./hocs/link";
import { RegistrableComponent } from "./types";
export type higherOrderWrapper<P> = (c: React.ComponentType<P>) => React.ComponentType<P>;
export function registerExotic<P = {}>(
comp: ExoticComponent<P>,
type?: LazyNNArgument,
property?: LazyNNArgument,
topology?: LazyNNArgument,
): Array<ComponentRegistration<React.ComponentType<P>>> {
(comp as any).type = type;
(comp as any).property = property;
(comp as any).topology = topology;
return register(comp as any);
}
export function register<P>(comp: RegistrableComponent<P>):
Array<ComponentRegistration<React.ComponentType<P>>> {
// @ts-ignore
const wrappedComp = (comp.hocs || []).reduce(
(prev: React.ComponentType<P>, cur: higherOrderWrapper<P>) => cur(prev),
comp,
);
const dataBoundComp = link(comp.mapDataToProps, comp.linkOpts)(wrappedComp);
return LinkedRenderStore.registerRenderer<React.ComponentType<any>>(
dataBoundComp,
comp.type,
comp.property,
comp.topology,
);
}