* Subscribe to events * @param events * @param browsingContexts * @returns {Promise }
(events, browsingContexts)
| 235 | * @returns {Promise<void>} |
| 236 | */ |
| 237 | async subscribe(events, browsingContexts) { |
| 238 | function toArray(arg) { |
| 239 | if (arg === undefined) { |
| 240 | return [] |
| 241 | } |
| 242 | |
| 243 | return Array.isArray(arg) ? [...arg] : [arg] |
| 244 | } |
| 245 | |
| 246 | const eventsArray = toArray(events) |
| 247 | const contextsArray = toArray(browsingContexts) |
| 248 | |
| 249 | const params = { |
| 250 | method: 'session.subscribe', |
| 251 | params: {}, |
| 252 | } |
| 253 | |
| 254 | if (eventsArray.length && eventsArray.some((event) => typeof event !== 'string')) { |
| 255 | throw new TypeError('events should be string or string array') |
| 256 | } |
| 257 | |
| 258 | if (contextsArray.length && contextsArray.some((context) => typeof context !== 'string')) { |
| 259 | throw new TypeError('browsingContexts should be string or string array') |
| 260 | } |
| 261 | |
| 262 | if (eventsArray.length) { |
| 263 | params.params.events = eventsArray |
| 264 | } |
| 265 | |
| 266 | if (contextsArray.length) { |
| 267 | params.params.contexts = contextsArray |
| 268 | } |
| 269 | |
| 270 | this.events.push(...eventsArray) |
| 271 | |
| 272 | await this.send(params) |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Unsubscribe to events |
no test coverage detected