MOBILE-3565 core: Fix some ESLint of CoreEventsProvider

main
Dani Palou 2020-10-14 08:30:07 +02:00
parent 46323cdc80
commit 529c35780c
1 changed files with 52 additions and 59 deletions

View File

@ -33,46 +33,47 @@ export interface CoreEventObserver {
*/ */
@Injectable() @Injectable()
export class CoreEventsProvider { export class CoreEventsProvider {
static SESSION_EXPIRED = 'session_expired';
static PASSWORD_CHANGE_FORCED = 'password_change_forced'; static readonly SESSION_EXPIRED = 'session_expired';
static USER_NOT_FULLY_SETUP = 'user_not_fully_setup'; static readonly PASSWORD_CHANGE_FORCED = 'password_change_forced';
static SITE_POLICY_NOT_AGREED = 'site_policy_not_agreed'; static readonly USER_NOT_FULLY_SETUP = 'user_not_fully_setup';
static LOGIN = 'login'; static readonly SITE_POLICY_NOT_AGREED = 'site_policy_not_agreed';
static LOGOUT = 'logout'; static readonly LOGIN = 'login';
static LANGUAGE_CHANGED = 'language_changed'; static readonly LOGOUT = 'logout';
static NOTIFICATION_SOUND_CHANGED = 'notification_sound_changed'; static readonly LANGUAGE_CHANGED = 'language_changed';
static SITE_ADDED = 'site_added'; static readonly NOTIFICATION_SOUND_CHANGED = 'notification_sound_changed';
static SITE_UPDATED = 'site_updated'; static readonly SITE_ADDED = 'site_added';
static SITE_DELETED = 'site_deleted'; static readonly SITE_UPDATED = 'site_updated';
static COMPLETION_MODULE_VIEWED = 'completion_module_viewed'; static readonly SITE_DELETED = 'site_deleted';
static USER_DELETED = 'user_deleted'; static readonly COMPLETION_MODULE_VIEWED = 'completion_module_viewed';
static PACKAGE_STATUS_CHANGED = 'package_status_changed'; static readonly USER_DELETED = 'user_deleted';
static COURSE_STATUS_CHANGED = 'course_status_changed'; static readonly PACKAGE_STATUS_CHANGED = 'package_status_changed';
static SECTION_STATUS_CHANGED = 'section_status_changed'; static readonly COURSE_STATUS_CHANGED = 'course_status_changed';
static COMPONENT_FILE_ACTION = 'component_file_action'; static readonly SECTION_STATUS_CHANGED = 'section_status_changed';
static SITE_PLUGINS_LOADED = 'site_plugins_loaded'; static readonly COMPONENT_FILE_ACTION = 'component_file_action';
static SITE_PLUGINS_COURSE_RESTRICT_UPDATED = 'site_plugins_course_restrict_updated'; static readonly SITE_PLUGINS_LOADED = 'site_plugins_loaded';
static LOGIN_SITE_CHECKED = 'login_site_checked'; static readonly SITE_PLUGINS_COURSE_RESTRICT_UPDATED = 'site_plugins_course_restrict_updated';
static LOGIN_SITE_UNCHECKED = 'login_site_unchecked'; static readonly LOGIN_SITE_CHECKED = 'login_site_checked';
static IAB_LOAD_START = 'inappbrowser_load_start'; static readonly LOGIN_SITE_UNCHECKED = 'login_site_unchecked';
static IAB_EXIT = 'inappbrowser_exit'; static readonly IAB_LOAD_START = 'inappbrowser_load_start';
static APP_LAUNCHED_URL = 'app_launched_url'; // App opened with a certain URL (custom URL scheme). static readonly IAB_EXIT = 'inappbrowser_exit';
static FILE_SHARED = 'file_shared'; static readonly APP_LAUNCHED_URL = 'app_launched_url'; // App opened with a certain URL (custom URL scheme).
static KEYBOARD_CHANGE = 'keyboard_change'; static readonly FILE_SHARED = 'file_shared';
static CORE_LOADING_CHANGED = 'core_loading_changed'; static readonly KEYBOARD_CHANGE = 'keyboard_change';
static ORIENTATION_CHANGE = 'orientation_change'; static readonly CORE_LOADING_CHANGED = 'core_loading_changed';
static LOAD_PAGE_MAIN_MENU = 'load_page_main_menu'; static readonly ORIENTATION_CHANGE = 'orientation_change';
static SEND_ON_ENTER_CHANGED = 'send_on_enter_changed'; static readonly LOAD_PAGE_MAIN_MENU = 'load_page_main_menu';
static MAIN_MENU_OPEN = 'main_menu_open'; static readonly SEND_ON_ENTER_CHANGED = 'send_on_enter_changed';
static SELECT_COURSE_TAB = 'select_course_tab'; static readonly MAIN_MENU_OPEN = 'main_menu_open';
static WS_CACHE_INVALIDATED = 'ws_cache_invalidated'; static readonly SELECT_COURSE_TAB = 'select_course_tab';
static SITE_STORAGE_DELETED = 'site_storage_deleted'; static readonly WS_CACHE_INVALIDATED = 'ws_cache_invalidated';
static FORM_ACTION = 'form_action'; static readonly SITE_STORAGE_DELETED = 'site_storage_deleted';
static ACTIVITY_DATA_SENT = 'activity_data_sent'; static readonly FORM_ACTION = 'form_action';
static readonly ACTIVITY_DATA_SENT = 'activity_data_sent';
protected logger: CoreLogger; protected logger: CoreLogger;
protected observables: { [s: string]: Subject<any> } = {}; protected observables: { [eventName: string]: Subject<unknown> } = {};
protected uniqueEvents = {}; protected uniqueEvents: { [eventName: string]: {data: unknown} } = {};
constructor() { constructor() {
this.logger = CoreLogger.getInstance('CoreEventsProvider'); this.logger = CoreLogger.getInstance('CoreEventsProvider');
@ -89,7 +90,7 @@ export class CoreEventsProvider {
* @param siteId Site where to trigger the event. Undefined won't check the site. * @param siteId Site where to trigger the event. Undefined won't check the site.
* @return Observer to stop listening. * @return Observer to stop listening.
*/ */
on(eventName: string, callBack: (value: any) => void, siteId?: string): CoreEventObserver { on(eventName: string, callBack: (value: unknown) => void, siteId?: string): CoreEventObserver {
// If it's a unique event and has been triggered already, call the callBack. // If it's a unique event and has been triggered already, call the callBack.
// We don't need to create an observer because the event won't be triggered again. // We don't need to create an observer because the event won't be triggered again.
if (this.uniqueEvents[eventName]) { if (this.uniqueEvents[eventName]) {
@ -99,7 +100,7 @@ export class CoreEventsProvider {
return { return {
off: (): void => { off: (): void => {
// Nothing to do. // Nothing to do.
} },
}; };
} }
@ -107,10 +108,10 @@ export class CoreEventsProvider {
if (typeof this.observables[eventName] == 'undefined') { if (typeof this.observables[eventName] == 'undefined') {
// No observable for this event, create a new one. // No observable for this event, create a new one.
this.observables[eventName] = new Subject<any>(); this.observables[eventName] = new Subject<unknown>();
} }
const subscription = this.observables[eventName].subscribe((value: any) => { const subscription = this.observables[eventName].subscribe((value: {siteId?: string; [key: string]: unknown}) => {
if (!siteId || value.siteId == siteId) { if (!siteId || value.siteId == siteId) {
callBack(value); callBack(value);
} }
@ -121,7 +122,7 @@ export class CoreEventsProvider {
off: (): void => { off: (): void => {
this.logger.debug(`Stop listening to event '${eventName}'`); this.logger.debug(`Stop listening to event '${eventName}'`);
subscription.unsubscribe(); subscription.unsubscribe();
} },
}; };
} }
@ -136,11 +137,8 @@ export class CoreEventsProvider {
* @param siteId Site where to trigger the event. Undefined won't check the site. * @param siteId Site where to trigger the event. Undefined won't check the site.
* @return Observer to stop listening. * @return Observer to stop listening.
*/ */
onMultiple(eventNames: string[], callBack: (value: any) => void, siteId?: string): CoreEventObserver { onMultiple(eventNames: string[], callBack: (value: unknown) => void, siteId?: string): CoreEventObserver {
const observers = eventNames.map((name) => this.on(name, callBack, siteId));
const observers = eventNames.map((name) => {
return this.on(name, callBack, siteId);
});
// Create and return a CoreEventObserver. // Create and return a CoreEventObserver.
return { return {
@ -148,7 +146,7 @@ export class CoreEventsProvider {
observers.forEach((observer) => { observers.forEach((observer) => {
observer.off(); observer.off();
}); });
} },
}; };
} }
@ -159,14 +157,11 @@ export class CoreEventsProvider {
* @param data Data to pass to the observers. * @param data Data to pass to the observers.
* @param siteId Site where to trigger the event. Undefined means no Site. * @param siteId Site where to trigger the event. Undefined means no Site.
*/ */
trigger(eventName: string, data?: any, siteId?: string): void { trigger(eventName: string, data?: unknown, siteId?: string): void {
this.logger.debug(`Event '${eventName}' triggered.`); this.logger.debug(`Event '${eventName}' triggered.`);
if (this.observables[eventName]) { if (this.observables[eventName]) {
if (siteId) { if (siteId) {
if (!data) { data = Object.assign(data || {}, { siteId });
data = {};
}
data.siteId = siteId;
} }
this.observables[eventName].next(data); this.observables[eventName].next(data);
} }
@ -179,17 +174,14 @@ export class CoreEventsProvider {
* @param data Data to pass to the observers. * @param data Data to pass to the observers.
* @param siteId Site where to trigger the event. Undefined means no Site. * @param siteId Site where to trigger the event. Undefined means no Site.
*/ */
triggerUnique(eventName: string, data: any, siteId?: string): void { triggerUnique(eventName: string, data: unknown, siteId?: string): void {
if (this.uniqueEvents[eventName]) { if (this.uniqueEvents[eventName]) {
this.logger.debug(`Unique event '${eventName}' ignored because it was already triggered.`); this.logger.debug(`Unique event '${eventName}' ignored because it was already triggered.`);
} else { } else {
this.logger.debug(`Unique event '${eventName}' triggered.`); this.logger.debug(`Unique event '${eventName}' triggered.`);
if (siteId) { if (siteId) {
if (!data) { data = Object.assign(data || {}, { siteId });
data = {};
}
data.siteId = siteId;
} }
// Store the data so it can be passed to observers that register from now on. // Store the data so it can be passed to observers that register from now on.
@ -203,6 +195,7 @@ export class CoreEventsProvider {
} }
} }
} }
} }
export class CoreEvents extends makeSingleton(CoreEventsProvider) {} export class CoreEvents extends makeSingleton(CoreEventsProvider) {}