Skip to content

Commit

Permalink
Convert RestChannel._publish to use promises
Browse files Browse the repository at this point in the history
  • Loading branch information
lawrence-forooghian committed Dec 11, 2023
1 parent d6c224b commit 28f4ae5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
30 changes: 16 additions & 14 deletions src/common/lib/client/restchannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class RestChannel {
});
}

return new Promise((resolve, reject) => {
await new Promise<void>((resolve, reject) => {
encodeMessagesArray(messages, this.channelOptions as CipherOptions, (err: Error) => {
if (err) {
reject(err);
Expand All @@ -119,23 +119,25 @@ class RestChannel {
return;
}

this._publish(serializeMessage(messages, client._MsgPack, format), headers, params, (err) =>
err ? reject(err) : resolve()
);
resolve();
});
});

await this._publish(serializeMessage(messages, client._MsgPack, format), headers, params);
}

_publish(requestBody: unknown, headers: Record<string, string>, params: any, callback: ErrCallback): void {
Resource.post(
this.client,
this.client.rest.channelMixin.basePath(this) + '/messages',
requestBody,
headers,
params,
null,
(err) => callback(err)
);
async _publish(requestBody: unknown, headers: Record<string, string>, params: any): Promise<void> {
return new Promise((resolve, reject) => {
Resource.post(
this.client,
this.client.rest.channelMixin.basePath(this) + '/messages',
requestBody,
headers,
params,
null,
(err) => (err ? reject(err) : resolve())
);
});
}

async status(): Promise<API.Types.ChannelDetails> {
Expand Down
20 changes: 10 additions & 10 deletions test/rest/message.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
channel = rest.channels.get('rest_implicit_client_id_0');

var originalPublish = channel._publish;
channel._publish = function (requestBody) {
channel._publish = async function (requestBody) {
var message = JSON.parse(requestBody)[0];
expect(message.name === 'event0', 'Outgoing message interecepted').to.be.ok;
expect(!message.clientId, 'client ID is not added by the client library as it is implicit').to.be.ok;
originalPublish.apply(channel, arguments);
return originalPublish.apply(channel, arguments);
};

await channel.publish('event0', null);
Expand All @@ -46,14 +46,14 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
channel = rest.channels.get('rest_explicit_client_id_0');

var originalPublish = channel._publish;
channel._publish = function (requestBody) {
channel._publish = async function (requestBody) {
var message = JSON.parse(requestBody)[0];
expect(message.name === 'event0', 'Outgoing message interecepted').to.be.ok;
expect(
message.clientId == clientId,
'client ID is added by the client library as it is explicit in the publish'
).to.be.ok;
originalPublish.apply(channel, arguments);
return originalPublish.apply(channel, arguments);
};

await channel.publish({ name: 'event0', clientId: clientId });
Expand All @@ -76,14 +76,14 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
channel = rest.channels.get('rest_explicit_client_id_1');

var originalPublish = channel._publish;
channel._publish = function (requestBody) {
channel._publish = async function (requestBody) {
var message = JSON.parse(requestBody)[0];
expect(message.name === 'event0', 'Outgoing message interecepted').to.be.ok;
expect(
message.clientId == invalidClientId,
'invalid client ID is added by the client library as it is explicit in the publish'
).to.be.ok;
originalPublish.apply(channel, arguments);
return originalPublish.apply(channel, arguments);
};

try {
Expand Down Expand Up @@ -143,7 +143,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
originalPublish = channel._publish,
originalDoUri = Ably.Realtime.Platform.Http.doUri;

channel._publish = function (requestBody) {
channel._publish = async function (requestBody) {
var messageOne = JSON.parse(requestBody)[0];
var messageTwo = JSON.parse(requestBody)[1];
expect(messageOne.name).to.equal('one', 'Outgoing message 1 interecepted');
Expand All @@ -154,7 +154,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
expect(idTwo, 'id set on message 2').to.be.ok;
expect(idOne && idOne.split(':')[1]).to.equal('0', 'check zero-based index');
expect(idTwo && idTwo.split(':')[1]).to.equal('1', 'check zero-based index');
originalPublish.apply(channel, arguments);
return originalPublish.apply(channel, arguments);
};

Ably.Rest.Platform.Http.doUri = function (method, uri, headers, body, params, callback) {
Expand Down Expand Up @@ -186,9 +186,9 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
var originalPublish = channel._publish;

/* Stub out _publish to check params */
channel._publish = function (requestBody, headers, params) {
channel._publish = async function (requestBody, headers, params) {
expect(params && params.testParam).to.equal('testParamValue');
originalPublish.apply(channel, arguments);
return originalPublish.apply(channel, arguments);
};

await channel.publish('foo', 'bar', { testParam: 'testParamValue' });
Expand Down

0 comments on commit 28f4ae5

Please sign in to comment.