@@ -19,22 +19,23 @@ export class Buffer {
19
19
eofSource : Subject < boolean > ;
20
20
minIndexUser : number ;
21
21
maxIndexUser : number ;
22
+ startIndexUser : number ;
23
+ startIndex : number ;
22
24
23
- private startIndex : number ;
24
25
private pristine : boolean ;
25
26
private cache : Cache ;
26
27
readonly logger : Logger ;
27
28
28
- constructor ( settings : Settings , startIndex : number , logger : Logger , $items ?: BehaviorSubject < Item [ ] > ) {
29
+ constructor ( settings : Settings , logger : Logger , $items ?: BehaviorSubject < Item [ ] > ) {
30
+ this . logger = logger ;
29
31
this . $items = $items || new BehaviorSubject < Item [ ] > ( [ ] ) ;
30
32
this . bofSource = new Subject < boolean > ( ) ;
31
33
this . eofSource = new Subject < boolean > ( ) ;
32
34
this . cache = new Cache ( settings . itemSize , logger ) ;
35
+ this . startIndexUser = settings . startIndex ;
33
36
this . minIndexUser = settings . minIndex ;
34
37
this . maxIndexUser = settings . maxIndex ;
35
38
this . reset ( ) ;
36
- this . startIndex = startIndex ;
37
- this . logger = logger ;
38
39
}
39
40
40
41
dispose ( forever ?: boolean ) {
@@ -54,14 +55,32 @@ export class Buffer {
54
55
this . cache . reset ( ) ;
55
56
this . absMinIndex = this . minIndexUser ;
56
57
this . absMaxIndex = this . maxIndexUser ;
57
- if ( typeof startIndex !== 'undefined' ) {
58
- this . startIndex = startIndex ;
59
- }
58
+ this . setCurrentStartIndex ( startIndex ) ;
60
59
this . _bof = false ;
61
60
this . _eof = false ;
62
61
this . pristine = false ;
63
62
}
64
63
64
+ setCurrentStartIndex ( newStartIndex ?: any ) {
65
+ const min = this . minIndexUser ;
66
+ const max = this . maxIndexUser ;
67
+ const start = this . startIndexUser ;
68
+ let index = Number ( newStartIndex ) ;
69
+ if ( Number . isNaN ( index ) ) {
70
+ this . logger . log ( ( ) => `fallback startIndex to settings.startIndex (${ start } )` ) ;
71
+ index = start ;
72
+ }
73
+ if ( index < min ) {
74
+ this . logger . log ( ( ) => `setting startIndex to settings.minIndex (${ min } ) because ${ index } < ${ min } ` ) ;
75
+ index = min ;
76
+ }
77
+ if ( index > max ) {
78
+ this . logger . log ( ( ) => `setting startIndex to settings.maxIndex (${ max } ) because ${ index } > ${ max } ` ) ;
79
+ index = max ;
80
+ }
81
+ this . startIndex = index ;
82
+ }
83
+
65
84
set items ( items : Item [ ] ) {
66
85
this . _items = items ;
67
86
this . $items . next ( items ) ;
@@ -227,6 +246,7 @@ export class Buffer {
227
246
this . absMaxIndex -= toRemove . length ;
228
247
} else {
229
248
this . absMinIndex += toRemove . length ;
249
+ this . startIndex += toRemove . length ;
230
250
}
231
251
if ( ! virtual ) {
232
252
this . items = result ;
0 commit comments