Skip to content

Commit 0aef7d3

Browse files
authored
Merge pull request #189 from gregolsky/v4.1
minor fixes
2 parents f1bc092 + 812db5c commit 0aef7d3

File tree

3 files changed

+57
-58
lines changed

3 files changed

+57
-58
lines changed

src/Documents/Conventions/DocumentConventions.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ export class DocumentConventions {
5050

5151
private _transformClassCollectionNameToDocumentIdPrefix: (maybeClassCollectionName: string) => string;
5252
private _documentIdGenerator: IdConvention;
53-
private _findIdentityPropertyNameFromCollectionName: (collectionName: string) => string;
5453

5554
private _findCollectionName: (constructorOrTypeChecker: ObjectTypeDescriptor) => string;
5655

56+
private _identityProperty: string;
57+
5758
private _findJsTypeName: (ctorOrTypeChecker: ObjectTypeDescriptor) => string;
5859
private _findJsType: (id: string, doc: object) => ObjectTypeDescriptor;
5960

@@ -77,8 +78,7 @@ export class DocumentConventions {
7778
public constructor() {
7879
this._readBalanceBehavior = "None";
7980
this._identityPartsSeparator = "/";
80-
81-
this._findIdentityPropertyNameFromCollectionName = () => "id";
81+
this._identityProperty = CONSTANTS.Documents.Metadata.ID_PROPERTY;
8282

8383
this._findJsType = (id: string, doc: object) => {
8484
const metadata = doc[CONSTANTS.Documents.Metadata.KEY];
@@ -271,6 +271,15 @@ export class DocumentConventions {
271271
this._useOptimisticConcurrency = useOptimisticConcurrency;
272272
}
273273

274+
public get identityProperty() {
275+
return this._identityProperty;
276+
}
277+
278+
public set identityProperty(val) {
279+
this._assertNotFrozen();
280+
this._identityProperty = val;
281+
}
282+
274283
public get findJsType() {
275284
return this._findJsType;
276285
}
@@ -298,15 +307,6 @@ export class DocumentConventions {
298307
this._findCollectionName = value;
299308
}
300309

301-
public get findIdentityPropertyNameFromCollectionName() {
302-
return this._findIdentityPropertyNameFromCollectionName;
303-
}
304-
305-
public set findIdentityPropertyNameFromCollectionName(value) {
306-
this._assertNotFrozen();
307-
this._findIdentityPropertyNameFromCollectionName = value;
308-
}
309-
310310
public get documentIdGenerator() {
311311
return this._documentIdGenerator;
312312
}
@@ -500,7 +500,7 @@ export class DocumentConventions {
500500
public getIdentityProperty(documentType: DocumentType): string {
501501
const typeDescriptor = this.getJsTypeByDocumentType(documentType);
502502
return this._registeredIdPropertyNames.get(typeDescriptor)
503-
|| CONSTANTS.Documents.Metadata.ID_PROPERTY;
503+
|| this._identityProperty;
504504
}
505505

506506
public updateFrom(configuration: ClientConfiguration): void {

src/Http/RequestExecutor.ts

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as os from "os";
22
import * as BluebirdPromise from "bluebird";
33
import * as semaphore from "semaphore";
44
import * as stream from "readable-stream";
5-
import { acquireSemaphore } from "../Utility/SemaphoreUtil";
5+
import { acquireSemaphore, AcquiredSemaphoreContext } from "../Utility/SemaphoreUtil";
66
import { getLogger, ILogger } from "../Utility/LogUtil";
77
import { Timer } from "../Primitives/Timer";
88
import { ServerNode } from "./ServerNode";
@@ -326,54 +326,53 @@ export class RequestExecutor implements IDisposable {
326326
return this._nodeSelector.getFastestNode();
327327
}
328328

329-
protected _updateClientConfiguration(): PromiseLike<void> {
330-
if (this._disposed) {
331-
return BluebirdPromise.resolve(null);
332-
}
329+
private async _updateClientConfigurationInternal(): Promise<void> {
330+
const oldDisableClientConfigurationUpdates = this._disableClientConfigurationUpdates;
331+
this._disableClientConfigurationUpdates = true;
333332

334-
const updateClientConfigurationInternal = () => {
335-
const oldDisableClientConfigurationUpdates = this._disableClientConfigurationUpdates;
336-
this._disableClientConfigurationUpdates = true;
333+
try {
334+
if (this._disposed) {
335+
return;
336+
}
337337

338-
return BluebirdPromise.resolve()
339-
.then(() => {
338+
const command = new GetClientConfigurationCommand();
339+
const { currentNode, currentIndex } = this.chooseNodeForRequest(command, null);
340+
await this.execute(command, null, {
341+
chosenNode: currentNode,
342+
nodeIndex: currentIndex,
343+
shouldRetry: false
344+
});
340345

341-
if (this._disposed) {
342-
return;
343-
}
346+
const clientConfigOpResult = command.result;
347+
if (!clientConfigOpResult) {
348+
return;
349+
}
344350

345-
const command = new GetClientConfigurationCommand();
346-
const currentIndexAndNode2: CurrentIndexAndNode = this.chooseNodeForRequest(command, null);
347-
return this.execute(command, null, {
348-
chosenNode: currentIndexAndNode2.currentNode,
349-
nodeIndex: currentIndexAndNode2.currentIndex,
350-
shouldRetry: false
351-
})
352-
.then(() => command.result);
353-
})
354-
.then((clientConfigOpResult: GetClientConfigurationOperationResult) => {
355-
if (!clientConfigOpResult) {
356-
return;
357-
}
351+
this._conventions.updateFrom(clientConfigOpResult.configuration);
352+
this._clientConfigurationEtag = clientConfigOpResult.etag;
353+
} catch (err) {
354+
this._log.error(err, "Error getting client configuration.");
355+
} finally {
356+
this._disableClientConfigurationUpdates = oldDisableClientConfigurationUpdates;
357+
}
358+
}
358359

359-
this._conventions.updateFrom(clientConfigOpResult.configuration);
360-
this._clientConfigurationEtag = clientConfigOpResult.etag;
360+
protected async _updateClientConfiguration(): Promise<void> {
361+
if (this._disposed) {
362+
return;
363+
}
361364

362-
})
363-
.tapCatch(err => this._log.error(err, "Error getting client configuration."))
364-
.finally(() => {
365-
this._disableClientConfigurationUpdates = oldDisableClientConfigurationUpdates;
366-
});
367-
};
365+
let semAcquiredContext: AcquiredSemaphoreContext;
368366

369-
const semAcquiredContext = acquireSemaphore(this._updateClientConfigurationSemaphore);
370-
const result = BluebirdPromise.resolve(semAcquiredContext.promise)
371-
.then(() => updateClientConfigurationInternal())
372-
.finally(() => {
367+
try {
368+
semAcquiredContext = acquireSemaphore(this._updateClientConfigurationSemaphore);
369+
await semAcquiredContext.promise;
370+
await this._updateClientConfigurationInternal();
371+
} finally {
372+
if (semAcquiredContext) {
373373
semAcquiredContext.dispose();
374-
});
375-
376-
return Promise.resolve(result);
374+
}
375+
}
377376
}
378377

379378
public updateTopology(node: ServerNode, timeout: number, forceUpdate: boolean = false): Promise<boolean> {

test/Documents/CustomKeyCaseConventionsTests.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ describe("With custom key case conventions set", function () {
9090
s.conventions.findCollectionNameForObjectLiteral = (o) => o["collection"];
9191
s.conventions.entityFieldNameConvention = "camel";
9292
s.conventions.remoteEntityFieldNameConvention = "pascal";
93-
s.conventions.findIdentityPropertyNameFromCollectionName = () => "Id";
93+
s.conventions.identityProperty = "Id";
9494
s.conventions.registerEntityIdPropertyName(Object, "Id");
9595
});
9696

@@ -129,7 +129,7 @@ describe("With custom key case conventions set", function () {
129129
s.conventions.findCollectionNameForObjectLiteral = (o) => o["collection"];
130130
s.conventions.entityFieldNameConvention = "camel";
131131
s.conventions.remoteEntityFieldNameConvention = "pascal";
132-
s.conventions.findIdentityPropertyNameFromCollectionName = () => "Id";
132+
s.conventions.identityProperty = "Id";
133133
s.conventions.registerEntityIdPropertyName(Object, "Id");
134134
});
135135

@@ -190,7 +190,7 @@ describe("With custom key case conventions set", function () {
190190
s.conventions.findCollectionNameForObjectLiteral = (o) => o["collection"];
191191
s.conventions.entityFieldNameConvention = "camel";
192192
s.conventions.remoteEntityFieldNameConvention = "pascal";
193-
s.conventions.findIdentityPropertyNameFromCollectionName = () => "Id";
193+
s.conventions.identityProperty = "Id";
194194
s.conventions.registerEntityIdPropertyName(Object, "Id");
195195
});
196196

@@ -226,7 +226,7 @@ describe("With custom key case conventions set", function () {
226226
s.conventions.findCollectionNameForObjectLiteral = (o) => o["collection"];
227227
s.conventions.entityFieldNameConvention = "camel";
228228
s.conventions.remoteEntityFieldNameConvention = "pascal";
229-
s.conventions.findIdentityPropertyNameFromCollectionName = () => "Id";
229+
s.conventions.identityProperty = "Id";
230230
s.conventions.registerEntityIdPropertyName(Object, "Id");
231231
});
232232

0 commit comments

Comments
 (0)