react-native-meteor
Advanced tools
Comparing version 1.0.0-beta1 to 1.0.0-beta10
@@ -0,1 +1,11 @@ | ||
# Android | ||
Add this to your AndroidManifest.xml file to autoreconnect fastly to DDP server if your device reconnects to network | ||
```xml | ||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | ||
``` | ||
# Installing decorators | ||
@@ -18,7 +28,7 @@ | ||
Looking for your help. The RN 0.20.0 might be working, please let me know ;) | ||
Looking for your help. The RN 0.20.0 solution might be working, please let me know ;) | ||
## With RN <0.16.0 (Babel 5) | ||
Use a .babecrc file at the root of your projet that contains : | ||
Use a .babelrc file at the root of your projet that contains : | ||
@@ -25,0 +35,0 @@ ```json |
{ | ||
"name": "react-native-meteor", | ||
"version": "1.0.0-beta1", | ||
"version": "1.0.0-beta10", | ||
"description": "DDP React-native Client", | ||
@@ -29,7 +29,9 @@ "main": "src/Meteor.js", | ||
"dependencies": { | ||
"ddp.js": "1.1.0", | ||
"ejson": "^2.1.2", | ||
"minimongo-cache": "0.0.48", | ||
"trackr": "^2.0.2" | ||
"react-mixin": "^3.0.3", | ||
"meteor-random": "0.0.3", | ||
"trackr": "^2.0.2", | ||
"wolfy87-eventemitter": "^4.3.0" | ||
} | ||
} |
103
README.md
@@ -15,3 +15,3 @@ [](https://badge.fury.io/gh/inProgress-team%2Freact-native-meteor) | ||
Meteor-like methods for React Native. **Currently in v1.0.0-beta1** ! For old docs, see [v0.6.2 documentation](https://github.com/inProgress-team/react-native-meteor/tree/0.6.2) (classic ddp interface). | ||
Meteor-like methods for React Native. **Currently in v1.0.0-beta9** ! For old docs, see [v0.6.2 documentation](https://github.com/inProgress-team/react-native-meteor/tree/0.6.2) (classic ddp interface). | ||
@@ -23,7 +23,7 @@ ## What is it for ? | ||
* be fully compatible with react-native and help react-native developers. | ||
* **Use the EXACT SAME METHODS as [Meteor documentation](http://docs.meteor.com/) used with React.** | ||
* **to match with [Meteor documentation](http://docs.meteor.com/) used with React.** | ||
## Install | ||
npm i --save react-native-meteor | ||
npm i --save react-native-meteor@latest | ||
@@ -37,4 +37,3 @@ [!! See detailed installation guide](https://github.com/inProgress-team/react-native-meteor/blob/master/docs/Install.md) | ||
import { View, Text, Component } from 'react-native'; | ||
import Meteor, { MeteorMixin } from 'react-native-meteor'; | ||
import reactMixin from 'react-mixin'; | ||
import Meteor, { connectMeteor, MeteorListView } from 'react-native-meteor'; | ||
@@ -48,3 +47,3 @@ /* | ||
} | ||
reactMixin(Todos.prototype, MeteorMixin); | ||
connectMeteor(Todos); | ||
export default Todos; | ||
@@ -54,3 +53,3 @@ | ||
@reactMixin.decorate(MeteorMixin) | ||
@connectMeteor | ||
export default class App extends Component { | ||
@@ -63,18 +62,26 @@ componentWillMount() { | ||
Meteor.subscribe('todos'); | ||
Meteor.subscribe('settings'); | ||
} | ||
getMeteorData() { | ||
return { | ||
todos: Meteor.collection('todos').find() | ||
settings: Meteor.collection('settings').findOne() | ||
}; | ||
} | ||
renderRow(todo) { | ||
return ( | ||
<Text>{todo.title}</Text> | ||
); | ||
} | ||
render() { | ||
const { todos } = this.data; | ||
const { settings } = this.data; | ||
{todos.map(todo=>{ | ||
return ( | ||
<View key={todo._id}> | ||
<Text>{todo.title}</Text> | ||
</View> | ||
) | ||
})} | ||
<View> | ||
<Text>{settings.title}</Text> | ||
<MeteorListView | ||
collection="todos" | ||
selector={{done: true}} | ||
options={{sort: {createdAt: -1}}} | ||
renderRow={this.renderRow} | ||
/> | ||
</View> | ||
@@ -85,3 +92,3 @@ } | ||
# MeteorMixin | ||
# connectMeteor | ||
@@ -93,4 +100,18 @@ ## startMeteorSubscriptions | ||
#### [Meteor.subscribe](http://docs.meteor.com/#/full/meteor_subscribe) | ||
##### Example usage | ||
your server side: | ||
```javascript | ||
Meteor.publish('todos', function(done, options){ | ||
return Todos.find({ done: done }, options); | ||
}); | ||
``` | ||
your react-native client code: | ||
```javascript | ||
//Meteor subscribe can be used like on meteor official site | ||
Meteor.subscribe('todos', true, {limit: 10, sort: {createdAt: -1}}); | ||
``` | ||
## getMeteorData | ||
@@ -109,5 +130,24 @@ | ||
# MeteorListView Component | ||
Same as [ListView](https://facebook.github.io/react-native/docs/listview.html) Component but does not need dataSource and accepts three arguments : | ||
- `collection` **string** *required* | ||
- `selector` [**string** / **object**] | ||
- `url` **object** | ||
### Example usage | ||
```javascript | ||
<MeteorListView | ||
collection="todos" | ||
selector={{done: true}} | ||
options={{sort: {createdAt: -1}}} | ||
renderItem={this.renderItem} | ||
/> | ||
``` | ||
# API | ||
## Meteor.connect(endpoint) | ||
## Meteor.connect(endpoint, options) | ||
@@ -119,7 +159,30 @@ Connect to a DDP server. You only have to do this once in your app. | ||
- `url` **string** *required* | ||
- `options` **object** Available options are : | ||
- autoConnect **boolean** [true] whether to establish the connection to the server upon instantiation. When false, one can manually establish the connection with the Meteor.ddp.connect method. | ||
- autoReconnect **boolean** [true] whether to try to reconnect to the server when the socket connection closes, unless the closing was initiated by a call to the disconnect method. | ||
- reconnectInterval **number** [10000] the interval in ms between reconnection attempts. | ||
## Meteor.disconnect() | ||
Disconnect from the DDP server. | ||
## Meteor methods | ||
* [Meteor.loginWithPassword](http://docs.meteor.com/#/full/meteor_loginwithpassword) (Please note that user is auto-resigned in - like in Meteor Web applications - thanks to React Native AsyncStorage.) | ||
* [Meteor.logout](http://docs.meteor.com/#/full/meteor_logout) | ||
* [Meteor.call](http://docs.meteor.com/#/full/meteor_call) | ||
* [Meteor.loginWithPassword](http://docs.meteor.com/#/full/meteor_loginwithpassword) (Please note that user signin is persisted - like in Meteor Web applications - thanks to React Native AsyncStorage.) | ||
* [Meteor.logout](http://docs.meteor.com/#/full/meteor_logout) | ||
## Meteor.ddp | ||
Once connected to the ddp server, you can access every method available in [ddp.js](https://github.com/mondora/ddp.js/). | ||
* Meteor.ddp.on('connected') | ||
* Meteor.ddp.on('added') | ||
* Meteor.ddp.on('changed') | ||
* ... | ||
# TODO | ||
- [ ] [Helper for Meteor-CollectionFS](https://github.com/inProgress-team/react-native-meteor/issues/18) | ||
Pull Requests are welcome ! :) |
@@ -5,2 +5,4 @@ import minimongo from 'minimongo-cache'; | ||
export default { | ||
endpoint: null, | ||
options: null, | ||
ddp: null, | ||
@@ -21,14 +23,19 @@ subscriptions: {}, | ||
_cbsLoggingIn: [], | ||
_subscribeLoggingIn(cb) { | ||
this._cbsLoggingIn.push(cb); | ||
_cbs: [], | ||
on(eventName, cb) { | ||
this._cbs.push({ | ||
eventName: eventName, | ||
callback: cb | ||
}); | ||
}, | ||
_unsubscribeLoggingIn(cb) { | ||
this._cbsLoggingIn.splice(this._cbsLoggingIn.indexOf(cb)); | ||
off(eventName, cb) { | ||
this._cbs.splice(this._cbs.findIndex(_cb=>_cb.callback == cb && _cb.eventName == eventName)); | ||
}, | ||
_notifyLoggingIn() { | ||
for(var i in this._cbsLoggingIn) { | ||
this._cbsLoggingIn[i](); | ||
} | ||
this._cbs.map(cb=>{ | ||
if(cb.eventName=='loggingIn' && typeof cb.callback=='function') { | ||
cb.callback(); | ||
} | ||
}); | ||
} | ||
} |
@@ -0,4 +1,6 @@ | ||
import { NetInfo } from 'react-native'; | ||
import reactMixin from 'react-mixin'; | ||
import Trackr from 'trackr'; | ||
import DDP from 'ddp.js'; | ||
import DDP from '../lib/ddp.js'; | ||
@@ -8,9 +10,17 @@ import Data from './Data'; | ||
import User from './User'; | ||
import ListView from './ListView'; | ||
module.exports = { | ||
MeteorMixin: Mixin, | ||
MeteorListView: ListView, | ||
connectMeteor(reactClass) { | ||
return reactMixin.onClass(reactClass, Mixin); | ||
}, | ||
collection(name) { | ||
const Meteor = this; | ||
return { | ||
find(selector, options) { | ||
if(!Data.db || !Data.db[name]) return []; | ||
if(typeof selector == 'string') return this.find({_id: selector}, options); | ||
return Data.db[name].find(selector, options) | ||
@@ -24,2 +34,18 @@ | ||
}, | ||
insert(item, callback) { | ||
return; | ||
if(!Data.db[name]) { Data.db.addCollection(name) } | ||
const itemSaved = Data.db[name].upsert({...item}); | ||
console.log('/'+name+'/insert'); | ||
Meteor.call('/'+name+'/insert', itemSaved, (err, res) => { | ||
if(err) return console.log(err); | ||
console.log(res); | ||
}); | ||
return ; | ||
} | ||
@@ -51,13 +77,43 @@ }; | ||
}, | ||
connect(endpoint) { | ||
Data.ddp = new DDP({ | ||
disconnect() { | ||
if(Data.ddp) { | ||
Data.ddp.disconnect(); | ||
} | ||
}, | ||
_reconnect() { | ||
if(Data._endpoint) { | ||
this.connect(Data._endpoint, Data._options); | ||
} | ||
}, | ||
connect(endpoint, options) { | ||
Data._endpoint = endpoint; | ||
Data._options = options; | ||
this.ddp = Data.ddp = new DDP({ | ||
endpoint: endpoint, | ||
SocketConstructor: WebSocket | ||
SocketConstructor: WebSocket, | ||
...options | ||
}); | ||
Data.ddp.on("connected", ()=>{ | ||
console.info("connected"); | ||
console.info("Connected to DDP server."); | ||
this._loadInitialUser(); | ||
if(!this._netInfoListener) { | ||
this._netInfoListener = isConnected=>{ | ||
if(isConnected) { | ||
this._reconnect(); | ||
} | ||
}; | ||
NetInfo.isConnected.addEventListener('change', this._netInfoListener); | ||
} | ||
}); | ||
Data.ddp.on("disconnected", ()=>{ | ||
console.info("Disconnected from DDP server."); | ||
}); | ||
Data.ddp.on("added", message => { | ||
@@ -71,4 +127,3 @@ if(!Data.db[message.collection]) { | ||
Data.ddp.on("ready", message => { | ||
console.info('READY', message.subs); | ||
//console.log('READY', Data.db.todos && Data.db.todos.find().length); | ||
//console.info('READY', message.subs); | ||
}); | ||
@@ -98,5 +153,15 @@ | ||
}); | ||
}, | ||
subscribe(name) { | ||
const params = Array.prototype.slice.call(arguments, 1); | ||
var params = Array.prototype.slice.call(arguments, 1); | ||
var callbacks = {}; | ||
if (params.length) { | ||
var lastParam = params[params.length - 1]; | ||
if (typeof lastParam == 'function') { | ||
callbacks.onReady = params.pop(); | ||
} else if (lastParam && (typeof lastParam.onReady == 'function' || typeof lastParam.onError == 'function' || typeof lastParam.onStop == 'function')) { | ||
callbacks = params.pop(); | ||
} | ||
} | ||
@@ -103,0 +168,0 @@ const stringKey = name+JSON.stringify(params); |
@@ -46,4 +46,7 @@ import Trackr from 'trackr'; | ||
} | ||
if(this._loggingInCallback) { | ||
Data._unsubscribeLoggingIn(this._loggingInCallback); | ||
if(this._meteorChangeCallback) { | ||
Data.db.off('change', this._meteorChangeCallback); | ||
Data.ddp.off('connected', this._meteorChangeCallback); | ||
Data.ddp.off('disconnected', this._meteorChangeCallback); | ||
Data.off('loggingIn', this._meteorChangeCallback); | ||
} | ||
@@ -80,35 +83,13 @@ | ||
component._meteorFirstRun = false; | ||
component._meteorChangeCallback = () => { updateData(component); }; | ||
updateData(component); | ||
Data.db.on('change', (records)=>{ | ||
updateData(component); | ||
}); | ||
Data.ddp.on('connected', ()=> { | ||
updateData(component); | ||
}); | ||
Data.ddp.on('disconnected', ()=> { | ||
updateData(component); | ||
}); | ||
Data.db.on('change', component._meteorChangeCallback); | ||
Data.ddp.on('connected', component._meteorChangeCallback); | ||
Data.ddp.on('disconnected', component._meteorChangeCallback); | ||
Data.on('loggingIn', component._meteorChangeCallback); | ||
component._loggingInCallback = ()=> { | ||
updateData(component); | ||
}; | ||
Data._subscribeLoggingIn(component._loggingInCallback); | ||
} | ||
//console.log('WATCH'); | ||
/* | ||
Trackr.autorun(() => { | ||
console.log('AUTORUN'); | ||
}); | ||
Trackr.afterFlush(() => { | ||
console.log('AFTER FLUSH'); | ||
console.log(component.getMeteorData().todos.length); | ||
component._meteorCalledSetState = true; | ||
component.setState(partialData); | ||
}); | ||
*/ | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
29734
15
641
179
0
6
+ Addedmeteor-random@0.0.3
+ Addedreact-mixin@^3.0.3
+ Addedwolfy87-eventemitter@^4.3.0
+ Addedcrypto@0.0.3(transitive)
+ Addedmeteor-random@0.0.3(transitive)
+ Addedobject-assign@4.1.1(transitive)
+ Addedreact-mixin@3.1.1(transitive)
+ Addedsmart-mixin@2.0.0(transitive)
- Removedddp.js@1.1.0
- Removedddp.js@1.1.0(transitive)