| 75 | } |
| 76 | |
| 77 | public async handleStatusUpdates(client: Client) { |
| 78 | for await (const status of client.status()) { |
| 79 | const data = |
| 80 | status.data && isObject(status.data) |
| 81 | ? JSON.stringify(status.data) |
| 82 | : status.data; |
| 83 | |
| 84 | switch (status.type) { |
| 85 | case 'error': |
| 86 | this.logger.error( |
| 87 | `NatsError: type: "${status.type}", data: "${data}".`, |
| 88 | ); |
| 89 | break; |
| 90 | |
| 91 | case 'disconnect': |
| 92 | this.connectionPromise = Promise.reject( |
| 93 | 'Error: Connection lost. Trying to reconnect...', |
| 94 | ); |
| 95 | // Prevent unhandled promise rejection |
| 96 | this.connectionPromise.catch(() => {}); |
| 97 | |
| 98 | this.logger.error( |
| 99 | `NatsError: type: "${status.type}", data: "${data}".`, |
| 100 | ); |
| 101 | |
| 102 | this._status$.next(NatsStatus.DISCONNECTED); |
| 103 | this.statusEventEmitter.emit( |
| 104 | NatsEventsMap.DISCONNECT, |
| 105 | status.data as string, |
| 106 | ); |
| 107 | break; |
| 108 | |
| 109 | case 'reconnecting': |
| 110 | this._status$.next(NatsStatus.RECONNECTING); |
| 111 | break; |
| 112 | |
| 113 | case 'reconnect': |
| 114 | this.connectionPromise = Promise.resolve(client); |
| 115 | this.logger.log( |
| 116 | `NatsStatus: type: "${status.type}", data: "${data}".`, |
| 117 | ); |
| 118 | |
| 119 | this._status$.next(NatsStatus.CONNECTED); |
| 120 | this.statusEventEmitter.emit( |
| 121 | NatsEventsMap.RECONNECT, |
| 122 | status.data as string, |
| 123 | ); |
| 124 | break; |
| 125 | |
| 126 | case 'pingTimer': |
| 127 | if (this.options.debug) { |
| 128 | this.logger.debug( |
| 129 | `NatsStatus: type: "${status.type}", data: "${data}".`, |
| 130 | ); |
| 131 | } |
| 132 | break; |
| 133 | |
| 134 | case 'update': |