Skip to content

Commit

Permalink
allow to subscribe for any schema event
Browse files Browse the repository at this point in the history
  • Loading branch information
capaj committed Feb 18, 2016
1 parent 101bef2 commit 6c3a563
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
19 changes: 10 additions & 9 deletions mr-rpc-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,8 @@ var expose = function (model, schema, opts) {
const subscribers = {}

evNames.forEach(function (evName) {
subscribers[evName] = new Set()
const modelSynchronization = modelSyncFactory(evName)
schema.on(evName, function (doc) {
Array.from(subscribers[evName]).forEach((socket) => {
socket.emit('schemaEvent', {
modelName: modelName,
evName: evName,
doc: doc
})
})
modelSynchronization(doc)
})
})
Expand Down Expand Up @@ -505,7 +497,16 @@ var expose = function (model, schema, opts) {
subscribe: function (evName) {
const socket = this
if (!subscribers.hasOwnProperty(evName)) {
throw new Error(`event ${evName} does not exist`)
subscribers[evName] = new Set()
schema.on(evName, (doc) => {
Array.from(subscribers[evName]).forEach((socket) => {
socket.emit('schemaEvent', {
modelName: modelName,
evName: evName,
doc: doc
})
})
})
}
opts.checkPermission(this, 'read')
const subscribersForThisEvent = subscribers[evName]
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "moonridge",
"version": "0.9.25",
"version": "0.9.26",
"main": "moonridge.js",
"author": "Capaj <capajj@gmail.com>",
"description": "Isomorphic library, which allows you to use Mongoose.js models via socket.io-rpc doing regular queries and liveQueries",
Expand Down
8 changes: 6 additions & 2 deletions test/mr-rpc-methods.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ describe('rpc methods', function () {
})
exposeCb(fakeRpcInstance)
})
it('should expose a query/liveQuery method which runs middlewares on built queries', function () {
it.skip('should expose a query/liveQuery method which runs middlewares on built queries', function () {
// TODO
})

it('should delete a listener when socket disconnects', function () {
it.skip('should delete a listener when socket disconnects', function () {
// TODO
})

it.skip('should subscribe to any schema event', function () {

})
})

0 comments on commit 6c3a563

Please sign in to comment.