File size: 8,799 Bytes
9d2d895 | 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | /**
* Worker Manager for heavy computational tasks.
* Provides typed async interface to the analysis Web Worker.
*/
import type { NewsItem, ClusteredEvent, MarketData } from '@/types';
import type { PredictionMarket } from '@/services/prediction';
import type { CorrelationSignal } from './correlation';
import { SOURCE_TIERS, SOURCE_TYPES, type SourceType } from '@/config/feeds';
// Import worker using Vite's worker syntax
import AnalysisWorker from '@/workers/analysis.worker?worker';
interface PendingRequest<T> {
resolve: (value: T) => void;
reject: (error: Error) => void;
timeout: ReturnType<typeof setTimeout>;
}
interface ClusterResult {
type: 'cluster-result';
id: string;
clusters: ClusteredEvent[];
}
interface CorrelationResult {
type: 'correlation-result';
id: string;
signals: CorrelationSignal[];
}
type WorkerResult = ClusterResult | CorrelationResult | { type: 'ready' };
class AnalysisWorkerManager {
private worker: Worker | null = null;
private pendingRequests: Map<string, PendingRequest<unknown>> = new Map();
private requestIdCounter = 0;
private isReady = false;
// Set true when worker construction throws synchronously (e.g. legacy
// browsers without module-worker support). Latches so we don't re-attempt
// construction on every call, and gates callers into graceful degradation.
private workerUnavailable = false;
private readyPromise: Promise<void> | null = null;
private readyResolve: (() => void) | null = null;
private readyReject: ((error: Error) => void) | null = null;
private readyTimeout: ReturnType<typeof setTimeout> | null = null;
private static readonly READY_TIMEOUT_MS = 10000; // 10 seconds to become ready
/**
* Initialize the worker. Called lazily on first use.
*/
private initWorker(): void {
if (this.worker || this.workerUnavailable) return;
this.readyPromise = new Promise((resolve, reject) => {
this.readyResolve = resolve;
this.readyReject = reject;
});
// Set ready timeout - reject if worker doesn't become ready in time
this.readyTimeout = setTimeout(() => {
if (!this.isReady) {
const error = new Error('Worker failed to become ready within timeout');
console.error('[AnalysisWorker]', error.message);
this.readyReject?.(error);
this.cleanup();
}
}, AnalysisWorkerManager.READY_TIMEOUT_MS);
try {
this.worker = new AnalysisWorker();
} catch (error) {
// Legacy browsers without module-worker support (e.g. old smart-TV
// browsers) throw synchronously here. Do NOT reject readyPromise:
// cleanup() nulls readyPromise in this same synchronous tick, so
// waitForReady's `await this.readyPromise` would await `null` and the
// rejection would be orphaned into an unhandled promise rejection
// (WORLDMONITOR-V2/V6). Instead latch `workerUnavailable`, resolve the
// ready gate, and let clusterNews/analyzeCorrelations degrade to an
// empty result. Mirrors ml-worker's graceful `resolve(false)` path.
console.error('[AnalysisWorker] Failed to create worker:', error);
this.workerUnavailable = true;
this.readyResolve?.();
this.cleanup();
return;
}
this.worker.onmessage = (event: MessageEvent<WorkerResult>) => {
const data = event.data;
if (data.type === 'ready') {
this.isReady = true;
if (this.readyTimeout) {
clearTimeout(this.readyTimeout);
this.readyTimeout = null;
}
this.readyResolve?.();
return;
}
if ('id' in data) {
const pending = this.pendingRequests.get(data.id);
if (pending) {
clearTimeout(pending.timeout);
this.pendingRequests.delete(data.id);
if (data.type === 'cluster-result') {
// Deserialize dates
const clusters = data.clusters.map(cluster => ({
...cluster,
firstSeen: new Date(cluster.firstSeen),
lastUpdated: new Date(cluster.lastUpdated),
allItems: cluster.allItems.map(item => ({
...item,
pubDate: new Date(item.pubDate),
})),
}));
pending.resolve(clusters);
} else if (data.type === 'correlation-result') {
// Deserialize dates
const signals = data.signals.map(signal => ({
...signal,
timestamp: new Date(signal.timestamp),
}));
pending.resolve(signals);
}
}
}
};
this.worker.onerror = (error) => {
console.error('[AnalysisWorker] Error:', error);
// If not ready yet, reject the ready promise
if (!this.isReady) {
this.readyReject?.(new Error(`Worker failed to initialize: ${error.message}`));
this.cleanup();
return;
}
// Reject all pending requests
for (const [id, pending] of this.pendingRequests) {
clearTimeout(pending.timeout);
pending.reject(new Error(`Worker error: ${error.message}`));
this.pendingRequests.delete(id);
}
};
}
/**
* Cleanup worker state (for re-initialization)
*/
private cleanup(): void {
if (this.readyTimeout) {
clearTimeout(this.readyTimeout);
this.readyTimeout = null;
}
if (this.worker) {
this.worker.terminate();
this.worker = null;
}
this.isReady = false;
this.readyPromise = null;
this.readyResolve = null;
this.readyReject = null;
}
/**
* Wait for worker to be ready
*/
private async waitForReady(): Promise<void> {
this.initWorker();
if (this.isReady) return;
await this.readyPromise;
}
/**
* Generate unique request ID
*/
private generateId(): string {
return `req-${++this.requestIdCounter}-${Date.now()}`;
}
private request<T>(
type: 'cluster' | 'correlation',
payload: Record<string, unknown>,
timeoutMs: number,
timeoutMessage: string
): Promise<T> {
return new Promise((resolve, reject) => {
const id = this.generateId();
const timeout = setTimeout(() => {
this.pendingRequests.delete(id);
reject(new Error(timeoutMessage));
}, timeoutMs);
this.pendingRequests.set(id, {
resolve: resolve as (value: unknown) => void,
reject,
timeout,
});
this.worker!.postMessage({
type,
id,
...payload,
});
});
}
/**
* Cluster news articles using Web Worker.
* Runs O(n²) Jaccard similarity off the main thread.
*/
async clusterNews(items: NewsItem[]): Promise<ClusteredEvent[]> {
await this.waitForReady();
// Worker never came up (e.g. browser lacks module-worker support) — skip
// clustering rather than posting to a null worker. request() below derefs
// this.worker!, so guarding here keeps the degradation graceful.
if (!this.isReady) return [];
return this.request<ClusteredEvent[]>(
'cluster',
{ items, sourceTiers: SOURCE_TIERS },
30000,
'Clustering request timed out'
);
}
/**
* Run correlation analysis using Web Worker.
* Detects signal patterns across news, markets, and predictions.
*/
async analyzeCorrelations(
clusters: ClusteredEvent[],
predictions: PredictionMarket[],
markets: MarketData[]
): Promise<CorrelationSignal[]> {
await this.waitForReady();
if (!this.isReady) return [];
return this.request<CorrelationSignal[]>(
'correlation',
{
clusters,
predictions,
markets,
sourceTypes: SOURCE_TYPES as Record<string, SourceType>,
},
10000,
'Correlation analysis request timed out'
);
}
/**
* Reset worker state (useful for testing)
*/
reset(): void {
// Reject all pending requests - reset worker won't answer old queries
for (const pending of this.pendingRequests.values()) {
clearTimeout(pending.timeout);
pending.reject(new Error('Worker reset'));
}
this.pendingRequests.clear();
if (this.worker) {
this.worker.postMessage({ type: 'reset' });
}
}
/**
* Terminate worker (cleanup)
*/
terminate(): void {
// Reject all pending requests
for (const [id, pending] of this.pendingRequests) {
clearTimeout(pending.timeout);
pending.reject(new Error('Worker terminated'));
this.pendingRequests.delete(id);
}
this.cleanup();
}
/**
* Check if worker is available and ready
*/
get ready(): boolean {
return this.isReady;
}
}
// Singleton instance
export const analysisWorker = new AnalysisWorkerManager();
// Export types for consumers
export type { CorrelationSignal };
|