ssb-conn-db
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -7,2 +7,5 @@ import { AddressData, Opts } from './types'; | ||
private readonly _writeTimeout; | ||
private readonly _loadedPromise; | ||
private _loadedResolve; | ||
private _loadedReject; | ||
private _scheduledWriteTask; | ||
@@ -22,3 +25,4 @@ constructor(opts: Partial<Opts>); | ||
listen(): any; | ||
loaded(): Promise<true>; | ||
} | ||
export = ConnDB; |
@@ -52,2 +52,3 @@ "use strict"; | ||
function ConnDB(opts) { | ||
var _this = this; | ||
var dirPath = opts.path || defaultOpts.path; | ||
@@ -64,2 +65,6 @@ var modernPath = path.join(dirPath, 'conn.json'); | ||
this._scheduledWriteTask = null; | ||
this._loadedPromise = new Promise(function (resolve, reject) { | ||
_this._loadedResolve = resolve; | ||
_this._loadedReject = reject; | ||
}); | ||
this._init(modernPath, legacyPath); | ||
@@ -73,2 +78,3 @@ } | ||
this._stateFile.set({}, function () { }); | ||
this._loadedResolve(true); | ||
return; | ||
@@ -79,6 +85,12 @@ } | ||
legacyStateFile.get(function (err, oldVals) { | ||
if (err) | ||
throw new Error('Failed to load gossip.json'); | ||
if (err) { | ||
_this._loadedReject(err); | ||
return; | ||
} | ||
var newVals = migration_1.migrateMany(oldVals); | ||
return _this._stateFile.set(newVals, function (_err2) { | ||
return _this._stateFile.set(newVals, function (err2) { | ||
if (err2) { | ||
_this._loadedReject(err2); | ||
return; | ||
} | ||
_this._load(newVals); | ||
@@ -90,3 +102,7 @@ }); | ||
if (modernExists) { | ||
this._stateFile.get(function (_err, vals) { | ||
this._stateFile.get(function (err, vals) { | ||
if (err) { | ||
_this._loadedReject(err); | ||
return; | ||
} | ||
_this._load(vals); | ||
@@ -112,2 +128,3 @@ }); | ||
} | ||
this._loadedResolve(true); | ||
}; | ||
@@ -218,4 +235,7 @@ ConnDB.prototype._serialize = function () { | ||
}; | ||
ConnDB.prototype.loaded = function () { | ||
return this._loadedPromise; | ||
}; | ||
return ConnDB; | ||
}()); | ||
module.exports = ConnDB; |
{ | ||
"name": "ssb-conn-db", | ||
"description": "Module that manages a local registry of connectable peers", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"homepage": "https://github.com/staltz/ssb-conn-db", | ||
@@ -30,2 +30,2 @@ "main": "lib/index.js", | ||
"license": "MIT" | ||
} | ||
} |
@@ -43,2 +43,3 @@ # ssb-conn-db | ||
* `connDB.listen()`: returns a pull stream that notifies of changes made to the database, as an object `{type, address}` where `type` is either `'insert'`, `'update'`, or `'delete'` | ||
* `connDB.loaded()`: returns a Promise that resolves successfully when the initial database loading (read) occurs, and rejects if there was a failure to load. | ||
@@ -45,0 +46,0 @@ Notice that the API above mostly mirrors the API of the JavaScript [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map). |
@@ -20,2 +20,5 @@ import * as fs from 'fs'; | ||
private readonly _writeTimeout: number; | ||
private readonly _loadedPromise: Promise<true>; | ||
private _loadedResolve!: (val: true) => void; | ||
private _loadedReject!: (err: any) => void; | ||
private _scheduledWriteTask: NodeJS.Timeout | null; | ||
@@ -35,2 +38,6 @@ | ||
this._scheduledWriteTask = null; | ||
this._loadedPromise = new Promise((resolve, reject) => { | ||
this._loadedResolve = resolve; | ||
this._loadedReject = reject; | ||
}); | ||
this._init(modernPath, legacyPath); | ||
@@ -45,2 +52,3 @@ } | ||
this._stateFile.set({}, () => {}); | ||
this._loadedResolve(true); | ||
return; | ||
@@ -52,5 +60,12 @@ } | ||
legacyStateFile.get((err: any, oldVals: any) => { | ||
if (err) throw new Error('Failed to load gossip.json'); | ||
if (err) { | ||
this._loadedReject(err); | ||
return; | ||
} | ||
const newVals = migrateMany(oldVals); | ||
return this._stateFile.set(newVals, (_err2: any) => { | ||
return this._stateFile.set(newVals, (err2: any) => { | ||
if (err2) { | ||
this._loadedReject(err2); | ||
return; | ||
} | ||
this._load(newVals); | ||
@@ -63,3 +78,7 @@ }); | ||
if (modernExists) { | ||
this._stateFile.get((_err: any, vals: any) => { | ||
this._stateFile.get((err: any, vals: any) => { | ||
if (err) { | ||
this._loadedReject(err); | ||
return; | ||
} | ||
this._load(vals); | ||
@@ -75,2 +94,3 @@ }); | ||
} | ||
this._loadedResolve(true); | ||
} | ||
@@ -184,4 +204,8 @@ | ||
} | ||
public loaded(): Promise<true> { | ||
return this._loadedPromise; | ||
} | ||
} | ||
export = ConnDB; |
@@ -129,1 +129,17 @@ const tape = require('tape'); | ||
}); | ||
tape('init: loaded() promise works', function(t) { | ||
const dirPath = path.join(__dirname, './example'); | ||
const connDB = new ConnDB({path: dirPath}); | ||
t.ok(connDB, 'connDB instance was created'); | ||
const entries = Array.from(connDB.entries()); | ||
t.equals(entries.length, 0, 'before loaded(), there is no data'); | ||
connDB.loaded().then(() => { | ||
const entries = Array.from(connDB.entries()); | ||
t.equals(entries.length, 1, 'after loaded(), there is data'); | ||
const [address, data] = entries[0]; | ||
t.equals(address, 'net:staltz.com:8008~noauth', 'the address looks ok'); | ||
t.equals(data.source, 'stored', 'the data for that address looks ok'); | ||
t.end(); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
42249
1034
50