New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

react-native-meteor

Package Overview
Dependencies
Maintainers
1
Versions
69
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-meteor - npm Package Compare versions

Comparing version 0.0.1 to 0.1.0

158

index.js
var DDP = require("ddp.js");
var ddp = new DDP({
endpoint: 'http://inprogresstest.meteor.com/websocket',
SocketConstructor: WebSocket
});
ddp.on("connected", function () {
console.log("Connected");
});
var ddp;
var onCallbacks = [];
var callCallbacksOn = function (eventName) {
ddp.on(eventName, function (message) {
var callbacks = onCallbacks.filter(function (callback) {
if(callback.eventName == eventName) return true;
return false;
}).map(function (callback) {
return callback.callback;
});
callbacks.forEach(function (callback) {
callback(message);
});
});
};
var subscriptions = [];
module.exports = {
suscribe: function (collection, change) {
var collection = "tasks";
var subId = ddp.sub(collection, [{sort: {createdAt: -1}}]);
var doneLoading = false;
var items = [];
on: function (event, callback) {
onCallbacks.push({
eventName: event,
callback: callback
});
},
unsuscribe: function (id) {
//unsubs.push(id);
ddp.unsub(id);
subscriptions = subscriptions.map(function (sub) {
if(sub.id == id) {
sub.removed = true;
}
return sub
});
},
suscribe: function (name, collectionName, params, callback) {
if(typeof collectionName != 'string') {
params = collectionName;
callback = params;
collectionName = name;
}
if(callback===undefined) {
callback = params;
params = [];
}
var subId = ddp.sub(name, params);
subscriptions.push({
id: subId,
collectionName: collectionName,
name: name,
callback: callback,
ready: false,
items: []
});
return subId;
},
connect: function (endpoint) {
ddp = new DDP({
endpoint: endpoint,
SocketConstructor: WebSocket
});
callCallbacksOn("connected");
ddp.on("added", function (message) {
if(message.collection == collection) {
message.fields.id = message.id;
items.push(message.fields);
if(doneLoading) {
change(items);
subscriptions = subscriptions.map(function (sub) {
if(sub.collectionName == message.collection) {
message.fields.id = message.id;
sub.items.push(message.fields);
if(sub.ready) {
sub.callback(sub.items);
}
}
}
return sub;
})
});
ddp.on("changed", function (message) {
if(message.collection == collection) {
items = items.map(function (item) {
if(item.id==message.id) return {
...item,
...message.fields
}
return item;
});
change(items);
}
ddp.on("ready", function (message) {
subscriptions = subscriptions.map(function (sub) {
if(sub.id == message.subs[0]) {
sub.ready = true;
sub.callback(sub.items);
}
return sub;
});
});
ddp.on("nosub", function (message) {
console.log('NO SUB');
subscriptions = subscriptions.filter(function (sub) {
if(sub.id == message.id) return false;
return true;
});
});
ddp.on("removed", function (message) {
if(message.collection == collection) {
items = items.filter(function (item) {
if(item.id==message.id) return false;
return true;
});
change(items);
}
subscriptions = subscriptions.map(function (sub) {
if(sub.collectionName == message.collection && !sub.removed) {
sub.items = sub.items.filter(function (item) {
if(item.id == message.id) return false;
return true;
});
sub.callback(sub.items);
}
return sub;
});
});
ddp.on("nosub", function (message) {
doneLoading = true;
change(items);
ddp.on("changed", function (message) {
subscriptions = subscriptions.map(function (sub) {
if(sub.collectionName == message.collection) {
sub.items = sub.items.map(function (item) {
if(item.id==message.id) return {
...item,
...message.fields
}
return item;
});
sub.callback(sub.items);
}
return sub;
});
});
}
};
{
"name": "react-native-meteor",
"version": "0.0.1",
"version": "0.1.0",
"description": "DDP React-native Client",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -0,1 +1,97 @@

[![Dependency Status](https://david-dm.org/inProgress-team/react-native-meteor.svg)](https://david-dm.org/inProgress-team/react-native-meteor)
[![devDependency Status](https://david-dm.org/inProgress-team/react-native-meteor/dev-status.svg)](https://david-dm.org/inProgress-team/react-native-meteor#info=devDependencies)
# react-native-meteor
React-native meteor ddp adapter
## What is it for ?
The purpose of this library is :
* to set up and maintain a ddp connection with a ddp server, freeing the developer from having to do it on their own
* be fully compatible with react-native and help react-native developers
## Install
npm i --save react-native-meteor
## Example usage
```javascript
var Example = React.createClass({
getInitialState: function() {
return {
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => row1!==row2,
}),
loaded: false,
};
},
componentDidMount: function() {
var self = this;
meteor.connect('http://YOURIP:3000/websocket');
meteor.on('connected', function () {
console.log('connected');
});
this.tasksSub = meteor.suscribe('tasks', function (tasks) {
self.setState({
dataSource: self.state.dataSource.cloneWithRows(tasks),
loaded: true
});
});
},
componentWillUnmount: function () {
meteor.unsuscribe(this.tasksSub);
}
});
```
## Public API
### connect(url)
Connect to a ddp server. You have to this only once in your app.
#### Arguments
- `url` **string** *required*
### suscribe(name, collectionName, params, callback)
Subscribes to a server publication.
#### 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)
- `params` **array** *optional* : parameters to pass to the server publish function.
- `callback` **function** *required* : callback called when there is a change in the publication. Returns all elements.
### unsuscribe(id)
Unsubscribes to a server publication.
#### Arguments
- `id` **string** *required* : id of the server publication
### on(eventName, callback)
Callback when an event is triggered
#### Arguments
- `eventName` **string** *required* : 'connected' only for the moment
- `callback` **function** *required*
#### Warning
You can only do one subscription on a same collection at one time
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc