Skip to content

Commit

Permalink
Convert RestChannelMixin.history to use promises
Browse files Browse the repository at this point in the history
  • Loading branch information
lawrence-forooghian committed Dec 8, 2023
1 parent c8a4497 commit 817ab7f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 26 deletions.
4 changes: 1 addition & 3 deletions src/common/lib/client/realtimechannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -881,9 +881,7 @@ class RealtimeChannel extends EventEmitter {
params.from_serial = this.properties.attachSerial;
}

return new Promise((resolve, reject) => {
restMixin.history(this, params, (err, result) => (err ? reject(err) : resolve(result)));
});
return restMixin.history(this, params);
} as any;

whenState = ((state: string, listener: ErrCallback) => {
Expand Down
4 changes: 1 addition & 3 deletions src/common/lib/client/restchannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ class RestChannel {

async history(params: RestHistoryParams | null): Promise<PaginatedResult<Message>> {
Logger.logAction(Logger.LOG_MICRO, 'RestChannel.history()', 'channel = ' + this.name);
return new Promise((resolve, reject) => {
this.client.rest.channelMixin.history(this, params, (err, result) => (err ? reject(err) : resolve(result)));
});
return this.client.rest.channelMixin.history(this, params);
}

async publish(...args: any[]): Promise<void> {
Expand Down
30 changes: 10 additions & 20 deletions src/common/lib/client/restchannelmixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import * as API from '../../../../ably';
import RestChannel from './restchannel';
import RealtimeChannel from './realtimechannel';
import * as Utils from '../util/utils';
import { PaginatedResultCallback } from '../../types/utils';
import Message, { fromResponseBody as messageFromResponseBody } from '../types/message';
import Defaults from '../util/defaults';
import PaginatedResource from './paginatedresource';
import PaginatedResource, { PaginatedResult } from './paginatedresource';
import Resource from './resource';

export interface RestHistoryParams {
Expand All @@ -22,9 +21,8 @@ export class RestChannelMixin {

static history(
channel: RestChannel | RealtimeChannel,
params: RestHistoryParams | null,
callback: PaginatedResultCallback<Message>
): void {
params: RestHistoryParams | null
): Promise<PaginatedResult<Message>> {
const client = channel.client,
format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json,
envelope = channel.client.http.supportsLinkHeaders ? undefined : format,
Expand All @@ -33,21 +31,13 @@ export class RestChannelMixin {
Utils.mixin(headers, client.options.headers);

const options = channel.channelOptions;
Utils.whenPromiseSettles(
new PaginatedResource(client, this.basePath(channel) + '/messages', headers, envelope, async function (
body,
headers,
unpacked
) {
return await messageFromResponseBody(
body as Message[],
options,
client._MsgPack,
unpacked ? undefined : format
);
}).get(params as Record<string, unknown>),
callback
);
return new PaginatedResource(client, this.basePath(channel) + '/messages', headers, envelope, async function (
body,
headers,
unpacked
) {
return await messageFromResponseBody(body as Message[], options, client._MsgPack, unpacked ? undefined : format);
}).get(params as Record<string, unknown>);
}

static async status(channel: RestChannel | RealtimeChannel): Promise<API.Types.ChannelDetails> {
Expand Down

0 comments on commit 817ab7f

Please sign in to comment.