* Static method to create a connection * @param {Object} options - Connection options * @param {string} options.name - Optional name for the connection * @param {Function} onConnectCallback - Callback that receives the second Port instance * @returns {Port} The initiating Port in
(options = {}, onConnectCallback)
| 221 | * @returns {Port} The initiating Port instance |
| 222 | */ |
| 223 | static connect(options = {}, onConnectCallback) { |
| 224 | // Handle overloaded parameters |
| 225 | if (typeof options === 'function') { |
| 226 | onConnectCallback = options; |
| 227 | options = {}; |
| 228 | } |
| 229 | |
| 230 | const name = options.name || ''; |
| 231 | |
| 232 | // Create two connected ports |
| 233 | const port1 = new Port(name); |
| 234 | const port2 = new Port(name); |
| 235 | |
| 236 | // Set sender information (in real implementation, this would contain extension info) |
| 237 | port2.sender = { |
| 238 | id: 'mock-extension-id', |
| 239 | url: 'mock://extension', |
| 240 | // In real implementation, these would be populated based on context |
| 241 | frameId: 0, |
| 242 | tab: null |
| 243 | }; |
| 244 | |
| 245 | // Establish the connection between ports |
| 246 | port1._connectTo(port2); |
| 247 | |
| 248 | // Trigger the onConnect callback asynchronously with the second port |
| 249 | if (typeof onConnectCallback === 'function') { |
| 250 | setTimeout(() => { |
| 251 | onConnectCallback(port2); |
| 252 | }, 0); |
| 253 | } |
| 254 | |
| 255 | return port1; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | window.chrome ??= {}; |
no test coverage detected