/
index.js
213 lines (191 loc) · 4.5 KB
/
index.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const React = require('react')
const ReactDOM = require('react-dom')
const { interval, from } = require('rxjs')
const redux = require('redux')
const msleep = require('async-msleep')
const { Subscribe, subscribable, devtool } = require('../src')
//
// city-state
//
@subscribable
class Counter {
constructor () {
this.count = 0
}
increment () {
this.count += 1
}
decrement () {
this.count -= 1
}
}
const counter = new Counter()
try {
devtool(counter, { name: 'Counter' })
} catch (_) {}
function CounterView () {
return (
<Subscribe to={[counter]}>
{() => {
return (
<div>
<h1>Counter</h1>
<p>state: {counter.count}</p>
<button onClick={() => counter.increment()}>Increment +</button><br />
<button onClick={() => counter.decrement()}>Decrement -</button><br />
</div>
)
}}
</Subscribe>
)
}
//
// city-state async
//
@subscribable
class CounterAsync {
constructor () {
this.count = 0
this.isComputing = false
}
async increment () {
this.isComputing = true
await msleep(1000)
this.count += 1
this.isComputing = false
}
async decrement () {
this.isComputing = true
await msleep(1000)
this.count -= 1
this.isComputing = false
}
}
const counterAsync = new CounterAsync()
try {
devtool(counterAsync, { name: 'CounterAsync' })
} catch (_) {}
function CounterAsyncView () {
return (
<Subscribe to={[counterAsync]}>
{(counterAsync) => {
return (
<div>
<h1>Counter Async</h1>
<p>state: {counterAsync.isComputing ? '...' : counterAsync.count}</p>
<button disabled={counterAsync.isComputing} onClick={() => counterAsync.increment()}>Increment +</button><br />
<button disabled={counterAsync.isComputing} onClick={() => counterAsync.decrement()}>Decrement -</button><br />
</div>
)
}}
</Subscribe>
)
}
//
// redux
//
function counterReducer (state = { count: 0 }, action) {
switch (action.type) {
case 'INCREMENT':
return {
count: state.count + 1
}
case 'DECREMENT':
return {
count: state.count - 1
}
default:
return state
}
}
const counterStore = redux.createStore(counterReducer)
function CounterReduxView () {
return (
<Subscribe to={[counterStore]}>
{(counterState = {}) => {
return (
<div>
<h1>Counter with redux</h1>
<p>state: {counterState.count}</p>
<button onClick={() => counterStore.dispatch({ type: 'INCREMENT' })}>Increment +</button><br />
<button onClick={() => counterStore.dispatch({ type: 'DECREMENT' })}>Decrement -</button><br />
</div>
)
}}
</Subscribe>
)
}
//
// Native Observable
//
let ObservableCounterView = null
// RxJS is throwing an error on Safari
try {
const counter2 = new Counter()
const observableCounter = from(counter2)
ObservableCounterView = () => {
return (
<Subscribe to={[observableCounter]}>
{(counterState = {}) => {
return (
<div>
<h1>Counter Observable</h1>
<p>state: {counterState.count}</p>
<button onClick={() => counter2.increment()}>Increment +</button><br />
<button onClick={() => counter2.decrement()}>Decrement -</button><br />
</div>
)
}}
</Subscribe>
)
}
} catch (error) {
ObservableCounterView = () => null
}
//
// rxjs
//
class TimerView extends React.Component {
constructor () {
super()
this.interval1 = interval(2000)
this.interval2 = interval(1000)
try {
devtool(this.interval1, { name: 'interval1' })
devtool(this.interval2, { name: 'interval2' })
} catch (_) {}
}
// RxJS is throwing an error on Safari
componentDidCatch () {
return null
}
render () {
return (
<Subscribe to={[this.interval1, this.interval2]}>
{(time1, time2) => {
return (
<div>
<h1>rxjs</h1>
<p>time1: {time1 || 0}</p>
<p>time2: {time2 || 0}</p>
</div>
)
}}
</Subscribe>
)
}
}
function Main () {
return (
<div>
<CounterView />
<CounterAsyncView />
<CounterReduxView />
<ObservableCounterView />
<TimerView />
</div>
)
}
const $container = document.createElement('div')
document.body.appendChild($container)
ReactDOM.render(<Main />, $container)