MOBILE-3565 classes: Fix linting on delegate

main
Pau Ferrer Ocaña 2020-10-09 13:08:15 +02:00
parent 5acc551488
commit 1cefe8fa17
1 changed files with 24 additions and 28 deletions

View File

@ -63,7 +63,7 @@ export class CoreDelegate {
/** /**
* Set of promises to update a handler, to prevent doing the same operation twice. * Set of promises to update a handler, to prevent doing the same operation twice.
*/ */
protected updatePromises: {[siteId: string]: {[name: string]: Promise<any>}} = {}; protected updatePromises: {[siteId: string]: {[name: string]: Promise<void>}} = {};
/** /**
* Whether handlers have been initialized. * Whether handlers have been initialized.
@ -73,7 +73,7 @@ export class CoreDelegate {
/** /**
* Promise to wait for handlers to be initialized. * Promise to wait for handlers to be initialized.
*/ */
protected handlersInitPromise: Promise<any>; protected handlersInitPromise: Promise<void>;
/** /**
* Function to resolve the handlers init promise. * Function to resolve the handlers init promise.
@ -136,7 +136,7 @@ export class CoreDelegate {
* @param params Parameters to pass to the function. * @param params Parameters to pass to the function.
* @return Function returned value or default value. * @return Function returned value or default value.
*/ */
private execute(handler: any, fnName: string, params?: any[]): any { private execute(handler: CoreDelegateHandler, fnName: string, params?: any[]): any {
if (handler && handler[fnName]) { if (handler && handler[fnName]) {
return handler[fnName].apply(handler, params); return handler[fnName].apply(handler, params);
} else if (this.defaultHandler && this.defaultHandler[fnName]) { } else if (this.defaultHandler && this.defaultHandler[fnName]) {
@ -243,7 +243,7 @@ export class CoreDelegate {
protected updateHandler(handler: CoreDelegateHandler, time: number): Promise<void> { protected updateHandler(handler: CoreDelegateHandler, time: number): Promise<void> {
const siteId = CoreSites.instance.getCurrentSiteId(); const siteId = CoreSites.instance.getCurrentSiteId();
const currentSite = CoreSites.instance.getCurrentSite(); const currentSite = CoreSites.instance.getCurrentSite();
let promise; let promise: Promise<boolean>;
if (this.updatePromises[siteId] && this.updatePromises[siteId][handler.name]) { if (this.updatePromises[siteId] && this.updatePromises[siteId][handler.name]) {
// There's already an update ongoing for this handler, return the promise. // There's already an update ongoing for this handler, return the promise.
@ -252,18 +252,14 @@ export class CoreDelegate {
this.updatePromises[siteId] = {}; this.updatePromises[siteId] = {};
} }
if (!CoreSites.instance.isLoggedIn()) { if (!CoreSites.instance.isLoggedIn() || this.isFeatureDisabled(handler, currentSite)) {
promise = Promise.reject(null);
} else if (this.isFeatureDisabled(handler, currentSite)) {
promise = Promise.resolve(false); promise = Promise.resolve(false);
} else { } else {
promise = Promise.resolve(handler.isEnabled()); promise = handler.isEnabled().catch(() => false);
} }
// Checks if the handler is enabled. // Checks if the handler is enabled.
this.updatePromises[siteId][handler.name] = promise.catch(() => { this.updatePromises[siteId][handler.name] = promise.then((enabled: boolean) => {
return false;
}).then((enabled: boolean) => {
// Check that site hasn't changed since the check started. // Check that site hasn't changed since the check started.
if (CoreSites.instance.getCurrentSiteId() === siteId) { if (CoreSites.instance.getCurrentSiteId() === siteId) {
const key = handler[this.handlerNameProperty] || handler.name; const key = handler[this.handlerNameProperty] || handler.name;
@ -298,9 +294,9 @@ export class CoreDelegate {
* *
* @return Resolved when done. * @return Resolved when done.
*/ */
protected updateHandlers(): Promise<void> { protected async updateHandlers(): Promise<void> {
const promises = [], const promises = [];
now = Date.now(); const now = Date.now();
this.logger.debug('Updating handlers for current site.'); this.logger.debug('Updating handlers for current site.');
@ -311,12 +307,11 @@ export class CoreDelegate {
promises.push(this.updateHandler(this.handlers[name], now)); promises.push(this.updateHandler(this.handlers[name], now));
} }
return Promise.all(promises).then(() => { try {
return true; await Promise.all(promises);
}, () => { } catch (e) {
// Never reject. // Never reject
return true; }
}).then(() => {
// Verify that this call is the last one that was started. // Verify that this call is the last one that was started.
if (this.isLastUpdateCall(now)) { if (this.isLastUpdateCall(now)) {
@ -325,7 +320,6 @@ export class CoreDelegate {
this.updateData(); this.updateData();
} }
});
} }
/** /**
@ -335,6 +329,7 @@ export class CoreDelegate {
updateData(): any { updateData(): any {
// To be overridden. // To be overridden.
} }
} }
export interface CoreDelegateHandler { export interface CoreDelegateHandler {
@ -346,7 +341,8 @@ export interface CoreDelegateHandler {
/** /**
* Whether or not the handler is enabled on a site level. * Whether or not the handler is enabled on a site level.
*
* @return Whether or not the handler is enabled on a site level. * @return Whether or not the handler is enabled on a site level.
*/ */
isEnabled(): boolean | Promise<boolean>; isEnabled(): Promise<boolean>;
} }