| 968 | } |
| 969 | |
| 970 | function generateEventMethod(evt) { |
| 971 | const { onMethodName, methodStr, paramsTypeName } = evt |
| 972 | const cbType = paramsTypeName ? `(params: ${paramsTypeName}) => void` : `(params: unknown) => void` |
| 973 | |
| 974 | const lines = [] |
| 975 | lines.push(` async ${onMethodName}(callback: ${cbType}): Promise<void> {`) |
| 976 | lines.push(` await this.bidi.subscribe('${methodStr}')`) |
| 977 | // bidi/index.js emits BiDi events by method name through its single shared |
| 978 | // message dispatcher (which already handles JSON parsing and closed-state |
| 979 | // guards). Using bidi.on() here avoids attaching a new ws.on('message', ...) |
| 980 | // listener on every subscription call, preventing listener accumulation and |
| 981 | // MaxListeners warnings. |
| 982 | lines.push(` this.bidi.on('${methodStr}', (params: unknown) => {`) |
| 983 | lines.push(` callback(${paramsTypeName ? `params as ${paramsTypeName}` : 'params'})`) |
| 984 | lines.push(` })`) |
| 985 | lines.push(` }`) |
| 986 | return lines.join('\n') |
| 987 | } |
| 988 | |
| 989 | // ============================================================ |
| 990 | // Entry point |