Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 5e27fc0

Browse files
committed
move to TypedEvent
1 parent 4935670 commit 5e27fc0

File tree

4 files changed

+29
-49
lines changed

4 files changed

+29
-49
lines changed
Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
1-
import {Event, AppDispatcher} from '../dispatcher/AppDispatcher';
1+
import {TypedEvent, AppDispatcher} from '../dispatcher/AppDispatcher';
22
import GreetingActionTypes from '../constants/action-types/GreetingActionTypes';
33

4-
export interface AddGreetingEvent {type: string; payload: string;}
5-
export interface NewGreetingChanged {type: string; payload: string;}
6-
export interface RemoveGreeting {type: string; payload: string;}
4+
export class AddGreetingEvent extends TypedEvent<string> {}
5+
export class NewGreetingChanged extends TypedEvent<string> {}
6+
export class RemoveGreeting extends TypedEvent<string> {}
77

88
export function addGreeting(newGreeting: string) {
9-
AppDispatcher.dispatch({
10-
payload: newGreeting,
11-
type: GreetingActionTypes.ADD_GREETING
12-
} as AddGreetingEvent);
9+
AppDispatcher.dispatch(new AddGreetingEvent(newGreeting));
1310
}
1411

1512
export function newGreetingChanged(newGreeting: string) {
16-
AppDispatcher.dispatch({
17-
payload: newGreeting,
18-
type: GreetingActionTypes.NEW_GREETING_CHANGED
19-
} as NewGreetingChanged);
13+
AppDispatcher.dispatch(new NewGreetingChanged(newGreeting));
2014
}
2115

2216
export function removeGreeting(greetingToRemove: string) {
23-
AppDispatcher.dispatch({
24-
payload: greetingToRemove,
25-
type: GreetingActionTypes.REMOVE_GREETING
26-
} as RemoveGreeting);
17+
AppDispatcher.dispatch(new RemoveGreeting(greetingToRemove));
2718
}

react-with-type-safe-flux/src/dispatcher/AppDispatcher.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { Dispatcher } from 'flux';
22

3-
export type Event = {type: string; payload: any};
3+
export class TypedEvent<P> {
4+
constructor(public payload: P) {}
5+
}
6+
7+
export type Event = TypedEvent<any>;
48

59
const dispatcherInstance: Flux.Dispatcher<Event> = new Dispatcher();
610

react-with-type-safe-flux/src/stores/GreetingStore.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,19 @@ import { AddGreetingEvent, RemoveGreeting, NewGreetingChanged } from '../actions
77
class GreeterStore extends FluxStore<GreetingState> {
88
constructor(dispatcher: Flux.Dispatcher<Event>) {
99
const onDispatch = (action: Event) => {
10-
switch(action.type) {
11-
case GreetingActionTypes.ADD_GREETING:
12-
let payload1 = (<AddGreetingEvent> action).payload;
13-
this._state.newGreeting = '';
14-
this._state.greetings = this._state.greetings.concat(payload1);
15-
this.emitChange();
16-
break;
17-
case GreetingActionTypes.REMOVE_GREETING:
18-
let payload2 = (<RemoveGreeting> action).payload;
19-
this._state.greetings = this._state.greetings.filter(g => g !== payload2);
20-
this.emitChange();
21-
break;
22-
case GreetingActionTypes.NEW_GREETING_CHANGED:
23-
let payload3 = (<NewGreetingChanged> action).payload;
24-
this._state.newGreeting = payload3;
25-
this.emitChange();
26-
break;
10+
if (action instanceof AddGreetingEvent) {
11+
const {payload} = action;
12+
this._state.newGreeting = '';
13+
this._state.greetings = this._state.greetings.concat(payload);
14+
this.emitChange();
15+
} else if (action instanceof RemoveGreeting) {
16+
const {payload} = action;
17+
this._state.greetings = this._state.greetings.filter(g => g !== payload);
18+
this.emitChange();
19+
} else if (action instanceof NewGreetingChanged) {
20+
const {payload} = action;
21+
this._state.newGreeting = payload;
22+
this.emitChange();
2723
}
2824
}
2925
super(dispatcher, onDispatch, () => ({

react-with-type-safe-flux/test/stores/GreetingStore.tests.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import GreetingStore from '../../src/stores/GreetingStore';
22
import GreetingActionTypes from '../../src/constants/action-types/GreetingActionTypes';
3+
import { AddGreetingEvent, RemoveGreeting, NewGreetingChanged } from '../../src/actions/GreetingActions';
34

45
const registeredCallback = GreetingStore._onDispatch.bind(GreetingStore);
56

@@ -16,10 +17,7 @@ describe('GreetingStore', () => {
1617
});
1718

1819
it('given an ADD_GREETING action with a newGreeting of \'Benjamin\', the newGreeting should be an empty string and greetings should contain \'Benjamin\'', () => {
19-
[{
20-
payload: 'Benjamin',
21-
type: GreetingActionTypes.ADD_GREETING,
22-
}].forEach(registeredCallback);
20+
[new AddGreetingEvent('Benjamin')].forEach(registeredCallback);
2321

2422
const { greetings, newGreeting } = GreetingStore.getState();
2523

@@ -28,13 +26,7 @@ describe('GreetingStore', () => {
2826
});
2927

3028
it('given an REMOVE_GREETING action with a greetingToRemove of \'Benjamin\', the state greetings should be an empty array', () => {
31-
[{
32-
payload: 'Benjamin',
33-
type: GreetingActionTypes.ADD_GREETING,
34-
}, {
35-
payload: 'Benjamin',
36-
type: GreetingActionTypes.REMOVE_GREETING,
37-
}].forEach(registeredCallback);
29+
[new AddGreetingEvent('Benjamin'), new RemoveGreeting('Benjamin')].forEach(registeredCallback);
3830

3931
const { greetings } = GreetingStore.getState();
4032

@@ -43,10 +35,7 @@ describe('GreetingStore', () => {
4335
});
4436

4537
it('given a NEW_GREETING_CHANGED action with a newGreeting of \'Benjamin\', the state newGreeting should be \'Benjamin\'', () => {
46-
[{
47-
payload: 'Benjamin',
48-
type: GreetingActionTypes.NEW_GREETING_CHANGED,
49-
}].forEach(registeredCallback);
38+
[new NewGreetingChanged('Benjamin')].forEach(registeredCallback);
5039

5140
const { newGreeting } = GreetingStore.getState();
5241

0 commit comments

Comments
 (0)