Skip to content

Commit

Permalink
fix: should pass extra callback arguments back to consumer (#361)
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarowolfx authored May 22, 2024
1 parent a4b49e0 commit cc5c48b
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 6 deletions.
14 changes: 9 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,22 @@ export class Paginator {
return originalMethod(query, callback);
}
const results = new Array<{}>();
let otherArgs: unknown[] = [];
const promise = new Promise((resolve, reject) => {
paginator
.runAsStream_(parsedArguments, originalMethod)
const stream = paginator.runAsStream_(parsedArguments, originalMethod);
stream
.on('error', reject)
.on('data', (data: {}) => results.push(data))
.on('end', () => resolve(results));
.on('end', () => {
otherArgs = stream._otherArgs || [];
resolve(results);
});
});
if (!callback) {
return promise.then(results => [results]);
return promise.then(results => [results, ...otherArgs]);
}
promise.then(
results => callback(null, results),
results => callback(null, results, ...otherArgs),
(err: Error) => callback(err)
);
}
Expand Down
10 changes: 9 additions & 1 deletion src/resource-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class ResourceStream<T> extends Transform implements ResourceEvents<T> {
_ended: boolean;
_maxApiCalls: number;
_nextQuery: {} | null;
_otherArgs: unknown[];
_reading: boolean;
_requestFn: Function;
_requestsMade: number;
Expand All @@ -46,6 +47,7 @@ export class ResourceStream<T> extends Transform implements ResourceEvents<T> {
this._requestFn = requestFn;
this._requestsMade = 0;
this._resultsToSend = args.maxResults === -1 ? Infinity : args.maxResults!;
this._otherArgs = [];
}
/* eslint-disable @typescript-eslint/no-explicit-any */
end(
Expand All @@ -67,12 +69,18 @@ export class ResourceStream<T> extends Transform implements ResourceEvents<T> {
try {
this._requestFn(
this._nextQuery,
(err: Error | null, results: T[], nextQuery: {} | null) => {
(
err: Error | null,
results: T[],
nextQuery: {} | null,
...otherArgs: []
) => {
if (err) {
this.destroy(err);
return;
}

this._otherArgs = otherArgs;
this._nextQuery = nextQuery;

if (this._resultsToSend !== Infinity) {
Expand Down
55 changes: 55 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,38 @@ describe('paginator', () => {

paginator.run_(parsedArguments, util.noop);
});

it('should return all results and extra args', done => {
const results = [{a: 1}, {b: 2}, {c: 3}];
const args: any[] = [{msg: 'OK'}, 10];

const parsedArguments = {
autoPaginate: true,
callback(
err: Error,
results_: {},
fakeRes: {},
anotherArg: number
) {
assert.deepStrictEqual(results_, results);
assert.deepStrictEqual(fakeRes, {msg: 'OK'});
assert.deepStrictEqual(anotherArg, 10);
done();
},
};

sandbox.stub(paginator, 'runAsStream_').callsFake(() => {
const stream = createFakeStream();
setImmediate(() => {
results.forEach(result => stream.push(result));
stream.push(null);
stream._otherArgs = args;
});
return stream;
});

paginator.run_(parsedArguments, util.noop);
});
});

describe('original method is promise based', () => {
Expand Down Expand Up @@ -434,6 +466,29 @@ describe('paginator', () => {
assert.deepStrictEqual(results_, results)
);
});

it('should resolve with all results and extra args', () => {
const results = [{a: 1}, {b: 2}, {c: 3}];
const args: any[] = [{msg: 'OK'}, 10];

sandbox.stub(paginator, 'runAsStream_').callsFake(() => {
const stream = createFakeStream();
setImmediate(() => {
results.forEach(result => stream.push(result));
stream.push(null);
stream._otherArgs = args;
});
return stream;
});

paginator
.run_(parsedArguments, util.noop)
.then(([results_, fakeRes, anotherArg]: unknown[]) => {
assert.deepStrictEqual(results_, results);
assert.deepEqual(fakeRes, {msg: 'OK'});
assert.deepEqual(anotherArg, 10);
});
});
});
});

Expand Down
11 changes: 11 additions & 0 deletions test/resource-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,17 @@ describe('ResourceStream', () => {
assert.strictEqual(stream._nextQuery, fakeQuery);
});

it('should cache the rest of the callback arguments', () => {
const fakeRes = {status: 'OK'};
const anotherArg = 10;

stream._read();
const callback = requestSpy.lastCall.args[1];
callback(null, [], {}, fakeRes, anotherArg);

assert.deepStrictEqual(stream._otherArgs, [fakeRes, anotherArg]);
});

it('should adjust the results to send counter', () => {
const maxResults = 100;
const results = [{}, {}];
Expand Down

0 comments on commit cc5c48b

Please sign in to comment.