Skip to content

Commit

Permalink
adding changes to not pollute
Browse files Browse the repository at this point in the history
  • Loading branch information
gastonrobledo committed Mar 19, 2022
1 parent 1b57ca6 commit 4f3887a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 31 deletions.
64 changes: 35 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,46 @@
const { registerCoreHelpers } = require('./helpers')

function asyncHelpers(handlebars) {
function asyncHelpers(hbs) {

const _compile = handlebars.compile,
_template = handlebars.VM.template
const handlebars = hbs.create(),
asyncCompiler = class extends hbs.JavaScriptCompiler {

handlebars.JavaScriptCompiler = class extends handlebars.JavaScriptCompiler {
constructor() {
super()
this.compiler = asyncCompiler
}

mergeSource(varDeclarations) {
const sources = super.mergeSource(varDeclarations)
return sources.prepend('return (async () => {').add(' })()')
}
mergeSource(varDeclarations) {
const sources = super.mergeSource(varDeclarations)
return sources.prepend('return (async () => {').add(' })()')
}

appendToBuffer(source, location, explicit) {
// Force a source as this simplifies the merge logic.
if (!Array.isArray(source)) {
source = [source]
}
source = this.source.wrap(source, location)

if (this.environment.isSimple) {
return ['return await ', source, ';']
} if (explicit) {
// This is a case where the buffer operation occurs as a child of another
// construct, generally braces. We have to explicitly output these buffer
// operations to ensure that the emitted code goes in the correct location.
return ['buffer += await ', source, ';']
}
source.appendToBuffer = true
source.prepend('await ')
return source
appendToBuffer(source, location, explicit) {
// Force a source as this simplifies the merge logic.
if (!Array.isArray(source)) {
source = [source]
}
source = this.source.wrap(source, location)

}
if (this.environment.isSimple) {
return ['return await ', source, ';']
} if (explicit) {
// This is a case where the buffer operation occurs as a child of another
// construct, generally braces. We have to explicitly output these buffer
// operations to ensure that the emitted code goes in the correct location.
return ['buffer += await ', source, ';']
}
source.appendToBuffer = true
source.prepend('await ')
return source

}
}

}
handlebars.JavaScriptCompiler = asyncCompiler

const _compile = handlebars.compile,
_template = handlebars.VM.template
handlebars.template = function(spec) {
spec.main_d = (prog, props, container, depth, data, blockParams, depths) => async(context) => {
// const main = await spec.main
Expand All @@ -59,4 +65,4 @@ function asyncHelpers(handlebars) {
return handlebars
}

module.exports = asyncHelpers
module.exports = asyncHelpers
4 changes: 2 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ describe('Test async helpers', () => {
extend: async function(partial, options) {
let context = this,
// noinspection JSUnresolvedVariable
template = Handlebars.partials[partial] || options.data?.partials?.partial
template = hbs.partials[partial] || options.data?.partials?.partial

// Partial template required
if (typeof template === 'undefined') {
Expand All @@ -171,7 +171,7 @@ describe('Test async helpers', () => {
await options.fn(context)

if (!(typeof template === 'function')) {
template = Handlebars.compile(template)
template = hbs.compile(template)
}

// Render final layout partial with revised blocks
Expand Down

0 comments on commit 4f3887a

Please sign in to comment.