Skip to content
Merged
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
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 12 additions & 3 deletions src/tasks/PageIterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,17 @@ export class PageIterator {
private complete: boolean;

/**
* @private
* Information to be added to the request
*/
private requestOptions: GraphRequestOptions;

/**
* @private
* Member holding the current position on the collection
*/
private cursor: number

/**
* @public
* @constructor
Expand All @@ -109,6 +116,7 @@ export class PageIterator {
this.nextLink = pageCollection["@odata.nextLink"];
this.deltaLink = pageCollection["@odata.deltaLink"];
this.callback = callback;
this.cursor = 0;
this.complete = false;
this.requestOptions = requestOptions;
}
Expand All @@ -123,9 +131,10 @@ export class PageIterator {
return false;
}
let advance = true;
while (advance && this.collection.length !== 0) {
const item = this.collection.shift();
while (advance && this.cursor < this.collection.length) {
const item = this.collection[this.cursor];
advance = this.callback(item);
this.cursor++;
}
return advance;
}
Expand Down Expand Up @@ -182,7 +191,7 @@ export class PageIterator {
advance = false;
}
}
if (this.nextLink === undefined && this.collection.length === 0) {
if (this.nextLink === undefined && this.cursor >= this.collection.length) {
this.complete = true;
}
}
Expand Down
7 changes: 7 additions & 0 deletions test/common/tasks/PageIterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ describe("PageIterator.ts", () => {
assert.isTrue(pageIterator.isComplete());
});

it("Should not mutate the collection", async () => {
const collection = getPageCollection();
const pageIterator = new PageIterator(client, collection, truthyCallback);
await pageIterator.iterate();
assert.deepEqual(collection, getPageCollection());
});

it("Should not iterate over an empty collection", async () => {
const pageIterator = new PageIterator(client, getEmptyPageCollection(), truthyCallback);
halfWayCallbackCounter = 1;
Expand Down