|
| 1 | +/// <reference lib="webworker" /> |
| 2 | +/* eslint-disable no-console */ |
| 3 | + |
| 4 | +/** |
| 5 | + * @internal |
| 6 | + * SharedWorker for managing subscriptions across SDK apps |
| 7 | + */ |
| 8 | + |
| 9 | +import {sharedWorkerStore} from '../sharedWorkerStore/sharedWorkerStore' |
| 10 | +import {type SubscriptionRequest} from '../sharedWorkerStore/types' |
| 11 | + |
| 12 | +declare const self: SharedWorkerGlobalScope |
| 13 | + |
| 14 | +console.log('[SharedWorker] Worker script loaded') |
| 15 | + |
| 16 | +// Handle new connections |
| 17 | +self.onconnect = (event: MessageEvent) => { |
| 18 | + const port = event.ports[0] |
| 19 | + |
| 20 | + console.log('[SharedWorker] New connection established') |
| 21 | + |
| 22 | + // Set up message handling for this port |
| 23 | + port.onmessage = async (e: MessageEvent) => { |
| 24 | + const {type, data} = e.data |
| 25 | + |
| 26 | + console.log('[SharedWorker] Received message:', type, data) |
| 27 | + |
| 28 | + try { |
| 29 | + switch (type) { |
| 30 | + case 'REGISTER_SUBSCRIPTION': |
| 31 | + handleRegisterSubscription(data, port) |
| 32 | + break |
| 33 | + case 'UNREGISTER_SUBSCRIPTION': |
| 34 | + handleUnregisterSubscription(data.subscriptionId, port) |
| 35 | + break |
| 36 | + case 'GET_SUBSCRIPTION_COUNT': |
| 37 | + handleGetSubscriptionCount(port) |
| 38 | + break |
| 39 | + case 'GET_ALL_SUBSCRIPTIONS': |
| 40 | + handleGetAllSubscriptions(port) |
| 41 | + break |
| 42 | + default: |
| 43 | + console.warn('[SharedWorker] Unknown message type:', type) |
| 44 | + port.postMessage({ |
| 45 | + type: 'ERROR', |
| 46 | + data: {error: `Unknown message type: ${type}`}, |
| 47 | + }) |
| 48 | + } |
| 49 | + } catch (error) { |
| 50 | + console.error('[SharedWorker] Error handling message:', error) |
| 51 | + port.postMessage({ |
| 52 | + type: 'ERROR', |
| 53 | + data: {error: (error as Error).message}, |
| 54 | + }) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // Start the port |
| 59 | + port.start() |
| 60 | + console.log('[SharedWorker] Port started, sending welcome message') |
| 61 | + port.postMessage({type: 'welcome'}) |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * @internal |
| 66 | + * Handle the registration of a subscription |
| 67 | + * @param subscription - The subscription to register |
| 68 | + * @param port - The port to send the response to |
| 69 | + */ |
| 70 | +function handleRegisterSubscription(subscription: SubscriptionRequest, port: MessagePort): void { |
| 71 | + try { |
| 72 | + sharedWorkerStore.getState().registerSubscription(subscription) |
| 73 | + |
| 74 | + // Send confirmation back to the client |
| 75 | + port.postMessage({ |
| 76 | + type: 'SUBSCRIPTION_REGISTERED', |
| 77 | + data: {subscriptionId: subscription.subscriptionId}, |
| 78 | + }) |
| 79 | + |
| 80 | + console.log('[SharedWorker] Registered subscription:', subscription.subscriptionId) |
| 81 | + } catch (error) { |
| 82 | + console.error('[SharedWorker] Failed to register subscription:', error) |
| 83 | + |
| 84 | + // Send error back to the client |
| 85 | + port.postMessage({ |
| 86 | + type: 'SUBSCRIPTION_ERROR', |
| 87 | + data: {error: (error as Error).message, subscriptionId: subscription.subscriptionId}, |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * @internal |
| 94 | + * Handle the unregistration of a subscription |
| 95 | + * @param subscriptionId - The ID of the subscription to unregister |
| 96 | + * @param port - The port to send the response to |
| 97 | + */ |
| 98 | +function handleUnregisterSubscription(subscriptionId: string, port: MessagePort): void { |
| 99 | + try { |
| 100 | + sharedWorkerStore.getState().unregisterSubscription(subscriptionId) |
| 101 | + |
| 102 | + // Send confirmation back to the client |
| 103 | + port.postMessage({ |
| 104 | + type: 'SUBSCRIPTION_UNREGISTERED', |
| 105 | + data: {subscriptionId}, |
| 106 | + }) |
| 107 | + |
| 108 | + console.log('[SharedWorker] Unregistered subscription:', subscriptionId) |
| 109 | + } catch (error) { |
| 110 | + console.error('[SharedWorker] Failed to unregister subscription:', error) |
| 111 | + |
| 112 | + // Send error back to the client |
| 113 | + port.postMessage({ |
| 114 | + type: 'SUBSCRIPTION_ERROR', |
| 115 | + data: {error: (error as Error).message, subscriptionId}, |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +function handleGetSubscriptionCount(port: MessagePort): void { |
| 121 | + try { |
| 122 | + const count = sharedWorkerStore.getState().getSubscriptionCount() |
| 123 | + |
| 124 | + port.postMessage({ |
| 125 | + type: 'SUBSCRIPTION_COUNT', |
| 126 | + data: {count}, |
| 127 | + }) |
| 128 | + } catch (error) { |
| 129 | + console.error('[SharedWorker] Failed to get subscription count:', error) |
| 130 | + |
| 131 | + port.postMessage({ |
| 132 | + type: 'SUBSCRIPTION_ERROR', |
| 133 | + data: {error: (error as Error).message}, |
| 134 | + }) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +function handleGetAllSubscriptions(port: MessagePort): void { |
| 139 | + try { |
| 140 | + const subscriptions = sharedWorkerStore.getState().getAllSubscriptions() |
| 141 | + |
| 142 | + port.postMessage({ |
| 143 | + type: 'ALL_SUBSCRIPTIONS', |
| 144 | + data: {subscriptions}, |
| 145 | + }) |
| 146 | + } catch (error) { |
| 147 | + console.error('[SharedWorker] Failed to get all subscriptions:', error) |
| 148 | + |
| 149 | + port.postMessage({ |
| 150 | + type: 'SUBSCRIPTION_ERROR', |
| 151 | + data: {error: (error as Error).message}, |
| 152 | + }) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +// Export for testing/development (this won't be used in the actual SharedWorker) |
| 157 | +/** @internal */ |
| 158 | +export {handleRegisterSubscription, handleUnregisterSubscription} |
0 commit comments