test-streamOBS.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { from, map, Observable, of, Subject } from "rxjs";
  2. import * as fs from "fs"
  3. import { BaseMessage } from "../dependencies/fisloggingservice/services/logging-service";
  4. export class StreamingService {
  5. private messagesJSON: any = fs.readFileSync("testRequest.json")
  6. private messages = JSON.parse(this.messagesJSON)
  7. // public stream(): Observable<BaseMessage> {
  8. // return new Observable(subject => {
  9. // let messages = this.messages
  10. // let count = 0
  11. // const intervalId = setInterval(() => {
  12. // subject.next(messages[count]);
  13. // count++;
  14. // if (count >= 4) {
  15. // clearInterval(intervalId);
  16. // subject.complete();
  17. // }
  18. // }, 1000)
  19. // })
  20. // }
  21. public stream(): Subject<BaseMessage> {
  22. let result: Subject<BaseMessage> = new Subject()
  23. let messages = this.messages
  24. let count = 0
  25. const intervalId = setInterval(() => {
  26. result.next(messages[count]);
  27. count++;
  28. if (count >= 4) {
  29. clearInterval(intervalId);
  30. result.complete();
  31. }
  32. }, 1000)
  33. return result
  34. }
  35. }