File tree 2 files changed +14
-4
lines changed
2 files changed +14
-4
lines changed Original file line number Diff line number Diff line change @@ -13,7 +13,8 @@ class Ora {
13
13
#linesToClear = 0 ;
14
14
#isDiscardingStdin = false ;
15
15
#lineCount = 0 ;
16
- #frameIndex = 0 ;
16
+ #frameIndex = - 1 ;
17
+ #lastSpinnerFrameTime = 0 ;
17
18
#options;
18
19
#spinner;
19
20
#stream;
@@ -109,7 +110,7 @@ class Ora {
109
110
}
110
111
111
112
set spinner ( spinner ) {
112
- this . #frameIndex = 0 ;
113
+ this . #frameIndex = - 1 ;
113
114
this . #initialInterval = undefined ;
114
115
115
116
if ( typeof spinner === 'object' ) {
@@ -222,14 +223,21 @@ class Ora {
222
223
}
223
224
224
225
frame ( ) {
226
+ // Ensure we only update the spinner frame at the wanted interval,
227
+ // even if the render method is called more often.
228
+ const now = Date . now ( ) ;
229
+ if ( this . #frameIndex === - 1 || now - this . #lastSpinnerFrameTime >= this . interval ) {
230
+ this . #frameIndex = ++ this . #frameIndex % this . #spinner. frames . length ;
231
+ this . #lastSpinnerFrameTime = now ;
232
+ }
233
+
225
234
const { frames} = this . #spinner;
226
235
let frame = frames [ this . #frameIndex] ;
227
236
228
237
if ( this . color ) {
229
238
frame = chalk [ this . color ] ( frame ) ;
230
239
}
231
240
232
- this . #frameIndex = ++ this . #frameIndex % frames . length ;
233
241
const fullPrefixText = ( typeof this . #prefixText === 'string' && this . #prefixText !== '' ) ? this . #prefixText + ' ' : '' ;
234
242
const fullText = typeof this . text === 'string' ? ' ' + this . text : '' ;
235
243
const fullSuffixText = ( typeof this . #suffixText === 'string' && this . #suffixText !== '' ) ? ' ' + this . #suffixText : '' ;
Original file line number Diff line number Diff line change @@ -267,8 +267,10 @@ test('reset frameIndex when setting new spinner', async t => {
267
267
} ,
268
268
} ) ;
269
269
270
+ t . is ( spinner . _frameIndex , - 1 ) ;
271
+
270
272
spinner . render ( ) ;
271
- t . is ( spinner . _frameIndex , 1 ) ;
273
+ t . is ( spinner . _frameIndex , 0 ) ;
272
274
273
275
spinner . spinner = { frames : [ 'baz' ] } ;
274
276
spinner . render ( ) ;
You can’t perform that action at this time.
0 commit comments