-
Notifications
You must be signed in to change notification settings - Fork 0
/
design3.js
71 lines (59 loc) · 1.5 KB
/
design3.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
const any = (fn, deps) => effect(sig => {
const main = loop(function* () {
const values = yield all(...deps);
sig(fn(...values));
});
const running = main();
return () => running.cancel();
});
////
let name = 'Harry',
surname = 'Potter',
width = window.innerWidth;
const windowWidth = effect(sig => {
const handleResize = sig(() => width = window.innerWidth);
window.addEventListener('resize', handleResize);
return () => {
window.removeEventListener('resize', handleResize);
};
});
const ask = effect;
// Renders and handles events
const userEvents = ask(ev => render(
<Card>
<Row label="Name">
<Input value={ name } onChange={ ev(v => name = v) } />
</Row>
<Row label="Surname">
<Input value={ surname } onChange={ ev(v => surname = v) } />
</Row>
<Row label="Width">
<Text>{ width }</Text>
</Row>
</Card>
));
const main = loop(function* () {
document.title = `${name} ${surname}`;
yield race(userEvents, windowWidth);
});
main();
////
// Won't work
any(isLogging => {
if (isLogging) {
any(foo => console.log(foo), [foo]);
any(bar => console.log(bar), [bar]);
any(bleck => console.log(bleck), [bleck]);
}
}, [isLogging]);
// Will work
loop(function* () {
// Check current value and setup effects
if (isLogging()) {
any(foo => console.log(foo), [foo]);
any(bar => console.log(bar), [bar]);
any(bleck => console.log(bleck), [bleck]);
}
// Wait for next update and repeat the process
yield isLogging;
})