Skip to content

Commit

Permalink
Prevents listener to be triggered if component is unmounted
Browse files Browse the repository at this point in the history
  • Loading branch information
Théo mathieu committed Mar 8, 2016
1 parent eb21100 commit 00cabb8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 39 deletions.
21 changes: 13 additions & 8 deletions src/Data.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@ export default {
}
},

_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();
}
});
}
}
2 changes: 1 addition & 1 deletion src/Meteor.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module.exports = {
});

Data.ddp.on("connected", ()=>{
console.info("connected");
console.info("Connected to DDP server.");
this._loadInitialUser();
});

Expand Down
42 changes: 12 additions & 30 deletions src/Mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default {
this._meteorFirstRun = true;

Trackr.autorun((computation)=>{
console.info('TRACKER UPDATE');
this._meteorComputation = computation;
this._meteorDataDep.depend();

Expand Down Expand Up @@ -43,8 +44,11 @@ export default {
this._meteorComputation.stop();
this._meteorComputation = null;
}
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);
}

}
Expand Down Expand Up @@ -76,37 +80,15 @@ const enqueueMeteorDataUpdate = function(component) {
// If it's the first time we've called enqueueMeteorDataUpdate since
// the component was mounted, set the state synchronously.
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);
});
*/
}

0 comments on commit 00cabb8

Please sign in to comment.