File tree 5 files changed +323
-269
lines changed
5 files changed +323
-269
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ import { ISentinelAddress } from './types'
2
+
3
+ function isSentinelEql ( a : ISentinelAddress , b : ISentinelAddress ) : boolean {
4
+ return ( ( a . host || '127.0.0.1' ) === ( b . host || '127.0.0.1' ) ) &&
5
+ ( ( a . port || 26379 ) === ( b . port || 26379 ) )
6
+ }
7
+
8
+ export default class SentinelIterator {
9
+ private cursor : number = 0
10
+
11
+ constructor ( private sentinels : ISentinelAddress [ ] ) { }
12
+
13
+ hasNext ( ) : boolean {
14
+ return this . cursor < this . sentinels . length
15
+ }
16
+
17
+ next ( ) : ISentinelAddress | null {
18
+ return this . hasNext ( ) ? this . sentinels [ this . cursor ++ ] : null
19
+ }
20
+
21
+ reset ( success : boolean ) : void {
22
+ if ( success && this . sentinels . length > 1 && this . cursor !== 1 ) {
23
+ const remains = this . sentinels . slice ( this . cursor - 1 )
24
+ this . sentinels = remains . concat ( this . sentinels . slice ( 0 , this . cursor - 1 ) )
25
+ }
26
+ this . cursor = 0
27
+ }
28
+
29
+ add ( sentinel : ISentinelAddress ) : boolean {
30
+ for ( let i = 0 ; i < this . sentinels . length ; i ++ ) {
31
+ if ( isSentinelEql ( sentinel , this . sentinels [ i ] ) ) {
32
+ return false
33
+ }
34
+ }
35
+
36
+ this . sentinels . push ( sentinel )
37
+ return true
38
+ }
39
+
40
+ toString ( ) : string {
41
+ return JSON . stringify ( this . sentinels )
42
+ }
43
+ }
You can’t perform that action at this time.
0 commit comments