You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.5 KiB

  1. import type { Disposable, Uri } from 'vscode';
  2. import { EventEmitter } from 'vscode';
  3. import type { ViewsConfigKeys } from './config';
  4. import type { GitCaches } from './git/gitProvider';
  5. import type { GitCommit } from './git/models/commit';
  6. import type { GitRevisionReference } from './git/models/reference';
  7. import type { WebviewIds } from './webviews/webviewBase';
  8. import type { WebviewViewIds } from './webviews/webviewViewBase';
  9. export type CommitSelectedEvent = EventBusEvent<'commit:selected'>;
  10. interface CommitSelectedEventArgs {
  11. readonly commit: GitRevisionReference | GitCommit;
  12. readonly pin?: boolean;
  13. readonly preserveFocus?: boolean;
  14. readonly preserveVisibility?: boolean;
  15. }
  16. export type FileSelectedEvent = EventBusEvent<'file:selected'>;
  17. interface FileSelectedEventArgs {
  18. readonly uri: Uri;
  19. readonly preserveFocus?: boolean;
  20. readonly preserveVisibility?: boolean;
  21. }
  22. export type GitCacheResetEvent = EventBusEvent<'git:cache:reset'>;
  23. interface GitCacheResetEventArgs {
  24. readonly repoPath?: string;
  25. readonly caches?: GitCaches[];
  26. }
  27. type EventBusEventMap = {
  28. 'commit:selected': CommitSelectedEventArgs;
  29. 'file:selected': FileSelectedEventArgs;
  30. 'git:cache:reset': GitCacheResetEventArgs;
  31. };
  32. interface EventBusEvent<T extends keyof EventBusEventMap = keyof EventBusEventMap> {
  33. name: T;
  34. data: EventBusEventMap[T];
  35. source?: EventBusSource | undefined;
  36. }
  37. export type EventBusSource =
  38. | 'gitlens.rebase'
  39. | `gitlens.${WebviewIds}`
  40. | `gitlens.views.${WebviewViewIds}`
  41. | `gitlens.views.${ViewsConfigKeys}`;
  42. export type EventBusOptions = {
  43. source?: EventBusSource;
  44. };
  45. export class EventBus implements Disposable {
  46. private readonly _emitter = new EventEmitter<EventBusEvent>();
  47. private get event() {
  48. return this._emitter.event;
  49. }
  50. dispose() {
  51. this._emitter.dispose();
  52. }
  53. fire<T extends keyof EventBusEventMap>(name: T, data: EventBusEventMap[T], options?: EventBusOptions) {
  54. this._emitter.fire({
  55. name: name,
  56. data: data,
  57. source: options?.source,
  58. });
  59. }
  60. fireAsync<T extends keyof EventBusEventMap>(name: T, data: EventBusEventMap[T], options?: EventBusOptions) {
  61. queueMicrotask(() => this.fire(name, data, options));
  62. }
  63. on<T extends keyof EventBusEventMap>(
  64. eventName: T,
  65. handler: (e: EventBusEvent<T>) => void,
  66. thisArgs?: unknown,
  67. disposables?: Disposable[],
  68. ) {
  69. return this.event(
  70. // eslint-disable-next-line prefer-arrow-callback
  71. function (e) {
  72. if (eventName !== e.name) return;
  73. handler.call(thisArgs, e as EventBusEvent<T>);
  74. },
  75. thisArgs,
  76. disposables,
  77. );
  78. }
  79. }