|
1 | 1 | const { registerCoreHelpers } = require('./helpers')
|
2 | 2 |
|
3 |
| -function asyncHelpers(handlebars) { |
4 |
| - const _compile = handlebars.compile, |
5 |
| - _template = handlebars.VM.template, |
6 |
| - _mergeSource = handlebars.JavaScriptCompiler.prototype.mergeSource |
| 3 | +function asyncHelpers(hbs) { |
7 | 4 |
|
8 |
| - handlebars.JavaScriptCompiler.prototype.mergeSource = function(varDeclarations) { |
9 |
| - const sources = _mergeSource.call(this, varDeclarations) |
10 |
| - return sources.prepend('return (async () => {').add(' })()') |
11 |
| - } |
| 5 | + const handlebars = hbs.create(), |
| 6 | + asyncCompiler = class extends hbs.JavaScriptCompiler { |
12 | 7 |
|
13 |
| - handlebars.JavaScriptCompiler.prototype.appendToBuffer = function(source, location, explicit) { |
14 |
| - // Force a source as this simplifies the merge logic. |
15 |
| - if (!Array.isArray(source)) { |
16 |
| - source = [source] |
17 |
| - } |
18 |
| - source = this.source.wrap(source, location) |
19 |
| - |
20 |
| - if (this.environment.isSimple) { |
21 |
| - return ['return await ', source, ';'] |
22 |
| - } if (explicit) { |
23 |
| - // This is a case where the buffer operation occurs as a child of another |
24 |
| - // construct, generally braces. We have to explicitly output these buffer |
25 |
| - // operations to ensure that the emitted code goes in the correct location. |
26 |
| - return ['buffer += await ', source, ';'] |
27 |
| - } |
28 |
| - source.appendToBuffer = true |
29 |
| - source.prepend('await ') |
30 |
| - return source |
| 8 | + constructor() { |
| 9 | + super() |
| 10 | + this.compiler = asyncCompiler |
| 11 | + } |
31 | 12 |
|
32 |
| - } |
| 13 | + mergeSource(varDeclarations) { |
| 14 | + const sources = super.mergeSource(varDeclarations) |
| 15 | + return sources.prepend('return (async () => {').add(' })()') |
| 16 | + } |
33 | 17 |
|
| 18 | + appendToBuffer(source, location, explicit) { |
| 19 | + // Force a source as this simplifies the merge logic. |
| 20 | + if (!Array.isArray(source)) { |
| 21 | + source = [source] |
| 22 | + } |
| 23 | + source = this.source.wrap(source, location) |
| 24 | + |
| 25 | + if (this.environment.isSimple) { |
| 26 | + return ['return await ', source, ';'] |
| 27 | + } if (explicit) { |
| 28 | + // This is a case where the buffer operation occurs as a child of another |
| 29 | + // construct, generally braces. We have to explicitly output these buffer |
| 30 | + // operations to ensure that the emitted code goes in the correct location. |
| 31 | + return ['buffer += await ', source, ';'] |
| 32 | + } |
| 33 | + source.appendToBuffer = true |
| 34 | + source.prepend('await ') |
| 35 | + return source |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + } |
| 40 | + handlebars.JavaScriptCompiler = asyncCompiler |
| 41 | + |
| 42 | + const _compile = handlebars.compile, |
| 43 | + _template = handlebars.VM.template |
34 | 44 | handlebars.template = function(spec) {
|
35 | 45 | spec.main_d = (prog, props, container, depth, data, blockParams, depths) => async(context) => {
|
36 | 46 | // const main = await spec.main
|
|
0 commit comments