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

Commit 468e0f1

Browse files
Perform the merge + fixups.
1 parent 5f4e424 commit 468e0f1

File tree

14 files changed

+107
-136
lines changed

14 files changed

+107
-136
lines changed

es6-babel-react-flux-karma/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ES6 + TypeScript + Babel + React + Karma: The Secret Recipe
1+
# TypeScript, Babel, React, and Karma Sample
22

33
## Getting started
44

@@ -13,7 +13,7 @@ npm run serve
1313
This will:
1414

1515
1. Download the npm packages you need
16-
2. Download the type definitions you need.
16+
2. Download the type definitions from DefinitelyTyped that you need.
1717
3. Compile the code and serve it up at [http://localhost:8080](http://localhost:8080)
1818

1919
Now you need dev tools. There's a world of choice out there; there's [Atom](https://atom.io/), there's [VS Code](https://www.visualstudio.com/en-us/products/code-vs.aspx), there's [Sublime](http://www.sublimetext.com/). There's even something called [Visual Studio](http://www.visualstudio.com). It's all your choice really.
Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
1-
import AppDispatcher from '../dispatcher/AppDispatcher';
2-
import GreetingActionTypes from '../constants/action-types/GreetingActionTypes';
1+
import {TypedEvent, AppDispatcher} from '../dispatcher/AppDispatcher';
2+
3+
export class AddGreetingEvent extends TypedEvent<string> {}
4+
export class NewGreetingChanged extends TypedEvent<string> {}
5+
export class RemoveGreeting extends TypedEvent<string> {}
36

47
export function addGreeting(newGreeting: string) {
5-
AppDispatcher.dispatch({
6-
newGreeting,
7-
type: GreetingActionTypes.ADD_GREETING
8-
});
8+
AppDispatcher.dispatch(new AddGreetingEvent(newGreeting));
99
}
1010

1111
export function newGreetingChanged(newGreeting: string) {
12-
AppDispatcher.dispatch({
13-
newGreeting,
14-
type: GreetingActionTypes.NEW_GREETING_CHANGED
15-
});
12+
AppDispatcher.dispatch(new NewGreetingChanged(newGreeting));
1613
}
1714

1815
export function removeGreeting(greetingToRemove: string) {
19-
AppDispatcher.dispatch({
20-
greetingToRemove,
21-
type: GreetingActionTypes.REMOVE_GREETING
22-
});
16+
AppDispatcher.dispatch(new RemoveGreeting(greetingToRemove));
2317
}

es6-babel-react-flux-karma/src/components/App.tsx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@ import GreetingState from '../types/GreetingState';
55
import WhoToGreet from './WhoToGreet';
66
import Greeting from './Greeting';
77

8-
class App extends React.Component<any, GreetingState> {
9-
constructor(props) {
8+
class App extends React.Component<{}, GreetingState> {
9+
constructor(props: {}) {
1010
super(props);
11-
this.state = this._getStateFromStores();
11+
this.state = this.getStateFromStores();
12+
}
13+
private onChange = () => {
14+
this.setState(this.getStateFromStores());
1215
}
1316

14-
componentWillMount() {
15-
GreetingStore.addChangeListener(this._onChange);
17+
public componentWillMount() {
18+
GreetingStore.addChangeListener(this.onChange);
1619
}
1720

18-
componentWillUnmount() {
19-
GreetingStore.removeChangeListener(this._onChange);
21+
public componentWillUnmount() {
22+
GreetingStore.removeChangeListener(this.onChange);
2023
}
2124

2225
render() {
@@ -32,11 +35,7 @@ class App extends React.Component<any, GreetingState> {
3235
);
3336
}
3437

35-
_onChange = () => {
36-
this.setState(this._getStateFromStores());
37-
}
38-
39-
_getStateFromStores() {
38+
private getStateFromStores() {
4039
return GreetingStore.getState();
4140
}
4241
}

es6-babel-react-flux-karma/src/components/Greeting.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interface Props {
77
}
88

99
class Greeting extends React.Component<Props, any> {
10-
constructor(props) {
10+
constructor(props: Props) {
1111
super(props);
1212
}
1313

@@ -28,7 +28,7 @@ class Greeting extends React.Component<Props, any> {
2828
);
2929
}
3030

31-
_onClick = (event) => {
31+
_onClick = (event: React.MouseEvent) => {
3232
GreetingActions.removeGreeting(this.props.targetOfGreeting);
3333
}
3434
}

es6-babel-react-flux-karma/src/components/WhoToGreet.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ interface Props {
66
}
77

88
class WhoToGreet extends React.Component<Props, any> {
9-
constructor(props) {
9+
constructor(props: Props) {
1010
super(props);
1111
}
1212

@@ -35,12 +35,12 @@ class WhoToGreet extends React.Component<Props, any> {
3535
return !this.props.newGreeting;
3636
}
3737

38-
_handleNewGreetingChange = (event) => {
39-
const { target: { value: newGreeting } } = event;
38+
_handleNewGreetingChange = (event: React.FormEvent) => {
39+
const newGreeting = (event.target as HTMLInputElement).value;
4040
GreetingActions.newGreetingChanged(newGreeting);
4141
}
4242

43-
_onSubmit = (event) => {
43+
_onSubmit = (event: React.FormEvent) => {
4444
event.preventDefault();
4545

4646
if (!this._preventSubmission) {

es6-babel-react-flux-karma/src/constants/action-types/GreetingActionTypes.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { Dispatcher } from 'flux';
22

3-
const dispatcherInstance = new Dispatcher();
3+
export class TypedEvent<P> {
4+
constructor(public payload: P) {}
5+
}
46

5-
export default dispatcherInstance;
7+
export type Event = TypedEvent<any>;
8+
9+
const dispatcherInstance: Flux.Dispatcher<Event> = new Dispatcher();
10+
11+
export { dispatcherInstance as AppDispatcher };

es6-babel-react-flux-karma/src/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<meta http-equiv="X-UA-Compatible" content="IE=edge">
66
<meta name="viewport" content="width=device-width, initial-scale=1">
77

8-
<title>ES6 + Babel + React + Flux + Karma: The Secret Recipe</title>
8+
<title>TypeScript, Babel, React, Flux, and Karma</title>
99

1010
<!-- inject:css -->
1111
<!-- endinject -->

es6-babel-react-flux-karma/src/stores/FluxStore.ts

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,56 @@
11
import { EventEmitter } from 'events';
2+
import { Event } from '../dispatcher/AppDispatcher';
3+
import * as Flux from "flux";
24

35
const CHANGE_EVENT = 'change';
46

57
class FluxStore<TState> {
6-
_changed: boolean;
7-
_emitter: EventEmitter;
8-
dispatchToken: string;
9-
_dispatcher: Flux.Dispatcher<any>;
10-
_cleanStateFn: () => TState;
11-
_state: TState;
12-
13-
constructor(dispatcher, cleanStateFn) {
14-
this._emitter = new EventEmitter();
15-
this._changed = false;
16-
this._dispatcher = dispatcher;
8+
private changed: boolean;
9+
private emitter: EventEmitter;
10+
private dispatchToken: string;
11+
private dispatcher: Flux.Dispatcher<Event>;
12+
private cleanStateFn: () => TState;
13+
protected state: TState;
14+
15+
constructor(dispatcher: Flux.Dispatcher<Event>, public onDispatch: (action: Event) => void, cleanStateFn: () => TState) {
16+
this.emitter = new EventEmitter();
17+
this.changed = false;
18+
this.dispatcher = dispatcher;
1719
this.dispatchToken = dispatcher.register(payload => {
18-
this._invokeOnDispatch(payload);
20+
this.invokeOnDispatch(payload);
1921
});
2022

21-
this._cleanStateFn = cleanStateFn;
22-
this._state = this._cleanStateFn();
23+
this.cleanStateFn = cleanStateFn;
24+
this.state = this.cleanStateFn();
2325
}
2426

2527
/**
2628
* Is idempotent per dispatched event
2729
*/
2830
emitChange() {
29-
this._changed = true;
31+
this.changed = true;
3032
}
3133

32-
hasChanged() { return this._changed; }
34+
hasChanged() { return this.changed; }
3335

34-
addChangeListener(callback) {
35-
this._emitter.on(CHANGE_EVENT, callback);
36+
addChangeListener(callback: () => void) {
37+
this.emitter.on(CHANGE_EVENT, callback);
3638
}
3739

38-
removeChangeListener(callback) {
39-
this._emitter.removeListener(CHANGE_EVENT, callback);
40+
removeChangeListener(callback: () => void) {
41+
this.emitter.removeListener(CHANGE_EVENT, callback);
4042
}
4143

42-
_cleanState() {
43-
this._changed = false;
44-
this._state = this._cleanStateFn();
44+
protected cleanState() {
45+
this.changed = false;
46+
this.state = this.cleanStateFn();
4547
}
4648

47-
_invokeOnDispatch(payload) {
48-
this._changed = false;
49-
this._onDispatch(payload);
50-
if (this._changed) {
51-
this._emitter.emit(CHANGE_EVENT);
52-
}
53-
}
54-
55-
_onDispatch(payload) {
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
49+
private invokeOnDispatch(payload: Event) {
50+
this.changed = false;
51+
this.onDispatch(payload);
52+
if (this.changed) {
53+
this.emitter.emit(CHANGE_EVENT);
5854
}
5955
}
6056
}

es6-babel-react-flux-karma/src/stores/GreetingStore.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
import FluxStore from './FluxStore';
2-
import GreetingActionTypes from '../constants/action-types/GreetingActionTypes';
3-
import AppDispatcher from '../dispatcher/AppDispatcher';
2+
import {Event, AppDispatcher} from '../dispatcher/AppDispatcher';
43
import GreetingState from '../types/GreetingState';
4+
import { AddGreetingEvent, RemoveGreeting, NewGreetingChanged } from '../actions/GreetingActions';
55

66
class GreeterStore extends FluxStore<GreetingState> {
7-
constructor(dispatcher) {
8-
super(dispatcher, () => ({
7+
constructor(dispatcher: Flux.Dispatcher<Event>) {
8+
const onDispatch = (action: Event) => {
9+
if (action instanceof AddGreetingEvent) {
10+
const { payload } = action;
11+
this.state.newGreeting = '';
12+
this.state.greetings = this.state.greetings.concat(payload);
13+
this.emitChange();
14+
}
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+
}
20+
else if (action instanceof NewGreetingChanged) {
21+
const {payload} = action;
22+
this.state.newGreeting = payload;
23+
this.emitChange();
24+
}
25+
}
26+
super(dispatcher, onDispatch, () => ({
927
greetings: [],
1028
newGreeting: ''
1129
}));
1230
}
1331

1432
getState() {
15-
return this._state
16-
}
17-
18-
_onDispatch(action) {
19-
switch(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-
}
33+
return this.state
3434
}
3535
}
3636

0 commit comments

Comments
 (0)