| 3 | import { sleep } from "@App/pkg/utils/utils"; |
| 4 | |
| 5 | export class MockMessageConnect implements MessageConnect { |
| 6 | EE: EventEmitter<string, any> | null; |
| 7 | constructor(EE: EventEmitter<string, any>) { |
| 8 | this.EE = EE; |
| 9 | } |
| 10 | |
| 11 | onMessage(callback: (data: TMessage) => void): void { |
| 12 | if (!this.EE) { |
| 13 | console.error("onMessage Invalid MockConnection"); |
| 14 | // 無法監聽的話不应该屏蔽错误 |
| 15 | throw new Error("onMessage Invalid MockConnection"); |
| 16 | } |
| 17 | this.EE.on("message", (data: any) => { |
| 18 | callback(data); |
| 19 | }); |
| 20 | } |
| 21 | |
| 22 | sendMessage(data: TMessage): void { |
| 23 | if (!this.EE) { |
| 24 | console.warn("Attempted to sendMessage on a disconnected MockConnection."); |
| 25 | // 無法 sendMessage 不应该屏蔽错误 |
| 26 | throw new Error("Attempted to sendMessage on a disconnected MockConnection."); |
| 27 | } |
| 28 | this.EE.emit("message", data); |
| 29 | } |
| 30 | |
| 31 | disconnect(ignoreAlreadyDisconnected?: boolean): void { |
| 32 | if (!this.EE) { |
| 33 | if (ignoreAlreadyDisconnected) return; |
| 34 | console.warn("Attempted to disconnect on a disconnected MockConnection."); |
| 35 | // 重复 disconnect() 不应该屏蔽错误 |
| 36 | throw new Error("Attempted to disconnect on a disconnected MockConnection."); |
| 37 | } |
| 38 | const EE = this.EE; |
| 39 | this.EE = null; |
| 40 | EE?.emit("disconnect", true); // MockMessageConnect 未有模拟由另一端触发 disconnect() 的情况 |
| 41 | } |
| 42 | |
| 43 | onDisconnect(callback: (isSelfDisconnected: boolean) => void) { |
| 44 | if (!this.EE) { |
| 45 | console.error("onDisconnect Invalid MockConnection."); |
| 46 | // 無法監聽的話不应该屏蔽错误 |
| 47 | throw new Error("onDisconnect Invalid MockConnection."); |
| 48 | } |
| 49 | this.EE.once("disconnect", callback); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | export class MockMessage implements Message { |
| 54 | constructor(protected EE: EventEmitter<string, any>) {} |
nothing calls this directly
no outgoing calls
no test coverage detected