Skip to content

Commit

Permalink
Merge pull request #1543 from ably/1542-next-page-not-null
Browse files Browse the repository at this point in the history
[SDK-3990] Fix `PaginatedResult.next()` behaviour when no next page
  • Loading branch information
lawrence-forooghian authored Dec 11, 2023
2 parents b65c244 + 8bd1706 commit fdd5a56
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
4 changes: 2 additions & 2 deletions ably.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3460,13 +3460,13 @@ declare namespace Types {
*
* @param results - A function which, upon success, will be fulfilled with a page of results for message and presence history, stats, and REST presence requests. Upon failure, the function will be called with information about the error.
*/
next(results: paginatedResultCallback<T>): void;
next(results: StandardCallback<PaginatedResult<T> | null>): void;
/**
* Returns a new `PaginatedResult` loaded with the next page of results. If there are no further pages, then `null` is returned.
*
* @returns A promise which, upon success, will be fulfilled with a page of results for message and presence history, stats, and REST presence requests. Upon failure, the promise will be rejected with an {@link ErrorInfo} object which explains the error.
*/
next(): Promise<PaginatedResult<T>>;
next(): Promise<PaginatedResult<T> | null>;
/**
* Returns the `PaginatedResult` for the current page of results.
*
Expand Down
10 changes: 5 additions & 5 deletions src/common/lib/client/paginatedresource.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Utils from '../util/utils';
import Logger from '../util/logger';
import Resource from './resource';
import ErrorInfo, { IPartialErrorInfo } from '../types/errorinfo';
import { IPartialErrorInfo } from '../types/errorinfo';
import { PaginatedResultCallback } from '../../types/utils';
import Rest from './rest';

Expand Down Expand Up @@ -187,29 +187,29 @@ export class PaginatedResult<T> {
const self = this;
if (relParams) {
if ('first' in relParams) {
this.first = function (callback: (result?: ErrorInfo | null) => void) {
this.first = function (callback) {
if (!callback && self.resource.rest.options.promises) {
return Utils.promisify(self, 'first', []);
}
self.get(relParams.first, callback);
};
}
if ('current' in relParams) {
this.current = function (callback: (results?: ErrorInfo | null) => void) {
this.current = function (callback) {
if (!callback && self.resource.rest.options.promises) {
return Utils.promisify(self, 'current', []);
}
self.get(relParams.current, callback);
};
}
this.next = function (callback: (results?: ErrorInfo | null) => void) {
this.next = function (callback) {
if (!callback && self.resource.rest.options.promises) {
return Utils.promisify(self, 'next', []);
}
if ('next' in relParams) {
self.get(relParams.next, callback);
} else {
callback(null);
callback(null, null);
}
};

Expand Down
20 changes: 20 additions & 0 deletions test/rest/history.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,26 @@ define(['shared_helper', 'async', 'chai'], function (helper, async, chai) {
}
});

restTestOnJsonMsgpack('history_no_next_page', function (done, rest, channelName) {
const channel = rest.channels.get(channelName);

channel.history(function (err, firstPage) {
if (err) {
done(err);
return;
}
firstPage.next(function (err, secondPage) {
if (err) {
done(err);
return;
}

expect(secondPage).to.equal(null);
done();
});
});
});

if (typeof Promise !== 'undefined') {
it('historyPromise', function (done) {
var rest = helper.AblyRest({ promises: true });
Expand Down

0 comments on commit fdd5a56

Please sign in to comment.