File

C:/GoDev/src/TCSTK-Angular/projects/tibco-tcstk/tc-core-lib/src/lib/common/tc-core-topic-comm.ts

Index

Properties
Methods

Constructor

constructor()

Methods

clearMessages
clearMessages(name: string)
Parameters :
Name Type Optional
name string No
Returns : void
createTopicIfNotExists
createTopicIfNotExists(name: string, init)
Parameters :
Name Type Optional
name string No
init No
Returns : void
getMessage
getMessage(name: string)
Parameters :
Name Type Optional
name string No
Returns : Observable<any>
sendMessage
sendMessage(name: string, message: string)
Parameters :
Name Type Optional
name string No
message string No
Returns : void

Properties

topics
Type : Array<literal type>
Default value : [ {name: 'sample.topic', subject: new BehaviorSubject<any>('init')} ]
import {Injectable} from '@angular/core';
import {BehaviorSubject, Observable} from 'rxjs';

@Injectable({providedIn: 'root'})
export class MessageTopicService {

  topics: Array<{ name: string, subject: BehaviorSubject<any> }> = [
    {name: 'sample.topic', subject: new BehaviorSubject<any>('init')}
  ];

  constructor() {
    console.log('Topic Message service Created...');
  }

  sendMessage(name: string, message: string) {
    this.createTopicIfNotExists(name, {text: message});
    this.topics.filter(x => x.name === name)[0].subject.next({text: message});
  }

  clearMessages(name: string) {
    this.createTopicIfNotExists(name, {text: 'init'});
    // this.queues.filter(x => x.name === name)[0].subject.next('init');
  }

  getMessage(name: string): Observable<any> {
    this.createTopicIfNotExists(name, {text: 'init'});
    return this.topics.filter(x => x.name === name)[0].subject.asObservable();
  }

  createTopicIfNotExists(name: string , init) {
    let queueExist = false;
    for (const q of this.topics) {
      if (q.name === name) {
        // console.log('Queue Exist: ' + q.name);
        queueExist = true;
      }
    }
    if (!queueExist) {
      console.log('Creating Topic: ' + name);
      const tempTopic = {name: name, subject: new BehaviorSubject<any>(init)};
      this.topics.push(tempTopic);
    }
  }
}

result-matching ""

    No results matching ""