-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.js
174 lines (150 loc) · 4.28 KB
/
converter.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
import Rx from 'rx';
import h from 'virtual-dom/h';
import hhelpers from 'hyperscript-helpers';
const {input, select, option} = hhelpers(h);
import {container$, sink$, combine$} from './rx-helpers';
import {renderToDom} from './rx-dom';
const h$ = container$;
// Simple flag to hide [+] [-]
const showIncDec = window.location.hash !== '#no-inc-dec';
const rates = {
'EUR': 1.36,
'JPY': 184,
'CHF': 1.47
};
function lookupRate(currency) {
return new Promise((resolve) => {
// Emulate async API call
setTimeout(() => resolve(rates[currency]), 1000);
});
}
function round(decimals) {
const n = Math.pow(10, decimals);
return float => Math.round(float * n) / n;
}
function noop(x) {
return x;
}
function replaceValue(newValue) {
return (oldValue) => newValue;
}
function intents(events) {
const updateAmount$ = Rx.Observable.merge(
events.amountChanged$.map(ev => ev.target.value).map(replaceValue),
events.resetClicked$.map(() => replaceValue(1)),
events.incrementClicked$.map(() => current => current + 1),
events.decrementClicked$.map(() => current => Math.max(0, current - 1))
);
const updateCurrency$ = Rx.Observable.merge(
events.currencyChanged$.map(ev => ev.target.value).map(replaceValue),
events.resetClicked$.map(() => replaceValue('EUR'))
);
return {
updateAmount$,
updateCurrency$
};
}
function model(intents) {
const amount$ = intents.updateAmount$.
startWith(noop).
scan((state, func) => func(state), 1).
shareReplay(1);
const currency$ = intents.updateCurrency$.
startWith(noop).
scan((state, func) => func(state), 'EUR').
distinctUntilChanged().
shareReplay(1);
const rate$ = currency$.
flatMap(currency => Rx.Observable.fromPromise(lookupRate(currency))).
shareReplay(1);
const rateCurrency$ = rate$.
withLatestFrom(currency$, (_, currency) => currency).
shareReplay(1);
const rateLoading$ = Rx.Observable.merge(
currency$.map(true),
rate$.map(false)
);
const converted$ = combine$(amount$, rate$,
(amount, rate) => amount * rate);
return {
amount$,
currency$,
rate$,
rateCurrency$,
rateLoading$,
converted$
};
}
function view() {
const events = {
amountChanged$: new Rx.Subject(),
currencyChanged$: new Rx.Subject(),
decrementClicked$: new Rx.Subject(),
incrementClicked$: new Rx.Subject(),
resetClicked$: new Rx.Subject()
};
function tree$(model) {
return h$('main', [
h$('form', [
h$('label', [
h('span.label', 'Amount'),
model.amount$.map(amount => input({
type: 'text',
value: amount,
oninput: sink$(events.amountChanged$)
})),
' GBP',
showIncDec && h('button.modifier', {
type: 'button',
onclick: sink$(events.incrementClicked$)
}, '+') || '',
showIncDec && h('button.modifier', {
type: 'button',
onclick: sink$(events.decrementClicked$)
}, '-') || ''
]),
h$('label', [
h('span.label', 'Currency'),
model.currency$.map(currency => {
return select({
onchange: sink$(events.currencyChanged$)
}, ['EUR', 'JPY', 'CHF'].map(curr => {
return option({selected: curr == currency}, curr);
}));
}),
' ',
model.rateLoading$.flatMap(loading => {
if (loading) {
return Rx.Observable.return(h('span.rate', 'loading rate…'));
} else {
return h$('span.rate', [
'rate: 1 GBP = ',
model.rate$,
' ',
model.rateCurrency$
]);
}
})
])
]),
h$('div.converted', [
h('span.label', 'Converted'),
model.converted$.map(round(2)),
' ',
model.rateCurrency$
]),
h('button.reset', {
type: 'button',
onclick: sink$(events.resetClicked$)
}, 'Reset')
]);
}
return {
tree$,
events
};
}
const out = document.getElementById('out');
const theView = view();
const theModel = model(intents(theView.events));
renderToDom(theView.tree$(theModel), out);