Skip to content

Commit 446107f

Browse files
committed
feat: re-define connect behavior
1 parent 855d34a commit 446107f

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

src/connect.tsx

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,41 @@ import { useAyanami } from './hooks'
55

66
type ConnectedComponent<P, S, A> = React.FunctionComponent<Omit<P, keyof S | keyof A>>
77

8-
export interface ComponentConnectedWithAyanami<M extends Ayanami<S>, S, P> {
9-
(): ConnectedComponent<P, S, ActionMethodOfAyanami<M, S>>
8+
type ConnectComponent<S, A> = <P>(Component: React.ComponentType<P>) => ConnectedComponent<P, S, A>
109

11-
<MS>(mapStateToProps: (state: S) => MS): ConnectedComponent<P, MS, ActionMethodOfAyanami<M, S>>
10+
export interface ComponentConnectedWithAyanami<M extends Ayanami<S>, S> {
11+
(): ConnectComponent<{}, {}>
12+
13+
<MS>(mapStateToProps: (state: S) => MS): ConnectComponent<MS, {}>
1214

1315
<MS, MA>(
1416
mapStateToProps: (state: S) => MS,
1517
mapActionsToProps: (actions: ActionMethodOfAyanami<M, S>) => MA,
16-
): ConnectedComponent<P, MS, MA>
18+
): ConnectComponent<MS, MA>
1719

1820
<MA>(
1921
mapStateToProps: null,
2022
mapActionsToProps: (actions: ActionMethodOfAyanami<M, S>) => MA,
21-
): ConnectedComponent<P, S, MA>
23+
): ConnectComponent<{}, MA>
2224
}
2325

24-
export function connectAyanami<M extends Ayanami<S>, S>(AyanamiClass: ConstructorOf<M>) {
25-
return function connectComponent<P>(
26-
Component: React.ComponentType<P>,
27-
): M extends Ayanami<infer SS>
28-
? ComponentConnectedWithAyanami<M, SS, P>
29-
: ComponentConnectedWithAyanami<M, S, P> {
30-
return ((
31-
mapStateToProps?: (props: S) => Partial<P>,
32-
mapActionsToProps?: (actions: ActionMethodOfAyanami<M, S>) => Partial<P>,
33-
) =>
34-
function ConnectAyanami(props: P) {
26+
export function connectAyanami<M extends Ayanami<S>, S>(
27+
AyanamiClass: ConstructorOf<M>,
28+
): M extends Ayanami<infer SS>
29+
? ComponentConnectedWithAyanami<M, SS>
30+
: ComponentConnectedWithAyanami<M, S> {
31+
return function connectMap<SP, AP>(
32+
mapStateToProps?: (props: S) => SP,
33+
mapActionsToProps?: (actions: ActionMethodOfAyanami<M, S>) => AP,
34+
) {
35+
return function connectComponent<P>(Component: React.ComponentType<P>) {
36+
return function ConnectAyanami(props: P) {
3537
const [state, action] = useAyanami(AyanamiClass)
36-
const mappedState = mapStateToProps ? mapStateToProps(state) : state
37-
const mappedAction = mapActionsToProps ? mapActionsToProps(action as any) : action
38+
const mappedState = mapStateToProps ? mapStateToProps(state) : {}
39+
const mappedAction = mapActionsToProps ? mapActionsToProps(action as any) : {}
3840

3941
return <Component {...mappedState} {...mappedAction} {...props} />
40-
}) as any
41-
}
42+
}
43+
}
44+
} as any
4245
}

test/specs/connect.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ class CountComponent extends React.Component<CountComponentProps> {
7171
private minus = (count: number) => () => this.props.minus(count)
7272
}
7373

74-
const ConnectedCountComponent = connectAyanami(Count)(CountComponent)(
74+
const ConnectedCountComponent = connectAyanami(Count)(
7575
({ count }) => ({ count }),
7676
({ add, minus }) => ({ add, minus }),
77-
)
77+
)(CountComponent)
7878

7979
describe('Connect spec:', () => {
8080
const testRenderer = create(<ConnectedCountComponent />)

0 commit comments

Comments
 (0)