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I,CAAC,eAAe,EAAE;YACxB,WAAW,CAAC,cAAc,CAAC,SAAS,CAAC,WAAW,CAAC,eAAe,CAAC,GAAG,IAAI,CAAC,eAAe,CAAC;SAC1F;QAED,MAAM,QAAQ,GAAkC,MAAO,IAAI,CAAC,aAAa,CAAC,SAAS,CAAI;YACrF,IAAI,EAAE,IAAI,CAAC,YAAY;YACvB,YAAY,EAAE,YAAY,CAAC,IAAI;YAC/B,UAAU,EAAE,IAAI,CAAC,UAAU;YAC3B,QAAQ,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC;YACtD,KAAK,EAAE,SAAS;YAChB,OAAO,EAAE,WAAW;YACpB,YAAY,EAAE,IAAI,CAAC,YAAY;SAChC,CAAkB,CAAC,CAAC,0EAA0E;QAE/F,OAAO,IAAI,kBAAkB,CAC3B,QAAQ,CAAC,MAAM,EACf,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAC5C,QAAQ,CAAC,IAAI,EACb,QAAQ,CAAC,OAAO,CACjB,CAAC;IACJ,CAAC;;AAnHuB,4CAAyB,GAAG,GAAG,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n/// <reference lib=\"esnext.asynciterable\" />\nimport { ChangeFeedOptions } from \"./ChangeFeedOptions\";\nimport { ChangeFeedResponse } from \"./ChangeFeedResponse\";\nimport { Resource } from \"./client\";\nimport { ClientContext } from \"./ClientContext\";\nimport { Constants, ResourceType, StatusCodes } from \"./common\";\nimport { FeedOptions } from \"./request\";\nimport { Response } from \"./request\";\n\n/**\n * Provides iterator for change feed.\n *\n * Use `Items.changeFeed()` to get an instance of the iterator.\n */\nexport class ChangeFeedIterator<T> {\n private static readonly IfNoneMatchAllHeaderValue = \"*\";\n private nextIfNoneMatch: string;\n private ifModifiedSince: string;\n private lastStatusCode: number;\n private isPartitionSpecified: boolean;\n\n /**\n * @internal\n */\n constructor(\n private clientContext: ClientContext,\n private resourceId: string,\n private resourceLink: string,\n private partitionKey: string | number | boolean,\n private changeFeedOptions: ChangeFeedOptions\n ) {\n // partition key XOR partition key range id\n const partitionKeyValid = partitionKey !== undefined;\n this.isPartitionSpecified = partitionKeyValid;\n\n let canUseStartFromBeginning = true;\n if (changeFeedOptions.continuation) {\n this.nextIfNoneMatch = changeFeedOptions.continuation;\n canUseStartFromBeginning = false;\n }\n\n if (changeFeedOptions.startTime) {\n // .toUTCString() is platform specific, but most platforms use RFC 1123.\n // In ECMAScript 2018, this was standardized to RFC 1123.\n // See for more info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toUTCString\n this.ifModifiedSince = changeFeedOptions.startTime.toUTCString();\n canUseStartFromBeginning = false;\n }\n\n if (canUseStartFromBeginning && !changeFeedOptions.startFromBeginning) {\n this.nextIfNoneMatch = ChangeFeedIterator.IfNoneMatchAllHeaderValue;\n }\n }\n\n /**\n * Gets a value indicating whether there are potentially additional results that can be retrieved.\n *\n * Initially returns true. This value is set based on whether the last execution returned a continuation token.\n *\n * @returns Boolean value representing if whether there are potentially additional results that can be retrieved.\n */\n get hasMoreResults(): boolean {\n return this.lastStatusCode !== StatusCodes.NotModified;\n }\n\n /**\n * Gets an async iterator which will yield pages of results from Azure Cosmos DB.\n */\n public async *getAsyncIterator(): AsyncIterable<ChangeFeedResponse<Array<T & Resource>>> {\n do {\n const result = await this.fetchNext();\n if (result.count > 0) {\n yield result;\n }\n } while (this.hasMoreResults);\n }\n\n /**\n * Read feed and retrieves the next page of results in Azure Cosmos DB.\n */\n public async fetchNext(): Promise<ChangeFeedResponse<Array<T & Resource>>> {\n const response = await this.getFeedResponse();\n this.lastStatusCode = response.statusCode;\n this.nextIfNoneMatch = response.headers[Constants.HttpHeaders.ETag];\n return response;\n }\n\n private async getFeedResponse(): Promise<ChangeFeedResponse<Array<T & Resource>>> {\n if (!this.isPartitionSpecified) {\n throw new Error(\n \"Container is partitioned, but no partition key or partition key range id was specified.\"\n );\n }\n const feedOptions: FeedOptions = { initialHeaders: {}, useIncrementalFeed: true };\n\n if (typeof this.changeFeedOptions.maxItemCount === \"number\") {\n feedOptions.maxItemCount = this.changeFeedOptions.maxItemCount;\n }\n\n if (this.changeFeedOptions.sessionToken) {\n feedOptions.sessionToken = this.changeFeedOptions.sessionToken;\n }\n\n if (this.nextIfNoneMatch) {\n feedOptions.accessCondition = {\n type: Constants.HttpHeaders.IfNoneMatch,\n condition: this.nextIfNoneMatch,\n };\n }\n\n if (this.ifModifiedSince) {\n feedOptions.initialHeaders[Constants.HttpHeaders.IfModifiedSince] = this.ifModifiedSince;\n }\n\n const response: Response<Array<T & Resource>> = await (this.clientContext.queryFeed<T>({\n path: this.resourceLink,\n resourceType: ResourceType.item,\n resourceId: this.resourceId,\n resultFn: (result) => (result ? result.Documents : []),\n query: undefined,\n options: feedOptions,\n partitionKey: this.partitionKey,\n }) as Promise<any>); // TODO: some funky issues with query feed. Probably need to change it up.\n\n return new ChangeFeedResponse(\n response.result,\n response.result ? response.result.length : 0,\n response.code,\n response.headers\n );\n }\n}\n"]}
\No newline at end of file