adapter.base.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { BehaviorSubject, distinctUntilChanged, filter, map, Observable, Observer, Subject, Unsubscribable } from "rxjs";
  2. import { AdapterInterface, ConnectionState, TransmissionRole, TransportType, TransportServiceInterface, AdapterProfile, ClientObject } from "../interface/interface";
  3. import ConsoleLogger from "../utils/log.utils";
  4. import { v4 as uuidv4 } from 'uuid'
  5. /* This transport manager will be instantiating the necessary transport to deal with tranmission and receiving from different receivers
  6. So how?: */
  7. export class AdapterBase<T> implements AdapterInterface<T> {
  8. protected console!: ConsoleLogger
  9. protected adapterProfile!: AdapterProfile
  10. constructor(clientId: string, adapterType: TransportType, transportService: TransportServiceInterface, role: TransmissionRole) {
  11. //logic here
  12. this.setAdapterProfile(clientId, adapterType, transportService, role)
  13. this.setupConnectionState(this.adapterProfile.transportService)
  14. }
  15. subscribe(observer: Observer<T>): Unsubscribable {
  16. throw new Error("Method not implemented.");
  17. }
  18. protected setAdapterProfile(clientId: string, adapterType: TransportType, transportService: TransportServiceInterface, role: TransmissionRole): void {
  19. this.adapterProfile = {} as AdapterProfile
  20. this.adapterProfile.id = uuidv4()
  21. this.adapterProfile.clientId = clientId as string
  22. this.adapterProfile.connectionState = new BehaviorSubject<ConnectionState>(`OFFLINE`)
  23. this.adapterProfile.role = role
  24. this.adapterProfile.transportType = adapterType
  25. this.adapterProfile.transportService = transportService
  26. }
  27. public getAdapterProfile(type?: `id` | `clientId` | `role` | `transportId` | `transportType` | `connectionState`): string | Observable<ConnectionState> | AdapterProfile | undefined {
  28. if (!type) return this.adapterProfile;
  29. const lookup: Record<string, any> = {
  30. id: this.adapterProfile.id,
  31. clientId: this.adapterProfile.clientId,
  32. role: this.adapterProfile.role,
  33. transportType: this.adapterProfile.transportType,
  34. transportId: this.adapterProfile.transportService.getInfo().transportServiceId,
  35. connectionState: this.adapterProfile.connectionState.asObservable()
  36. };
  37. return lookup[type];
  38. }
  39. protected setupConnectionState(transportService: TransportServiceInterface): void {
  40. transportService.subscribeForEvent().pipe(
  41. filter(event => event.type === `Transport Event`),
  42. filter(event => (event.data as ClientObject).clientId === this.adapterProfile.clientId),
  43. map(event => {
  44. if (event.event == 'Client Disconnected' || event.event == 'Server Disconnected') {
  45. return 'OFFLINE'
  46. } else {
  47. return `ONLINE`
  48. }
  49. }),
  50. distinctUntilChanged()
  51. ).subscribe((signal: ConnectionState) => {
  52. this.adapterProfile.connectionState.next(signal)
  53. if (signal == 'OFFLINE') this.console.error({ message: `${this.adapterProfile.clientId} disconnected` })
  54. if (signal == 'ONLINE') this.console.log({ message: `${this.adapterProfile.clientId} connected and ready to go` })
  55. })
  56. }
  57. }