[EDIT] updated to Ground DB v2, made code more readable
I am trying to use GroundDB in my project, so my Meteor-React Cordova App can also run offline. In the main container I have the following code:
let fullyLoaded = new ReactiveVar(false);
let subscribed = false;
let subscribedOnce = false;
let checking = false;
export default createContainer(() => {
if(Meteor.status().connected && !subscribedOnce && !subscribed){
subscribed = true;
console.log("subscribing");
Meteor.subscribe("localization",
()=> {
localizationGrounded.keep(Localization.findNonGrounded());
console.log("everything has been loaded once.");
console.log("Localization count after subscription: " + Localization.find().fetch().length);
fullyLoaded.set(true);
subscribed = false;
subscribedOnce = true;
}
);
}
if(fullyLoaded.get()){
console.log("Localization Count: " + Localization.find().fetch().length)
}
return {
isLoggedIn: !!Meteor.userId(),
isLoading: !fullyLoaded.get() || subscribed,
};
}, Main);
This code is supposed to subscribe to "localization" if it hasn't been loaded already. The Localization Collection is implemented as follows, the find() and findOne() method has been overwritten to call find() for the grounded DB:
export const Localization = new Mongo.Collection('localization');
if(Meteor.isClient){
export let localizationGrounded = new Ground.Collection('localization', {
cleanupLocalData: false
});
//rename find() to findNonGrounded
Localization.findNonGrounded = Localization.find;
localizationGrounded.observeSource(Localization.findNonGrounded());
Localization.find = function(...args){
console.log("finding from ground db");
return localizationGrounded.find(...args);
};
Localization.findOne = function(...args){
console.log("finding one from ground db");
return localizationGrounded.findOne(...args);
}
}
This however produces the following output:
subscribing
everything has been loaded once
finding from ground db
Localization count after subscription: 28
finding from ground db
Localization count: 28
Looks fine, right? Unfortunately, the createContainer() function is called once more immediately after that, resulting in
...
Localization count: 28
//Lots of "finding one from ground db" indicating the page is being localized correctly
finding from ground db
Localization Count: 0
//more "finding one from ground db", this time returning undefined
Please help me to fix this. Thanks in advance
Taxel