Skip to content

Commit

Permalink
Ready handle
Browse files Browse the repository at this point in the history
  • Loading branch information
Théo mathieu committed Mar 31, 2016
1 parent c2eab55 commit fb81062
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ export default class App extends Component {
const url = 'http://192.168.X.X:3000/websocket';
Meteor.connect(url);
}
startMeteorSubscriptions() {
Meteor.subscribe('todos');
Meteor.subscribe('settings');
}
getMeteorData() {
const handle = Meteor.subscribe('todos');
Meteor.subscribe('settings');

return {
todosReady: handle.ready(),
settings: Meteor.collection('settings').findOne()
};
}
Expand All @@ -55,10 +55,12 @@ export default class App extends Component {
);
}
render() {
const { settings } = this.data;
const { settings, todosReady } = this.data;

<View>
<Text>{settings.title}</Text>
{!todosReady && <Text>Not ready</Text>}

<MeteorListView
collection="todos"
selector={{done: true}}
Expand All @@ -74,16 +76,16 @@ export default class App extends Component {

# connectMeteor

## startMeteorSubscriptions

## getMeteorData

Inside this method, you can create subscriptions (see below) when component is mounted. It will automatically unsubscribe if the component is unmounted.

* [Meteor.subscribe](http://docs.meteor.com/#/full/meteor_subscribe)
* [Meteor.subscribe](http://docs.meteor.com/#/full/meteor_subscribe) : returns an handle

## getMeteorData

Inside getMeteorData, you can access any Meteor reactive data source, which means :
Inside getMeteorData, you can also access any Meteor reactive data source, which means :

* Meteor.subscribe handle
* Meteor.collection(collectionName)
* [.find(selector, options)](http://docs.meteor.com/#/full/find)
* [.findOne(selector, options)](http://docs.meteor.com/#/full/findone)
Expand All @@ -94,6 +96,8 @@ Inside getMeteorData, you can access any Meteor reactive data source, which mean

# Additionals collection methods

These methods (except update) work offline. That means that elements are correctly updated offline, and when you reconnect to ddp, Meteor calls are taken care of.

* Meteor.collection(collectionName)
* [.insert(doc, callback)](http://docs.meteor.com/#/full/insert)
* [.update(id, modifier, [options], [callback])](http://docs.meteor.com/#/full/update)
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": "1.0.0-beta24",
"version": "1.0.0-beta25",
"description": "Full Meteor Client for React Native",
"main": "src/Meteor.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/CollectionFS/FSCollectionImagesPreloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default class FSCollectionImagesPreloader extends Component {
<View style={styles.hidden}>
{items && items.map(item=>{
return (
<Image style={styles.hidden} key={item._id} onLoadEnd={()=>console.log('LOADED IMAGE')} source={{uri: item.url()}} />
<Image style={styles.hidden} key={item._id} source={{uri: item.url()}} />
);
})}
</View>
Expand Down
22 changes: 20 additions & 2 deletions src/Meteor.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ module.exports = {

for(var i in Data.subscriptions) {
const sub = Data.subscriptions[i];
//console.log(sub.name, EJSON.clone(sub.params), sub.subIdRemember);
sub.ready = true;
sub.readyDeps.changed();
sub.readyCallback && sub.readyCallback();
}

});
Expand Down Expand Up @@ -191,6 +193,15 @@ module.exports = {
id = existing.id;
existing.inactive = false;

if (callbacks.onReady) {
// If the sub is not already ready, replace any ready callback with the
// one provided now. (It's not really clear what users would expect for
// an onReady callback inside an autorun; the semantics we provide is
// that at the time the sub first becomes ready, we call the last
// onReady callback provided, if any.)
if (!existing.ready)
existing.readyCallback = callbacks.onReady;
}
if (callbacks.onStop) {
existing.stopCallback = callbacks.onStop;
}
Expand All @@ -209,10 +220,13 @@ module.exports = {
params: EJSON.clone(params),
inactive: false,
ready: false,
readyDeps: new Trackr.Dependency,
readyCallback: callbacks.onReady,
stopCallback: callbacks.onStop,
stop: function() {
Data.ddp.unsub(this.subIdRemember);
delete Data.subscriptions[this.id];
this.ready && this.readyDeps.changed();

if (callbacks.onStop) {
callbacks.onStop();
Expand All @@ -230,7 +244,11 @@ module.exports = {
Data.subscriptions[id].stop();
},
ready: function () {
//TODO
if (!Data.subscriptions[id]) return false;

var record = Data.subscriptions[id];
record.readyDeps.depend();
return record.ready;
},
subscriptionId: id
};
Expand Down
1 change: 1 addition & 0 deletions src/components/Mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default {
}

if(this.startMeteorSubscriptions) {
console.warn('startMeteorSubscriptions is deprecated and will be removed soon. Please create your subscriptions in getMeteorData.');
this._meteorSubscriptionsManager = new MeteorSubscriptionsManager(this);
this._meteorSubscriptionsManager.getMeteorSubscriptions();
}
Expand Down

0 comments on commit fb81062

Please sign in to comment.