1 | import { __asyncGenerator, __await } from "tslib";
|
2 | import { ChangeFeedResponse } from "./ChangeFeedResponse";
|
3 | import { Constants, ResourceType, StatusCodes } from "./common";
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | export class ChangeFeedIterator {
|
10 | |
11 |
|
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 |
|
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 |
|
29 |
|
30 |
|
31 | this.ifModifiedSince = changeFeedOptions.startTime.toUTCString();
|
32 | canUseStartFromBeginning = false;
|
33 | }
|
34 | if (canUseStartFromBeginning && !changeFeedOptions.startFromBeginning) {
|
35 | this.nextIfNoneMatch = ChangeFeedIterator.IfNoneMatchAllHeaderValue;
|
36 | }
|
37 | }
|
38 | |
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | get hasMoreResults() {
|
46 | return this.lastStatusCode !== StatusCodes.NotModified;
|
47 | }
|
48 | |
49 |
|
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 |
|
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 | });
|
99 | return new ChangeFeedResponse(response.result, response.result ? response.result.length : 0, response.code, response.headers);
|
100 | }
|
101 | }
|
102 | ChangeFeedIterator.IfNoneMatchAllHeaderValue = "*";
|
103 |
|
\ | No newline at end of file |