Skip to content

Commit c529910

Browse files
authored
Merge pull request #461 from anephenix/fix/resolve-performance-regression
Fix | Fix performance regression in auditEventListeners function
2 parents 0ca73d9 + ac7c6ad commit c529910

File tree

3 files changed

+23
-35
lines changed

3 files changed

+23
-35
lines changed

dist/index.d.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,12 @@ export default class Sarus {
7676
* @param {object} eventListeners - The eventListeners object parameter
7777
* @returns {object} The eventListeners object parameter, with any missing events prefilled in
7878
*/
79-
auditEventListeners(eventListeners: PartialEventListenersInterface): EventListenersInterface;
79+
auditEventListeners(eventListeners: PartialEventListenersInterface | undefined): {
80+
open: Function[];
81+
message: Function[];
82+
error: Function[];
83+
close: Function[];
84+
};
8085
/**
8186
* Connects the WebSocket client, and attaches event listeners
8287
*/

dist/index.js

+8-20
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
11
"use strict";
2-
var __assign = (this && this.__assign) || function () {
3-
__assign = Object.assign || function(t) {
4-
for (var s, i = 1, n = arguments.length; i < n; i++) {
5-
s = arguments[i];
6-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7-
t[p] = s[p];
8-
}
9-
return t;
10-
};
11-
return __assign.apply(this, arguments);
12-
};
132
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
143
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
154
if (ar || !(i in from)) {
@@ -118,8 +107,9 @@ var Sarus = /** @class */ (function () {
118107
*/
119108
this.state = "connecting";
120109
// Extract the properties that are passed to the class
121-
var url = props.url, binaryType = props.binaryType, protocols = props.protocols, _b = props.eventListeners, eventListeners = _b === void 0 ? constants_1.DEFAULT_EVENT_LISTENERS_OBJECT : _b, reconnectAutomatically = props.reconnectAutomatically, retryProcessTimePeriod = props.retryProcessTimePeriod, // TODO - write a test case to check this
122-
retryConnectionDelay = props.retryConnectionDelay, _c = props.storageType, storageType = _c === void 0 ? "memory" : _c, _d = props.storageKey, storageKey = _d === void 0 ? "sarus" : _d;
110+
var url = props.url, binaryType = props.binaryType, protocols = props.protocols, eventListeners = props.eventListeners, // = DEFAULT_EVENT_LISTENERS_OBJECT,
111+
reconnectAutomatically = props.reconnectAutomatically, retryProcessTimePeriod = props.retryProcessTimePeriod, // TODO - write a test case to check this
112+
retryConnectionDelay = props.retryConnectionDelay, _b = props.storageType, storageType = _b === void 0 ? "memory" : _b, _c = props.storageKey, storageKey = _c === void 0 ? "sarus" : _c;
123113
this.eventListeners = this.auditEventListeners(eventListeners);
124114
// Sets the WebSocket server url for the client to connect to.
125115
this.url = validateWebSocketUrl(url);
@@ -260,14 +250,12 @@ var Sarus = /** @class */ (function () {
260250
* @returns {object} The eventListeners object parameter, with any missing events prefilled in
261251
*/
262252
Sarus.prototype.auditEventListeners = function (eventListeners) {
263-
var defaultEventListeners = {
264-
open: [],
265-
message: [],
266-
error: [],
267-
close: [],
253+
return {
254+
open: (eventListeners === null || eventListeners === void 0 ? void 0 : eventListeners.open) || [],
255+
message: (eventListeners === null || eventListeners === void 0 ? void 0 : eventListeners.message) || [],
256+
error: (eventListeners === null || eventListeners === void 0 ? void 0 : eventListeners.error) || [],
257+
close: (eventListeners === null || eventListeners === void 0 ? void 0 : eventListeners.close) || [],
268258
};
269-
var mergedEventListeners = __assign(__assign({}, defaultEventListeners), eventListeners); // Type assertion added here
270-
return mergedEventListeners;
271259
};
272260
/**
273261
* Connects the WebSocket client, and attaches event listeners

src/index.ts

+9-14
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ export default class Sarus {
193193
url,
194194
binaryType,
195195
protocols,
196-
eventListeners = DEFAULT_EVENT_LISTENERS_OBJECT,
196+
eventListeners, // = DEFAULT_EVENT_LISTENERS_OBJECT,
197197
reconnectAutomatically,
198198
retryProcessTimePeriod, // TODO - write a test case to check this
199199
retryConnectionDelay,
@@ -360,20 +360,15 @@ export default class Sarus {
360360
* @param {object} eventListeners - The eventListeners object parameter
361361
* @returns {object} The eventListeners object parameter, with any missing events prefilled in
362362
*/
363-
auditEventListeners(eventListeners: PartialEventListenersInterface) {
364-
const defaultEventListeners: EventListenersInterface = {
365-
open: [],
366-
message: [],
367-
error: [],
368-
close: [],
363+
auditEventListeners(
364+
eventListeners: PartialEventListenersInterface | undefined,
365+
) {
366+
return {
367+
open: eventListeners?.open || [],
368+
message: eventListeners?.message || [],
369+
error: eventListeners?.error || [],
370+
close: eventListeners?.close || [],
369371
};
370-
371-
const mergedEventListeners: EventListenersInterface = {
372-
...defaultEventListeners,
373-
...eventListeners,
374-
} as EventListenersInterface; // Type assertion added here
375-
376-
return mergedEventListeners;
377372
}
378373

379374
/**

0 commit comments

Comments
 (0)