| 24 | export const RealtimeClient = once( |
| 25 | () => |
| 26 | class extends ClientSocket() { |
| 27 | private _nextCommandId = 0; |
| 28 | |
| 29 | private readonly _logger = mainLogger.sub('RealtimeClient'); |
| 30 | |
| 31 | private readonly _hsetBuffer = new Map<string, any>(); |
| 32 | |
| 33 | readonly commandBuffer: RealtimeCommand[] = []; |
| 34 | private readonly _responsePromises = new Map<number, Resolvable<any>>(); |
| 35 | |
| 36 | readonly subscriptions: Record<string, Set<RealtimeContext>> = {}; |
| 37 | readonly pending = reactive(new Map<string, Resolvable<any>>()); |
| 38 | readonly changed = new Set<string>(); |
| 39 | |
| 40 | get loading() { |
| 41 | return this.pending.size > 0; |
| 42 | } |
| 43 | |
| 44 | readonly values: Record<string, any> = reactive({}); |
| 45 | |
| 46 | readonly globalCtx = new (RealtimeContext())(); |
| 47 | |
| 48 | private _isFirstConnection = true; |
| 49 | |
| 50 | constructor() { |
| 51 | super(process.env.REALTIME_SERVER_URL); |
| 52 | } |
| 53 | |
| 54 | connect() { |
| 55 | super.connect(); |
| 56 | |
| 57 | this.socket?.addEventListener('open', () => { |
| 58 | if (this._isFirstConnection) { |
| 59 | this._logger.info('First connection'); |
| 60 | this._isFirstConnection = false; |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | this._logger.info('Reconnecting'); |
| 65 | |
| 66 | for (const fullKey of Object.keys(this.subscriptions)) { |
| 67 | const [key, field] = splitStr(fullKey, '>', 2); |
| 68 | const [prefix, suffix] = splitStr(key, ':', 2); |
| 69 | |
| 70 | this.commandBuffer.push({ |
| 71 | type: RealtimeCommandType.SUBSCRIBE, |
| 72 | args: [prefix, suffix, field], |
| 73 | }); |
| 74 | } |
| 75 | |
| 76 | if (this.commandBuffer.length > 0) { |
| 77 | setTimeout(this.flushCommandBuffer); |
| 78 | } |
| 79 | }); |
| 80 | |
| 81 | this.socket?.addEventListener('message', (event) => { |
| 82 | this._handleMessage(new Uint8Array(event.data)); |
| 83 | }); |
nothing calls this directly
no test coverage detected