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

Commit 4d7b2c1

Browse files
committed
use {type: string; payload: any} as type for actions
1 parent 580b89e commit 4d7b2c1

File tree

5 files changed

+37
-38
lines changed

5 files changed

+37
-38
lines changed

react-with-type-safe-flux/src/actions/GreetingActions.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ import GreetingActionTypes from '../constants/action-types/GreetingActionTypes';
33

44
export function addGreeting(newGreeting: string) {
55
AppDispatcher.dispatch({
6-
newGreeting,
6+
payload: newGreeting,
77
type: GreetingActionTypes.ADD_GREETING
8-
} as Event);
8+
});
99
}
1010

1111
export function newGreetingChanged(newGreeting: string) {
1212
AppDispatcher.dispatch({
13-
newGreeting,
13+
payload: newGreeting,
1414
type: GreetingActionTypes.NEW_GREETING_CHANGED
15-
} as Event);
15+
});
1616
}
1717

1818
export function removeGreeting(greetingToRemove: string) {
1919
AppDispatcher.dispatch({
20-
greetingToRemove,
20+
payload: greetingToRemove,
2121
type: GreetingActionTypes.REMOVE_GREETING
22-
} as Event);
22+
});
2323
}

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

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

3-
export type Event = {type: string};
3+
export type Event = {type: string; payload: any};
44

55
const dispatcherInstance: Flux.Dispatcher<Event> = new Dispatcher();
66

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import { EventEmitter } from 'events';
2+
import { Event } from '../dispatcher/AppDispatcher';
23

34
const CHANGE_EVENT = 'change';
45

5-
class FluxStore<PayloadType, TState> {
6+
class FluxStore<TState> {
67
_changed: boolean;
78
_emitter: EventEmitter;
89
dispatchToken: string;
9-
_dispatcher: Flux.Dispatcher<PayloadType>;
10+
_dispatcher: Flux.Dispatcher<Event>;
1011
_cleanStateFn: () => TState;
1112
_state: TState;
12-
protected _onDispatch: (payload: PayloadType) => void;
1313

14-
constructor(dispatcher: Flux.Dispatcher<PayloadType>, cleanStateFn: () => TState) {
14+
constructor(dispatcher: Flux.Dispatcher<Event>, protected _onDispatch: (action: Event) => void, cleanStateFn: () => TState) {
1515
this._emitter = new EventEmitter();
1616
this._changed = false;
1717
this._dispatcher = dispatcher;
18-
this.dispatchToken = dispatcher.register((payload: PayloadType) => {
18+
this.dispatchToken = dispatcher.register((payload: Event) => {
1919
this._invokeOnDispatch(payload);
2020
});
2121

@@ -45,7 +45,7 @@ class FluxStore<PayloadType, TState> {
4545
this._state = this._cleanStateFn();
4646
}
4747

48-
_invokeOnDispatch(payload: PayloadType) {
48+
_invokeOnDispatch(payload: Event) {
4949
this._changed = false;
5050
this._onDispatch(payload);
5151
if (this._changed) {

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

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,26 @@ import GreetingActionTypes from '../constants/action-types/GreetingActionTypes';
33
import {Event, AppDispatcher} from '../dispatcher/AppDispatcher';
44
import GreetingState from '../types/GreetingState';
55

6-
class GreeterStore<TPayload> extends FluxStore<TPayload, GreetingState> {
7-
constructor(dispatcher: Flux.Dispatcher<TPayload>) {
8-
super(dispatcher, () => ({
6+
class GreeterStore extends FluxStore<GreetingState> {
7+
constructor(dispatcher: Flux.Dispatcher<Event>) {
8+
const onDispatch = (action: Event) => {
9+
switch(action.type) {
10+
case GreetingActionTypes.ADD_GREETING:
11+
this._state.newGreeting = '';
12+
this._state.greetings = this._state.greetings.concat(action.payload);
13+
this.emitChange();
14+
break;
15+
case GreetingActionTypes.REMOVE_GREETING:
16+
this._state.greetings = this._state.greetings.filter(g => g !== action.payload);
17+
this.emitChange();
18+
break;
19+
case GreetingActionTypes.NEW_GREETING_CHANGED:
20+
this._state.newGreeting = action.payload;
21+
this.emitChange();
22+
break;
23+
}
24+
}
25+
super(dispatcher, onDispatch, () => ({
926
greetings: [],
1027
newGreeting: ''
1128
}));
@@ -14,24 +31,6 @@ class GreeterStore<TPayload> extends FluxStore<TPayload, GreetingState> {
1431
getState() {
1532
return this._state
1633
}
17-
18-
_onDispatch = (action: any) => {
19-
switch((<Event>action).type) {
20-
case GreetingActionTypes.ADD_GREETING:
21-
this._state.newGreeting = '';
22-
this._state.greetings = this._state.greetings.concat(action.newGreeting);
23-
this.emitChange();
24-
break;
25-
case GreetingActionTypes.REMOVE_GREETING:
26-
this._state.greetings = this._state.greetings.filter(g => g !== action.greetingToRemove);
27-
this.emitChange();
28-
break;
29-
case GreetingActionTypes.NEW_GREETING_CHANGED:
30-
this._state.newGreeting = action.newGreeting;
31-
this.emitChange();
32-
break;
33-
}
34-
}
3534
}
3635

3736
const greeterStoreInstance = new GreeterStore(AppDispatcher);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('GreetingStore', () => {
1717

1818
it('given an ADD_GREETING action with a newGreeting of \'Benjamin\', the newGreeting should be an empty string and greetings should contain \'Benjamin\'', () => {
1919
[{
20-
newGreeting: 'Benjamin',
20+
payload: 'Benjamin',
2121
type: GreetingActionTypes.ADD_GREETING,
2222
}].forEach(registeredCallback);
2323

@@ -29,10 +29,10 @@ describe('GreetingStore', () => {
2929

3030
it('given an REMOVE_GREETING action with a greetingToRemove of \'Benjamin\', the state greetings should be an empty array', () => {
3131
[{
32-
newGreeting: 'Benjamin',
32+
payload: 'Benjamin',
3333
type: GreetingActionTypes.ADD_GREETING,
3434
}, {
35-
greetingToRemove: 'Benjamin',
35+
payload: 'Benjamin',
3636
type: GreetingActionTypes.REMOVE_GREETING,
3737
}].forEach(registeredCallback);
3838

@@ -44,7 +44,7 @@ describe('GreetingStore', () => {
4444

4545
it('given a NEW_GREETING_CHANGED action with a newGreeting of \'Benjamin\', the state newGreeting should be \'Benjamin\'', () => {
4646
[{
47-
newGreeting: 'Benjamin',
47+
payload: 'Benjamin',
4848
type: GreetingActionTypes.NEW_GREETING_CHANGED,
4949
}].forEach(registeredCallback);
5050

0 commit comments

Comments
 (0)