(ignoreAlreadyDisconnected: boolean)
| 186 | } |
| 187 | |
| 188 | disconnect(ignoreAlreadyDisconnected: boolean) { |
| 189 | if (!this.target) { |
| 190 | if (ignoreAlreadyDisconnected) return; |
| 191 | console.warn("Attempted to disconnect on a disconnected Target."); |
| 192 | // 重复 disconnect() 不应该屏蔽错误 |
| 193 | throw new Error("Attempted to disconnect on a disconnected Target."); |
| 194 | } |
| 195 | this.isSelfDisconnected = true; |
| 196 | const body: WindowMessageBody<TMessage> = { |
| 197 | messageId: this.messageId, |
| 198 | type: "disconnect", |
| 199 | data: null, |
| 200 | }; |
| 201 | this.target.postMessage(body); |
| 202 | // Note: .disconnect() will NOT automatically trigger the 'cleanup' listener |
| 203 | listenerMgr.emit(`cleanup:${this.listenerId}`); |
| 204 | } |
| 205 | |
| 206 | onDisconnect(callback: (isSelfDisconnected: boolean) => void) { |
| 207 | if (!this.target) { |
nothing calls this directly
no test coverage detected