-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ts
More file actions
81 lines (67 loc) · 1.71 KB
/
index.ts
File metadata and controls
81 lines (67 loc) · 1.71 KB
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
import rxloop, { decorators, Action, OperatorsMap } from '../../../';
import devTools from '@rxloop/devtools';
import { map, filter } from "rxjs/operators";
import { Observable } from 'rxjs'
const {reducer, model, pipe}= decorators<number>()
@model(0)
class Counter {
@reducer
increment(state: number) {
return state + 1;
}
@reducer
decrement(state: number) {
return state + 1;
}
@pipe
incrementAsync(action$:Observable<Action>, {call}: OperatorsMap) {
return action$.pipe(
call(async() => {
return 1;
}),
map(()=> {
return { type: "increment" };
})
);
}
}
const a = model(0)(Counter)
const app = rxloop({
plugins: [ devTools() ]
});
app.model(a);
app.model({
name: 'counter2',
state: 0,
reducers:{
increment(state) {
return state + 1;
},
decrement(state) {
return state - 1;
}
}
});
app.start();
var valueEl = document.getElementById('value') as HTMLElement;
app.stream('counter').subscribe((state) => {
valueEl.innerHTML = state;
});
document.getElementById('increment')
.addEventListener('click', function () {
app.dispatch({ type: 'counter/increment' })
})
document.getElementById('decrement')
.addEventListener('click', function () {
app.dispatch({ type: 'counter/decrement' })
})
document.getElementById('incrementIfOdd')
.addEventListener('click', function () {
if (app.getState('counter') % 2 !== 0) {
app.dispatch({ type: 'counter/increment' })
}
})
document.getElementById('incrementAsync')
.addEventListener('click', function () {
app.dispatch({ type: 'counter/incrementAsync' })
})