1 | ;
|
2 | /**
|
3 | * -------------------------------------------------------------------------------------------
|
4 | * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
|
5 | * See License in the project root for license information.
|
6 | * -------------------------------------------------------------------------------------------
|
7 | */
|
8 | Object.defineProperty(exports, "__esModule", { value: true });
|
9 | exports.PageIterator = void 0;
|
10 | var tslib_1 = require("tslib");
|
11 | /**
|
12 | * @class
|
13 | * Class for PageIterator
|
14 | */
|
15 | var PageIterator = /** @class */ (function () {
|
16 | /**
|
17 | * @public
|
18 | * @constructor
|
19 | * Creates new instance for PageIterator
|
20 | * @param {Client} client - The graph client instance
|
21 | * @param {PageCollection} pageCollection - The page collection object
|
22 | * @param {PageIteratorCallback} callBack - The callback function
|
23 | * @param {GraphRequestOptions} requestOptions - The request options
|
24 | * @returns An instance of a PageIterator
|
25 | */
|
26 | function PageIterator(client, pageCollection, callback, requestOptions) {
|
27 | this.client = client;
|
28 | this.collection = pageCollection.value;
|
29 | this.nextLink = pageCollection["@odata.nextLink"];
|
30 | this.deltaLink = pageCollection["@odata.deltaLink"];
|
31 | this.callback = callback;
|
32 | this.cursor = 0;
|
33 | this.complete = false;
|
34 | this.requestOptions = requestOptions;
|
35 | }
|
36 | /**
|
37 | * @private
|
38 | * Iterates over a collection by enqueuing entries one by one and kicking the callback with the enqueued entry
|
39 | * @returns A boolean indicating the continue flag to process next page
|
40 | */
|
41 | PageIterator.prototype.iterationHelper = function () {
|
42 | if (this.collection === undefined) {
|
43 | return false;
|
44 | }
|
45 | var advance = true;
|
46 | while (advance && this.cursor < this.collection.length) {
|
47 | var item = this.collection[this.cursor];
|
48 | advance = this.callback(item);
|
49 | this.cursor++;
|
50 | }
|
51 | return advance;
|
52 | };
|
53 | /**
|
54 | * @private
|
55 | * @async
|
56 | * Helper to make a get request to fetch next page with nextLink url and update the page iterator instance with the returned response
|
57 | * @returns A promise that resolves to a response data with next page collection
|
58 | */
|
59 | PageIterator.prototype.fetchAndUpdateNextPageData = function () {
|
60 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
61 | var graphRequest, response;
|
62 | return tslib_1.__generator(this, function (_a) {
|
63 | switch (_a.label) {
|
64 | case 0:
|
65 | graphRequest = this.client.api(this.nextLink);
|
66 | if (this.requestOptions) {
|
67 | if (this.requestOptions.headers) {
|
68 | graphRequest = graphRequest.headers(this.requestOptions.headers);
|
69 | }
|
70 | if (this.requestOptions.middlewareOptions) {
|
71 | graphRequest = graphRequest.middlewareOptions(this.requestOptions.middlewareOptions);
|
72 | }
|
73 | if (this.requestOptions.options) {
|
74 | graphRequest = graphRequest.options(this.requestOptions.options);
|
75 | }
|
76 | }
|
77 | return [4 /*yield*/, graphRequest.get()];
|
78 | case 1:
|
79 | response = _a.sent();
|
80 | this.collection = response.value;
|
81 | this.cursor = 0;
|
82 | this.nextLink = response["@odata.nextLink"];
|
83 | this.deltaLink = response["@odata.deltaLink"];
|
84 | return [2 /*return*/];
|
85 | }
|
86 | });
|
87 | });
|
88 | };
|
89 | /**
|
90 | * @public
|
91 | * Getter to get the deltaLink in the current response
|
92 | * @returns A deltaLink which is being used to make delta requests in future
|
93 | */
|
94 | PageIterator.prototype.getDeltaLink = function () {
|
95 | return this.deltaLink;
|
96 | };
|
97 | /**
|
98 | * @public
|
99 | * @async
|
100 | * Iterates over the collection and kicks callback for each item on iteration. Fetches next set of data through nextLink and iterates over again
|
101 | * This happens until the nextLink is drained out or the user responds with a red flag to continue from callback
|
102 | * @returns A Promise that resolves to nothing on completion and throws error incase of any discrepancy.
|
103 | */
|
104 | PageIterator.prototype.iterate = function () {
|
105 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
106 | var advance;
|
107 | return tslib_1.__generator(this, function (_a) {
|
108 | switch (_a.label) {
|
109 | case 0:
|
110 | advance = this.iterationHelper();
|
111 | _a.label = 1;
|
112 | case 1:
|
113 | if (!advance) return [3 /*break*/, 5];
|
114 | if (!(this.nextLink !== undefined)) return [3 /*break*/, 3];
|
115 | return [4 /*yield*/, this.fetchAndUpdateNextPageData()];
|
116 | case 2:
|
117 | _a.sent();
|
118 | advance = this.iterationHelper();
|
119 | return [3 /*break*/, 4];
|
120 | case 3:
|
121 | advance = false;
|
122 | _a.label = 4;
|
123 | case 4: return [3 /*break*/, 1];
|
124 | case 5:
|
125 | if (this.nextLink === undefined && this.cursor >= this.collection.length) {
|
126 | this.complete = true;
|
127 | }
|
128 | return [2 /*return*/];
|
129 | }
|
130 | });
|
131 | });
|
132 | };
|
133 | /**
|
134 | * @public
|
135 | * @async
|
136 | * To resume the iteration
|
137 | * Note: This internally calls the iterate method, It's just for more readability.
|
138 | * @returns A Promise that resolves to nothing on completion and throws error incase of any discrepancy
|
139 | */
|
140 | PageIterator.prototype.resume = function () {
|
141 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
142 | return tslib_1.__generator(this, function (_a) {
|
143 | return [2 /*return*/, this.iterate()];
|
144 | });
|
145 | });
|
146 | };
|
147 | /**
|
148 | * @public
|
149 | * To get the completeness status of the iterator
|
150 | * @returns Boolean indicating the completeness
|
151 | */
|
152 | PageIterator.prototype.isComplete = function () {
|
153 | return this.complete;
|
154 | };
|
155 | return PageIterator;
|
156 | }());
|
157 | exports.PageIterator = PageIterator;
|
158 | //# sourceMappingURL=PageIterator.js.map |
\ | No newline at end of file |