-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdater.ts
More file actions
40 lines (35 loc) · 1.66 KB
/
updater.ts
File metadata and controls
40 lines (35 loc) · 1.66 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
import {Job, RecurrenceRule, RecurrenceSpecDateRange, RecurrenceSpecObjLit, scheduleJob} from "node-schedule"
import ICache from "./ICache";
export default class Updater<T, U> {
public readonly jobName: string;
public readonly updateJob: Job;
public onChange?: (newValue: T, oldValue: T) => Promise<void>;
private upperUpdater!: Updater<U, any>;
private lowerUpdaters: Array<Updater<any, T>> = [];
constructor(
public updateFunction: (parameter: U) => Promise<T>,
public readonly cache: ICache<T>,
public readonly name: string,
public readonly jobRule: string | number | RecurrenceRule | RecurrenceSpecDateRange | RecurrenceSpecObjLit | Date,
requiredUpdater?: Updater<U, any>
) {
this.jobName = `${name}_updater_job`;
this.updateJob = scheduleJob(this.jobName, this.jobRule, () => this.update());
if (requiredUpdater) this.setRequiredUpdater(requiredUpdater);
}
public setRequiredUpdater(requiredUpdater: Updater<U, any>) {
this.upperUpdater = requiredUpdater;
requiredUpdater.lowerUpdaters.push(this);
return this;
}
public async update(): Promise<void> {
const upperValue = await this.upperUpdater?.cache.getValue();
const lastValue = await this.cache.getValue();
const newValue = await this.updateFunction(upperValue);
if (await this.cache.updateValue(newValue)) {
if (this.onChange) await this.onChange(newValue, lastValue);
return Promise.all(this.lowerUpdaters.map(lowerUpdater => lowerUpdater.update())).then(() => void 0);
}
else return new Promise<void>(resolve => resolve());
}
}