1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-11 18:24:43 +02:00

Background sync support

This commit is contained in:
Laurent Cozic 2017-07-22 18:35:39 +01:00
parent 7c60e32e64
commit 4f7edf8371
2 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1,50 @@
import { time } from 'lib/time-utils.js';
class PoorManIntervals {
static setInterval(callback, interval) {
PoorManIntervals.intervalId_++;
PoorManIntervals.intervals_.push({
id: PoorManIntervals.intervalId_,
callback: callback,
interval: interval,
lastIntervalTime: time.unixMs(),
});
return PoorManIntervals.intervalId_;
}
static intervalById(id) {
for (let i = 0; i < PoorManIntervals.intervals_.length; i++) {
if (PoorManIntervals.intervals_[i].id == id) return PoorManIntervals.intervals_[id];
}
return null;
}
static clearInterval(id) {
for (let i = 0; i < PoorManIntervals.intervals_.length; i++) {
if (PoorManIntervals.intervals_[i].id == id) {
PoorManIntervals.intervals_.splice(i, 1);
break;
}
}
}
static update() {
for (let i = 0; i < PoorManIntervals.intervals_.length; i++) {
let interval = PoorManIntervals.intervals_[i];
const now = time.unixMs();
if (now - interval.lastIntervalTime >= interval.interval) {
interval.lastIntervalTime = now;
interval.callback();
}
}
}
}
PoorManIntervals.intervalId_ = 0;
PoorManIntervals.intervals_ = [];
export { PoorManIntervals }

View File

@ -34,6 +34,7 @@ import { DatabaseDriverReactNative } from 'lib/database-driver-react-native';
import { reg } from 'lib/registry.js';
import { _, setLocale } from 'lib/locale.js';
import RNFetchBlob from 'react-native-fetch-blob';
import { PoorManIntervals } from 'lib/poor-man-intervals.js';
let defaultState = {
notes: [],
@ -303,6 +304,10 @@ const reducer = (state = defaultState, action) => {
throw error;
}
// Check the registered intervals here since we know this function
// will be called regularly.
PoorManIntervals.update();
return newState;
}
@ -410,6 +415,11 @@ async function initialize(dispatch, backButtonHandler) {
return backButtonHandler();
});
PoorManIntervals.setInterval(() => {
reg.logger().info('Running background sync on timer...');
reg.scheduleSync(1);
}, 1000 * 60 * 5);
initializationState_ = 'done';
reg.logger().info('Application initialized');