|
@ -1,39 +1,59 @@ |
|
|
import type { Disposable } from 'vscode'; |
|
|
|
|
|
|
|
|
import type { Disposable, Uri } from 'vscode'; |
|
|
import { EventEmitter } from 'vscode'; |
|
|
import { EventEmitter } from 'vscode'; |
|
|
|
|
|
import type { ViewsConfigKeys } from './config'; |
|
|
|
|
|
import type { GitCommit } from './git/models/commit'; |
|
|
|
|
|
import type { GitRevisionReference } from './git/models/reference'; |
|
|
|
|
|
import type { WebviewIds } from './webviews/webviewBase'; |
|
|
|
|
|
import type { WebviewViewIds } from './webviews/webviewViewBase'; |
|
|
|
|
|
|
|
|
export type EventBusPackage = { |
|
|
|
|
|
name: string; |
|
|
|
|
|
data?: unknown; |
|
|
|
|
|
source?: string; |
|
|
|
|
|
|
|
|
export type CommitSelectedEvent = EventBusEvent<'commit:selected'>; |
|
|
|
|
|
export type FileSelectedEvent = EventBusEvent<'file:selected'>; |
|
|
|
|
|
|
|
|
|
|
|
type EventBusEventMap = { |
|
|
|
|
|
'commit:selected': CommitSelectedEventArgs; |
|
|
|
|
|
'file:selected': FileSelectedEventArgs; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
interface CommitSelectedEventArgs { |
|
|
|
|
|
readonly commit: GitRevisionReference | GitCommit; |
|
|
|
|
|
readonly pin?: boolean; |
|
|
|
|
|
readonly preserveFocus?: boolean; |
|
|
|
|
|
readonly preserveVisibility?: boolean; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
interface FileSelectedEventArgs { |
|
|
|
|
|
readonly uri: Uri; |
|
|
|
|
|
readonly preserveFocus?: boolean; |
|
|
|
|
|
readonly preserveVisibility?: boolean; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
interface EventBusEvent<T extends keyof EventBusEventMap = keyof EventBusEventMap> { |
|
|
|
|
|
name: T; |
|
|
|
|
|
data?: EventBusEventMap[T] | undefined; |
|
|
|
|
|
source?: EventBusSource | undefined; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export type EventBusSource = |
|
|
|
|
|
| 'gitlens.rebase' |
|
|
|
|
|
| `gitlens.${WebviewIds}` |
|
|
|
|
|
| `gitlens.views.${WebviewViewIds}` |
|
|
|
|
|
| `gitlens.views.${ViewsConfigKeys}`; |
|
|
|
|
|
|
|
|
export type EventBusOptions = { |
|
|
export type EventBusOptions = { |
|
|
source?: string; |
|
|
|
|
|
|
|
|
source?: EventBusSource; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
export class EventBus implements Disposable { |
|
|
export class EventBus implements Disposable { |
|
|
private _emitter: EventEmitter<EventBusPackage>; |
|
|
|
|
|
|
|
|
|
|
|
constructor() { |
|
|
|
|
|
this._emitter = new EventEmitter(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private readonly _emitter = new EventEmitter<EventBusEvent>(); |
|
|
private get event() { |
|
|
private get event() { |
|
|
return this._emitter.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, |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
dispose() { |
|
|
|
|
|
this._emitter.dispose(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
fire(name: string, data?: unknown, options?: EventBusOptions) { |
|
|
|
|
|
|
|
|
fire<T extends keyof EventBusEventMap>(name: T, data?: EventBusEventMap[T], options?: EventBusOptions) { |
|
|
this._emitter.fire({ |
|
|
this._emitter.fire({ |
|
|
name: name, |
|
|
name: name, |
|
|
data: data, |
|
|
data: data, |
|
@ -41,7 +61,20 @@ export class EventBus implements Disposable { |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
dispose() { |
|
|
|
|
|
this._emitter?.dispose(); |
|
|
|
|
|
|
|
|
on<T extends keyof EventBusEventMap>( |
|
|
|
|
|
eventName: T, |
|
|
|
|
|
handler: (e: EventBusEvent<T>) => void, |
|
|
|
|
|
thisArgs?: any, |
|
|
|
|
|
disposables?: Disposable[], |
|
|
|
|
|
) { |
|
|
|
|
|
return this.event( |
|
|
|
|
|
// eslint-disable-next-line prefer-arrow-callback
|
|
|
|
|
|
function (e) { |
|
|
|
|
|
if (eventName !== e.name) return; |
|
|
|
|
|
handler.call(thisArgs, e as EventBusEvent<T>); |
|
|
|
|
|
}, |
|
|
|
|
|
thisArgs, |
|
|
|
|
|
disposables, |
|
|
|
|
|
); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |