Files
orca/src/shared/git-status-read-lease-owner.ts
T
Jinjing debf4affe7 Display total lines of code change in branch header (#12771)
* Add branch line total chip to source control header

Display the total lines added and removed across a branch from its fork point, measured via `git diff <mergeBase>`. Only computed when the chip is visible (request gate on merge base OID), with 500ms soft deadline to protect status latency and 15s hard timeout. Deduplicated across concurrent pollers and cached alongside line stats. Omitted on failure — always shows exact or nothing, never a partial estimate. Updates throughout the stack: native git status, relay, renderer store/API, and UI components.

* Pin branch line total to app locale

Format line counts using the app's configured locale instead of the system
locale, ensuring consistent cross-platform display and test reliability.

* test: wait for coalescer joins instead of fixed sleep

Hold the diff until the second status pass actually takes the
branch-total coalescer lease instead of using a fixed 400ms sleep.
Fixes timing-dependent flakiness on slow machines.
2026-08-05 14:46:00 -07:00

102 lines
2.5 KiB
TypeScript

type StatusReadEntry<T> = {
controller: AbortController
promise: Promise<T>
liveLeases: number
settled: boolean
}
function getAbortReason(signal: AbortSignal): unknown {
try {
signal.throwIfAborted()
} catch (error) {
return error
}
return new DOMException('This operation was aborted', 'AbortError')
}
export class GitStatusReadLeaseOwner<T> {
private readonly entries = new Map<string, StatusReadEntry<T>>()
lease(
key: string,
signal: AbortSignal | undefined,
load: (sharedSignal: AbortSignal) => Promise<T>
): Promise<T> {
if (signal?.aborted) {
return Promise.reject(getAbortReason(signal))
}
let entry = this.entries.get(key)
if (!entry) {
const controller = new AbortController()
const promise = load(controller.signal)
const createdEntry = { controller, promise, liveLeases: 0, settled: false }
entry = createdEntry
this.entries.set(key, createdEntry)
void promise.then(
() => this.settle(key, createdEntry),
() => this.settle(key, createdEntry)
)
}
entry.liveLeases += 1
return this.createLease(key, entry, signal)
}
invalidate(): void {
this.entries.clear()
}
private createLease(
key: string,
entry: StatusReadEntry<T>,
signal: AbortSignal | undefined
): Promise<T> {
return new Promise<T>((resolve, reject) => {
let active = true
const release = (abortReason?: unknown): boolean => {
if (!active) {
return false
}
active = false
signal?.removeEventListener('abort', onAbort)
entry.liveLeases -= 1
if (abortReason !== undefined && entry.liveLeases === 0 && !entry.settled) {
if (this.entries.get(key) === entry) {
this.entries.delete(key)
}
entry.controller.abort(abortReason)
}
return true
}
const onAbort = (): void => {
const reason = getAbortReason(signal!)
if (release(reason)) {
reject(reason)
}
}
signal?.addEventListener('abort', onAbort, { once: true })
void entry.promise.then(
(value) => {
if (release()) {
resolve(value)
}
},
(error: unknown) => {
if (release()) {
reject(error)
}
}
)
})
}
private settle(key: string, entry: StatusReadEntry<T>): void {
entry.settled = true
if (this.entries.get(key) === entry) {
this.entries.delete(key)
}
}
}