File

src/services/CommBroker.ts

Constructor

constructor()

Methods

Static toString
toString()
Returns : void
Static fire
fire(iMessage: IMessage)

With fire we push a stream of IMessages into our stream.


Returns : void
Static onEvent
onEvent(event: string)
Returns : void
Static onInstance
onInstance(instance: any)
Returns : void
Static onInstanceAndEvent
onInstanceAndEvent(instance: any, event: string)
Returns : void
setService
setService(i_name: any, i_service: any)

Register a service that others can query.


Parameters :
  • i_name : String
  • i_service : Object
Returns : void
getService
getService(i_name: any)

Get a registered service.


Parameters :
  • i_name : String
Returns : any
getAllServices
getAllServices()

Expose all services and data members.


Returns : void
clearServices
clearServices()

Clear all current registered services


Returns : void
setValue
setValue(i_name: any, i_value: any, i_fireEvent: string)

Register a data member that others can query.


Parameters :
  • i_name : String
  • i_value : Object
  • i_fireEvent : Event
Returns : void
getValue
getValue(i_name: any)

Get a registered data member.


Parameters :
  • i_name : String
Returns : any

Properties

Protected randomName
randomName: number
Protected services
services: string[]
Protected streamMessages
streamMessages: Subject<IMessage>
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;
    }
}

results matching ""

    No results matching ""