@@ -6,12 +6,26 @@ import {
6
6
observable ,
7
7
} from 'mobx'
8
8
export class ObservableTimer {
9
- public secondsPassed : number = - 1
9
+ @observable
10
+ public secondsPassed : number = 0
11
+
12
+ @action
13
+ public increment ( ) {
14
+ this . secondsPassed ++ ;
15
+ }
16
+
17
+ @action
18
+ public resetTimer ( ) {
19
+ this . secondsPassed = 0 ;
20
+ }
21
+
10
22
constructor ( ) {
11
23
makeAutoObservable ( this , {
12
24
secondsPassed : observable ,
13
25
handleSecondsUpdated : action ,
14
- staticSeconds : computed ,
26
+ minutesPassed : computed ,
27
+ increase : action ,
28
+ resetTimer : action ,
15
29
} )
16
30
17
31
let intervalId = setInterval ( ( ) => {
@@ -20,26 +34,17 @@ export class ObservableTimer {
20
34
}
21
35
22
36
// Computed value example
23
- public get staticSeconds ( ) {
24
- return 5 * this . secondsPassed
37
+ public get minutesPassed ( ) {
38
+ return this . secondsPassed / 60 ;
25
39
}
26
40
27
41
increase ( ) {
28
- console . log ( 'increasing ' + this . secondsPassed )
42
+ // console.log('increasing ' + this.secondsPassed)
29
43
this . secondsPassed += 1
30
44
}
31
45
32
- reset ( ) {
33
- this . secondsPassed = 0
34
- }
35
-
36
46
// Change the seconds from outside the class (eg. not the timer)
37
47
public handleSecondsUpdated ( secondsPassed : number ) {
38
- console . log ( 'Handling change' )
39
48
this . secondsPassed = secondsPassed
40
49
}
41
- }
42
-
43
- // const TimerView = observer(({ timer }) => (
44
- // <button onClick={() => timer.reset()}>Seconds passed: {timer.secondsPassed}</button>
45
- // ))
50
+ }
0 commit comments