Skip to content

Commit

Permalink
Cache contact summary generator function
Browse files Browse the repository at this point in the history
Don't compile it every time the contact summary is requested.
Invalidation is handled by the page being refreshed on configuration
change.

#3837
  • Loading branch information
garethbowen committed Aug 31, 2017
1 parent ab87b1c commit e7c6e34
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions static/js/services/contact-summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,22 @@ angular.module('inboxServices').service('ContactSummary',
'use strict';
'ngInject';

var getScript = function() {
return Settings().then(function(settings) {
return settings[SETTING_NAME];
});
var generatorFunction;

var getGeneratorFunction = function() {
if (!generatorFunction) {
generatorFunction = Settings()
.then(function(settings) {
return settings[SETTING_NAME];
})
.then(function(script) {
if (!script) {
return function() {};
}
return new Function('contact', 'reports', 'lineage', script); // jshint ignore:line
});
}
return generatorFunction;
};

var applyFilter = function(field) {
Expand Down Expand Up @@ -46,12 +58,8 @@ angular.module('inboxServices').service('ContactSummary',
};

return function(contact, reports, lineage) {
return getScript()
.then(function(script) {
if (!script) {
return;
}
var fn = new Function('contact', 'reports', 'lineage', script); // jshint ignore:line
return getGeneratorFunction()
.then(function(fn) {
return fn(contact, reports || [], lineage || []);
})
.then(applyFilters);
Expand Down

0 comments on commit e7c6e34

Please sign in to comment.