Skip to content
Open
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
28 changes: 27 additions & 1 deletion lib/Model/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
}
}
}
Expand Down Expand Up @@ -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;
Expand Down