| 23 | } |
| 24 | |
| 25 | export class TelemetryClient { |
| 26 | private queue: PendingTelemetryEvent[] = []; |
| 27 | private sink: EventSink | null = null; |
| 28 | private deviceId: string | null = null; |
| 29 | private sessionId: string | null = null; |
| 30 | private disabled = false; |
| 31 | |
| 32 | setContext(input: TelemetryContextIds): void { |
| 33 | if (input.deviceId !== undefined) this.deviceId = input.deviceId; |
| 34 | if (input.sessionId !== undefined) this.sessionId = input.sessionId; |
| 35 | } |
| 36 | |
| 37 | withContext(input: TelemetryContextIds): TelemetryClient { |
| 38 | return new ScopedTelemetryClient(this, input); |
| 39 | } |
| 40 | |
| 41 | attachSink(sink: EventSink): void { |
| 42 | if (this.sink !== null && this.sink !== sink) { |
| 43 | this.sink.stopPeriodicFlush(); |
| 44 | this.sink.flushSync(); |
| 45 | } |
| 46 | this.sink = sink; |
| 47 | for (const event of this.queue) { |
| 48 | const record = toTelemetryEvent(event); |
| 49 | if (record.device_id === null && event.contextOverrides?.deviceId !== true) { |
| 50 | record.device_id = this.deviceId; |
| 51 | } |
| 52 | if (record.session_id === null && event.contextOverrides?.sessionId !== true) { |
| 53 | record.session_id = this.sessionId; |
| 54 | } |
| 55 | sink.accept(record); |
| 56 | } |
| 57 | this.queue = []; |
| 58 | } |
| 59 | |
| 60 | disable(): void { |
| 61 | this.disabled = true; |
| 62 | this.queue = []; |
| 63 | if (this.sink !== null) { |
| 64 | this.sink.stopPeriodicFlush(); |
| 65 | this.sink.clearBuffer(); |
| 66 | this.sink = null; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | enable(): void { |
| 71 | this.disabled = false; |
| 72 | } |
| 73 | |
| 74 | track(event: string, properties: TelemetryProperties = {}): void { |
| 75 | this.trackWithContext(event, properties, {}); |
| 76 | } |
| 77 | |
| 78 | trackWithContext( |
| 79 | event: string, |
| 80 | properties: TelemetryProperties = {}, |
| 81 | context: TelemetryContextIds, |
| 82 | ): void { |
nothing calls this directly
no outgoing calls
no test coverage detected