refactor(events): remove Events feat from core

This commit is contained in:
Adam Bradley
2018-03-02 14:52:15 -06:00
parent 6da9882c6c
commit 756dc6e306
4 changed files with 94 additions and 97 deletions

View File

@ -1,5 +1,6 @@
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { import {
APP_INITIALIZER,
CUSTOM_ELEMENTS_SCHEMA, CUSTOM_ELEMENTS_SCHEMA,
ModuleWithProviders, ModuleWithProviders,
NgModule NgModule
@ -23,7 +24,7 @@ import { ActionSheetController } from './providers/action-sheet-controller';
import { AlertController } from './providers/alert-controller'; import { AlertController } from './providers/alert-controller';
import { AngularComponentMounter } from './providers/angular-component-mounter'; import { AngularComponentMounter } from './providers/angular-component-mounter';
import { App } from './providers/app'; import { App } from './providers/app';
import { Events } from './providers/events'; import { Events, setupProvideEvents } from './providers/events';
import { LoadingController } from './providers/loading-controller'; import { LoadingController } from './providers/loading-controller';
import { MenuController } from './providers/menu-controller'; import { MenuController } from './providers/menu-controller';
import { ModalController } from './providers/modal-controller'; import { ModalController } from './providers/modal-controller';
@ -81,7 +82,9 @@ export class IonicAngularModule {
LoadingController, LoadingController,
MenuController, MenuController,
Platform, Platform,
ToastController ToastController,
{ provide: APP_INITIALIZER, useFactory: setupProvideEvents, multi: true },
] ]
}; };
} }

View File

@ -1,20 +1,100 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { IonicWindow } from '../types/interfaces';
@Injectable() @Injectable()
export class Events { export class Events {
private c: {[topic: string]: Function[]} = [] as any;
subscribe(topic: string, handler: (event?: any) => void) { /**
return (window as IonicWindow).Ionic.Events.subscribe(topic, handler); * Subscribe to an event topic. Events that get posted to that topic will trigger the provided handler.
*
* @param {string} topic the topic to subscribe to
* @param {function} handler the event handler
*/
subscribe(topic: string, ...handlers: Function[]) {
if (!this.c[topic]) {
this.c[topic] = [];
}
handlers.forEach((handler) => {
this.c[topic].push(handler);
});
} }
unsubscribe(topic: string, handler: (event?: any) => void) { /**
return (window as IonicWindow).Ionic.Events.unsubscribe(topic, handler); * Unsubscribe from the given topic. Your handler will no longer receive events published to this topic.
*
* @param {string} topic the topic to unsubscribe from
* @param {function} handler the event handler
*
* @return true if a handler was removed
*/
unsubscribe(topic: string, handler: Function = null) {
const t = this.c[topic];
if (!t) {
// Wasn't found, wasn't removed
return false;
}
if (!handler) {
// Remove all handlers for this topic
delete this.c[topic];
return true;
}
// We need to find and remove a specific handler
const i = t.indexOf(handler);
if (i < 0) {
// Wasn't found, wasn't removed
return false;
}
t.splice(i, 1);
// If the channel is empty now, remove it from the channel map
if (!t.length) {
delete this.c[topic];
}
return true;
} }
publish(topic: string, event?: any) { /**
return (window as IonicWindow).Ionic.Events.publish(topic, event); * Publish an event to the given topic.
} *
* @param {string} topic the topic to publish to
* @param {any} eventData the data to send as the event
*/
publish(topic: string, ...args: any[]) {
const t = this.c[topic];
if (!t) {
return null;
}
const responses: any[] = [];
t.forEach((handler: any) => {
responses.push(handler(...args));
});
return responses;
}
}
export function setupEvents() {
const events = new Events();
window.addEventListener('online', ev => events.publish('app:online', ev));
window.addEventListener('offline', ev => events.publish('app:offline', ev));
window.addEventListener('orientationchange', ev => events.publish('app:rotated', ev));
return events;
}
export function setupProvideEvents() {
return function() {
return setupEvents();
};
} }

View File

@ -1,90 +1,6 @@
export class Events {
private c: {[topic: string]: Function[]} = [] as any;
/**
* Subscribe to an event topic. Events that get posted to that topic will trigger the provided handler.
*
* @param {string} topic the topic to subscribe to
* @param {function} handler the event handler
*/
subscribe(topic: string, ...handlers: Function[]) {
if (!this.c[topic]) {
this.c[topic] = [];
}
handlers.forEach((handler) => {
this.c[topic].push(handler);
});
}
/**
* Unsubscribe from the given topic. Your handler will no longer receive events published to this topic.
*
* @param {string} topic the topic to unsubscribe from
* @param {function} handler the event handler
*
* @return true if a handler was removed
*/
unsubscribe(topic: string, handler: Function = null) {
const t = this.c[topic];
if (!t) {
// Wasn't found, wasn't removed
return false;
}
if (!handler) {
// Remove all handlers for this topic
delete this.c[topic];
return true;
}
// We need to find and remove a specific handler
const i = t.indexOf(handler);
if (i < 0) {
// Wasn't found, wasn't removed
return false;
}
t.splice(i, 1);
// If the channel is empty now, remove it from the channel map
if (!t.length) {
delete this.c[topic];
}
return true;
}
/**
* Publish an event to the given topic.
*
* @param {string} topic the topic to publish to
* @param {any} eventData the data to send as the event
*/
publish(topic: string, ...args: any[]) {
const t = this.c[topic];
if (!t) {
return null;
}
const responses: any[] = [];
t.forEach((handler: any) => {
responses.push(handler(...args));
});
return responses;
}
}
export function setupEvents(win: Window, doc: Document) { export function setupEvents(win: Window, doc: Document) {
const events = new Events();
win.addEventListener('online', ev => events.publish('app:online', ev));
win.addEventListener('offline', ev => events.publish('app:offline', ev));
win.addEventListener('orientationchange', ev => events.publish('app:rotated', ev));
win.addEventListener('statusTap', () => { win.addEventListener('statusTap', () => {
const centerElm = doc.elementFromPoint(win.innerWidth / 2, win.innerHeight / 2); const centerElm = doc.elementFromPoint(win.innerWidth / 2, win.innerHeight / 2);
@ -94,8 +10,6 @@ export function setupEvents(win: Window, doc: Document) {
scrollElm.componentOnReady(() => scrollElm.scrollToTop(300)); scrollElm.componentOnReady(() => scrollElm.scrollToTop(300));
} }
} }
}); });
return events;
} }

View File

@ -31,7 +31,7 @@ Ionic.config = Context.config = createConfigController(
Context.platforms Context.platforms
); );
Ionic.Events = Context.Events = setupEvents(window, document); setupEvents(window, document);
// first see if the mode was set as an attribute on <html> // first see if the mode was set as an attribute on <html>
// which could have been set by the user, or by prerendering // which could have been set by the user, or by prerendering