Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK-3883] Remove ability to pass array of event names to EventEmitter.prototype.once #1453

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/common/lib/client/realtimechannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,11 @@ class RealtimeChannel extends Channel {
// Ignore 'attaching' -- could be just due to to a resume & reattach, should not
// call back setOptions until we're definitely attached with the new options (or
// else in a terminal state)
this._allChannelChanges.once(
const self = this;
this._allChannelChanges.on(
['attached', 'update', 'detached', 'failed'],
function (this: { event: string }, stateChange: ConnectionStateChange) {
function listener(this: { event: string }, stateChange: ConnectionStateChange) {
self._allChannelChanges.off(listener);
switch (this.event) {
case 'update':
case 'attached':
Expand Down
19 changes: 2 additions & 17 deletions src/common/lib/util/eventemitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,14 @@ class EventEmitter {
* @param event the name of the event to listen to
* @param listener the listener to be called
*/
once(event?: string | string[] | null, listener?: Function): void;
once(event?: string | null, listener?: Function): void;

once(...args: unknown[]): void | Promise<void> {
const argCount = args.length;
if ((argCount === 0 || (argCount === 1 && typeof args[0] !== 'function')) && Platform.Config.Promise) {
const event = args[0];
return new Platform.Config.Promise((resolve) => {
this.once(event as string | string[] | null, resolve);
this.once(event as string | null, resolve);
});
}

Expand All @@ -256,21 +256,6 @@ class EventEmitter {
throw new Error('EventEmitter.once(): Invalid arguments:' + Platform.Config.inspect(args));
}
this.anyOnce.push(secondArg);
} else if (Utils.isArray(firstArg)) {
const self = this;
const listenerWrapper = function (this: any) {
const innerArgs = Array.prototype.slice.call(arguments);
Utils.arrForEach(firstArg, function (eventName) {
self.off(eventName, listenerWrapper);
});
if (typeof secondArg !== 'function') {
throw new Error('EventEmitter.once(): Invalid arguments:' + Platform.Config.inspect(args));
}
secondArg.apply(this, innerArgs);
};
Utils.arrForEach(firstArg, function (eventName) {
self.on(eventName, listenerWrapper);
});
} else {
if (typeof firstArg !== 'string') {
throw new Error('EventEmitter.once(): Invalid arguments:' + Platform.Config.inspect(args));
Expand Down
38 changes: 0 additions & 38 deletions test/realtime/event_emitter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,31 +342,6 @@ define(['shared_helper', 'chai'], function (helper, chai) {
closeAndFinish(done, realtime);
});

it('arrayOfEventsWithOnce', function (done) {
var realtime = helper.AblyRealtime({ autoConnect: false }),
callbackCalled = 0,
eventEmitter = realtime.connection;

var callback = function (arg) {
callbackCalled += 1;
expect(arg).to.equal('expected');
};

try {
callbackCalled = 0;
eventEmitter.once(['a', 'b', 'c'], callback);
eventEmitter.emit('a', 'expected');
eventEmitter.emit('b', 'wrong');
eventEmitter.emit('c', 'wrong');
expect(callbackCalled).to.equal(1, 'listener called back only once, for the first event emitted');
} catch (err) {
closeAndFinish(done, realtime, err);
return;
}

closeAndFinish(done, realtime);
});

/* check that listeners added in a listener cb are not called during that
* emit instance */
it('listenerAddedInListenerCb', function (done) {
Expand Down Expand Up @@ -481,19 +456,6 @@ define(['shared_helper', 'chai'], function (helper, chai) {
closeAndFinish(done, realtime, err);
});
});

it('arrayOfEventsWithOnce', function (done) {
var realtime = helper.AblyRealtime({ autoConnect: false }),
eventEmitter = realtime.connection;

const p = eventEmitter.once(['a', 'b', 'c']);
eventEmitter.emit('b');
p.then(function () {
closeAndFinish(done, realtime);
}).catch(function (err) {
closeAndFinish(done, realtime, err);
});
});
});
}
});
Expand Down