-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathsetup.ts
More file actions
96 lines (82 loc) · 2.84 KB
/
Copy pathsetup.ts
File metadata and controls
96 lines (82 loc) · 2.84 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { OverlayManager } from '../../overlayManager';
import { ipcMain } from 'electron';
import type { IrSdkSourceBridge } from '@irdashies/types';
import logger from '../../logger';
import {
createSessionLifecycle,
type SessionLifecycle,
} from '../../sessionLifecycle';
import type { ChannelBus } from '../channelBridge';
let isDemoMode = false;
let currentBridge: IrSdkSourceBridge | undefined;
const onBridgeChangedCallbacks = new Set<(bridge: IrSdkSourceBridge) => void>();
// Singleton lifecycle — created once; survives bridge restarts so subscribers
// registered before a demo-mode toggle are preserved.
let sessionLifecycle: SessionLifecycle | undefined;
export function getSessionLifecycle(): SessionLifecycle {
if (!sessionLifecycle) {
sessionLifecycle = createSessionLifecycle();
}
return sessionLifecycle;
}
export function getCurrentBridge(): IrSdkSourceBridge | undefined {
return currentBridge;
}
export function getIsDemoMode(): boolean {
return isDemoMode;
}
export function onBridgeChanged(callback: (bridge: IrSdkSourceBridge) => void) {
onBridgeChangedCallbacks.add(callback);
return () => onBridgeChangedCallbacks.delete(callback);
}
export async function iRacingSDKSetup(
overlayManager: OverlayManager,
channelBus?: ChannelBus
) {
ipcMain.on('toggleDemoMode', async (_, value: boolean) => {
isDemoMode = value;
if (currentBridge) {
currentBridge.stop();
currentBridge = undefined;
}
// Flip the UI immediately; the data source swaps underneath. Otherwise the
// mode change is gated behind the full bridge teardown/rebuild below
// (dynamic import, native SDK load, sdk.ready()).
overlayManager.publishMessage('demoModeChanged', value);
const { notifyDemoModeChanged } =
await import('../dashboard/dashboardBridge');
notifyDemoModeChanged(value);
await setupBridge(overlayManager, channelBus);
});
await setupBridge(overlayManager, channelBus);
}
async function setupBridge(
overlayManager: OverlayManager,
channelBus?: ChannelBus
) {
try {
if (currentBridge) {
currentBridge.stop();
currentBridge = undefined;
}
const isTapeReplay = Boolean(process.env.IRDASHIES_TELEMETRY_REPLAY);
const module =
isDemoMode || (process.platform !== 'win32' && !isTapeReplay)
? await import('./mock-data/mockSdkBridge')
: await import('./iracingSdkBridge');
const { publishIRacingSDKEvents } = module;
const lifecycle = isDemoMode ? undefined : getSessionLifecycle();
currentBridge = await publishIRacingSDKEvents(
overlayManager,
lifecycle,
channelBus
);
if (onBridgeChangedCallbacks.size > 0 && currentBridge) {
const bridge = currentBridge;
onBridgeChangedCallbacks.forEach((cb) => cb(bridge));
}
} catch (err) {
logger.error('Failed to load bridge');
throw err;
}
}