| 98 | const routedEvents = isRouted ? param : {}; |
| 99 | |
| 100 | window.WebSocket = class WebSocket { |
| 101 | public readyState = 1; |
| 102 | public binaryType = "blob"; |
| 103 | |
| 104 | #listeners = new Map<string, CallbackFn>(); |
| 105 | #callEventsDelay: number | undefined; |
| 106 | #url: string; |
| 107 | |
| 108 | constructor(url?: string) { |
| 109 | this.#url = url ?? ""; |
| 110 | } |
| 111 | |
| 112 | send() {} |
| 113 | |
| 114 | addEventListener(type: string, callback: CallbackFn) { |
| 115 | this.#listeners.set(type, callback); |
| 116 | |
| 117 | // Determine which events this socket should receive. |
| 118 | let events = broadcastEvents; |
| 119 | if (isRouted) { |
| 120 | const matchingKey = Object.keys(routedEvents).find((key) => |
| 121 | this.#url.includes(key), |
| 122 | ); |
| 123 | events = matchingKey ? routedEvents[matchingKey] : []; |
| 124 | } |
| 125 | |
| 126 | if (events.length === 0) { |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | // Runs when the last event listener is added |
| 131 | clearTimeout(this.#callEventsDelay); |
| 132 | this.#callEventsDelay = window.setTimeout(() => { |
| 133 | for (const entry of events) { |
| 134 | const callback = this.#listeners.get(entry.event); |
| 135 | |
| 136 | if (callback) { |
| 137 | entry.event === "message" |
| 138 | ? callback({ data: entry.data }) |
| 139 | : callback(); |
| 140 | } |
| 141 | } |
| 142 | }, 0); |
| 143 | } |
| 144 | |
| 145 | removeEventListener(_type: string, _callback: CallbackFn) {} |
| 146 | |
| 147 | close() {} |
| 148 | } as unknown as typeof WebSocket; |
| 149 | |
| 150 | return <Story />; |
| 151 | }; |
nothing calls this directly
no outgoing calls
no test coverage detected