Skip to content

Commit a9d30b7

Browse files
committed
RDBC-263 fix warning for HiloIdGenerator - promise was created in a handler
1 parent f59e87c commit a9d30b7

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

src/Documents/Identity/HiloIdGenerator.ts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as semaphore from "semaphore";
33

44
import { IDocumentStore } from "../../Documents/IDocumentStore";
55
import { DateUtil } from "../../Utility/DateUtil";
6-
import { acquireSemaphore } from "../../Utility/SemaphoreUtil";
6+
import { acquireSemaphore, AcquiredSemaphoreContext } from "../../Utility/SemaphoreUtil";
77
import { StringUtil } from "../../Utility/StringUtil";
88
import { HiloReturnCommand } from "./Commands/HiloReturnCommand";
99
import { NextHiloCommand, HiLoResult } from "./Commands/NextHiloCommand";
@@ -44,9 +44,9 @@ export class HiloIdGenerator {
4444
return this._prefix + nextId + "-" + this._serverTag;
4545
}
4646

47-
public nextId(): Promise<number> {
47+
public async nextId(): Promise<number> {
4848

49-
const getNextIdWithinRange = (): Promise<number> => {
49+
const getNextIdWithinRange = async (): Promise<number> => {
5050
// local range is not exhausted yet
5151
const range = this._range;
5252

@@ -55,26 +55,30 @@ export class HiloIdGenerator {
5555
return Promise.resolve(id);
5656
}
5757

58-
//local range is exhausted , need to get a new range
59-
const acquiredSemContext = acquireSemaphore(this._generatorLock, {
60-
contextName: `${this.constructor.name}_${this._tag}`
61-
});
62-
return Promise.resolve(acquiredSemContext.promise)
63-
.then(() => {
64-
65-
const maybeNewRange = this._range;
66-
if (maybeNewRange !== range) {
67-
id = maybeNewRange.increment();
68-
if (id <= maybeNewRange.maxId) {
69-
return BluebirdPromise.resolve(id)
70-
.finally(() => acquiredSemContext.dispose());
71-
}
72-
}
73-
74-
return BluebirdPromise.resolve(this._getNextRange())
75-
.finally(() => acquiredSemContext.dispose())
76-
.then(() => getNextIdWithinRange());
58+
let acquiredSemContext: AcquiredSemaphoreContext;
59+
try {
60+
//local range is exhausted , need to get a new range
61+
acquiredSemContext = acquireSemaphore(this._generatorLock, {
62+
contextName: `${this.constructor.name}_${this._tag}`
7763
});
64+
65+
await acquiredSemContext.promise;
66+
67+
const maybeNewRange = this._range;
68+
if (maybeNewRange !== range) {
69+
id = maybeNewRange.increment();
70+
if (id <= maybeNewRange.maxId) {
71+
return id;
72+
}
73+
}
74+
75+
await this._getNextRange();
76+
return getNextIdWithinRange();
77+
} finally {
78+
if (acquiredSemContext) {
79+
acquiredSemContext.dispose();
80+
}
81+
}
7882
};
7983

8084
return getNextIdWithinRange();

0 commit comments

Comments
 (0)