// Ticker History Service
class TickerHistoryService {
constructor() {
this.history = [];
this.index = -1;
this.init();
}
async init() {
this.io = await IODesktop();
await this.io.contexts.subscribe('ticker-context', (data) => {
if (data?.ticker && !data.fromHistory) {
this.addToHistory(data.ticker);
}
});
await this.io.interop.register('TickerHistory.Back', () => this.goBack());
await this.io.interop.register('TickerHistory.Forward', () => this.goForward());
await this.io.interop.register('TickerHistory.GetState', () => ({
ticker: this.current(),
canGoBack: this.index > 0,
canGoForward: this.index < this.history.length - 1
}));
}
addToHistory(ticker) {
const t = ticker.toUpperCase();
if (this.current() === t) return;
if (this.index < this.history.length - 1) {
this.history = this.history.slice(0, this.index + 1);
}
this.history.push(t);
this.index = this.history.length - 1;
if (this.history.length > 50) {
this.history.shift();
this.index--;
}
}
async goBack() {
if (this.index <= 0) return { success: false };
this.index--;
const ticker = this.history[this.index];
await this.io.contexts.update('ticker-context', {
ticker,
fromHistory: true
});
return { success: true, ticker };
}
async goForward() {
if (this.index >= this.history.length - 1) return { success: false };
this.index++;
const ticker = this.history[this.index];
await this.io.contexts.update('ticker-context', {
ticker,
fromHistory: true
});
return { success: true, ticker };
}
current() {
return this.index >= 0 ? this.history[this.index] : null;
}
}
// Client Helper
class TickerClient {
constructor() {
this.init();
}
async init() {
this.io = await IODesktop();
await this.io.contexts.subscribe('ticker-context', (data) => {
if (data?.ticker && this.onTicker) {
this.onTicker(data.ticker);
}
});
}
async setTicker(ticker) {
await this.io.contexts.update('ticker-context', {
ticker: ticker.toUpperCase(),
fromHistory: false
});
}
async goBack() {
const result = await this.io.interop.invoke('TickerHistory.Back');
return result.returned;
}
async goForward() {
const result = await this.io.interop.invoke('TickerHistory.Forward');
return result.returned;
}
onTickerChange(callback) {
this.onTicker = callback;
}
}
// Auto-initialize based on URL
if (location.search.includes('service')) {
new TickerHistoryService();
} else {
window.ticker = new TickerClient();
}