Skip to content

Commit

Permalink
Added on('added') on('changed') on('removed') callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Théo mathieu committed Feb 19, 2016
1 parent 845b316 commit 1349a23
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
26 changes: 12 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ Unsubscribes to a server publication.

- `id` **string** *required* : id of the server publication

### on(eventName, collectionName, callback)

Subscribe to DDP events.

#### Arguments

- `eventName` **string** *required* : Name of the event (`connected`, `disconnected`, `added`, `changed`, `removed`)

- `collectionName` **string** *optional* : collection name to specify when event name = added, changed or removed.

- `callback` **function** *required* : callback called when event is emitted. Returns : element (added), element (changed), id (removed)

### itemSubscribe(name, collectionName, id, callback)

Subscribes to an item in a collection (the collection need to be subscribed with same name and collection name parameter). Returns the subscriptionId.
Expand All @@ -124,16 +136,6 @@ Unsubscribes to a item subscription.

- `subId` **string** *required* : id of the subscription

### on(eventName, callback)

Callback when an event is triggered

#### Arguments

- `eventName` **string** *required* : 'connected' and 'disconnected' only for the moment

- `callback` **function** *required*


### method(name, [args], callback)

Expand Down Expand Up @@ -178,7 +180,3 @@ Login to meteor server via a token
### logout(callback)

Logout from meteor server

#### Warning

You can only do one subscription on a same collection at one time
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ module.exports = {
ddp.on("added", function (message) {
subscriptions = subscriptions.map(function (sub) {
if(sub.collectionName == message.collection) {

queue.emit('added', sub.collectionName, message.fields);

message.fields.id = message.id;
sub.items.push(message.fields);
if(sub.ready) {
Expand Down Expand Up @@ -236,6 +239,9 @@ module.exports = {
ddp.on("removed", function (message) {
subscriptions = subscriptions.map(function (sub) {
if(sub.collectionName == message.collection && !sub.removed) {

queue.emit('removed', sub.collectionName, message.id);

sub.items = sub.items.filter(function (item) {
if(item.id == message.id) return false;
return true;
Expand All @@ -249,6 +255,11 @@ module.exports = {
ddp.on("changed", function (message) {
subscriptions = subscriptions.map(function (sub) {
if(sub.collectionName == message.collection) {

var item = Object.assign({}, message.fields);
item.id = message.id;
queue.emit('changed', sub.collectionName, item);

sub.items = sub.items.map(function (item) {
if(item.id==message.id) {
var res = {
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.5.1",
"version": "0.6.0",
"description": "DDP React-native Client",
"main": "index.js",
"scripts": {
Expand Down
16 changes: 13 additions & 3 deletions queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,25 @@
var callbacks = [];

module.exports = {
on: function (eventName, callback) {
on: function (eventName, collectionName, callback) {
if(callback === undefined) {
callback = collectionName;
collectionName = '';
}
callbacks.push({
eventName: eventName,
collectionName: collectionName,
callback: callback
});
},
emit: function (eventName, message) {
emit: function (eventName, collectionName, message) {
if(message === undefined) {
message = collectionName;
collectionName = '';
}

var eventCallbacks = callbacks.filter(function (callback) {
if(callback.eventName == eventName) return true;
if(callback.eventName == eventName && callback.collectionName == collectionName) return true;
return false;
}).map(function (callback) {
return callback.callback;
Expand Down

0 comments on commit 1349a23

Please sign in to comment.