72 lines
2.2 KiB
TypeScript
72 lines
2.2 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import { BehaviorSubject } from 'rxjs';
|
|
|
|
export interface SharedData {
|
|
type: string;
|
|
value: any;
|
|
}
|
|
|
|
@Injectable({ providedIn: 'root' })
|
|
export class SharedStateService {
|
|
private runnerCountSubject = new BehaviorSubject<number>(12);
|
|
runnerCount$ = this.runnerCountSubject.asObservable();
|
|
|
|
private sharedDataSubject = new BehaviorSubject<SharedData>({ type: '', value: null });
|
|
sharedData$ = this.sharedDataSubject.asObservable();
|
|
|
|
private selectedRaceSubject = new BehaviorSubject<number>(1);
|
|
selectedRace$ = this.selectedRaceSubject.asObservable();
|
|
|
|
private channel = new BroadcastChannel('shared-display-channel');
|
|
|
|
constructor() {
|
|
this.channel.onmessage = (event) => {
|
|
console.log('[BroadcastChannel] Received data:', event.data);
|
|
if (event.data?.runnerCount !== undefined) {
|
|
this.runnerCountSubject.next(event.data.runnerCount);
|
|
}
|
|
if (event.data?.type !== undefined && event.data?.value !== undefined) {
|
|
this.sharedDataSubject.next({ type: event.data.type, value: event.data.value });
|
|
if (event.data.type === 'selectedRace') {
|
|
this.selectedRaceSubject.next(event.data.value);
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
setRunnerCount(count: number) {
|
|
console.log('[SharedStateService] Broadcasting runner count:', count);
|
|
this.channel.postMessage({ runnerCount: count });
|
|
this.runnerCountSubject.next(count);
|
|
}
|
|
|
|
updateSharedData(data: SharedData) {
|
|
console.log('[SharedStateService] Broadcasting data:', data);
|
|
this.channel.postMessage(data);
|
|
this.sharedDataSubject.next(data);
|
|
if (data.type === 'selectedRace') {
|
|
this.selectedRaceSubject.next(data.value);
|
|
}
|
|
}
|
|
|
|
getSelectedRace(): number {
|
|
return this.selectedRaceSubject.value;
|
|
}
|
|
|
|
|
|
private salesTotalSubject = new BehaviorSubject<number>(0);
|
|
salesTotal$ = this.salesTotalSubject.asObservable();
|
|
|
|
private receiveTotalSubject = new BehaviorSubject<number>(0);
|
|
receiveTotal$ = this.receiveTotalSubject.asObservable();
|
|
|
|
// Methods to update
|
|
setSalesTotal(total: number) {
|
|
this.salesTotalSubject.next(total);
|
|
}
|
|
|
|
setReceiveTotal(total: number) {
|
|
this.receiveTotalSubject.next(total);
|
|
}
|
|
|
|
} |