Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit bb50cd9

Browse files
feat(#773): use stencil/store for timer
1 parent 66457fc commit bb50cd9

File tree

7 files changed

+128
-207
lines changed

7 files changed

+128
-207
lines changed

remote/package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

remote/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"@stencil/core": "^1.15.0",
5757
"@stencil/postcss": "^1.0.1",
5858
"@stencil/sass": "^1.3.2",
59+
"@stencil/store": "^1.3.0",
5960
"@types/socket.io-client": "^1.4.33",
6061
"@types/w3c-generic-sensor": "^1.0.2",
6162
"@types/webrtc": "0.0.26",

remote/src/app/app-root.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ export class AppRoot {
4646
}
4747
}
4848

49-
componentDidUnload() {
50-
this.timerService.destroy();
49+
async componentDidUnload() {
50+
await this.timerService.destroy();
5151

5252
if (this.themeSubscription) {
5353
this.themeSubscription.unsubscribe();

remote/src/app/components/app-stopwatch-time/app-stopwatch-time.tsx

Lines changed: 7 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,24 @@
1-
import {Component, State, h} from '@stencil/core';
1+
import {Component, h} from '@stencil/core';
22

3-
import {BehaviorSubject, Subscription} from 'rxjs';
4-
5-
import {addMilliseconds, formatDistanceStrict} from 'date-fns';
6-
7-
import {TimerInterval, TimerService} from '../../services/timer/timer.service';
3+
import timerStore from '../../stores/timer.store';
84

95
@Component({
106
tag: 'app-stopwatch-time',
11-
styleUrl: 'app-stopwatch-time.scss'
7+
styleUrl: 'app-stopwatch-time.scss',
128
})
139
export class AppStopwatchTime {
14-
private watcherSubscription: Subscription;
15-
private timerSubscription: Subscription;
16-
17-
@State()
18-
private remainingText: string;
19-
20-
@State()
21-
private remainingTime: number = 0;
22-
23-
private timerService: TimerService;
24-
25-
constructor() {
26-
this.timerService = TimerService.getInstance();
27-
}
28-
29-
async componentWillLoad() {
30-
this.watcherSubscription = this.timerService.watch().subscribe((timer: BehaviorSubject<TimerInterval>) => {
31-
if (timer) {
32-
this.timerSubscription = timer.subscribe(
33-
(interval: TimerInterval) => {
34-
if (interval) {
35-
this.remainingTime = interval.timerRemaining;
36-
37-
const end: Date = addMilliseconds(new Date(), this.remainingTime);
38-
this.remainingText = formatDistanceStrict(end, new Date());
39-
} else {
40-
this.remainingTime = 0;
41-
this.remainingText = formatDistanceStrict(new Date(), new Date());
42-
}
43-
},
44-
(_err) => {
45-
// Do nothing
46-
},
47-
async () => {
48-
this.remainingTime = 0;
49-
this.remainingText = formatDistanceStrict(new Date(), new Date());
50-
51-
if (this.timerSubscription) {
52-
this.timerSubscription.unsubscribe();
53-
}
54-
}
55-
);
56-
}
57-
});
58-
}
59-
60-
async componentDidUnload() {
61-
if (this.timerSubscription) {
62-
this.timerSubscription.unsubscribe();
63-
}
64-
65-
if (this.watcherSubscription) {
66-
this.watcherSubscription.unsubscribe();
67-
}
68-
}
69-
7010
render() {
7111
let color: string = 'primary';
72-
if (this.remainingTime >= 30000 && this.remainingTime < 60000) {
12+
if (timerStore.state.remainingTime >= 30000 && timerStore.state.remainingTime < 60000) {
7313
color = 'secondary';
74-
} else if (this.remainingTime < 30000 && this.remainingTime >= 0) {
14+
} else if (timerStore.state.remainingTime < 30000 && timerStore.state.remainingTime >= 0) {
7515
color = 'tertiary';
7616
}
7717

78-
if (this.remainingText) {
18+
if (timerStore.state.remainingText) {
7919
return (
8020
<ion-chip color="danger" style={{background: `var(--ion-color-${color})`, color: 'var(--ion-color-light)'}}>
81-
<ion-label>{this.remainingText}</ion-label>
21+
<ion-label>{timerStore.state.remainingText}</ion-label>
8222
</ion-chip>
8323
);
8424
} else {

remote/src/app/pages/app-timer/app-timer.tsx

Lines changed: 15 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,22 @@
1-
import {Component, Element, State, h} from '@stencil/core';
1+
import {Component, Element, State, h, Listen} from '@stencil/core';
22
import {DatetimeChangeEventDetail} from '@ionic/core';
33

44
import {differenceInMilliseconds, isAfter, startOfDay} from 'date-fns';
55

6-
import {BehaviorSubject, Subscription} from 'rxjs';
6+
import timerStore from '../../stores/timer.store';
77

88
import {Comparator} from '../../services/utils/utils';
99

10-
import {TimerInterval, TimerService} from '../../services/timer/timer.service';
1110
import {NotificationService} from '../../services/notification/notification.service';
11+
import {TimerService} from '../../services/timer/timer.service';
1212

1313
@Component({
1414
tag: 'app-timer',
15-
styleUrl: 'app-timer.scss'
15+
styleUrl: 'app-timer.scss',
1616
})
1717
export class AppTimer {
1818
@Element() el: HTMLElement;
1919

20-
private watcherSubscription: Subscription;
21-
private timerSubscription: Subscription;
22-
23-
@State()
24-
private timerRemaining: number;
25-
26-
@State()
27-
private timerLength: number;
28-
2920
@State()
3021
private timerRunning: boolean;
3122

@@ -35,6 +26,8 @@ export class AppTimer {
3526
private timerService: TimerService;
3627
private notificationService: NotificationService;
3728

29+
private destroyListener;
30+
3831
constructor() {
3932
this.timerService = TimerService.getInstance();
4033
this.notificationService = NotificationService.getInstance();
@@ -43,8 +36,6 @@ export class AppTimer {
4336
async componentDidLoad() {
4437
this.notificationService.init();
4538

46-
this.watchTimer();
47-
4839
this.timerRunning = await this.timerService.isTimerStarted();
4940

5041
if (this.timerRunning) {
@@ -53,36 +44,11 @@ export class AppTimer {
5344
}
5445

5546
async componentDidUnload() {
56-
if (this.timerSubscription) {
57-
this.timerSubscription.unsubscribe();
58-
}
59-
60-
if (this.watcherSubscription) {
61-
this.watcherSubscription.unsubscribe();
47+
if (this.destroyListener) {
48+
this.destroyListener();
6249
}
6350
}
6451

65-
private watchTimer() {
66-
this.watcherSubscription = this.timerService.watch().subscribe((timer: BehaviorSubject<TimerInterval>) => {
67-
if (timer) {
68-
this.timerSubscription = timer.subscribe(
69-
(interval: TimerInterval) => {
70-
if (interval) {
71-
this.timerRemaining = interval.timerRemaining;
72-
this.timerLength = interval.timerLength;
73-
}
74-
},
75-
(_err) => {
76-
// Do nothing
77-
},
78-
async () => {
79-
await this.clearStopwatch();
80-
}
81-
);
82-
}
83-
});
84-
}
85-
8652
async startTimer(length: number) {
8753
await this.timerService.start(length);
8854
this.timerRunning = true;
@@ -99,18 +65,10 @@ export class AppTimer {
9965
this.timerPause = pause;
10066
}
10167

102-
private async clearStopwatch() {
103-
if (this.timerSubscription) {
104-
this.timerSubscription.unsubscribe();
105-
}
106-
107-
// Reset timerRemaining
108-
this.timerRemaining = 0;
109-
this.timerLength = 0;
68+
@Listen('stopTimer', {target: 'document'})
69+
async clearStopwatch() {
11070
this.timerRunning = false;
11171

112-
await this.timerService.clearEndAt();
113-
11472
await this.resetDatetime();
11573
}
11674

@@ -245,15 +203,15 @@ export class AppTimer {
245203
value={startOfDay(new Date()).toDateString()}
246204
onIonCancel={() => this.toggleFabActivated()}
247205
onIonChange={(e: CustomEvent<DatetimeChangeEventDetail>) => this.initTimerLengthAndStartTimer(e)}></ion-datetime>
248-
</ion-content>
206+
</ion-content>,
249207
];
250208
}
251209

252210
private renderContent() {
253-
if (this.timerRemaining >= 0) {
211+
if (timerStore.state.remainingTime >= 0 && timerStore.state.timer) {
254212
return (
255213
<div class="content">
256-
<app-stopwatch length={this.timerLength} remaining={this.timerRemaining}></app-stopwatch>
214+
<app-stopwatch length={timerStore.state.timer.timerLength} remaining={timerStore.state.remainingTime}></app-stopwatch>
257215
</div>
258216
);
259217
} else {
@@ -274,11 +232,11 @@ export class AppTimer {
274232
}
275233

276234
private renderActions() {
277-
if (this.timerRunning === null || (this.timerRunning && this.timerRemaining === null)) {
235+
if (this.timerRunning === null || (this.timerRunning && timerStore.state.remainingTime === null)) {
278236
return undefined;
279237
}
280238

281-
const style = {visibility: `${this.timerRemaining === null || this.timerRemaining === undefined ? 'hidden' : 'inherit'}`};
239+
const style = {visibility: `${timerStore.state.remainingTime === null || timerStore.state.remainingTime === undefined ? 'hidden' : 'inherit'}`};
282240

283241
return (
284242
<ion-fab vertical="bottom" horizontal="end" slot="fixed" onClick={(e: UIEvent) => e.stopPropagation()} style={style}>

0 commit comments

Comments
 (0)