From bc5ea6ca219e775211a3b3c6f3651e283c17c781 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?The=CC=81o=20mathieu?= Date: Mon, 18 Jan 2016 21:44:13 +0100 Subject: [PATCH] item subscription and item unsubscribe + 0.3.0 --- README.md | 26 ++++++++++++++++++++++++++ index.js | 48 ++++++++++++++++++++++++++++++++++++++++++++---- package.json | 2 +- 3 files changed, 71 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 0df8e7c..19b2031 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,32 @@ Unsubscribes to a server publication. - `id` **string** *required* : id of the server publication +### itemSuscribe(name, collectionName, id, callback) + +Subscribes to an item in a collection (the collection need to be suscribed with same name and collection name parameter). Returns the subscriptionId. + +#### Arguments + +- `name` **string** *required* : name of the server subscription + +- `collectionName` **string** *optional* : name of the collection you suscribe (in case the subscription name is different than collection name) + +- `id` **array** *required* : id of the item to suscribe to + +- `callback` **function** *required* : callback called when there is a change to the item. Returns the element. + +### itemUnsubscribe(name, collectionName, subId) + +Unsubscribes to a item subscription. + +#### Arguments + +- `name` **string** *required* : name of the server subscription + +- `collectionName` **string** *optional* : name of the collection you suscribe (in case the subscription name is different than collection name) + +- `subId` **string** *required* : id of the subscription + ### on(eventName, callback) Callback when an event is triggered diff --git a/index.js b/index.js index a732528..84c6592 100644 --- a/index.js +++ b/index.js @@ -45,8 +45,8 @@ module.exports = { }, suscribe: function (name, collectionName, params, callback) { if(typeof collectionName != 'string') { - params = collectionName; callback = params; + params = collectionName; collectionName = name; } if(callback===undefined) { @@ -61,11 +61,44 @@ module.exports = { name: name, callback: callback, ready: false, - items: [] + items: [], + itemsSubs: [] }); return subId; }, + itemSuscribe: function (name, collectionName, id, callback) { + if(typeof callback == 'undefined') { + callback = id; + id = collectionName; + collectionName = name; + } + var sub = subscriptions.find((subscription)=>{return name==subscription.name && collectionName == subscription.collectionName}); + if(sub) { + var subId = parseInt(Math.random()*100000000000, 10); + sub.itemsSubs.push({ + subId: subId, + id: id, + callback: callback + }); + + return subId; + } + return false; + }, + itemUnsuscribe: function (name, collectionName, subId) { + if(typeof subId == 'undefined') { + subId = collectionName; + collectionName = name; + } + var sub = subscriptions.find((subscription)=>{return name==subscription.name && collectionName == subscription.collectionName}); + for(var i in sub.itemsSubs) { + if(sub.itemsSubs[i].subId == subId) { + sub.itemsSubs.splice(i, 1); + } + } + + }, connect: function (endpoint) { ddp = new DDP({ @@ -163,10 +196,17 @@ module.exports = { if(sub.collectionName == message.collection) { sub.items = sub.items.map(function (item) { if(item.id==message.id) { - return { + var res = { ...item, ...message.fields - }; + } + //NOTIFY ITEMS subs + sub.itemsSubs.map(subItem=>{ + if(subItem.id == res.id) { + subItem.callback(res); + } + }); + return res; } return item; }); diff --git a/package.json b/package.json index e923328..537802e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-meteor", - "version": "0.2.0", + "version": "0.3.0", "description": "DDP React-native Client", "main": "index.js", "scripts": {