1
1
/* @flow */
2
2
'use strict'
3
3
4
- const each = require ( 'async/each' )
5
- const many = require ( 'pull-many' )
6
- const pull = require ( 'pull-stream' )
7
-
8
4
const Key = require ( 'interface-datastore' ) . Key
9
5
const Errors = require ( 'interface-datastore' ) . Errors
10
6
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
13
10
const replaceStartWith = utils . replaceStartWith
14
11
15
12
const Keytransform = require ( './keytransform' )
@@ -34,10 +31,8 @@ class MountDatastore /* :: <Value> */ {
34
31
this . mounts = mounts . slice ( )
35
32
}
36
33
37
- open ( callback /* : Callback<void> */ ) /* : void */ {
38
- each ( this . mounts , ( m , cb ) => {
39
- m . datastore . open ( cb )
40
- } , callback )
34
+ async open ( ) /* : Promise<void> */ {
35
+ return Promise . all ( this . mounts . map ( ( m ) => m . datastore . open ( ) ) )
41
36
}
42
37
43
38
/**
@@ -60,53 +55,44 @@ class MountDatastore /* :: <Value> */ {
60
55
}
61
56
}
62
57
63
- put ( key /* : Key */ , value /* : Value */ , callback /* : Callback <void> */ ) /* : void */ {
58
+ async put ( key /* : Key */ , value /* : Value */ ) /* : Promise <void> */ {
64
59
const match = this . _lookup ( key )
65
60
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' ) )
69
62
}
70
63
71
- match . datastore . put ( match . rest , value , callback )
64
+ return match . datastore . put ( match . rest , value )
72
65
}
73
66
74
- get ( key /* : Key */ , callback /* : Callback <Value> */ ) /* : void */ {
67
+ async get ( key /* : Key */ ) /* : Promise <Value> */ {
75
68
const match = this . _lookup ( key )
76
69
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' ) )
80
71
}
81
-
82
- match . datastore . get ( match . rest , callback )
72
+ return match . datastore . get ( match . rest )
83
73
}
84
74
85
- has ( key /* : Key */ , callback /* : Callback <bool> */ ) /* : void */ {
75
+ async has ( key /* : Key */ ) /* : Promise <bool> */ {
86
76
const match = this . _lookup ( key )
87
77
if ( match == null ) {
88
- callback ( null , false )
89
- return
78
+ return false
90
79
}
91
-
92
- match . datastore . has ( match . rest , callback )
80
+ return match . datastore . has ( match . rest )
93
81
}
94
82
95
- delete ( key /* : Key */ , callback /* : Callback <void> */ ) /* : void */ {
83
+ async delete ( key /* : Key */ ) /* : Promise <void> */ {
96
84
const match = this . _lookup ( key )
97
85
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' ) )
101
87
}
102
88
103
- match . datastore . delete ( match . rest , callback )
89
+ return match . datastore . delete ( match . rest )
104
90
}
105
91
106
- close ( callback /* : Callback <void> */ ) /* : void */ {
107
- each ( this . mounts , ( m , cb ) => {
108
- m . datastore . close ( cb )
109
- } , callback )
92
+ async close ( ) /* : Promise <void> */ {
93
+ return Promise . all ( this . mounts . map ( ( m ) => {
94
+ return m . datastore . close ( )
95
+ } ) )
110
96
}
111
97
112
98
batch ( ) /* : Batch<Value> */ {
@@ -137,10 +123,8 @@ class MountDatastore /* :: <Value> */ {
137
123
const match = lookup ( key )
138
124
match . batch . delete ( match . rest )
139
125
} ,
140
- commit : ( callback /* : Callback<void> */ ) /* : void */ => {
141
- each ( Object . keys ( batchMounts ) , ( p , cb ) => {
142
- batchMounts [ p ] . commit ( cb )
143
- } , callback )
126
+ commit : async ( ) /* : Promise<void> */ => {
127
+ return Promise . all ( Object . keys ( batchMounts ) . map ( p => batchMounts [ p ] . commit ( ) ) )
144
128
}
145
129
}
146
130
}
@@ -168,27 +152,33 @@ class MountDatastore /* :: <Value> */ {
168
152
} )
169
153
} )
170
154
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 ) } )
181
158
if ( q . offset != null ) {
182
159
let i = 0
183
- tasks . push ( pull . filter ( ( ) => i ++ >= q . offset ) )
160
+ it = filter ( it , ( ) => i ++ >= q . offset )
184
161
}
162
+ if ( q . limit != null ) it = take ( it , q . limit )
185
163
186
- if ( q . limit != null ) {
187
- tasks . push ( pull . take ( q . limit ) )
188
- }
189
-
190
- return pull . apply ( null , tasks )
164
+ return it
191
165
}
192
166
}
193
167
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
+
194
184
module . exports = MountDatastore
0 commit comments