Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix not remove cache minimongo when reconnected #233

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"react-komposer": "^1.8.0",
"react-mixin": "^3.0.3",
"trackr": "^2.0.2",
"react-dom": "^16.0.0-alpha.12",
"wolfy87-eventemitter": "^4.3.0",
"underscore": "^1.8.3"
},
Expand Down
3 changes: 2 additions & 1 deletion src/Data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import ReactNative from 'react-native';
import minimongo from 'minimongo-cache';
import Trackr from 'trackr';
import { InteractionManager } from 'react-native';
import ReactDOM from 'react-dom';
process.nextTick = setImmediate;

const db = new minimongo();
db.debug = false;
db.batchedUpdates = ReactNative.unstable_batchedUpdates;
db.batchedUpdates = ReactDOM.unstable_batchedUpdates;

function runAfterOtherComputations(fn){
InteractionManager.runAfterInteractions(() => {
Expand Down
39 changes: 39 additions & 0 deletions src/Meteor.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ module.exports = {
const sub = Data.subscriptions[i];
Data.ddp.unsub(sub.subIdRemember);
sub.subIdRemember = Data.ddp.sub(sub.name, sub.params);
sub.resubscribed = false; // set resubscribed to false, to keep track of which subs have had 'ready' called
}

},
_collectionIdsHash: {}, // the map to store collections/document ids
waitDdpConnected: Data.waitDdpConnected.bind(Data),
reconnect() {
Data.ddp && Data.ddp.connect();
Expand Down Expand Up @@ -108,6 +110,8 @@ module.exports = {
let lastDisconnect = null;
Data.ddp.on("disconnected", ()=>{

this._collectionIdsHash = {}; // empty out the map on disconnect, because all subs will be restarted on reconnect

Data.notify('change');

console.info("Disconnected from DDP server.");
Expand All @@ -127,6 +131,14 @@ module.exports = {
Data.db.addCollection(message.collection)
}
Data.db[message.collection].upsert({_id: message.id, ...message.fields});

// add the collection/document id to the map
if (!this._collectionIdsHash[message.collection]) {
this._collectionIdsHash[message.collection] = [];
}
if (this._collectionIdsHash[message.collection].indexOf(message.id) < 0) {
this._collectionIdsHash[message.collection].push(message.id);
}
});

Data.ddp.on("ready", message => {
Expand All @@ -142,6 +154,28 @@ module.exports = {
sub.ready = true;
sub.readyDeps.changed();
sub.readyCallback && sub.readyCallback();
sub.resubscribed = true; // set this subscription as resubscribed
}
}

// iterate through all the subscriptions to see if they've all been "re-readied"
var allResubscribed = true;

for (var sub in Data.subscriptions) {
if (Data.subscriptions[sub].resubscribed == false) allResubscribed = false
}

// if every subscription has finished, the collection/document ids map should contain the ids of every subscribed document.
// therefore, we are able to remove all documents from minimongo that do not appear in the map
if (allResubscribed) {
for (var collection in this._collectionIdsHash) {
if (this._collectionIdsHash[collection].length && Data.db && Data.db.collections && Data.db[collection]) {
Data.db[collection].remove({
_id: {
$nin: this._collectionIdsHash[collection]
}
});
}
}
}
});
Expand All @@ -152,6 +186,11 @@ module.exports = {

Data.ddp.on("removed", message => {
Data.db[message.collection] && Data.db[message.collection].del(message.id);

// remove the document id from the map
if (this._collectionIdsHash[message.collection] && this._collectionIdsHash[message.collection].indexOf(message.id) > -1) {
this._collectionIdsHash[message.collection].splice(this._collectionIdsHash[message.collection].indexOf(message.id), 1);
}
});
Data.ddp.on("result", message => {
const call = Data.calls.find(call=>call.id==message.id);
Expand Down