Skip to content

Commit

Permalink
item subscription and item unsubscribe + 0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Théo mathieu committed Jan 18, 2016
1 parent 271fef1 commit bc5ea6c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 44 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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({
Expand Down Expand Up @@ -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;
});
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": "react-native-meteor",
"version": "0.2.0",
"version": "0.3.0",
"description": "DDP React-native Client",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit bc5ea6c

Please sign in to comment.