|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 |
| -const asyncMap = require('async/map') |
| 3 | +const { map } = require('streaming-iterables') |
4 | 4 |
|
5 | 5 | /**
|
6 | 6 | * BlockService is a hybrid block datastore. It stores data in a local
|
@@ -54,73 +54,71 @@ class BlockService {
|
54 | 54 | * Put a block to the underlying datastore.
|
55 | 55 | *
|
56 | 56 | * @param {Block} block
|
57 |
| - * @param {function(Error)} callback |
58 |
| - * @returns {void} |
| 57 | + * @returns {Promise} |
59 | 58 | */
|
60 |
| - put (block, callback) { |
| 59 | + put (block) { |
61 | 60 | if (this.hasExchange()) {
|
62 |
| - this._bitswap.put(block, callback) |
| 61 | + return this._bitswap.put(block) |
63 | 62 | } else {
|
64 |
| - this._repo.blocks.put(block, callback) |
| 63 | + return this._repo.blocks.put(block) |
65 | 64 | }
|
66 | 65 | }
|
67 | 66 |
|
68 | 67 | /**
|
69 | 68 | * Put a multiple blocks to the underlying datastore.
|
70 | 69 | *
|
71 | 70 | * @param {Array<Block>} blocks
|
72 |
| - * @param {function(Error)} callback |
73 |
| - * @returns {void} |
| 71 | + * @returns {Promise} |
74 | 72 | */
|
75 |
| - putMany (blocks, callback) { |
| 73 | + putMany (blocks) { |
76 | 74 | if (this.hasExchange()) {
|
77 |
| - this._bitswap.putMany(blocks, callback) |
| 75 | + return this._bitswap.putMany(blocks) |
78 | 76 | } else {
|
79 |
| - this._repo.blocks.putMany(blocks, callback) |
| 77 | + return this._repo.blocks.putMany(blocks) |
80 | 78 | }
|
81 | 79 | }
|
82 | 80 |
|
83 | 81 | /**
|
84 | 82 | * Get a block by cid.
|
85 | 83 | *
|
86 | 84 | * @param {CID} cid
|
87 |
| - * @param {function(Error, Block)} callback |
88 |
| - * @returns {void} |
| 85 | + * @returns {Promise<Block>} |
89 | 86 | */
|
90 |
| - get (cid, callback) { |
| 87 | + get (cid) { |
91 | 88 | if (this.hasExchange()) {
|
92 |
| - this._bitswap.get(cid, callback) |
| 89 | + return this._bitswap.get(cid) |
93 | 90 | } else {
|
94 |
| - this._repo.blocks.get(cid, callback) |
| 91 | + return this._repo.blocks.get(cid) |
95 | 92 | }
|
96 | 93 | }
|
97 | 94 |
|
98 | 95 | /**
|
99 | 96 | * Get multiple blocks back from an array of cids.
|
100 | 97 | *
|
101 | 98 | * @param {Array<CID>} cids
|
102 |
| - * @param {function(Error, Block)} callback |
103 |
| - * @returns {void} |
| 99 | + * @returns {Iterator<Block>} |
104 | 100 | */
|
105 |
| - getMany (cids, callback) { |
| 101 | + getMany (cids) { |
106 | 102 | if (!Array.isArray(cids)) {
|
107 |
| - callback(new Error('first arg must be an array of cids')) |
108 |
| - } else if (this.hasExchange()) { |
109 |
| - this._bitswap.getMany(cids, callback) |
| 103 | + throw new Error('first arg must be an array of cids') |
| 104 | + } |
| 105 | + |
| 106 | + if (this.hasExchange()) { |
| 107 | + return this._bitswap.getMany(cids) |
110 | 108 | } else {
|
111 |
| - asyncMap(cids, (cid, cb) => this._repo.blocks.get(cid, cb), callback) |
| 109 | + const getRepoBlocks = map((cid) => this._repo.blocks.get(cid)) |
| 110 | + return getRepoBlocks(cids) |
112 | 111 | }
|
113 | 112 | }
|
114 | 113 |
|
115 | 114 | /**
|
116 | 115 | * Delete a block from the blockstore.
|
117 | 116 | *
|
118 | 117 | * @param {CID} cid
|
119 |
| - * @param {function(Error)} callback |
120 |
| - * @return {void} |
| 118 | + * @returns {Promise} |
121 | 119 | */
|
122 |
| - delete (cid, callback) { |
123 |
| - this._repo.blocks.delete(cid, callback) |
| 120 | + delete (cid) { |
| 121 | + return this._repo.blocks.delete(cid) |
124 | 122 | }
|
125 | 123 | }
|
126 | 124 |
|
|
0 commit comments