|
1 | 1 | const hyperdb = require('hyperdb') |
2 | 2 | const stream = require('readable-stream') |
| 3 | +const thunky = require('thunky') |
3 | 4 | const pump = require('pump') |
4 | 5 | const inherits = require('inherits') |
5 | 6 | const events = require('events') |
@@ -37,27 +38,29 @@ function Graph (storage, key, opts) { |
37 | 38 | ? constants.HEXSTORE_INDEXES_REDUCED |
38 | 39 | : constants.HEXSTORE_INDEXES |
39 | 40 | this._indexKeys = Object.keys(this._indexes) |
40 | | - |
| 41 | + this.isReady = false |
| 42 | + this.ready = thunky(this._ready.bind(this)) |
41 | 43 | this.db.on('error', (e) => { |
42 | 44 | this.emit('error', e) |
43 | 45 | }) |
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 | + }) |
56 | 50 | }) |
57 | 51 | } |
58 | 52 |
|
59 | 53 | inherits(Graph, events.EventEmitter) |
60 | 54 |
|
| 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 | + |
61 | 64 | Graph.prototype._onNew = function (cb) { |
62 | 65 | this._version = pkg.version |
63 | 66 | const metadata = [ |
@@ -169,17 +172,20 @@ Graph.prototype.getStream = function (triple, opts) { |
169 | 172 |
|
170 | 173 | Graph.prototype.get = function (triple, opts, callback) { |
171 | 174 | 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 | + }) |
173 | 178 | } |
174 | | - |
175 | 179 | function doAction (action) { |
176 | 180 | return function (triples, callback) { |
177 | 181 | 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 | + }) |
183 | 189 | } |
184 | 190 | } |
185 | 191 |
|
@@ -245,15 +251,19 @@ Graph.prototype.search = function (query, options, callback) { |
245 | 251 | callback = options |
246 | 252 | options = undefined |
247 | 253 | } |
248 | | - utils.collect(this.searchStream(query, options), callback) |
| 254 | + this.ready(() => { |
| 255 | + utils.collect(this.searchStream(query, options), callback) |
| 256 | + }) |
249 | 257 | } |
250 | 258 |
|
251 | 259 | Graph.prototype.queryStream = function (query) { |
252 | 260 | return new SparqlIterator(query, { hypergraph: this }) |
253 | 261 | } |
254 | 262 |
|
255 | 263 | Graph.prototype.query = function (query, callback) { |
256 | | - utils.collect(this.queryStream(query), callback) |
| 264 | + this.ready(() => { |
| 265 | + utils.collect(this.queryStream(query), callback) |
| 266 | + }) |
257 | 267 | } |
258 | 268 |
|
259 | 269 | Graph.prototype.close = function (callback) { |
|
0 commit comments