-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-service.js
More file actions
91 lines (81 loc) · 2.67 KB
/
server-service.js
File metadata and controls
91 lines (81 loc) · 2.67 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
// Server Service - Abstraction layer for API calls
// Switches between HTTP server and WASM based on USE_WASM flag
const ServerService = {
// Set to true to use WASM, false to use HTTP server
USE_WASM: true,
// WASM module ready state
wasmReady: false,
async init() {
if (this.USE_WASM) {
await this.initWasm();
}
},
async initWasm() {
if (this.wasmReady) return;
try {
const go = new Go();
const result = await WebAssembly.instantiateStreaming(
fetch('server.wasm'),
go.importObject,
);
// Start the Go runtime (don't await - it blocks forever)
go.run(result.instance);
// Wait for functions to be registered
await new Promise((resolve) => {
const check = () => {
if (typeof window.getTickers === 'function' &&
typeof window.getOHLC === 'function' &&
typeof window.getDaily === 'function' &&
typeof window.getDates === 'function' &&
typeof window.getTickerForDate === 'function') {
resolve();
} else {
setTimeout(check, 10);
}
};
check();
});
this.wasmReady = true;
console.log('[ServerService] WASM module loaded');
} catch (e) {
console.error('[ServerService] Failed to load WASM:', e);
throw e;
}
},
async getTickers() {
if (this.USE_WASM) {
await this.initWasm();
// WASM exposes getTickers() globally
const result = window.getTickers();
return JSON.parse(result);
} else {
const response = await fetch('/api/tickers');
if (!response.ok) throw new Error('Failed to fetch tickers');
return response.json();
}
},
async getOHLC(ticker, year, windowParam) {
if (this.USE_WASM) {
await this.initWasm();
// WASM exposes getOHLC(ticker, year, window) globally
console.log('[ServerService] getOHLC called, window.getOHLC:', typeof window.getOHLC);
const result = window.getOHLC(ticker, year, windowParam);
return JSON.parse(result);
} else {
const response = await fetch(`/api/ohlc?ticker=${ticker}&year=${year}&window=${windowParam}`);
if (!response.ok) throw new Error('Failed to fetch OHLC data');
return response.json();
}
},
async getDaily(ticker, year, month) {
if (this.USE_WASM) {
await this.initWasm();
const result = window.getDaily(ticker, year, month);
return JSON.parse(result);
} else {
const response = await fetch(`/api/daily?ticker=${ticker}&year=${year}&month=${month}`);
if (!response.ok) throw new Error('Failed to fetch daily data');
return response.json();
}
},
};