Skip to content
This repository was archived by the owner on Mar 23, 2023. It is now read-only.

Commit a0fd42f

Browse files
authored
Merge pull request #20 from ipfs/refactor/async-await
Refactor/async await
2 parents 2046782 + 4c23666 commit a0fd42f

12 files changed

+309
-532
lines changed

package.json

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@
55
"leadMaintainer": "Pedro Teixeira <[email protected]>",
66
"main": "src/index.js",
77
"scripts": {
8-
"lint": "aegir lint",
9-
"build": "aegir build",
108
"test": "aegir test",
11-
"flow": "flow",
12-
"test:node": "aegir test --target node",
13-
"test:browser": "aegir test --target browser",
14-
"release": "aegir release --docs",
15-
"release-minor": "aegir release --type minor --docs",
16-
"release-major": "aegir release --type major --docs",
17-
"coverage": "aegir coverage",
18-
"coverage-publish": "aegir coverage --provider codecov",
19-
"docs": "aegir docs"
9+
"test:node": "aegir test -t node",
10+
"test:browser": "aegir test -t browser",
11+
"test:webworker": "aegir test -t webworker",
12+
"build": "aegir build",
13+
"lint": "aegir lint",
14+
"release": "aegir release",
15+
"release-minor": "aegir release --type minor",
16+
"release-major": "aegir release --type major",
17+
"coverage": "nyc -s npm run test:node && nyc report --reporter=html",
18+
"dep-check": "aegir dep-check"
2019
},
2120
"files": [
2221
"src",
@@ -39,16 +38,14 @@
3938
},
4039
"homepage": "https://github.com/ipfs/js-datastore-core#readme",
4140
"devDependencies": {
42-
"aegir": "^18.2.2",
41+
"aegir": "^19.0.3",
42+
"async-iterator-all": "^1.0.0",
4343
"chai": "^4.2.0",
44-
"dirty-chai": "^2.0.1",
45-
"flow-bin": "~0.98.1"
44+
"dirty-chai": "^2.0.1"
4645
},
4746
"dependencies": {
48-
"async": "^2.6.1",
49-
"interface-datastore": "~0.6.0",
50-
"pull-many": "^1.0.8",
51-
"pull-stream": "^3.6.9"
47+
"debug": "^4.1.1",
48+
"interface-datastore": "~0.7.0"
5249
},
5350
"engines": {
5451
"node": ">=6.0.0",

src/keytransform.js

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/* @flow */
22
'use strict'
33

4-
const pull = require('pull-stream')
4+
const utils = require('interface-datastore').utils
5+
const map = utils.map
56

67
/* ::
78
import type {Key, Datastore, Batch, Query, QueryResult, Callback} from 'interface-datastore'
@@ -38,24 +39,24 @@ class KeyTransformDatastore /* :: <Value> */ {
3839
this.transform = transform
3940
}
4041

41-
open (callback /* : Callback<void> */) /* : void */ {
42-
this.child.open(callback)
42+
open () /* : Promise<void> */ {
43+
return this.child.open()
4344
}
4445

45-
put (key /* : Key */, val /* : Value */, callback /* : Callback<void> */) /* : void */ {
46-
this.child.put(this.transform.convert(key), val, callback)
46+
put (key /* : Key */, val /* : Value */) /* : Promise<void> */ {
47+
return this.child.put(this.transform.convert(key), val)
4748
}
4849

49-
get (key /* : Key */, callback /* : Callback<Value> */) /* : void */ {
50-
this.child.get(this.transform.convert(key), callback)
50+
get (key /* : Key */) /* : Promise<Value> */ {
51+
return this.child.get(this.transform.convert(key))
5152
}
5253

53-
has (key /* : Key */, callback /* : Callback<bool> */) /* : void */ {
54-
this.child.has(this.transform.convert(key), callback)
54+
has (key /* : Key */) /* : Promise<bool> */ {
55+
return this.child.has(this.transform.convert(key))
5556
}
5657

57-
delete (key /* : Key */, callback /* : Callback<void> */) /* : void */ {
58-
this.child.delete(this.transform.convert(key), callback)
58+
delete (key /* : Key */) /* : Promise<void> */ {
59+
return this.child.delete(this.transform.convert(key))
5960
}
6061

6162
batch () /* : Batch<Value> */ {
@@ -67,24 +68,21 @@ class KeyTransformDatastore /* :: <Value> */ {
6768
delete: (key /* : Key */) /* : void */ => {
6869
b.delete(this.transform.convert(key))
6970
},
70-
commit: (callback /* : Callback<void> */) /* : void */ => {
71-
b.commit(callback)
71+
commit: () /* : Promise<void> */ => {
72+
return b.commit()
7273
}
7374
}
7475
}
7576

76-
query (q /* : Query<Value> */) /* : QueryResult<Value> */ {
77-
return pull(
78-
this.child.query(q),
79-
pull.map(e => {
80-
e.key = this.transform.invert(e.key)
81-
return e
82-
})
83-
)
77+
query (q /* : Query<Value> */) /* : Iterator */ {
78+
return map(this.child.query(q), e => {
79+
e.key = this.transform.invert(e.key)
80+
return e
81+
})
8482
}
8583

86-
close (callback /* : Callback<void> */) /* : void */ {
87-
this.child.close(callback)
84+
close () /* : Promise<void> */ {
85+
return this.child.close()
8886
}
8987
}
9088

src/mount.js

Lines changed: 45 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
/* @flow */
22
'use strict'
33

4-
const each = require('async/each')
5-
const many = require('pull-many')
6-
const pull = require('pull-stream')
7-
84
const Key = require('interface-datastore').Key
95
const Errors = require('interface-datastore').Errors
106
const utils = require('interface-datastore').utils
11-
const asyncFilter = utils.asyncFilter
12-
const asyncSort = utils.asyncSort
7+
const filter = utils.filter
8+
const take = utils.take
9+
const sortAll = utils.sortAll
1310
const replaceStartWith = utils.replaceStartWith
1411

1512
const Keytransform = require('./keytransform')
@@ -34,10 +31,8 @@ class MountDatastore /* :: <Value> */ {
3431
this.mounts = mounts.slice()
3532
}
3633

37-
open (callback /* : Callback<void> */) /* : void */ {
38-
each(this.mounts, (m, cb) => {
39-
m.datastore.open(cb)
40-
}, callback)
34+
open () /* : Promise<void> */ {
35+
return Promise.all(this.mounts.map((m) => m.datastore.open()))
4136
}
4237

4338
/**
@@ -60,53 +55,44 @@ class MountDatastore /* :: <Value> */ {
6055
}
6156
}
6257

63-
put (key /* : Key */, value /* : Value */, callback /* : Callback<void> */) /* : void */ {
58+
put (key /* : Key */, value /* : Value */) /* : Promise<void> */ {
6459
const match = this._lookup(key)
6560
if (match == null) {
66-
return callback(
67-
Errors.dbWriteFailedError(new Error('No datastore mounted for this key'))
68-
)
61+
throw Errors.dbWriteFailedError(new Error('No datastore mounted for this key'))
6962
}
7063

71-
match.datastore.put(match.rest, value, callback)
64+
return match.datastore.put(match.rest, value)
7265
}
7366

74-
get (key /* : Key */, callback /* : Callback<Value> */) /* : void */ {
67+
get (key /* : Key */) /* : Promise<Value> */ {
7568
const match = this._lookup(key)
7669
if (match == null) {
77-
return callback(
78-
Errors.notFoundError(new Error('No datastore mounted for this key'))
79-
)
70+
throw Errors.notFoundError(new Error('No datastore mounted for this key'))
8071
}
81-
82-
match.datastore.get(match.rest, callback)
72+
return match.datastore.get(match.rest)
8373
}
8474

85-
has (key /* : Key */, callback /* : Callback<bool> */) /* : void */ {
75+
has (key /* : Key */) /* : Promise<bool> */ {
8676
const match = this._lookup(key)
8777
if (match == null) {
88-
callback(null, false)
89-
return
78+
return false
9079
}
91-
92-
match.datastore.has(match.rest, callback)
80+
return match.datastore.has(match.rest)
9381
}
9482

95-
delete (key /* : Key */, callback /* : Callback<void> */) /* : void */ {
83+
delete (key /* : Key */) /* : Promise<void> */ {
9684
const match = this._lookup(key)
9785
if (match == null) {
98-
return callback(
99-
Errors.dbDeleteFailedError(new Error('No datastore mounted for this key'))
100-
)
86+
throw Errors.dbDeleteFailedError(new Error('No datastore mounted for this key'))
10187
}
10288

103-
match.datastore.delete(match.rest, callback)
89+
return match.datastore.delete(match.rest)
10490
}
10591

106-
close (callback /* : Callback<void> */) /* : void */ {
107-
each(this.mounts, (m, cb) => {
108-
m.datastore.close(cb)
109-
}, callback)
92+
close () /* : Promise<void> */ {
93+
return Promise.all(this.mounts.map((m) => {
94+
return m.datastore.close()
95+
}))
11096
}
11197

11298
batch () /* : Batch<Value> */ {
@@ -137,10 +123,8 @@ class MountDatastore /* :: <Value> */ {
137123
const match = lookup(key)
138124
match.batch.delete(match.rest)
139125
},
140-
commit: (callback /* : Callback<void> */) /* : void */ => {
141-
each(Object.keys(batchMounts), (p, cb) => {
142-
batchMounts[p].commit(cb)
143-
}, callback)
126+
commit: () /* : Promise<void> */ => {
127+
return Promise.all(Object.keys(batchMounts).map(p => batchMounts[p].commit()))
144128
}
145129
}
146130
}
@@ -168,27 +152,33 @@ class MountDatastore /* :: <Value> */ {
168152
})
169153
})
170154

171-
let tasks = [many(qs)]
172-
173-
if (q.filters != null) {
174-
tasks = tasks.concat(q.filters.map(f => asyncFilter(f)))
175-
}
176-
177-
if (q.orders != null) {
178-
tasks = tasks.concat(q.orders.map(o => asyncSort(o)))
179-
}
180-
155+
let it = _many(qs)
156+
if (q.filters) q.filters.forEach(f => { it = filter(it, f) })
157+
if (q.orders) q.orders.forEach(o => { it = sortAll(it, o) })
181158
if (q.offset != null) {
182159
let i = 0
183-
tasks.push(pull.filter(() => i++ >= q.offset))
160+
it = filter(it, () => i++ >= q.offset)
184161
}
162+
if (q.limit != null) it = take(it, q.limit)
185163

186-
if (q.limit != null) {
187-
tasks.push(pull.take(q.limit))
188-
}
189-
190-
return pull.apply(null, tasks)
164+
return it
191165
}
192166
}
193167

168+
function _many (iterable) {
169+
return (async function * () {
170+
let completed = iterable.map(() => false)
171+
while (!completed.every(Boolean)) {
172+
for (const [idx, itr] of iterable.entries()) {
173+
const it = await itr.next()
174+
if (it.done) {
175+
completed[idx] = true
176+
continue
177+
}
178+
yield it.value
179+
}
180+
}
181+
})()
182+
}
183+
194184
module.exports = MountDatastore

src/namespace.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class NamespaceDatastore/* :: <Value> */ extends KeytransformDatastore /* :: <Va
4141
this.prefix = prefix
4242
}
4343

44-
query (q /* : Query<Value> */)/* : QueryResult<Value> */ {
44+
query (q /* : Query<Value> */)/* : Iterator */ {
4545
if (q.prefix && this.prefix.toString() !== '/') {
4646
return super.query(Object.assign({}, q, {
4747
prefix: this.prefix.child(new Key(q.prefix)).toString()

src/shard.js

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,24 +121,11 @@ function parseShardFun (str /* : string */) /* : ShardV1 */ {
121121
}
122122
}
123123

124-
exports.readShardFun = (path /* : string */, store /* : Datastore<Buffer> */, callback /* : Callback<ShardV1> */) /* : void */ => {
124+
exports.readShardFun = async (path /* : string */, store /* : Datastore<Buffer> */) /* : Promise<ShardV1> */ => {
125125
const key = new Key(path).child(new Key(SHARDING_FN))
126126
const get = typeof store.getRaw === 'function' ? store.getRaw.bind(store) : store.get.bind(store)
127-
128-
get(key, (err, res) => {
129-
if (err) {
130-
return callback(err)
131-
}
132-
133-
let shard
134-
try {
135-
shard = parseShardFun((res || '').toString().trim())
136-
} catch (err) {
137-
return callback(err)
138-
}
139-
140-
callback(null, shard)
141-
})
127+
const res = await get(key)
128+
return parseShardFun((res || '').toString().trim())
142129
}
143130

144131
exports.readme = readme

0 commit comments

Comments
 (0)