All files / packages/coordination/src session.ts

100% Statements 12/12
100% Branches 0/0
100% Functions 12/12
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64                                        106x       118x       13x       155x       140x       18x       30x       106x       63x       24x       20x      
import type { Driver } from '@ydbjs/core'
 
import { Election } from './election.js'
import { Mutex } from './mutex.js'
import {
	type CreateSessionOptions,
	type SessionRuntime,
	type SessionStatus,
	createRuntime,
} from './runtime/session-runtime.js'
import { Semaphore } from './semaphore.js'
 
export type CoordinationSessionStatus = SessionStatus
 
export type { CreateSessionOptions }
 
export class CoordinationSession implements AsyncDisposable {
	#runtime: SessionRuntime
 
	constructor(driver: Driver, options: CreateSessionOptions, signal?: AbortSignal) {
		this.#runtime = createRuntime(driver, options, signal)
	}
 
	get sessionId(): bigint | null {
		return this.#runtime.sessionId
	}
 
	get status(): CoordinationSessionStatus {
		return this.#runtime.status
	}
 
	get signal(): AbortSignal {
		return this.#runtime.signal
	}
 
	semaphore(name: string): Semaphore {
		return new Semaphore(name, this.#runtime.transport, this.signal)
	}
 
	mutex(name: string): Mutex {
		return new Mutex(this.semaphore(name))
	}
 
	election(name: string): Election {
		return new Election(this.semaphore(name), () => this.#runtime.sessionId)
	}
 
	waitReady(signal?: AbortSignal): Promise<void> {
		return this.#runtime.transport.waitReady(signal)
	}
 
	close(signal?: AbortSignal): Promise<void> {
		return this.#runtime.close(signal)
	}
 
	destroy(reason?: unknown): void {
		this.#runtime.destroy(reason)
	}
 
	async [Symbol.asyncDispose](): Promise<void> {
		await this.#runtime.close()
	}
}