refactor(angular): removes Events service (#19600)

BREAKING CHANGES

The Events Service in @ionic/angular has been removed. 

- Use "Observables" for a similar pub/sub architecture: https://angular.io/guide/observables
- Use "Redux" for advanced state management: https://ngrx.io
This commit is contained in:
Liam DeBeasi 2019-10-09 14:05:20 -04:00 committed by Brandy Carney
parent 42bb7e956b
commit e5f2a18230
3 changed files with 2 additions and 93 deletions

View File

@ -20,7 +20,6 @@ export * from './directives/proxies';
export { AngularDelegate } from './providers/angular-delegate';
export { ActionSheetController } from './providers/action-sheet-controller';
export { AlertController } from './providers/alert-controller';
export { Events } from './providers/events';
export { LoadingController } from './providers/loading-controller';
export { MenuController } from './providers/menu-controller';
export { PickerController } from './providers/picker-controller';

View File

@ -1,81 +0,0 @@
import { Injectable } from '@angular/core';
export type EventHandler = (...args: any[]) => any;
@Injectable({
providedIn: 'root',
})
export class Events {
private c = new Map<string, EventHandler[]>();
constructor() {
console.warn(`[DEPRECATION][Events]: The Events provider is deprecated and it will be removed in the next major release.
- Use "Observables" for a similar pub/sub architecture: https://angular.io/guide/observables
- Use "Redux" for advanced state management: https://ngrx.io`);
}
/**
* Subscribe to an event topic. Events that get posted to that topic will trigger the provided handler.
*
* @param topic the topic to subscribe to
* @param handler the event handler
*/
subscribe(topic: string, ...handlers: EventHandler[]) {
let topics = this.c.get(topic);
if (!topics) {
this.c.set(topic, topics = []);
}
topics.push(...handlers);
}
/**
* Unsubscribe from the given topic. Your handler will no longer receive events published to this topic.
*
* @param topic the topic to unsubscribe from
* @param handler the event handler
*
* @return true if a handler was removed
*/
unsubscribe(topic: string, handler?: EventHandler): boolean {
if (!handler) {
return this.c.delete(topic);
}
const topics = this.c.get(topic);
if (!topics) {
return false;
}
// We need to find and remove a specific handler
const index = topics.indexOf(handler);
if (index < 0) {
// Wasn't found, wasn't removed
return false;
}
topics.splice(index, 1);
if (topics.length === 0) {
this.c.delete(topic);
}
return true;
}
/**
* Publish an event to the given topic.
*
* @param topic the topic to publish to
* @param eventData the data to send as the event
*/
publish(topic: string, ...args: any[]): any[] | null {
const topics = this.c.get(topic);
if (!topics) {
return null;
}
return topics.map(handler => {
try {
return handler(...args);
} catch (e) {
console.error(e);
return null;
}
});
}
}

View File

@ -1,7 +1,7 @@
import { Component, NgZone } from '@angular/core';
import {
Platform, ModalController, AlertController, ActionSheetController,
PopoverController, ToastController, Events, PickerController, MenuController,
PopoverController, ToastController, PickerController, MenuController,
LoadingController, NavController, DomController, Config
} from '@ionic/angular';
@ -13,7 +13,6 @@ export class ProvidersComponent {
isLoaded = false;
isReady = false;
isEvent = false;
isResumed = false;
isPaused = false;
isResized = false;
@ -25,7 +24,6 @@ export class ProvidersComponent {
constructor(
actionSheetCtrl: ActionSheetController,
alertCtrl: AlertController,
events: Events,
loadingCtrl: LoadingController,
menuCtrl: MenuController,
pickerCtrl: PickerController,
@ -40,7 +38,7 @@ export class ProvidersComponent {
) {
// test all providers load
if (
actionSheetCtrl && alertCtrl && events && loadingCtrl && menuCtrl && pickerCtrl &&
actionSheetCtrl && alertCtrl && loadingCtrl && menuCtrl && pickerCtrl &&
modalCtrl && platform && popoverCtrl && toastCtrl && navCtrl && domCtrl && config
) {
this.isLoaded = true;
@ -69,13 +67,6 @@ export class ProvidersComponent {
this.isDesktop = platform.is('desktop');
this.isMobile = platform.is('mobile');
// test events
events.subscribe('topic', () => {
this.isEvent = true;
NgZone.assertInAngularZone();
});
events.publish('topic');
// test config
this.isTesting = config.getBoolean('_testing');
config.set('keyboardHeight', 12345);