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

Commit 580b89e

Browse files
committed
introduce Event type in dispatcher
1 parent 7e4eac4 commit 580b89e

File tree

4 files changed

+12
-15
lines changed

4 files changed

+12
-15
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
import AppDispatcher from '../dispatcher/AppDispatcher';
1+
import {Event, AppDispatcher} from '../dispatcher/AppDispatcher';
22
import GreetingActionTypes from '../constants/action-types/GreetingActionTypes';
33

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

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

1818
export function removeGreeting(greetingToRemove: string) {
1919
AppDispatcher.dispatch({
2020
greetingToRemove,
2121
type: GreetingActionTypes.REMOVE_GREETING
22-
});
22+
} as Event);
2323
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Dispatcher } from 'flux';
22

3-
const dispatcherInstance: Flux.Dispatcher<any> = new Dispatcher();
3+
export type Event = {type: string};
44

5-
export default dispatcherInstance;
5+
const dispatcherInstance: Flux.Dispatcher<Event> = new Dispatcher();
6+
7+
export const AppDispatcher = dispatcherInstance;

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class FluxStore<PayloadType, TState> {
99
_dispatcher: Flux.Dispatcher<PayloadType>;
1010
_cleanStateFn: () => TState;
1111
_state: TState;
12+
protected _onDispatch: (payload: PayloadType) => void;
1213

1314
constructor(dispatcher: Flux.Dispatcher<PayloadType>, cleanStateFn: () => TState) {
1415
this._emitter = new EventEmitter();
@@ -51,12 +52,6 @@ class FluxStore<PayloadType, TState> {
5152
this._emitter.emit(CHANGE_EVENT);
5253
}
5354
}
54-
55-
_onDispatch(payload: PayloadType) {
56-
if (process.env.NODE_ENV !== 'production') {
57-
console.error(`${this.constructor.name} has not overridden FluxStore.__onDispatch(), which is required`); // eslint-disable-line no-console
58-
}
59-
}
6055
}
6156

6257
export default FluxStore;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import FluxStore from './FluxStore';
22
import GreetingActionTypes from '../constants/action-types/GreetingActionTypes';
3-
import AppDispatcher from '../dispatcher/AppDispatcher';
3+
import {Event, AppDispatcher} from '../dispatcher/AppDispatcher';
44
import GreetingState from '../types/GreetingState';
55

66
class GreeterStore<TPayload> extends FluxStore<TPayload, GreetingState> {
@@ -15,8 +15,8 @@ class GreeterStore<TPayload> extends FluxStore<TPayload, GreetingState> {
1515
return this._state
1616
}
1717

18-
_onDispatch(action: any) {
19-
switch(action.type) {
18+
_onDispatch = (action: any) => {
19+
switch((<Event>action).type) {
2020
case GreetingActionTypes.ADD_GREETING:
2121
this._state.newGreeting = '';
2222
this._state.greetings = this._state.greetings.concat(action.newGreeting);

0 commit comments

Comments
 (0)