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

feat: .getMany #81

Merged
merged 5 commits into from
Oct 27, 2018
Merged

feat: .getMany #81

merged 5 commits into from
Oct 27, 2018

Conversation

daviddias
Copy link
Member

No description provided.

@ghost ghost assigned daviddias Jun 4, 2018
@ghost ghost added the status/in-progress In progress label Jun 4, 2018
@daviddias daviddias requested a review from vmx June 4, 2018 09:32
@daviddias daviddias changed the title feat: .getMany feat: use .get([cid Array]) from the DAG API Jun 4, 2018
@daviddias daviddias changed the title feat: use .get([cid Array]) from the DAG API feat: .getMany Jun 4, 2018
@daviddias
Copy link
Member Author

Ready for merge and release

expect(blocks).to.eql(blocks)
cb()
})
expect(testBlocks).to.eql(testBlocks)
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be expect(_blocks).to.eql(testBlocks) (and then _blocks should be renamed to blocks).

expect(err).to.not.exist()
bs.getMany(cids, (err, _blocks) => {
expect(err).to.not.exist()
expect(testBlocks).to.eql(testBlocks)
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be expect(_blocks).to.eql(testBlocks) (and then _blocks should be renamed to blocks).

PS: I repost this comment as GitHub doesn't show the original one as I expect it.

src/index.js Outdated
@@ -94,6 +96,24 @@ class BlockService {
return this._repo.blocks.get(cid, callback)
}

/**
* Get multiple blocks a block by cid.

Choose a reason for hiding this comment

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

I think this comment should've been

Get multiple blocks by cids

?

src/index.js Outdated
return callback(new Error('first arg must be an array of cids'))
}
if (this.hasExchange()) {
return this._bitswap.getMany(cids, callback)

Choose a reason for hiding this comment

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

One thing that baffled me at first sight was that this synchronously returns, while in the next few lines, we asynchronously fetch from the local repository.

After looking at Bitswap's API I noticed that getMany() isn't actually synchronous either, which makes me wonder:

Are we only returning here to stop getMany() ?

If yes, couldn't we then also go with:

this.hasExchange() ? 
  this._bitswap.getMany(cids, callback) : 
  asyncMap(cids, (cid, cb) => this._repo.blocks.get(cid, db, callback)

Then it might be more obvious that we aren't interested in returning anything here (even though getMany kind of says the opposite, but hey... it's async)

Copy link
Member

Choose a reason for hiding this comment

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

I prefer if/else over complex ternary operators. So if this would be changed for clarity (I think it's good as it currently is, it's the same pattern as the error above), then I'd rather say:

if (this.hasExchange()) {
  this._bitswap.getMany(cids, callback)
} else {
  asyncMap(cids, (cid, cb) => this._repo.blocks.get(cid, cb), callback)
}

Choose a reason for hiding this comment

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

Got it. Yea I don't have strong feelings about ternary vs none-ternary either.

My concern was mainly that it looks like we're actually returning something synchronously, while the API is async.

Copy link
Member

Choose a reason for hiding this comment

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

@PascalPrecht I agree that it is confusing to someone not used to that style. Perhaps early returns are more clear when written as:

if (this.hasExchange()) {
  this._bitswap.getMany(cids, callback)
  return
}

Though I don't have strong feelings about it. Either way is fine for me, but it should perhaps end up in the Coding Guideles to make it easy to get started.

@daviddias daviddias merged commit 8aa9e0a into master Oct 27, 2018
@daviddias daviddias deleted the feat/getMany branch October 27, 2018 16:51
@ghost ghost removed the status/in-progress In progress label Oct 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants