service.worker.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import './transferHandlers';
  2. import * as comlink from 'comlink';
  3. import { LiveChannelAddress } from '@grafana/data';
  4. import { LiveDataStreamOptions, LiveQueryDataOptions } from '@grafana/runtime';
  5. import { remoteObservableAsObservable } from './remoteObservable';
  6. import { CentrifugeService, CentrifugeSrvDeps } from './service';
  7. let centrifuge: CentrifugeService;
  8. const initialize = (
  9. deps: CentrifugeSrvDeps,
  10. remoteDataStreamSubscriberReadiness: comlink.RemoteObject<
  11. CentrifugeSrvDeps['dataStreamSubscriberReadiness'] & comlink.ProxyMarked
  12. >
  13. ) => {
  14. centrifuge = new CentrifugeService({
  15. ...deps,
  16. dataStreamSubscriberReadiness: remoteObservableAsObservable(remoteDataStreamSubscriberReadiness),
  17. });
  18. };
  19. const getConnectionState = () => {
  20. return comlink.proxy(centrifuge.getConnectionState());
  21. };
  22. const getDataStream = (options: LiveDataStreamOptions) => {
  23. return comlink.proxy(centrifuge.getDataStream(options));
  24. };
  25. const getQueryData = async (options: LiveQueryDataOptions) => {
  26. return await centrifuge.getQueryData(options);
  27. };
  28. const getStream = (address: LiveChannelAddress) => {
  29. return comlink.proxy(centrifuge.getStream(address));
  30. };
  31. const getPresence = async (address: LiveChannelAddress) => {
  32. return await centrifuge.getPresence(address);
  33. };
  34. const workObj = {
  35. initialize,
  36. getConnectionState,
  37. getDataStream,
  38. getStream,
  39. getQueryData,
  40. getPresence,
  41. };
  42. export type RemoteCentrifugeService = typeof workObj;
  43. comlink.expose(workObj);
  44. export default class {
  45. constructor() {}
  46. }