src/services/CommBroker.ts
constructor()
|
Defined in src/services/CommBroker.ts:84
|
Static toString |
toString()
|
Defined in src/services/CommBroker.ts:107
|
Returns :
void
|
Static fire |
fire(iMessage: IMessage)
|
Defined in src/services/CommBroker.ts:116
|
With fire we push a stream of IMessages into our stream.
Returns :
void
|
Static onEvent |
onEvent(event: string)
|
Defined in src/services/CommBroker.ts:126
|
Returns :
void
|
Static onInstance |
onInstance(instance: any)
|
Defined in src/services/CommBroker.ts:133
|
Returns :
void
|
Static onInstanceAndEvent |
onInstanceAndEvent(instance: any, event: string)
|
Defined in src/services/CommBroker.ts:140
|
Returns :
void
|
setService |
setService(i_name: any, i_service: any)
|
Defined in src/services/CommBroker.ts:154
|
Register a service that others can query.
Parameters :
Returns :
void
|
getService |
getService(i_name: any)
|
Defined in src/services/CommBroker.ts:164
|
Get a registered service.
Parameters :
Returns :
any
|
getAllServices |
getAllServices()
|
Defined in src/services/CommBroker.ts:181
|
Expose all services and data members.
Returns :
void
|
clearServices |
clearServices()
|
Defined in src/services/CommBroker.ts:189
|
Clear all current registered services
Returns :
void
|
setValue |
setValue(i_name: any, i_value: any, i_fireEvent: string)
|
Defined in src/services/CommBroker.ts:203
|
Register a data member that others can query.
Parameters :
Returns :
void
|
getValue |
getValue(i_name: any)
|
Defined in src/services/CommBroker.ts:223
|
Get a registered data member.
Parameters :
Returns :
any
|
Protected randomName |
randomName: |
Defined in src/services/CommBroker.ts:84
|
Protected services |
services: |
Defined in src/services/CommBroker.ts:83
|
Protected streamMessages |
streamMessages: |
Defined in src/services/CommBroker.ts:82
|
import {Component, Injectable} from '@angular/core';
import {Observer} from "rxjs/Observer";
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/share';
import {Subject} from "rxjs/Subject";
import * as _ from 'lodash'
import {Lib} from "../Lib";
/**
CommBroker is a React type Mediator injectable service provider
Examples
============
self.onEvent('click').subscribe((e:IMessage)=> {
console.log('I heard a click');
});
// won't work, event != jump
self.onEvent('jump').subscribe((e:IMessage)=> {
console.log('I heard a click');
});
// wont work, this != self
self.onInstance(null).subscribe((e:IMessage)=> {
console.log('XYZ#$%^& should never come here...');
});
self.onInstanceAndEvent(this, 'click').subscribe((e:IMessage)=> {
console.log(`I heard you commBroker on event click ` + e.message);
});
self.onInstanceAndEvent(self, 'click').first().subscribe((e:IMessage)=> {
var commBroker:CommBroker = e.fromInstance;
console.log(`just one ${e.event} from ${commBroker.toString()}`);
});
self.onInstanceAndEvent(self, 'click').subscribe((e:IMessage) => {
console.log(`handle success: rx ${e.event}`)
}, (e) => {
console.log(`handle error ${e}`)
}, () => {
console.log(`handle complete`)
});
// before stream ready
var msg1:IMessage = {
fromInstance: self,
event: 'click',
context: 1,
message: 'before stream is ready'
};
self.fire(msg1);
// after stream ready
setTimeout(function () {
var msg2:IMessage = {
fromInstance: self,
event: 'click',
context: 2,
message: 'after stream is ready'
};
self.fire(msg2);
}, 2000)
**/
export interface IMessage {
event: string,
fromInstance: any,
context?: any,
message?: any
}
// create an alias type just so it's easier to associate
type SubjectMessage = Subject<IMessage>;
type SubjectMessages = Subject<Array<SubjectMessage>>;
@Injectable()
export class CommBroker {
private streamMessages: Subject<IMessage>;
private services: string[];
private randomName: number = Math.random();
constructor() {
var self = this;
self.services = [];
self.streamMessages = new Subject() as SubjectMessage;
// we use share as a shorthand for publish() which converts cold to hot
// observable as well as connect() / refCount()
// when connected references moves from 0 to 1
self.streamMessages.share();
// if we wish to use a unidirectional stream we can convert to Observable instead of subject
//self.stream = new Observable(trigger => {
// trigger.next(msg);
// trigger.error('boom');
// trigger.complete('done');
//}).share();
// this is the only global member we expose
document['commBroker'] = this;
}
public toString() {
return 'CommBroker ' + this.randomName; // to test uniqueness
}
/**
With fire we push a stream of IMessages into our stream.
@method fire
@params IMessage
**/
public fire(iMessage: IMessage): void {
try {
this.streamMessages.next(iMessage);
} catch (e) {
if (Lib.DevMode())
console.error('CommBroker fire exception in function: ' + iMessage + ' ' + e);
}
}
public onEvent(event: string) {
var self = this;
return self.streamMessages.filter((e) => {
return e.event == event;
})
}
public onInstance(instance: any) {
var self = this;
return self.streamMessages.filter((e) => {
return e.fromInstance == instance;
})
}
public onInstanceAndEvent(instance: any, event: string) {
var self = this;
return self.streamMessages.filter((e) => {
return e.fromInstance == instance && e.event == event
})
}
/**
Register a service that others can query.
@method setService
@param {String} i_name
@param {Object} i_service
@return none
**/
setService(i_name, i_service) {
this.services[i_name] = i_service;
}
/**
Get a registered service.
@method getService
@param {String} i_name
@return {Object} services member
**/
getService(i_name): any {
if (i_name == undefined) {
//log('cant get set undefined service ' + i_name);
return undefined;
}
if (this.services[i_name]) {
return this.services[i_name]
} else {
return undefined;
}
}
/**
Expose all services and data members.
@method getAllServices
@return {Object} services
**/
getAllServices() {
return this.services;
}
/**
Clear all current registered services
@method clearServices
**/
clearServices() {
var self = this;
// delete self.services;
self.services = undefined;
}
/**
Register a data member that others can query.
@method setValue
@param {String} i_name
@param {Object} i_value
@param {Event} i_fireEvent
@return none
**/
setValue(i_name: any, i_value: any, i_fireEvent?: string): void {
var self = this;
this.services[i_name] = i_value;
if (i_fireEvent) {
var msg: IMessage = {
fromInstance: self,
event: i_fireEvent,
context: null,
message: {name: i_name, value: i_value}
};
self.fire(msg);
}
}
/**
Get a registered data member.
@method getValue
@param {String} i_name
@return {Object} m_services member
**/
getValue(i_name): any {
var v: any = this.services[i_name];
if (v === 0)
return this.services[i_name]
if (!_.isUndefined(v))
return this.services[i_name]
return undefined;
}
}