Skip to content

Commit

Permalink
Fix formatting issues in code
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewmolyuk committed Apr 4, 2024
1 parent 8675d44 commit f514e2d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 28 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ npm install mongo-leader
## Example

```javascript
const { Leader } = require('mongo-leader');
const { MongoClient } = require('mongodb');
const { Leader } = require('mongo-leader')
const { MongoClient } = require('mongodb')

const url = 'mongodb://localhost:27017';
const url = 'mongodb://localhost:27017'

MongoClient.connect(url, { useNewUrlParser: true }, function (err, client) {
const db = client.db('test');
const leader = new Leader(db, { ttl: 5000, wait: 1000 });
const db = client.db('test')
const leader = new Leader(db, { ttl: 5000, wait: 1000 })
setInterval(() => {
leader.isLeader().then(leader => console.log(`Am I leader? : ${leader}`));
}, 100);
});
leader.isLeader().then((leader) => console.log(`Am I leader? : ${leader}`))
}, 100)
})
```

## API
Expand Down
15 changes: 9 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Leader extends EventEmitter {
}

elect() {
if (this.stopped) return
if (this.paused) return
this.db
.collection(this.key)
.findOneAndUpdate(
Expand All @@ -77,7 +77,7 @@ class Leader extends EventEmitter {
}

renew() {
if (this.stopped) return
if (this.paused) return
this.db
.collection(this.key)
.findOneAndUpdate(
Expand All @@ -95,12 +95,15 @@ class Leader extends EventEmitter {
})
}

stop() {
this.stopped = true
pause() {
if (!this.paused) this.paused = true
}

start() {
this.stopped = false
resume() {
if (this.paused) {
this.paused = false
this.elect()
}
}
}

Expand Down
28 changes: 14 additions & 14 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('Leader', () => {
expect(leader.key).toMatch(/^leader-?/)
} finally {
// Cleanup
leader.stop()
leader.pause()
}
})
})
Expand All @@ -34,7 +34,7 @@ describe('Leader', () => {
expect(mockCollection.createIndex).toHaveBeenCalled()
} finally {
// Cleanup
leader.stop()
leader.pause()
}
})
})
Expand All @@ -50,7 +50,7 @@ describe('Leader', () => {
expect(result).toBe(true)
} finally {
// Cleanup
leader.stop()
leader.pause()
}
})
})
Expand All @@ -65,7 +65,7 @@ describe('Leader', () => {
expect(mockCollection.findOneAndUpdate).toHaveBeenCalled()
} finally {
// Cleanup
leader.stop()
leader.pause()
}
})
})
Expand All @@ -80,22 +80,22 @@ describe('Leader', () => {
expect(mockCollection.findOneAndUpdate).toHaveBeenCalled()
} finally {
// Cleanup
leader.stop()
leader.pause()
}
})
})
describe('stop', () => {
it('should stop the leader', function () {
describe('pause', () => {
it('should pause the leader', function () {
// Arrange
const leader = new Leader(mockDb)
try {
// Act
leader.stop()
leader.pause()
// Assert
expect(leader.stopped).toBe(true)
expect(leader.paused).toBe(true)
} finally {
// Cleanup
leader.stop()
leader.pause()
}
})
})
Expand All @@ -105,13 +105,13 @@ describe('Leader', () => {
const leader = new Leader(mockDb)
try {
// Act
leader.stop()
leader.start()
leader.pause()
leader.resume()
// Assert
expect(leader.stopped).toBe(false)
expect(leader.paused).toBe(false)
} finally {
// Cleanup
leader.stop()
leader.pause()
}
})
})
Expand Down

0 comments on commit f514e2d

Please sign in to comment.