Skip to content

Commit 9ca8f5e

Browse files
committed
add thunky to enable some use before db is ready
1 parent 8492c87 commit 9ca8f5e

3 files changed

Lines changed: 190 additions & 80 deletions

File tree

index.js

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const hyperdb = require('hyperdb')
22
const stream = require('readable-stream')
3+
const thunky = require('thunky')
34
const pump = require('pump')
45
const inherits = require('inherits')
56
const events = require('events')
@@ -37,27 +38,29 @@ function Graph (storage, key, opts) {
3738
? constants.HEXSTORE_INDEXES_REDUCED
3839
: constants.HEXSTORE_INDEXES
3940
this._indexKeys = Object.keys(this._indexes)
40-
41+
this.isReady = false
42+
this.ready = thunky(this._ready.bind(this))
4143
this.db.on('error', (e) => {
4244
this.emit('error', e)
4345
})
44-
this.db.on('ready', (e) => {
45-
if (utils.isNewDatabase(this.db)) {
46-
this._onNew((err) => {
47-
if (err) return this.emit('error', err)
48-
this.emit('ready', e)
49-
})
50-
} else {
51-
this._onInit((err) => {
52-
if (err) return this.emit('error', err)
53-
this.emit('ready', e)
54-
})
55-
}
46+
this.db.ready(() => {
47+
this.ready(() => {
48+
this.emit('ready')
49+
})
5650
})
5751
}
5852

5953
inherits(Graph, events.EventEmitter)
6054

55+
Graph.prototype._ready = function (cb) {
56+
this.isReady = true
57+
if (utils.isNewDatabase(this.db)) {
58+
this._onNew(cb)
59+
} else {
60+
this._onInit(cb)
61+
}
62+
}
63+
6164
Graph.prototype._onNew = function (cb) {
6265
this._version = pkg.version
6366
const metadata = [
@@ -169,17 +172,20 @@ Graph.prototype.getStream = function (triple, opts) {
169172

170173
Graph.prototype.get = function (triple, opts, callback) {
171174
if (typeof opts === 'function') return this.get(triple, undefined, opts)
172-
utils.collect(this.getStream(triple, opts), callback)
175+
this.ready(() => {
176+
utils.collect(this.getStream(triple, opts), callback)
177+
})
173178
}
174-
175179
function doAction (action) {
176180
return function (triples, callback) {
177181
if (!triples) return callback(new Error('Must pass triple'))
178-
let entries = (!triples.reduce) ? [triples] : triples
179-
entries = entries.reduce((prev, triple) => {
180-
return prev.concat(this._generateBatch(triple, action))
181-
}, [])
182-
this.db.batch(entries.reverse(), callback)
182+
this.ready(() => {
183+
let entries = (!triples.reduce) ? [triples] : triples
184+
entries = entries.reduce((prev, triple) => {
185+
return prev.concat(this._generateBatch(triple, action))
186+
}, [])
187+
this.db.batch(entries.reverse(), callback)
188+
})
183189
}
184190
}
185191

@@ -245,15 +251,19 @@ Graph.prototype.search = function (query, options, callback) {
245251
callback = options
246252
options = undefined
247253
}
248-
utils.collect(this.searchStream(query, options), callback)
254+
this.ready(() => {
255+
utils.collect(this.searchStream(query, options), callback)
256+
})
249257
}
250258

251259
Graph.prototype.queryStream = function (query) {
252260
return new SparqlIterator(query, { hypergraph: this })
253261
}
254262

255263
Graph.prototype.query = function (query, callback) {
256-
utils.collect(this.queryStream(query), callback)
264+
this.ready(() => {
265+
utils.collect(this.queryStream(query), callback)
266+
})
257267
}
258268

259269
Graph.prototype.close = function (callback) {

0 commit comments

Comments
 (0)