|
| 1 | +/** |
| 2 | + * WebSocket event types for real-time translation progress |
| 3 | + * Sent from translation server to dev widget clients |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * Base event structure |
| 8 | + */ |
| 9 | +interface BaseEvent { |
| 10 | + type: string; |
| 11 | + timestamp: number; |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * Server connection established |
| 16 | + */ |
| 17 | +export interface ConnectedEvent extends BaseEvent { |
| 18 | + type: "connected"; |
| 19 | + serverUrl: string; |
| 20 | + version: string; |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Single translation started |
| 25 | + */ |
| 26 | +export interface TranslationStartEvent extends BaseEvent { |
| 27 | + type: "translation:start"; |
| 28 | + hash: string; |
| 29 | + locale: string; |
| 30 | + sourceText: string; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Single translation completed successfully |
| 35 | + */ |
| 36 | +export interface TranslationCompleteEvent extends BaseEvent { |
| 37 | + type: "translation:complete"; |
| 38 | + hash: string; |
| 39 | + locale: string; |
| 40 | + translatedText: string; |
| 41 | + duration: number; // ms |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Single translation failed |
| 46 | + */ |
| 47 | +export interface TranslationErrorEvent extends BaseEvent { |
| 48 | + type: "translation:error"; |
| 49 | + hash: string; |
| 50 | + locale: string; |
| 51 | + error: string; |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Batch translation started |
| 56 | + */ |
| 57 | +export interface BatchStartEvent extends BaseEvent { |
| 58 | + type: "batch:start"; |
| 59 | + locale: string; |
| 60 | + total: number; |
| 61 | + hashes: string[]; |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Batch translation progress update |
| 66 | + */ |
| 67 | +export interface BatchProgressEvent extends BaseEvent { |
| 68 | + type: "batch:progress"; |
| 69 | + locale: string; |
| 70 | + completed: number; |
| 71 | + total: number; |
| 72 | + percent: number; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Batch translation completed |
| 77 | + */ |
| 78 | +export interface BatchCompleteEvent extends BaseEvent { |
| 79 | + type: "batch:complete"; |
| 80 | + locale: string; |
| 81 | + total: number; |
| 82 | + successful: number; |
| 83 | + failed: number; |
| 84 | + duration: number; // ms |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Metadata updated (new translatable text discovered) |
| 89 | + */ |
| 90 | +export interface MetadataUpdateEvent extends BaseEvent { |
| 91 | + type: "metadata:update"; |
| 92 | + newEntries: number; |
| 93 | + totalEntries: number; |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Cache updated |
| 98 | + */ |
| 99 | +export interface CacheUpdateEvent extends BaseEvent { |
| 100 | + type: "cache:update"; |
| 101 | + locale: string; |
| 102 | + entriesCount: number; |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Server is now busy processing translations |
| 107 | + */ |
| 108 | +export interface ServerBusyEvent extends BaseEvent { |
| 109 | + type: "server:busy"; |
| 110 | + activeTranslations: number; |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * Server is now idle (no active translations) |
| 115 | + */ |
| 116 | +export interface ServerIdleEvent extends BaseEvent { |
| 117 | + type: "server:idle"; |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Union type of all possible events |
| 122 | + */ |
| 123 | +export type TranslationServerEvent = |
| 124 | + | ConnectedEvent |
| 125 | + | TranslationStartEvent |
| 126 | + | TranslationCompleteEvent |
| 127 | + | TranslationErrorEvent |
| 128 | + | BatchStartEvent |
| 129 | + | BatchProgressEvent |
| 130 | + | BatchCompleteEvent |
| 131 | + | MetadataUpdateEvent |
| 132 | + | CacheUpdateEvent |
| 133 | + | ServerBusyEvent |
| 134 | + | ServerIdleEvent; |
| 135 | + |
| 136 | +type TranslationServerEventByType = { |
| 137 | + [T in TranslationServerEvent as T["type"]]: T; |
| 138 | +}; |
| 139 | + |
| 140 | +/** |
| 141 | + * Helper to create events with timestamp |
| 142 | + */ |
| 143 | +export function createEvent<T extends keyof TranslationServerEventByType>( |
| 144 | + type: T, |
| 145 | + event?: Omit<TranslationServerEventByType[T], "timestamp" | "type">, |
| 146 | +): TranslationServerEventByType[T] { |
| 147 | + return { |
| 148 | + ...event, |
| 149 | + timestamp: Date.now(), |
| 150 | + type, |
| 151 | + } as TranslationServerEventByType[T]; |
| 152 | +} |
0 commit comments