From 9833a363cfc7a8e8f40dacb74091e5065c4938ae Mon Sep 17 00:00:00 2001 From: Benjamin Kitt Date: Wed, 24 Sep 2014 21:40:04 -0500 Subject: [PATCH] Fixed bug leading to multiple emits for the same collection. Lowercase name for npm registry. Tweaked example. --- README.md | 2 +- examples/example.js | 6 ++++-- lib/main.js | 40 +++++++++++++++++++++------------------- package.json | 4 ++-- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index a267ba5..91da647 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Many of the options are similar to Mongo Watch, but the connection options have To install oploggery from npm, run: -> `npm install Oploggery` +> `npm install oploggery` # Usage diff --git a/examples/example.js b/examples/example.js index 61576cf..3f04847 100644 --- a/examples/example.js +++ b/examples/example.js @@ -1,6 +1,8 @@ var Oploggery = require('../index.js'); var oplogger = new Oploggery({ - uri: 'mongodb://localhost:27017/test' + uri: 'mongodb://localhost:27017/hrDataTest' }); -oplogger.watch(); \ No newline at end of file +oplogger.watch('test.users', function(data) { + console.log(data); +}); \ No newline at end of file diff --git a/lib/main.js b/lib/main.js index aa3340f..22e8693 100644 --- a/lib/main.js +++ b/lib/main.js @@ -56,27 +56,29 @@ Oploggery = (function() { notify = notify || console.log; this.ready(function() { - var watcher = function(data) { - var channel, event, formatter, relevant; - - relevant = (collection === 'all') || (data.ns === collection); - if (!relevant) { - return; - } - - channel = collection ? "change:" + collection : 'change'; - formatter = formats[this.options.format] || formats.raw; - event = formatter(data); - if (this.options.convertObjectIDs === true) { - event = walk(event, convertObjectID); - } - return this.channel.emit(collection, event); - }.bind(this); - this.stream.on('data', watcher); - this.watching[collection] = watcher; + if (!this.watching[collection]) { + var watcher = function(data) { + var channel, event, formatter, relevant; + + relevant = (collection === 'all') || (data.ns === collection); + if (!relevant) { + return; + } + + channel = collection ? "change:" + collection : 'change'; + formatter = formats[this.options.format] || formats.raw; + event = formatter(data); + if (this.options.convertObjectIDs === true) { + event = walk(event, convertObjectID); + } + this.channel.emit(collection, event); + }.bind(this); + this.stream.on('data', watcher); + this.watching[collection] = watcher; + } }.bind(this)); - return this.channel.on(collection, notify); + this.channel.on(collection, notify); }; Oploggery.prototype.stop = function(collection) { diff --git a/package.json b/package.json index ead3cd7..a13e293 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "Oploggery", - "version": "0.1.0", + "name": "oploggery", + "version": "0.1.1", "description": "A MongoDB oplog watcher for Node.js", "main": "index.js", "directories": {