Browse Source

Adds an event bus to the container

main
Keith Daulton 2 years ago
parent
commit
efb8341d0a
2 changed files with 54 additions and 0 deletions
  1. +7
    -0
      src/container.ts
  2. +47
    -0
      src/eventBus.ts

+ 7
- 0
src/container.ts View File

@ -11,6 +11,7 @@ import type { ToggleFileAnnotationCommandArgs } from './commands';
import type { FileAnnotationType, ModeConfig } from './configuration';
import { AnnotationsToggleMode, configuration, DateSource, DateStyle } from './configuration';
import { Commands } from './constants';
import { EventBus } from './eventBus';
import { GitFileSystemProvider } from './git/fsProvider';
import { GitProviderService } from './git/gitProviderService';
import { GitHubAuthenticationProvider } from './git/remotes/github';
@ -180,6 +181,7 @@ export class Container {
context.subscriptions.push((this._lineTracker = new GitLineTracker(this)));
context.subscriptions.push((this._keyboard = new Keyboard()));
context.subscriptions.push((this._vsls = new VslsController(this)));
context.subscriptions.push((this._eventBus = new EventBus()));
context.subscriptions.push((this._fileAnnotationController = new FileAnnotationController(this)));
context.subscriptions.push((this._lineAnnotationController = new LineAnnotationController(this)));
@ -343,6 +345,11 @@ export class Container {
return 'production';
}
private _eventBus: EventBus;
get events() {
return this._eventBus;
}
private _fileAnnotationController: FileAnnotationController;
get fileAnnotations() {
return this._fileAnnotationController;

+ 47
- 0
src/eventBus.ts View File

@ -0,0 +1,47 @@
import type { Disposable } from 'vscode';
import { EventEmitter } from 'vscode';
export type EventBusPackage = {
name: string;
data?: unknown;
source?: string;
};
export type EventBusOptions = {
source?: string;
};
export class EventBus implements Disposable {
private _emitter: EventEmitter<EventBusPackage>;
constructor() {
this._emitter = new EventEmitter();
}
private get event() {
return this._emitter.event;
}
on(eventName: string, handler: (e: EventBusPackage) => void, thisArgs?: any, disposables?: Disposable[]) {
return this.event(
e => {
if (eventName !== e.name) return;
handler.call(thisArgs, e);
},
thisArgs,
disposables,
);
}
fire(name: string, data?: unknown, options?: EventBusOptions) {
this._emitter.fire({
name: name,
data: data,
source: options?.source,
});
}
dispose() {
this._emitter?.dispose();
}
}

Loading…
Cancel
Save