123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- import { interval as RxjsInterval, Subject, Subscription } from "rxjs"
- import { BehaviorSubject } from "rxjs"
- import { BufferService } from "../services/buffer.service"
- import { ConnectionState, Message } from "../interfaces/general.interface"
- import { v4 as uuidv4 } from 'uuid'
- import { error } from "console"
- console.log(`Testing for HTTP buffer service.`)
- let source: Subject<Message> = new Subject()
- let initialReport: ConnectionState = { status: 'DIRECT_PUBLISH' }
- let connectionStateSubject: BehaviorSubject<ConnectionState> = new BehaviorSubject(initialReport)
- let bufferService: BufferService = new BufferService(source, connectionStateSubject, 'test')
- /* So, have an interval obseravable that will post a method every second, but it will be buffered instead of being post.
- or something like that. */
- // Create an Observable that emits something every 1 second
- const interval = RxjsInterval(1000);
- interval.subscribe({
- next: time => {
- let message = {
- id: uuidv4(),
- message: `I am to be posted`
- }
- source.next(message)
- }
- })
- bufferService.getMessages().subscribe({
- next: message => {
- // Usage example:
- fetch('http://localhost:9999/data', {
- method: 'POST',
- body: JSON.stringify(message),
- headers: {
- "Content-type": "application/json; charset=UTF-8"
- }
- }).then((response) => {
- console.log(`sending ${message.id}`)
- console.log(response.status)
- connectionStateSubject.next({ status: 'DIRECT_PUBLISH' })
- }).catch((error) => {
- console.error(error)
- connectionStateSubject.next({ status: 'BUFFER' })
- periodicCheck()
- })
- }
- })
- function periodicCheck() {
- let timer = RxjsInterval(1000).subscribe({
- next: everySecond => {
- fetch('http://localhost:9999/', {
- method: 'GET',
- headers: {
- "Content-type": "application/json; charset=UTF-8"
- }
- }).then((response) => {
- if (response.ok) {
- connectionStateSubject.next({ status: 'DIRECT_PUBLISH' })
- timer.unsubscribe()
- } else {
- connectionStateSubject.next({ status: 'BUFFER' })
- }
- }).catch((error) => {
- connectionStateSubject.next({ status: 'BUFFER' })
- })
- }
- })
- }
- // async function postData(url, data): Promise<any> {
- // return new Promise(async (resolve, reject) => {
- // try {
- // const response = await fetch(url, {
- // method: 'POST',
- // mode: 'cors',
- // cache: 'no-cache',
- // credentials: 'same-origin',
- // headers: {
- // 'Content-Type': 'application/json'
- // },
- // redirect: 'follow',
- // referrerPolicy: 'no-referrer',
- // body: JSON.stringify(data)
- // });
- // if (!response.ok) {
- // // throw new Error(`HTTP error! Status: ${response.status}`);
- // reject(`HTTP error! Status: ${response.status}`);
- // }
- // let responseData;
- // try {
- // responseData = await response.json();
- // } catch (jsonError) {
- // // throw new Error('Failed to parse JSON response');
- // reject('Failed to parse JSON response');
- // }
- // resolve(responseData);
- // } catch (error) {
- // console.error('Error making POST request:', error);
- // throw error;
- // }
- // })
- // }
|