@@ -45,14 +45,13 @@ export class HiloIdGenerator {
4545 }
4646
4747 public async nextId ( ) : Promise < number > {
48-
49- const getNextIdWithinRange = async ( ) : Promise < number > => {
48+ while ( true ) {
5049 // local range is not exhausted yet
5150 const range = this . _range ;
5251
5352 let id = range . increment ( ) ;
5453 if ( id <= range . maxId ) {
55- return Promise . resolve ( id ) ;
54+ return id ;
5655 }
5756
5857 let acquiredSemContext : AcquiredSemaphoreContext ;
@@ -73,15 +72,12 @@ export class HiloIdGenerator {
7372 }
7473
7574 await this . _getNextRange ( ) ;
76- return getNextIdWithinRange ( ) ;
7775 } finally {
7876 if ( acquiredSemContext ) {
7977 acquiredSemContext . dispose ( ) ;
8078 }
8179 }
82- } ;
83-
84- return getNextIdWithinRange ( ) ;
80+ }
8581 }
8682
8783 public returnUnusedRange ( ) : Promise < void > {
@@ -91,24 +87,24 @@ export class HiloIdGenerator {
9187 return executor . execute ( new HiloReturnCommand ( this . _tag , range . current , range . maxId ) ) ;
9288 }
9389
94- protected _getNextRange ( ) : Promise < void > {
90+ protected async _getNextRange ( ) : Promise < void > {
9591 const hiloCmd = new NextHiloCommand (
9692 this . _tag ,
9793 this . _lastBatchSize ,
9894 this . _lastRangeAt ,
9995 this . _identityPartsSeparator ,
10096 this . _range . maxId ,
10197 this . _store . conventions ) ;
102- return this . _store . getRequestExecutor ( this . _dbName ) . execute ( hiloCmd )
103- . then ( ( ) => {
104- const result : HiLoResult = hiloCmd . result ;
105- this . _prefix = result . prefix ;
106- this . _lastBatchSize = result . lastSize ;
107- this . _serverTag = result . serverTag || null ;
108- this . _lastRangeAt = result . lastRangeAt ;
109-
110- this . _range = new HiloRangeValue ( result . low , result . high ) ;
111- } ) ;
98+
99+ await this . _store . getRequestExecutor ( this . _dbName ) . execute ( hiloCmd ) ;
100+
101+ const result : HiLoResult = hiloCmd . result ;
102+ this . _prefix = result . prefix ;
103+ this . _lastBatchSize = result . lastSize ;
104+ this . _serverTag = result . serverTag || null ;
105+ this . _lastRangeAt = result . lastRangeAt ;
106+
107+ this . _range = new HiloRangeValue ( result . low , result . high ) ;
112108 }
113109
114110 protected _assembleDocumentId ( currentRangeValue : number ) : string {
0 commit comments