| 11 | } from '../types'; |
| 12 | |
| 13 | export class GitEventService { |
| 14 | private static instance: GitEventService; |
| 15 | private readonly eventPrefix = 'git:'; |
| 16 | |
| 17 | private constructor() {} |
| 18 | |
| 19 | public static getInstance(): GitEventService { |
| 20 | if (!GitEventService.instance) { |
| 21 | GitEventService.instance = new GitEventService(); |
| 22 | } |
| 23 | return GitEventService.instance; |
| 24 | } |
| 25 | |
| 26 | on<T extends GitEventType>( |
| 27 | eventType: T, |
| 28 | listener: GitEventListener<T>, |
| 29 | options?: GitEventSubscriptionOptions |
| 30 | ): () => void { |
| 31 | const eventName = this.getEventName(eventType); |
| 32 | |
| 33 | const wrappedListener = (data: Extract<GitEvent, { type: T }>['data']) => { |
| 34 | if (options?.filter && !options.filter(data)) { |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | if (options?.repositoryPath && 'repositoryPath' in data) { |
| 39 | if (data.repositoryPath !== options.repositoryPath) { |
| 40 | return; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | const event = { type: eventType, data } as Extract<GitEvent, { type: T }>; |
| 45 | listener(event); |
| 46 | }; |
| 47 | |
| 48 | if (options?.once) { |
| 49 | return globalEventBus.once(eventName, wrappedListener as any); |
| 50 | } |
| 51 | |
| 52 | return globalEventBus.on(eventName, wrappedListener as any); |
| 53 | } |
| 54 | |
| 55 | once<T extends GitEventType>( |
| 56 | eventType: T, |
| 57 | listener: GitEventListener<T> |
| 58 | ): () => void { |
| 59 | return this.on(eventType, listener, { once: true }); |
| 60 | } |
| 61 | |
| 62 | off<T extends GitEventType>( |
| 63 | eventType: T, |
| 64 | listener: GitEventListener<T> |
| 65 | ): void { |
| 66 | const eventName = this.getEventName(eventType); |
| 67 | globalEventBus.off(eventName, listener as any); |
| 68 | } |
| 69 | |
| 70 | emit<T extends GitEventType>( |
nothing calls this directly
no outgoing calls
no test coverage detected