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

chore: callbacks -> async / await #85

Merged
merged 1 commit into from
Jun 18, 2019
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
39 changes: 14 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,31 +72,20 @@ const repo = new IPFSRepo('example')

// create a block
const data = new Buffer('hello world')
multihashing(data, 'sha2-256', (err, multihash) => {
if (err) {
throw err
}

const cid = new CID(multihash)
const block = new Block(data, cid)

// create a service
const bs = new BlockService(repo)

// add the block, then retrieve it
bs.put(block, (err) => {
if (err) {
throw err
}
bs.get(cid, (err, b) => {
if (err) {
throw err
}
console.log(block.data.toString() === b.data.toString())
// => true
})
})
})
const multihash = await multihashing(data, 'sha2-256')

const cid = new CID(multihash)
const block = new Block(data, cid)

// create a service
const service = new BlockService(repo)

// add the block, then retrieve it
await service.put(block)

const result = await service.get(cid)
console.log(block.data.toString() === result.data.toString())
// => true
```

### Browser: Browserify, Webpack, other bundlers
Expand Down
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,18 @@
"chai": "^4.2.0",
"cids": "~0.5.7",
"dirty-chai": "^2.0.1",
"fs-extra": "^8.0.1",
"ipfs-block": "~0.8.0",
"ipfs-repo": "~0.26.2",
"ipfs-repo": "~0.27.0",
"lodash": "^4.17.11",
"multihashing-async": "~0.5.2",
"ncp": "^2.0.0",
"rimraf": "^2.6.3"
"multihashing-async": "~0.7.0"
},
"engines": {
"node": ">=6.0.0",
"npm": ">=3.0.0"
},
"dependencies": {
"async": "^2.6.2"
"streaming-iterables": "^4.0.2"
},
"contributors": [
"David Dias <daviddias.p@gmail.com>",
Expand Down
52 changes: 25 additions & 27 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const asyncMap = require('async/map')
const { map } = require('streaming-iterables')

/**
* BlockService is a hybrid block datastore. It stores data in a local
Expand Down Expand Up @@ -54,73 +54,71 @@ class BlockService {
* Put a block to the underlying datastore.
*
* @param {Block} block
* @param {function(Error)} callback
* @returns {void}
* @returns {Promise}
*/
put (block, callback) {
put (block) {
if (this.hasExchange()) {
this._bitswap.put(block, callback)
return this._bitswap.put(block)
} else {
this._repo.blocks.put(block, callback)
return this._repo.blocks.put(block)
}
}

/**
* Put a multiple blocks to the underlying datastore.
*
* @param {Array<Block>} blocks
* @param {function(Error)} callback
* @returns {void}
* @returns {Promise}
*/
putMany (blocks, callback) {
putMany (blocks) {
if (this.hasExchange()) {
this._bitswap.putMany(blocks, callback)
return this._bitswap.putMany(blocks)
} else {
this._repo.blocks.putMany(blocks, callback)
return this._repo.blocks.putMany(blocks)
}
}

/**
* Get a block by cid.
*
* @param {CID} cid
* @param {function(Error, Block)} callback
* @returns {void}
* @returns {Promise<Block>}
*/
get (cid, callback) {
get (cid) {
if (this.hasExchange()) {
this._bitswap.get(cid, callback)
return this._bitswap.get(cid)
} else {
this._repo.blocks.get(cid, callback)
return this._repo.blocks.get(cid)
}
}

/**
* Get multiple blocks back from an array of cids.
*
* @param {Array<CID>} cids
* @param {function(Error, Block)} callback
* @returns {void}
* @returns {Iterator<Block>}
*/
getMany (cids, callback) {
getMany (cids) {
if (!Array.isArray(cids)) {
callback(new Error('first arg must be an array of cids'))
} else if (this.hasExchange()) {
this._bitswap.getMany(cids, callback)
throw new Error('first arg must be an array of cids')
}

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

/**
* Delete a block from the blockstore.
*
* @param {CID} cid
* @param {function(Error)} callback
* @return {void}
* @returns {Promise}
*/
delete (cid, callback) {
this._repo.blocks.delete(cid, callback)
delete (cid) {
return this._repo.blocks.delete(cid)
}
}

Expand Down
Loading