EventBusLogger.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import React, { PureComponent } from 'react';
  2. import { PartialObserver, Unsubscribable } from 'rxjs';
  3. import {
  4. BusEvent,
  5. CircularVector,
  6. DataHoverEvent,
  7. DataHoverClearEvent,
  8. DataSelectEvent,
  9. EventBus,
  10. } from '@grafana/data';
  11. import { CustomScrollbar } from '@grafana/ui';
  12. interface Props {
  13. eventBus: EventBus;
  14. }
  15. interface State {
  16. isError?: boolean;
  17. counter: number;
  18. }
  19. interface BusEventEx {
  20. key: number;
  21. type: string;
  22. path: string;
  23. payload: any;
  24. }
  25. let counter = 100;
  26. export class EventBusLoggerPanel extends PureComponent<Props, State> {
  27. history = new CircularVector<BusEventEx>({ capacity: 40, append: 'head' });
  28. subs: Unsubscribable[];
  29. constructor(props: Props) {
  30. super(props);
  31. this.state = { counter };
  32. const subs: Unsubscribable[] = [];
  33. subs.push(props.eventBus.getStream(DataHoverEvent).subscribe(this.eventObserver));
  34. subs.push(props.eventBus.getStream(DataHoverClearEvent).subscribe(this.eventObserver));
  35. subs.push(props.eventBus.getStream(DataSelectEvent).subscribe(this.eventObserver));
  36. this.subs = subs;
  37. }
  38. componentWillUnmount() {
  39. for (const sub of this.subs) {
  40. sub.unsubscribe();
  41. }
  42. }
  43. eventObserver: PartialObserver<BusEvent> = {
  44. next: (event: BusEvent) => {
  45. const origin = event.origin as any;
  46. this.history.add({
  47. key: counter++,
  48. type: event.type,
  49. path: origin?.path,
  50. payload: event.payload,
  51. });
  52. this.setState({ counter });
  53. },
  54. };
  55. render() {
  56. return (
  57. <CustomScrollbar autoHeightMin="100%" autoHeightMax="100%">
  58. {this.history.map((v, idx) => (
  59. <div key={v.key}>
  60. {JSON.stringify(v.path)} {v.type} / X:{JSON.stringify(v.payload.x)} / Y:{JSON.stringify(v.payload.y)}
  61. </div>
  62. ))}
  63. </CustomScrollbar>
  64. );
  65. }
  66. }