mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-21 14:09:34 +01:00
39 lines
854 B
JavaScript
39 lines
854 B
JavaScript
|
|
import { checkIsUserPremium, CHECK_IDS, EVENTS } from "../../common";
|
||
|
|
|
||
|
|
export class AutoSync {
|
||
|
|
/**
|
||
|
|
*
|
||
|
|
* @param {import("../index").default} db
|
||
|
|
* @param {number} interval
|
||
|
|
*/
|
||
|
|
constructor(db, interval) {
|
||
|
|
this.db = db;
|
||
|
|
this.interval = interval;
|
||
|
|
this.timeout = null;
|
||
|
|
}
|
||
|
|
|
||
|
|
async start() {
|
||
|
|
if (!(await checkIsUserPremium(CHECK_IDS.databaseSync))) return;
|
||
|
|
|
||
|
|
this.databaseUpdatedEvent = this.db.eventManager.subscribeSingle(
|
||
|
|
EVENTS.databaseUpdated,
|
||
|
|
this.schedule.bind(this)
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
stop() {
|
||
|
|
clearTimeout(this.timeout);
|
||
|
|
if (this.databaseUpdatedEvent) this.databaseUpdatedEvent.unsubscribe();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @private
|
||
|
|
*/
|
||
|
|
schedule() {
|
||
|
|
this.stop();
|
||
|
|
this.timeout = setTimeout(() => {
|
||
|
|
this.db.eventManager.publish(EVENTS.databaseSyncRequested, false, false);
|
||
|
|
}, this.interval);
|
||
|
|
}
|
||
|
|
}
|