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

Add support for "smart updates" of filters. Add smart update of unload event. #242

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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