-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
134 lines (118 loc) · 3.21 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { Model, Observable, Store, createContainer } from "gasoline";
import './index.css';
const counter = new Model({
state: {
counter: 0,
running: true,
interval: 1000
},
actions: {
toggle(state) {
return { ...state, running: !state.running }
},
increment(state) {
return { ...state, counter: state.counter + 1 }
},
decrement(state) {
return { ...state, counter: state.counter - 1 }
},
reset(state) {
return { ...state, counter: 0 }
},
changeInterval(state, interval) {
return { ...state, interval, counter: 0 }
}
},
process(action$, counter) {
const autoIncrement$ = action$.ofType(
Store.START,
counter.actionTypes.toggle,
counter.actionTypes.changeInterval,
counter.actionTypes.reset
).switchMap(() => {
const state = counter.state
if (state.running) {
return Observable
.interval(state.interval)
.map(counter.actionCreators.increment)
}
return Observable.empty()
})
return autoIncrement$
}
})
const store = new Store(counter);
type CounterProps = {
running: boolean,
counter: number,
interval: number,
onIntervalChange: React.ChangeEventHandler<HTMLSelectElement>,
onResetClick: React.MouseEventHandler<HTMLButtonElement>,
onToggleClick: React.MouseEventHandler<HTMLButtonElement>,
onIncrementClick: React.MouseEventHandler<HTMLButtonElement>,
onDecrementClick: React.MouseEventHandler<HTMLButtonElement>
}
function Counter(props: CounterProps) {
return (
<div>
<div>
Counter: <strong>{props.counter}</strong>
</div>
<br />
<div>
<button disabled={props.running} onClick={props.onIncrementClick}>
INC
</button>
<button disabled={props.running} onClick={props.onDecrementClick}>
DEC
</button>
<button onClick={props.onResetClick}>
Reset
</button>
<button onClick={props.onToggleClick}>
{props.running ? "Stop" : "Start"}
</button>
</div>
<br />
<div>
<label>Interval:</label>
<select value={props.interval} onChange={props.onIntervalChange}>
<option value={10}>10ms</option>
<option value={100}>100ms</option>
<option value={250}>250ms</option>
<option value={1000}>1s</option>
<option value={2000}>2s</option>
</select>
</div>
</div>
)
}
const CounterContainer = createContainer(() => {
const { actions, state$ } = counter
function onIntervalChange(e: React.ChangeEvent<HTMLSelectElement>) {
actions.changeInterval(Number(e.target.value))
}
return state$.map(state =>
<Counter
running={state.running}
interval={state.interval}
counter={state.counter}
onDecrementClick={actions.decrement}
onIncrementClick={actions.increment}
onToggleClick={actions.toggle}
onResetClick={actions.reset}
onIntervalChange={onIntervalChange}
/>
)
})
store.ready(() => {
ReactDOM.render(
<CounterContainer />,
document.querySelector("#app")
)
})
store.start()