Skip to content
This repository was archived by the owner on Dec 26, 2022. It is now read-only.

Commit 76ab193

Browse files
committed
Throttle
1 parent 24fd383 commit 76ab193

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

src/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export const Features = {
22
myDay: false,
33
};
4+
5+
export const THROTTLE_SAVE_JSON_MS = 500;

src/helpers/Throttle.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default function throttle(func: (...args: any[]) => void, ms: number) {
2+
let timer: number | undefined;
3+
4+
function wrapper(this: any, ...args: any[]) {
5+
if (timer !== undefined) {
6+
window.clearTimeout(timer);
7+
}
8+
timer = window.setTimeout(() => {
9+
func.apply(this, args);
10+
}, ms);
11+
}
12+
13+
return wrapper;
14+
}

src/modules/tasks/TaskStore.ts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { autorun, makeAutoObservable, observable } from 'mobx';
1+
import { autorun, makeAutoObservable } from 'mobx';
22
import { v4 as uuid } from 'uuid';
33
import TaskService from './TaskService';
44
import TaskModel, { ITimeRangeModel } from './models/TaskModel';
@@ -16,13 +16,19 @@ import {
1616
ETimeRangeEvents,
1717
} from '../../services/gaService/EEvents';
1818
import { DEFAULT_PROJECT_ID } from '../projects/models/ProjectModel';
19+
import throttle from '../../helpers/Throttle';
20+
import { THROTTLE_SAVE_JSON_MS } from '../../config';
1921

2022
export default class TaskStore {
2123
tasks: TasksByProject = {};
2224
activeTask: TaskModel | undefined;
2325
versionHash = uuid();
2426
private tasksService = new TaskService();
2527
private interval: NodeJS.Timeout | undefined;
28+
private saveInStorage = throttle(() => {
29+
this.tasksService.save(this.tasks);
30+
this.updateVersion();
31+
}, THROTTLE_SAVE_JSON_MS);
2632

2733
constructor(private rootStore: RootStore) {
2834
makeAutoObservable(this);
@@ -36,13 +42,12 @@ export default class TaskStore {
3642

3743
set(projectId: string, tasksInProject: TaskModel[]) {
3844
this.tasks[projectId] = tasksInProject;
39-
this.tasksService.save(this.tasks);
45+
this.saveInStorage();
4046
}
4147

4248
setTime(task: TaskModel, timeIndex: number, timeRange: ITimeRangeModel) {
4349
task.time[timeIndex] = timeRange;
44-
this.tasksService.save(this.tasks);
45-
this.updateVersion();
50+
this.saveInStorage();
4651
GaService.event(EEventCategory.TimeRange, ETimeRangeEvents.Update);
4752
}
4853

@@ -51,9 +56,8 @@ export default class TaskStore {
5156
this.stopTimer();
5257
}
5358

54-
task.time.splice(timeIndex, 1);
55-
this.tasksService.save(this.tasks);
56-
this.updateVersion();
59+
task.time.splice(timeIndex, 1); // TODO move to task
60+
this.saveInStorage();
5761
GaService.event(EEventCategory.TimeRange, ETimeRangeEvents.Delete);
5862
}
5963

@@ -94,8 +98,7 @@ export default class TaskStore {
9498
this.tasks[projectId] = [];
9599
}
96100
this.tasks[projectId] = [...this.tasks[projectId], task];
97-
this.updateVersion();
98-
this.tasksService.save(this.tasks);
101+
this.saveInStorage();
99102
GaService.event(EEventCategory.Tasks, ETasksEvents.Create);
100103
}
101104

@@ -130,36 +133,31 @@ export default class TaskStore {
130133
);
131134
}
132135
}
133-
this.tasksService.save(this.tasks);
134-
this.updateVersion();
136+
this.saveInStorage();
135137
GaService.event(EEventCategory.Tasks, ETasksEvents.Delete);
136138
}
137139

138140
removeProjectTasks(projectKey: string) {
139141
delete this.tasks[projectKey];
140-
this.tasksService.save(this.tasks);
141-
this.updateVersion();
142+
this.saveInStorage();
142143
}
143144

144145
startTimer(task: TaskModel) {
145146
this.stopTimer(true);
146147
this.activeTask = task;
147148
task.start();
148149
this.setupReminder(task);
149-
this.tasksService.save(this.tasks);
150-
this.updateVersion();
150+
this.saveInStorage();
151151
}
152152

153153
stopTimer(silent?: boolean) {
154154
if (this.activeTask) {
155155
this.activeTask.stop();
156-
// this.activeTask = undefined;
157156
}
158157

159158
if (!silent) {
160159
this.setupReminder();
161-
this.tasksService.save(this.tasks);
162-
this.updateVersion();
160+
this.saveInStorage();
163161
}
164162
}
165163

@@ -193,7 +191,7 @@ export default class TaskStore {
193191
checkTaskFn
194192
);
195193

196-
this.set(projectId, this.tasks[projectId]);
194+
this.saveInStorage();
197195
}
198196
GaService.event(EEventCategory.Tasks, ETasksEvents.Check);
199197
}
@@ -212,7 +210,7 @@ export default class TaskStore {
212210
markExpanded
213211
);
214212

215-
this.set(projectId, this.tasks[projectId]);
213+
this.saveInStorage();
216214
}
217215
}
218216

0 commit comments

Comments
 (0)