diff --git a/lib/Model/filter.js b/lib/Model/filter.js index a1412565c..5872d6162 100644 --- a/lib/Model/filter.js +++ b/lib/Model/filter.js @@ -3,6 +3,8 @@ var Model = require('./Model'); var defaultFns = require('./defaultFns'); Model.INITS.push(function(model) { + var smartUpdates = ['unload']; + model.root._filters = new Filters(model); model.on('all', filterListener); function filterListener(segments, eventArgs) { @@ -15,7 +17,14 @@ Model.INITS.push(function(model) { util.mayImpact(filter.segments, segments) || (filter.inputsSegments && util.mayImpactAny(filter.inputsSegments, segments)) ) { - filter.update(pass); + var method = eventArgs[0]; + if(smartUpdates.indexOf(method) > -1) { + // Do smart update which tries to limit the execution needed + filter._smartUpdate(pass, method, eventArgs, segments); + } else { + // Do regular update which reruns everything + filter.update(pass); + } } } } @@ -241,6 +250,23 @@ Filter.prototype.update = function(pass) { this.model.pass(pass, true)._setArrayDiff(this.idsSegments, ids); }; +Filter.prototype._smartUpdate = function(pass, method, eventArgs, segments) { + var ids = this.model._get(this.idsSegments) || []; + + if(method === 'unload') { + var doc = eventArgs[1]; + var id = doc.id; + var pos = ids.indexOf(id); + + // If it was already filtered, we ain't need to do anything + if(pos < 0) return; + + ids.splice(pos, 1); + } + + this.model.pass(pass, true)._setArrayDiff(this.idsSegments, ids); +}; + Filter.prototype.ref = function(from) { from = this.model.path(from); this.from = from;