Skip to content

Commit 1aec64d

Browse files
committed
MAGE-1000: Split out template engine adapter to use parsing outside of promises
1 parent e290951 commit 1aec64d

File tree

2 files changed

+37
-36
lines changed

2 files changed

+37
-36
lines changed

view/frontend/web/js/instantsearch.js

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,15 @@
11
define([
22
'jquery',
33
'algoliaBundle',
4-
'algoliaHoganLib',
5-
'algoliaMustacheLib',
64
'algoliaTemplateEngine',
75
'Magento_Catalog/js/price-utils',
86
'algoliaCommon',
97
'algoliaInsights',
108
'algoliaHooks',
11-
], function ($, algoliaBundle, Hogan, Mustache, templateEngine, priceUtils) {
12-
13-
const processTemplate = (template, templateVars, useMustache = false) => {
14-
const hoganStart = performance.now();
15-
const wrapperTemplate = Hogan.compile(template);
16-
const hoganResult = wrapperTemplate.render(templateVars);
17-
const hoganEnd = performance.now();
18-
console.log("Hogan execution time: %s ms", hoganEnd - hoganStart);
19-
20-
if (useMustache) {
21-
const mustacheStart = performance.now();
22-
const mustacheResult = Mustache.render(template, templateVars);
23-
const mustacheEnd = performance.now();
24-
console.log("Mustache execution time: %s ms", mustacheEnd - mustacheStart);
25-
return mustacheResult;
26-
}
27-
28-
return hoganResult;
29-
};
30-
9+
], function ($, algoliaBundle, templateEngine, priceUtils) {
3110
$(async function ($) {
11+
const templateProcessor = await templateEngine.getSelectedEngineAdapter();
12+
3213
/** We have nothing to do here if instantsearch is not enabled **/
3314
if (
3415
typeof algoliaConfig === 'undefined' ||
@@ -130,7 +111,7 @@ define([
130111
translations : algoliaConfig.translations,
131112
};
132113

133-
const wrapperHtml = await templateEngine.processTemplate(template, templateVars);
114+
const wrapperHtml = templateProcessor.process(template, templateVars);
134115
$('.algolia-instant-selector-results').html(wrapperHtml).show();
135116

136117
/**
@@ -375,7 +356,7 @@ define([
375356
}
376357

377358
const template = $('#instant-stats-template').html();
378-
return processTemplate(template, data, true);
359+
return templateProcessor.process(template, data);
379360
},
380361
},
381362
},

view/frontend/web/js/internals/template-engine.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,46 @@ define([], function () {
22
const ENGINE_TYPE_HOGAN = 'hogan';
33
const ENGINE_TYPE_MUSTACHE = 'mustache';
44

5-
return {
6-
ENGINE_TYPE_HOGAN,
7-
ENGINE_TYPE_MUSTACHE,
8-
9-
getSelectedEngine: () => ENGINE_TYPE_MUSTACHE, // override via mixin
10-
11-
processTemplate: async function (template, data, measure = false) {
12-
const adapter = await this.getEngineAdapter(this.getSelectedEngine());
13-
if (measure) {
5+
const trackingAdapter = {
6+
processAndMeasure: function(template, data, tag = '') {
147
const start = performance.now();
15-
const result = adapter.process(template, data);
8+
const result = this.process(template, data);
169
const end = performance.now();
10+
const prefix = tag ? `${tag}: ` : '';
1711
console.log(
18-
`### Template execution time with "${this.getSelectedEngine()}": %s ms`,
12+
`${prefix}Template execution time with "${this.engineType}": %s ms`,
1913
end - start
2014
);
2115
return result;
2216
}
17+
};
2318

24-
return adapter.process(template, data);
19+
return {
20+
ENGINE_TYPE_HOGAN,
21+
ENGINE_TYPE_MUSTACHE,
22+
23+
getSelectedEngineType: () => ENGINE_TYPE_MUSTACHE, // override via mixin
24+
25+
// Retrieve the engine before calling process() if not able to use promises
26+
getSelectedEngineAdapter: async function() {
27+
return this.getEnhancedEngineAdapter(this.getSelectedEngineType());
28+
},
29+
30+
// Convenience method
31+
processTemplate: async function (template, data, measure = false) {
32+
const adapter = await this.getSelectedEngineAdapter();
33+
return (measure)
34+
? adapter.processAndMeasure(template, data)
35+
: adapter.process(template, data);
36+
},
37+
38+
getEnhancedEngineAdapter: async function (type) {
39+
const adapter = await this.getEngineAdapter(type);
40+
return {
41+
engineType: this.getSelectedEngineType(),
42+
...trackingAdapter,
43+
...adapter
44+
};
2545
},
2646

2747
getEngineAdapter: async function (type) {

0 commit comments

Comments
 (0)