@keymetrics/interdb
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -0,6 +1,9 @@ | ||
/** | ||
* Events constants | ||
*/ | ||
module.exports = { | ||
UPDATE_DATABASE: 'interdb:command:updateDb', | ||
SYNC_DATABASE: 'interdb:command:sync', | ||
LASTUPDATE_DATABASE: 'interdb:command:lastUpdate' | ||
DB_UPDATE: 'interdb:db:update', | ||
DB_SYNC: 'interdb:db:sync', | ||
DB_LASTUPDATE: 'interdb:db:lastUpdate' | ||
} |
'use strict' | ||
const fs = require('fs') | ||
const cst = require('../constants.js') | ||
@@ -30,3 +31,3 @@ class Db { | ||
broadcastDB () { | ||
this.interdb.clients.broadcast('interdb:command:updateDb', this.db) | ||
this.interdb.clients.broadcast(cst.DB_UPDATE, this.db) | ||
} | ||
@@ -59,8 +60,8 @@ | ||
updateAll (db, cb) { | ||
if (this.getLastUpdate() < db.lastUpdate) { | ||
this.db = db | ||
fs.writeFile(this.path, JSON.stringify(this.db), err => { | ||
if (typeof cb === 'function') return cb(err) | ||
}) | ||
} | ||
if (this.getLastUpdate() >= db.lastUpdate) return cb() | ||
this.db = db | ||
fs.writeFile(this.path, JSON.stringify(this.db), err => { | ||
return cb(err) | ||
}) | ||
} | ||
@@ -67,0 +68,0 @@ |
@@ -12,2 +12,5 @@ 'use strict' | ||
this.conf = conf | ||
} | ||
start () { | ||
this.clients = new Synapsis({ | ||
@@ -18,5 +21,2 @@ namespace: this.conf.namespace, | ||
}) | ||
} | ||
start () { | ||
this.clients.start() | ||
@@ -30,10 +30,9 @@ | ||
this.emit('ready') | ||
this.clients.once('peer:connected', (identity, socket) => { | ||
this.syncDatabase(socket, () => { | ||
this.emit('ready') | ||
this.emit('synced') | ||
}) | ||
}) | ||
setTimeout(() => { | ||
this.emit('ready') | ||
}, 2000) | ||
}) | ||
@@ -45,3 +44,3 @@ | ||
// When a peer ask last update of db, send the last update to him | ||
this.clients.router(cst.LASTUPDATE_DATABASE, (reply) => { | ||
this.clients.router(cst.DB_LASTUPDATE, (reply) => { | ||
reply(null, this.db.getLastUpdate()) | ||
@@ -51,3 +50,3 @@ }) | ||
// When a peer ask to sync db, send the local db to him | ||
this.clients.router(cst.SYNC_DATABASE, (reply) => { | ||
this.clients.router(cst.DB_SYNC, (reply) => { | ||
reply(null, this.db.getDatabase()) | ||
@@ -57,5 +56,5 @@ }) | ||
// When a peer ask to update with new db, update local db | ||
this.clients.router(cst.UPDATE_DATABASE, (data) => { | ||
this.clients.router(cst.DB_UPDATE, (data) => { | ||
this.db.updateAll(data, () => { | ||
this.emit('interdb:db:refreshed') | ||
this.emit('refreshed') | ||
}) | ||
@@ -65,5 +64,8 @@ }) | ||
stop () { | ||
stop (cb) { | ||
this.db = undefined | ||
this.clients.stop() | ||
this.clients.stop(() => { | ||
delete this.clients | ||
if (typeof cb === 'function') return cb() | ||
}) | ||
} | ||
@@ -75,3 +77,3 @@ | ||
this.clients.broadcast(cst.LASTUPDATE_DATABASE, (err, res, identity) => { | ||
this.clients.broadcast(cst.DB_LASTUPDATE, (err, res, identity) => { | ||
if (err) return console.log(err) | ||
@@ -84,3 +86,4 @@ | ||
}) | ||
this.clients.send(lastUpdates[0].id, cst.SYNC_DATABASE, (err, res, identity) => { | ||
if (lastUpdates[0].lastUpdate <= this.db.getLastUpdate()) return cb() | ||
this.clients.send(lastUpdates[0].id, cst.DB_SYNC, (err, res, identity) => { | ||
if (err) return console.log(err) | ||
@@ -87,0 +90,0 @@ |
{ | ||
"name": "@keymetrics/interdb", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "Shared and discoverable database services", | ||
@@ -34,4 +34,4 @@ "main": "index.js", | ||
"debug": "*", | ||
"synapsis": "^0.6.0" | ||
"synapsis": "^0.6.1" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
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
54868
159
Updatedsynapsis@^0.6.1