Skip to content

Commit

Permalink
Merge branch 'fix-sentinel-warning'
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Jul 19, 2018
2 parents 032e08b + 0f42e45 commit e76c44f
Show file tree
Hide file tree
Showing 5 changed files with 330 additions and 283 deletions.
269 changes: 0 additions & 269 deletions lib/connectors/SentinelConnector.ts

This file was deleted.

43 changes: 43 additions & 0 deletions lib/connectors/SentinelConnector/SentinelIterator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {ISentinelAddress} from './types'

function isSentinelEql (a: ISentinelAddress, b: ISentinelAddress): boolean {
return ((a.host || '127.0.0.1') === (b.host || '127.0.0.1')) &&
((a.port || 26379) === (b.port || 26379))
}

export default class SentinelIterator {
private cursor: number = 0

constructor (private sentinels: ISentinelAddress[]) {}

hasNext (): boolean {
return this.cursor < this.sentinels.length
}

next (): ISentinelAddress | null {
return this.hasNext() ? this.sentinels[this.cursor++] : null
}

reset (moveCurrentEndpointToFirst: boolean): void {
if (moveCurrentEndpointToFirst && this.sentinels.length > 1 && this.cursor !== 1) {
const remains = this.sentinels.slice(this.cursor - 1)
this.sentinels = remains.concat(this.sentinels.slice(0, this.cursor - 1))
}
this.cursor = 0
}

add (sentinel: ISentinelAddress): boolean {
for (let i = 0; i < this.sentinels.length; i++) {
if (isSentinelEql(sentinel, this.sentinels[i])) {
return false
}
}

this.sentinels.push(sentinel)
return true
}

toString (): string {
return `${JSON.stringify(this.sentinels)} @${this.cursor}`
}
}

0 comments on commit e76c44f

Please sign in to comment.