2018-01-24 14:06:29 +00:00
|
|
|
// (C) Copyright 2015 Martin Dougiamas
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-03-01 15:55:49 +00:00
|
|
|
import { CoreSitesProvider } from '@providers/sites';
|
|
|
|
import { CoreSyncProvider } from '@providers/sync';
|
|
|
|
import { CoreLoggerProvider } from '@providers/logger';
|
|
|
|
import { CoreAppProvider } from '@providers/app';
|
2018-01-24 14:06:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class to create sync providers. It provides some common functions.
|
|
|
|
*/
|
|
|
|
export class CoreSyncBaseProvider {
|
2018-02-14 16:19:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Logger instance get from CoreLoggerProvider.
|
|
|
|
* @type {any}
|
|
|
|
*/
|
|
|
|
protected logger;
|
|
|
|
|
2018-01-24 14:06:29 +00:00
|
|
|
/**
|
|
|
|
* Component of the sync provider.
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
component = 'core';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sync provider's interval.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
syncInterval = 300000;
|
|
|
|
|
|
|
|
// Store sync promises.
|
2018-01-29 09:05:20 +00:00
|
|
|
protected syncPromises: { [siteId: string]: { [uniqueId: string]: Promise<any> } } = {};
|
2018-01-24 14:06:29 +00:00
|
|
|
|
2018-02-14 16:19:09 +00:00
|
|
|
constructor(component: string, protected sitesProvider: CoreSitesProvider, protected loggerProvider: CoreLoggerProvider,
|
|
|
|
protected appProvider: CoreAppProvider) {
|
|
|
|
this.logger = this.loggerProvider.getInstance(component);
|
|
|
|
this.component = component;
|
|
|
|
}
|
2018-01-24 14:06:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an ongoing sync to the syncPromises list. On finish the promise will be removed.
|
|
|
|
*
|
|
|
|
* @param {number} id Unique sync identifier per component.
|
|
|
|
* @param {Promise<any>} promise The promise of the sync to add.
|
|
|
|
* @param {string} [siteId] Site ID. If not defined, current site.
|
|
|
|
* @return {Promise<any>} The sync promise.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
addOngoingSync(id: number, promise: Promise<any>, siteId?: string): Promise<any> {
|
2018-01-24 14:06:29 +00:00
|
|
|
siteId = siteId || this.sitesProvider.getCurrentSiteId();
|
|
|
|
|
|
|
|
const uniqueId = this.getUniqueSyncId(id);
|
|
|
|
if (!this.syncPromises[siteId]) {
|
|
|
|
this.syncPromises[siteId] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
this.syncPromises[siteId][uniqueId] = promise;
|
|
|
|
|
|
|
|
// Promise will be deleted when finish.
|
|
|
|
return promise.finally(() => {
|
|
|
|
delete this.syncPromises[siteId][uniqueId];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If there's an ongoing sync for a certain identifier return it.
|
|
|
|
*
|
|
|
|
* @param {number} id Unique sync identifier per component.
|
|
|
|
* @param {string} [siteId] Site ID. If not defined, current site.
|
|
|
|
* @return {Promise<any>} Promise of the current sync or undefined if there isn't any.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
getOngoingSync(id: number, siteId?: string): Promise<any> {
|
2018-01-24 14:06:29 +00:00
|
|
|
siteId = siteId || this.sitesProvider.getCurrentSiteId();
|
|
|
|
|
|
|
|
if (this.isSyncing(id, siteId)) {
|
|
|
|
// There's already a sync ongoing for this discussion, return the promise.
|
|
|
|
const uniqueId = this.getUniqueSyncId(id);
|
2018-01-29 09:05:20 +00:00
|
|
|
|
2018-01-24 14:06:29 +00:00
|
|
|
return this.syncPromises[siteId][uniqueId];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the synchronization time. Returns 0 if no time stored.
|
|
|
|
*
|
|
|
|
* @param {number} id Unique sync identifier per component.
|
|
|
|
* @param {string} [siteId] Site ID. If not defined, current site.
|
|
|
|
* @return {Promise<number>} Promise resolved with the time.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
getSyncTime(id: number, siteId?: string): Promise<number> {
|
2018-01-24 14:06:29 +00:00
|
|
|
return this.sitesProvider.getSiteDb(siteId).then((db) => {
|
2018-01-29 09:05:20 +00:00
|
|
|
return db.getRecord(CoreSyncProvider.SYNC_TABLE, { component: this.component, id: id }).then((entry) => {
|
2018-01-24 14:06:29 +00:00
|
|
|
return entry.time;
|
|
|
|
}).catch(() => {
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the synchronization warnings of an instance.
|
|
|
|
*
|
|
|
|
* @param {number} id Unique sync identifier per component.
|
|
|
|
* @param {string} [siteId] Site ID. If not defined, current site.
|
|
|
|
* @return {Promise<string[]>} Promise resolved with the warnings.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
getSyncWarnings(id: number, siteId?: string): Promise<string[]> {
|
2018-01-24 14:06:29 +00:00
|
|
|
return this.sitesProvider.getSiteDb(siteId).then((db) => {
|
2018-01-29 09:05:20 +00:00
|
|
|
return db.getRecord(CoreSyncProvider.SYNC_TABLE, { component: this.component, id: id }).then((entry) => {
|
2018-01-24 14:06:29 +00:00
|
|
|
try {
|
|
|
|
return JSON.parse(entry.warnings);
|
2018-01-29 09:05:20 +00:00
|
|
|
} catch (ex) {
|
2018-01-24 14:06:29 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}).catch(() => {
|
|
|
|
return [];
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a unique identifier from component and id.
|
|
|
|
*
|
|
|
|
* @param {number} id Unique sync identifier per component.
|
|
|
|
* @return {string} Unique identifier from component and id.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
protected getUniqueSyncId(id: number): string {
|
2018-01-24 14:06:29 +00:00
|
|
|
return this.component + '#' + id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a there's an ongoing syncronization for the given id.
|
|
|
|
*
|
|
|
|
* @param {number} id Unique sync identifier per component.
|
|
|
|
* @param {string} [siteId] Site ID. If not defined, current site.
|
|
|
|
* @return {boolean} Whether it's synchronizing.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
isSyncing(id: number, siteId?: string): boolean {
|
2018-01-24 14:06:29 +00:00
|
|
|
siteId = siteId || this.sitesProvider.getCurrentSiteId();
|
|
|
|
|
|
|
|
const uniqueId = this.getUniqueSyncId(id);
|
2018-01-29 09:05:20 +00:00
|
|
|
|
2018-01-24 14:06:29 +00:00
|
|
|
return !!(this.syncPromises[siteId] && this.syncPromises[siteId][uniqueId]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a sync is needed: if a certain time has passed since the last time.
|
|
|
|
*
|
|
|
|
* @param {number} id Unique sync identifier per component.
|
|
|
|
* @param {string} [siteId] Site ID. If not defined, current site.
|
|
|
|
* @return {Promise<boolean>} Promise resolved with boolean: whether sync is needed.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
isSyncNeeded(id: number, siteId?: string): Promise<boolean> {
|
2018-01-24 14:06:29 +00:00
|
|
|
return this.getSyncTime(id, siteId).then((time) => {
|
|
|
|
return Date.now() - this.syncInterval >= time;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the synchronization time.
|
|
|
|
*
|
|
|
|
* @param {number} id Unique sync identifier per component.
|
|
|
|
* @param {string} [siteId] Site ID. If not defined, current site.
|
|
|
|
* @param {number} [time] Time to set. If not defined, current time.
|
|
|
|
* @return {Promise<any>} Promise resolved when the time is set.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
setSyncTime(id: number, siteId?: string, time?: number): Promise<any> {
|
2018-01-24 14:06:29 +00:00
|
|
|
return this.sitesProvider.getSiteDb(siteId).then((db) => {
|
|
|
|
time = typeof time != 'undefined' ? time : Date.now();
|
2018-01-29 09:05:20 +00:00
|
|
|
|
|
|
|
return db.insertOrUpdateRecord(CoreSyncProvider.SYNC_TABLE, { time: time }, { component: this.component, id: id });
|
2018-01-24 14:06:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the synchronization warnings.
|
|
|
|
*
|
|
|
|
* @param {number} id Unique sync identifier per component.
|
|
|
|
* @param {string[]} warnings Warnings to set.
|
|
|
|
* @param {string} [siteId] Site ID. If not defined, current site.
|
|
|
|
* @return {Promise<any>} Promise resolved when done.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
setSyncWarnings(id: number, warnings: string[], siteId?: string): Promise<any> {
|
2018-01-24 14:06:29 +00:00
|
|
|
return this.sitesProvider.getSiteDb(siteId).then((db) => {
|
|
|
|
warnings = warnings || [];
|
2018-01-29 09:05:20 +00:00
|
|
|
|
|
|
|
return db.insertOrUpdateRecord(CoreSyncProvider.SYNC_TABLE, { warnings: JSON.stringify(warnings) },
|
|
|
|
{ component: this.component, id: id });
|
2018-01-24 14:06:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-14 16:19:09 +00:00
|
|
|
/**
|
|
|
|
* Execute a sync function on selected sites.
|
|
|
|
*
|
|
|
|
* @param {string} syncFunctionLog Log message to explain the sync function purpose.
|
|
|
|
* @param {string} syncFunction Sync function to execute.
|
|
|
|
* @param {any} [params] Object that defines the params that admit the funcion.
|
|
|
|
* @param {string} [siteId] Site ID to sync. If not defined, sync all sites.
|
|
|
|
* @return {Promise<any>} Resolved with siteIds selected. Rejected if offline.
|
|
|
|
*/
|
|
|
|
syncOnSites(syncFunctionLog: string, syncFunction: string, params?: any, siteId?: string): Promise<any> {
|
|
|
|
if (!this.appProvider.isOnline()) {
|
|
|
|
this.logger.debug(`Cannot sync '${syncFunctionLog}' because device is offline.`);
|
|
|
|
|
|
|
|
return Promise.reject(null);
|
|
|
|
}
|
|
|
|
|
2018-02-22 11:12:48 +00:00
|
|
|
if (!this[syncFunction]) {
|
2018-02-14 16:19:09 +00:00
|
|
|
this.logger.debug(`Cannot sync '${syncFunctionLog}' function '${syncFunction}' does not exist.`);
|
|
|
|
|
|
|
|
return Promise.reject(null);
|
|
|
|
}
|
|
|
|
params = params || {};
|
|
|
|
|
|
|
|
let promise;
|
|
|
|
if (!siteId) {
|
|
|
|
// No site ID defined, sync all sites.
|
|
|
|
this.logger.debug(`Try to sync '${syncFunctionLog}' in all sites.`);
|
|
|
|
promise = this.sitesProvider.getSitesIds();
|
|
|
|
} else {
|
|
|
|
this.logger.debug(`Try to sync '${syncFunctionLog}' in site '${siteId}'.`);
|
|
|
|
promise = Promise.resolve([siteId]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return promise.then((siteIds) => {
|
|
|
|
const sitePromises = [];
|
|
|
|
siteIds.forEach((siteId) => {
|
|
|
|
params['siteId'] = siteId;
|
|
|
|
// Execute function for every site selected.
|
2018-02-22 11:12:48 +00:00
|
|
|
sitePromises.push(this[syncFunction].apply(this, params));
|
2018-02-14 16:19:09 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.all(sitePromises);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-24 14:06:29 +00:00
|
|
|
/**
|
|
|
|
* If there's an ongoing sync for a certain identifier, wait for it to end.
|
|
|
|
* If there's no sync ongoing the promise will be resolved right away.
|
|
|
|
*
|
|
|
|
* @param {number} id Unique sync identifier per component.
|
|
|
|
* @param {string} [siteId] Site ID. If not defined, current site.
|
|
|
|
* @return {Promise<any>} Promise resolved when there's no sync going on for the identifier.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
waitForSync(id: number, siteId?: string): Promise<any> {
|
2018-01-24 14:06:29 +00:00
|
|
|
const promise = this.getOngoingSync(id, siteId);
|
|
|
|
if (promise) {
|
2018-01-29 09:05:20 +00:00
|
|
|
return promise.catch(() => {
|
|
|
|
// Ignore errors.
|
|
|
|
});
|
2018-01-24 14:06:29 +00:00
|
|
|
}
|
2018-01-29 09:05:20 +00:00
|
|
|
|
2018-01-24 14:06:29 +00:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
}
|