mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-17 10:41:13 +08:00
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:

committed by
Brandy Carney

parent
e623fbff80
commit
17170f01da
@ -20,7 +20,6 @@ export * from './directives/proxies';
|
|||||||
export { AngularDelegate } from './providers/angular-delegate';
|
export { AngularDelegate } from './providers/angular-delegate';
|
||||||
export { ActionSheetController } from './providers/action-sheet-controller';
|
export { ActionSheetController } from './providers/action-sheet-controller';
|
||||||
export { AlertController } from './providers/alert-controller';
|
export { AlertController } from './providers/alert-controller';
|
||||||
export { Events } from './providers/events';
|
|
||||||
export { LoadingController } from './providers/loading-controller';
|
export { LoadingController } from './providers/loading-controller';
|
||||||
export { MenuController } from './providers/menu-controller';
|
export { MenuController } from './providers/menu-controller';
|
||||||
export { PickerController } from './providers/picker-controller';
|
export { PickerController } from './providers/picker-controller';
|
||||||
|
@ -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;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,7 +1,7 @@
|
|||||||
import { Component, NgZone } from '@angular/core';
|
import { Component, NgZone } from '@angular/core';
|
||||||
import {
|
import {
|
||||||
Platform, ModalController, AlertController, ActionSheetController,
|
Platform, ModalController, AlertController, ActionSheetController,
|
||||||
PopoverController, ToastController, Events, PickerController, MenuController,
|
PopoverController, ToastController, PickerController, MenuController,
|
||||||
LoadingController, NavController, DomController, Config
|
LoadingController, NavController, DomController, Config
|
||||||
} from '@ionic/angular';
|
} from '@ionic/angular';
|
||||||
|
|
||||||
@ -13,7 +13,6 @@ export class ProvidersComponent {
|
|||||||
|
|
||||||
isLoaded = false;
|
isLoaded = false;
|
||||||
isReady = false;
|
isReady = false;
|
||||||
isEvent = false;
|
|
||||||
isResumed = false;
|
isResumed = false;
|
||||||
isPaused = false;
|
isPaused = false;
|
||||||
isResized = false;
|
isResized = false;
|
||||||
@ -25,7 +24,6 @@ export class ProvidersComponent {
|
|||||||
constructor(
|
constructor(
|
||||||
actionSheetCtrl: ActionSheetController,
|
actionSheetCtrl: ActionSheetController,
|
||||||
alertCtrl: AlertController,
|
alertCtrl: AlertController,
|
||||||
events: Events,
|
|
||||||
loadingCtrl: LoadingController,
|
loadingCtrl: LoadingController,
|
||||||
menuCtrl: MenuController,
|
menuCtrl: MenuController,
|
||||||
pickerCtrl: PickerController,
|
pickerCtrl: PickerController,
|
||||||
@ -40,7 +38,7 @@ export class ProvidersComponent {
|
|||||||
) {
|
) {
|
||||||
// test all providers load
|
// test all providers load
|
||||||
if (
|
if (
|
||||||
actionSheetCtrl && alertCtrl && events && loadingCtrl && menuCtrl && pickerCtrl &&
|
actionSheetCtrl && alertCtrl && loadingCtrl && menuCtrl && pickerCtrl &&
|
||||||
modalCtrl && platform && popoverCtrl && toastCtrl && navCtrl && domCtrl && config
|
modalCtrl && platform && popoverCtrl && toastCtrl && navCtrl && domCtrl && config
|
||||||
) {
|
) {
|
||||||
this.isLoaded = true;
|
this.isLoaded = true;
|
||||||
@ -69,13 +67,6 @@ export class ProvidersComponent {
|
|||||||
this.isDesktop = platform.is('desktop');
|
this.isDesktop = platform.is('desktop');
|
||||||
this.isMobile = platform.is('mobile');
|
this.isMobile = platform.is('mobile');
|
||||||
|
|
||||||
// test events
|
|
||||||
events.subscribe('topic', () => {
|
|
||||||
this.isEvent = true;
|
|
||||||
NgZone.assertInAngularZone();
|
|
||||||
});
|
|
||||||
events.publish('topic');
|
|
||||||
|
|
||||||
// test config
|
// test config
|
||||||
this.isTesting = config.getBoolean('_testing');
|
this.isTesting = config.getBoolean('_testing');
|
||||||
config.set('keyboardHeight', 12345);
|
config.set('keyboardHeight', 12345);
|
||||||
|
Reference in New Issue
Block a user