Skip to content

Commit

Permalink
[fix] Return null if the event handler is not set
Browse files Browse the repository at this point in the history
Make the `onclose`, `onerror`, `onmessage`, and `onopen` getters return
`null` instead of `undefined` if the event handler is not set.
  • Loading branch information
lpinca committed Jul 29, 2021
1 parent 8c61563 commit 6756cf5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
10 changes: 5 additions & 5 deletions lib/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,31 +124,31 @@ class WebSocket extends EventEmitter {
*/
/* istanbul ignore next */
get onclose() {
return undefined;
return null;
}

/**
* @type {Function}
*/
/* istanbul ignore next */
get onerror() {
return undefined;
return null;
}

/**
* @type {Function}
*/
/* istanbul ignore next */
get onopen() {
return undefined;
return null;
}

/**
* @type {Function}
*/
/* istanbul ignore next */
get onmessage() {
return undefined;
return null;
}

/**
Expand Down Expand Up @@ -528,7 +528,7 @@ Object.defineProperty(WebSocket.prototype, 'CLOSED', {
if (listener[kForOnEventAttribute]) return listener[kListener];
}

return undefined;
return null;
},
set(handler) {
for (const listener of this.listeners(method)) {
Expand Down
14 changes: 7 additions & 7 deletions test/websocket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2097,10 +2097,10 @@ describe('WebSocket', () => {

const ws = new WebSocket('ws://localhost', { agent: new CustomAgent() });

assert.strictEqual(ws.onmessage, undefined);
assert.strictEqual(ws.onclose, undefined);
assert.strictEqual(ws.onerror, undefined);
assert.strictEqual(ws.onopen, undefined);
assert.strictEqual(ws.onmessage, null);
assert.strictEqual(ws.onclose, null);
assert.strictEqual(ws.onerror, null);
assert.strictEqual(ws.onopen, null);

ws.onmessage = NOOP;
ws.onerror = NOOP;
Expand All @@ -2114,7 +2114,7 @@ describe('WebSocket', () => {

ws.onmessage = 'foo';

assert.strictEqual(ws.onmessage, undefined);
assert.strictEqual(ws.onmessage, null);
assert.strictEqual(ws.listenerCount('onmessage'), 0);
});

Expand Down Expand Up @@ -2149,7 +2149,7 @@ describe('WebSocket', () => {
ws.on('open', NOOP);

assert.deepStrictEqual(ws.listeners('open'), [NOOP]);
assert.strictEqual(ws.onopen, undefined);
assert.strictEqual(ws.onopen, null);
});

it("doesn't remove listeners added with `on`", () => {
Expand Down Expand Up @@ -2214,7 +2214,7 @@ describe('WebSocket', () => {
assert.strictEqual(listeners.length, 1);
assert.strictEqual(listeners[0][kListener], NOOP);

assert.strictEqual(ws.onopen, undefined);
assert.strictEqual(ws.onopen, null);
});

it("doesn't remove listeners added with `addEventListener`", () => {
Expand Down

0 comments on commit 6756cf5

Please sign in to comment.