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:
parent
7c60e32e64
commit
4f7edf8371
50
ReactNativeClient/lib/poor-man-intervals.js
Normal file
50
ReactNativeClient/lib/poor-man-intervals.js
Normal 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 }
|
@ -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');
|
||||
|
Loading…
Reference in New Issue
Block a user