UNPKG

4.57 kBJavaScriptView Raw
1import { __asyncGenerator, __await } from "tslib";
2import { ChangeFeedResponse } from "./ChangeFeedResponse";
3import { Constants, ResourceType, StatusCodes } from "./common";
4/**
5 * Provides iterator for change feed.
6 *
7 * Use `Items.changeFeed()` to get an instance of the iterator.
8 */
9export class ChangeFeedIterator {
10 /**
11 * @internal
12 */
13 constructor(clientContext, resourceId, resourceLink, partitionKey, changeFeedOptions) {
14 this.clientContext = clientContext;
15 this.resourceId = resourceId;
16 this.resourceLink = resourceLink;
17 this.partitionKey = partitionKey;
18 this.changeFeedOptions = changeFeedOptions;
19 // partition key XOR partition key range id
20 const partitionKeyValid = partitionKey !== undefined;
21 this.isPartitionSpecified = partitionKeyValid;
22 let canUseStartFromBeginning = true;
23 if (changeFeedOptions.continuation) {
24 this.nextIfNoneMatch = changeFeedOptions.continuation;
25 canUseStartFromBeginning = false;
26 }
27 if (changeFeedOptions.startTime) {
28 // .toUTCString() is platform specific, but most platforms use RFC 1123.
29 // In ECMAScript 2018, this was standardized to RFC 1123.
30 // See for more info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toUTCString
31 this.ifModifiedSince = changeFeedOptions.startTime.toUTCString();
32 canUseStartFromBeginning = false;
33 }
34 if (canUseStartFromBeginning && !changeFeedOptions.startFromBeginning) {
35 this.nextIfNoneMatch = ChangeFeedIterator.IfNoneMatchAllHeaderValue;
36 }
37 }
38 /**
39 * Gets a value indicating whether there are potentially additional results that can be retrieved.
40 *
41 * Initially returns true. This value is set based on whether the last execution returned a continuation token.
42 *
43 * @returns Boolean value representing if whether there are potentially additional results that can be retrieved.
44 */
45 get hasMoreResults() {
46 return this.lastStatusCode !== StatusCodes.NotModified;
47 }
48 /**
49 * Gets an async iterator which will yield pages of results from Azure Cosmos DB.
50 */
51 getAsyncIterator() {
52 return __asyncGenerator(this, arguments, function* getAsyncIterator_1() {
53 do {
54 const result = yield __await(this.fetchNext());
55 if (result.count > 0) {
56 yield yield __await(result);
57 }
58 } while (this.hasMoreResults);
59 });
60 }
61 /**
62 * Read feed and retrieves the next page of results in Azure Cosmos DB.
63 */
64 async fetchNext() {
65 const response = await this.getFeedResponse();
66 this.lastStatusCode = response.statusCode;
67 this.nextIfNoneMatch = response.headers[Constants.HttpHeaders.ETag];
68 return response;
69 }
70 async getFeedResponse() {
71 if (!this.isPartitionSpecified) {
72 throw new Error("Container is partitioned, but no partition key or partition key range id was specified.");
73 }
74 const feedOptions = { initialHeaders: {}, useIncrementalFeed: true };
75 if (typeof this.changeFeedOptions.maxItemCount === "number") {
76 feedOptions.maxItemCount = this.changeFeedOptions.maxItemCount;
77 }
78 if (this.changeFeedOptions.sessionToken) {
79 feedOptions.sessionToken = this.changeFeedOptions.sessionToken;
80 }
81 if (this.nextIfNoneMatch) {
82 feedOptions.accessCondition = {
83 type: Constants.HttpHeaders.IfNoneMatch,
84 condition: this.nextIfNoneMatch,
85 };
86 }
87 if (this.ifModifiedSince) {
88 feedOptions.initialHeaders[Constants.HttpHeaders.IfModifiedSince] = this.ifModifiedSince;
89 }
90 const response = await this.clientContext.queryFeed({
91 path: this.resourceLink,
92 resourceType: ResourceType.item,
93 resourceId: this.resourceId,
94 resultFn: (result) => (result ? result.Documents : []),
95 query: undefined,
96 options: feedOptions,
97 partitionKey: this.partitionKey,
98 }); // TODO: some funky issues with query feed. Probably need to change it up.
99 return new ChangeFeedResponse(response.result, response.result ? response.result.length : 0, response.code, response.headers);
100 }
101}
102ChangeFeedIterator.IfNoneMatchAllHeaderValue = "*";
103//# sourceMappingURL=ChangeFeedIterator.js.map
\No newline at end of file