* Subscribe to repository state changes. * @returns Unsubscribe function.
(
repositoryPath: string,
callback: GitStateSubscriber,
options: SubscribeOptions = {}
)
| 96 | * @returns Unsubscribe function. |
| 97 | */ |
| 98 | subscribe( |
| 99 | repositoryPath: string, |
| 100 | callback: GitStateSubscriber, |
| 101 | options: SubscribeOptions = {} |
| 102 | ): () => void { |
| 103 | const normalizedPath = this.normalizePath(repositoryPath); |
| 104 | |
| 105 | if (!this.subscribers.has(normalizedPath)) { |
| 106 | this.subscribers.set(normalizedPath, new Set()); |
| 107 | } |
| 108 | |
| 109 | const entry: SubscriberEntry = { callback, options }; |
| 110 | this.subscribers.get(normalizedPath)!.add(entry); |
| 111 | |
| 112 | if (options.immediate !== false) { |
| 113 | const currentState = this.states.get(normalizedPath); |
| 114 | if (currentState) { |
| 115 | callback(currentState, null, ['basic', 'status', 'detailed']); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return () => { |
| 120 | this.subscribers.get(normalizedPath)?.delete(entry); |
| 121 | }; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Get current state synchronously (cached). |