Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.

Commit 9061274

Browse files
reactive migrated. sort of.
1 parent 856f3aa commit 9061274

File tree

3 files changed

+57
-48
lines changed

3 files changed

+57
-48
lines changed

src/components/Notifications.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ interface NotificationProps {
150150
variant: string;
151151
}
152152

153+
interface NotificationsState {
154+
notifications: NotificationProps[];
155+
}
156+
153157
const Notification: ComponentType<NotificationProps> = withStyles(styles)(
154158
({
155159
autoHideDuration,
@@ -200,9 +204,9 @@ const Notification: ComponentType<NotificationProps> = withStyles(styles)(
200204
),
201205
);
202206

203-
const Notifications: StatelessComponent<{
204-
notifications: NotificationProps[];
205-
}> = ({ notifications }) => (
207+
const Notifications: StatelessComponent<NotificationsState> = ({
208+
notifications = [],
209+
}) => (
206210
<div>
207211
{notifications.map(notification => (
208212
<Notification key={notification.messageId} {...notification} />

src/reactive/index.js

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/reactive/index.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import React, { Component, ComponentType } from 'react';
2+
3+
import { Observable, ReplaySubject, Subscription } from 'rxjs';
4+
5+
export const createAction = <T>() => new ReplaySubject<T>(1);
6+
7+
export const createState = <TState>(
8+
reducer$,
9+
initialState$ = Observable.of({}),
10+
): Observable<TState> =>
11+
initialState$
12+
.merge(reducer$)
13+
.scan(
14+
// @ts-ignore
15+
(state, [scope, reducer]) =>
16+
// tslint:disable-next-line:no-object-literal-type-assertion
17+
({
18+
...(state as object),
19+
[scope]: reducer(state[scope]),
20+
} as TState),
21+
)
22+
.publishReplay(1)
23+
.refCount() as Observable<TState>;
24+
25+
export const connect = <TProps extends object, TState extends object>(
26+
state$: Observable<TState>,
27+
) => (
28+
WrappedComponent: ComponentType<TProps & TState>,
29+
): ComponentType<TProps> =>
30+
class extends Component<TProps, TState> {
31+
subscription: Subscription;
32+
33+
componentWillMount() {
34+
this.subscription = state$.subscribe(s => {
35+
this.setState(s || {});
36+
});
37+
}
38+
39+
componentWillUnmount() {
40+
this.subscription.unsubscribe();
41+
}
42+
43+
render() {
44+
// tslint:disable-next-line:no-object-literal-type-assertion
45+
return React.createElement(WrappedComponent, {
46+
...(this.state as object),
47+
...(this.props as object),
48+
} as TProps & TState);
49+
}
50+
};

0 commit comments

Comments
 (0)