Skip to content

Commit 6756cf5

Browse files
committedJul 29, 2021
[fix] Return null if the event handler is not set
Make the `onclose`, `onerror`, `onmessage`, and `onopen` getters return `null` instead of `undefined` if the event handler is not set.
1 parent 8c61563 commit 6756cf5

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed
 

‎lib/websocket.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -124,31 +124,31 @@ class WebSocket extends EventEmitter {
124124
*/
125125
/* istanbul ignore next */
126126
get onclose() {
127-
return undefined;
127+
return null;
128128
}
129129

130130
/**
131131
* @type {Function}
132132
*/
133133
/* istanbul ignore next */
134134
get onerror() {
135-
return undefined;
135+
return null;
136136
}
137137

138138
/**
139139
* @type {Function}
140140
*/
141141
/* istanbul ignore next */
142142
get onopen() {
143-
return undefined;
143+
return null;
144144
}
145145

146146
/**
147147
* @type {Function}
148148
*/
149149
/* istanbul ignore next */
150150
get onmessage() {
151-
return undefined;
151+
return null;
152152
}
153153

154154
/**
@@ -528,7 +528,7 @@ Object.defineProperty(WebSocket.prototype, 'CLOSED', {
528528
if (listener[kForOnEventAttribute]) return listener[kListener];
529529
}
530530

531-
return undefined;
531+
return null;
532532
},
533533
set(handler) {
534534
for (const listener of this.listeners(method)) {

‎test/websocket.test.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -2097,10 +2097,10 @@ describe('WebSocket', () => {
20972097

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

2100-
assert.strictEqual(ws.onmessage, undefined);
2101-
assert.strictEqual(ws.onclose, undefined);
2102-
assert.strictEqual(ws.onerror, undefined);
2103-
assert.strictEqual(ws.onopen, undefined);
2100+
assert.strictEqual(ws.onmessage, null);
2101+
assert.strictEqual(ws.onclose, null);
2102+
assert.strictEqual(ws.onerror, null);
2103+
assert.strictEqual(ws.onopen, null);
21042104

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

21152115
ws.onmessage = 'foo';
21162116

2117-
assert.strictEqual(ws.onmessage, undefined);
2117+
assert.strictEqual(ws.onmessage, null);
21182118
assert.strictEqual(ws.listenerCount('onmessage'), 0);
21192119
});
21202120

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

21512151
assert.deepStrictEqual(ws.listeners('open'), [NOOP]);
2152-
assert.strictEqual(ws.onopen, undefined);
2152+
assert.strictEqual(ws.onopen, null);
21532153
});
21542154

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

2217-
assert.strictEqual(ws.onopen, undefined);
2217+
assert.strictEqual(ws.onopen, null);
22182218
});
22192219

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

0 commit comments

Comments
 (0)
Please sign in to comment.