Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

online -> exchange #67

Merged
merged 1 commit into from
Jul 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class BlockService {
* @param {Bitswap} bitswap
* @returns {void}
*/
goOnline (bitswap) {
setExchange (bitswap) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The @param note above should probably change to be {Exchange} exchange rather than hardcoded bitswap.

On another note, we should probably add some lightweight checks that the expected functions exists on the object that gets passed in, to prevent scenarios when you set an exchange then call put and get a error about that the function is undefined, we can fail as early as when setting the exchange, with a more friendly error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened - #68

this._bitswap = bitswap
}

Expand All @@ -36,7 +36,7 @@ class BlockService {
*
* @returns {void}
*/
goOffline () {
unsetExchange () {
this._bitswap = null
}

Expand All @@ -45,7 +45,7 @@ class BlockService {
*
* @returns {bool}
*/
isOnline () {
hasExchange () {
return this._bitswap != null
}

Expand All @@ -57,7 +57,7 @@ class BlockService {
* @returns {void}
*/
put (block, callback) {
if (this.isOnline()) {
if (this.hasExchange()) {
return this._bitswap.put(block, callback)
}

Expand All @@ -72,7 +72,7 @@ class BlockService {
* @returns {void}
*/
putMany (blocks, callback) {
if (this.isOnline()) {
if (this.hasExchange()) {
return this._bitswap.putMany(blocks, callback)
}

Expand All @@ -87,7 +87,7 @@ class BlockService {
* @returns {void}
*/
get (cid, callback) {
if (this.isOnline()) {
if (this.hasExchange()) {
return this._bitswap.get(cid, callback)
}

Expand Down
22 changes: 11 additions & 11 deletions test/block-service-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,23 +129,23 @@ module.exports = (repo) => {
})
})

it('goes offline', () => {
it('sets and unsets exchange', () => {
bs = new BlockService(repo)
bs.goOnline({})
expect(bs.isOnline()).to.be.eql(true)
bs.goOffline()
expect(bs.isOnline()).to.be.eql(false)
bs.setExchange({})
expect(bs.hasExchange()).to.be.eql(true)
bs.unsetExchange()
expect(bs.hasExchange()).to.be.eql(false)
})
})

describe('online', () => {
describe('has exchange', () => {
beforeEach(() => {
bs = new BlockService(repo)
})

it('isOnline returns true when online', () => {
bs.goOnline({})
expect(bs.isOnline()).to.be.eql(true)
it('hasExchange returns true when online', () => {
bs.setExchange({})
expect(bs.hasExchange()).to.be.eql(true)
})

it('retrieves a block through bitswap', (done) => {
Expand All @@ -156,7 +156,7 @@ module.exports = (repo) => {
}
}

bs.goOnline(bitswap)
bs.setExchange(bitswap)

const data = new Buffer('secret')

Expand All @@ -178,7 +178,7 @@ module.exports = (repo) => {
callback()
}
}
bs.goOnline(bitswap)
bs.setExchange(bitswap)

const data = new Buffer('secret sauce')

Expand Down