@@ -45,14 +45,13 @@ export class HiloIdGenerator {
45
45
}
46
46
47
47
public async nextId ( ) : Promise < number > {
48
-
49
- const getNextIdWithinRange = async ( ) : Promise < number > => {
48
+ while ( true ) {
50
49
// local range is not exhausted yet
51
50
const range = this . _range ;
52
51
53
52
let id = range . increment ( ) ;
54
53
if ( id <= range . maxId ) {
55
- return Promise . resolve ( id ) ;
54
+ return id ;
56
55
}
57
56
58
57
let acquiredSemContext : AcquiredSemaphoreContext ;
@@ -73,15 +72,12 @@ export class HiloIdGenerator {
73
72
}
74
73
75
74
await this . _getNextRange ( ) ;
76
- return getNextIdWithinRange ( ) ;
77
75
} finally {
78
76
if ( acquiredSemContext ) {
79
77
acquiredSemContext . dispose ( ) ;
80
78
}
81
79
}
82
- } ;
83
-
84
- return getNextIdWithinRange ( ) ;
80
+ }
85
81
}
86
82
87
83
public returnUnusedRange ( ) : Promise < void > {
@@ -91,24 +87,24 @@ export class HiloIdGenerator {
91
87
return executor . execute ( new HiloReturnCommand ( this . _tag , range . current , range . maxId ) ) ;
92
88
}
93
89
94
- protected _getNextRange ( ) : Promise < void > {
90
+ protected async _getNextRange ( ) : Promise < void > {
95
91
const hiloCmd = new NextHiloCommand (
96
92
this . _tag ,
97
93
this . _lastBatchSize ,
98
94
this . _lastRangeAt ,
99
95
this . _identityPartsSeparator ,
100
96
this . _range . maxId ,
101
97
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 ) ;
112
108
}
113
109
114
110
protected _assembleDocumentId ( currentRangeValue : number ) : string {
0 commit comments