Skip to content

Commit 8afa250

Browse files
committed
Add support for sessionId to events too
Allow to filter events by `sessionId`. There is one gotcha that when using promises without filter, the `sessionId` is not returned due to the limitation of promises to return only one value, and the fact that changing the return type would be a breaking change. Related to #441.
1 parent 78f207c commit 8afa250

File tree

4 files changed

+74
-11
lines changed

4 files changed

+74
-11
lines changed

README.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,8 @@ Emitted when the remote instance sends any notification through the WebSocket.
689689

690690
- `method`: a string describing the notification (e.g.,
691691
`'Network.requestWillBeSent'`);
692-
- `params`: an object containing the payload.
692+
- `params`: an object containing the payload;
693+
- `sessionId`: an optional string representing the session identifier.
693694

694695
Refer to the [Chrome Debugging Protocol] specification for more information.
695696

@@ -706,14 +707,16 @@ client.on('event', (message) => {
706707
#### Event: '`<domain>`.`<method>`'
707708

708709
```js
709-
function (params) {}
710+
function (params, sessionId) {}
710711
```
711712

712713
Emitted when the remote instance sends a notification for `<domain>.<method>`
713714
through the WebSocket.
714715

715716
`params` is an object containing the payload.
716717

718+
`sessionId` is an optional string representing the session identifier.
719+
717720
This is just a utility event which allows to easily listen for specific
718721
notifications (see [`'event'`](#event-event)), for example:
719722

@@ -727,6 +730,18 @@ Additionally, the equivalent `<domain>.on('<method>', ...)` syntax is available,
727730
client.Network.on('requestWillBeSent', console.log);
728731
```
729732

733+
#### Event: '`<domain>`.`<method>`.`<sessionId>`'
734+
735+
```js
736+
function (params, sessionId) {}
737+
```
738+
739+
Equivalent to the following but only for those events belonging to the given `session`:
740+
741+
```js
742+
client.on('<domain>.<event>', callback);
743+
```
744+
730745
#### Event: 'ready'
731746

732747
```js
@@ -819,16 +834,16 @@ For example:
819834
client.Page.navigate({url: 'https://github.com'}, console.log);
820835
```
821836

822-
#### client.`<domain>`.`<event>`([callback])
837+
#### client.`<domain>`.`<event>`([sessionId], [callback])
823838

824839
Just a shorthand for:
825840

826841
```js
827-
client.on('<domain>.<event>', callback);
842+
client.on('<domain>.<event>[.<sessionId>]', callback);
828843
```
829844

830845
When `callback` is omitted the event is registered only once and a `Promise`
831-
object is returned.
846+
object is returned. Notice though that in this case the optional `sessionId` usually passed to `callback` is not returned.
832847

833848
When `callback` is provided, it returns a function that can be used to
834849
unsubscribe `callback` from the event, it can be useful when anonymous functions
@@ -837,7 +852,7 @@ are used as callbacks.
837852
For example:
838853

839854
```js
840-
const unsubscribe = client.Network.requestWillBeSent((params) => {
855+
const unsubscribe = client.Network.requestWillBeSent((params, sessionId) => {
841856
console.log(params.request.url);
842857
});
843858
unsubscribe();

lib/api.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,18 @@ function addCommand(chrome, domainName, command) {
3737

3838
function addEvent(chrome, domainName, event) {
3939
const eventName = `${domainName}.${event.name}`;
40-
const handler = (handler) => {
40+
const handler = (sessionId, handler) => {
41+
if (typeof sessionId === 'function') {
42+
handler = sessionId;
43+
sessionId = undefined;
44+
}
45+
const rawEventName = sessionId ? `${eventName}.${sessionId}` : eventName;
4146
if (typeof handler === 'function') {
42-
chrome.on(eventName, handler);
43-
return () => chrome.removeListener(eventName, handler);
47+
chrome.on(rawEventName, handler);
48+
return () => chrome.removeListener(rawEventName, handler);
4449
} else {
4550
return new Promise((fulfill, reject) => {
46-
chrome.once(eventName, fulfill);
51+
chrome.once(rawEventName, fulfill);
4752
});
4853
}
4954
};

lib/chrome.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,10 @@ class Chrome extends EventEmitter {
267267
}
268268
// event
269269
else if (message.method) {
270+
const {method, params, sessionId} = message;
270271
this.emit('event', message);
271-
this.emit(message.method, message.params);
272+
this.emit(method, params, sessionId);
273+
this.emit(`${method}.${sessionId}`, params, sessionId);
272274
}
273275
}
274276

test/event.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,45 @@ describe('registering event', () => {
9292
});
9393
});
9494
});
95+
describe('passing a sessionId', () => {
96+
it('should only listen for those events', async () => {
97+
// fetch and connect to the browser target
98+
const version = await Chrome.Version();
99+
const chrome = await Chrome({
100+
target: version.webSocketDebuggerUrl
101+
});
102+
// create another target
103+
await chrome.Target.createTarget({url: 'about:blank'});
104+
// fetch the targets (two pages) and attach to each of them
105+
const {targetInfos} = await chrome.Target.getTargets();
106+
const {sessionId: sessionId0} = await chrome.Target.attachToTarget({
107+
targetId: targetInfos[0].targetId,
108+
flatten: true
109+
});
110+
const {sessionId: sessionId1} = await chrome.Target.attachToTarget({
111+
targetId: targetInfos[1].targetId,
112+
flatten: true
113+
});
114+
// enable the Page events in both of them
115+
await chrome.Page.enable(sessionId0);
116+
await chrome.Page.enable(sessionId1);
117+
// trigger a reload in both of them
118+
chrome.Page.reload(sessionId0);
119+
chrome.Page.reload(sessionId1);
120+
// awaits individual events
121+
await Promise.all([
122+
chrome.Page.loadEventFired(sessionId0),
123+
chrome.Page.loadEventFired(sessionId1),
124+
new Promise((fulfill, reject) => {
125+
let counter = 0;
126+
chrome.Page.loadEventFired((params) => {
127+
if (++counter === 2) {
128+
fulfill();
129+
}
130+
});
131+
})
132+
]);
133+
return chrome.close();
134+
});
135+
});
95136
});

0 commit comments

Comments
 (0)