Skip to content

Commit

Permalink
Merge pull request #4 from gastonrobledo/bugfix/give-a-hoot-dont-pollute
Browse files Browse the repository at this point in the history
Bugfix/give a hoot dont pollute
  • Loading branch information
gastonrobledo authored Mar 19, 2022
2 parents 3aa361a + 4f3887a commit fafb106
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3,293 deletions.
64 changes: 37 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,46 @@
const { registerCoreHelpers } = require('./helpers')

function asyncHelpers(handlebars) {
const _compile = handlebars.compile,
_template = handlebars.VM.template,
_mergeSource = handlebars.JavaScriptCompiler.prototype.mergeSource
function asyncHelpers(hbs) {

handlebars.JavaScriptCompiler.prototype.mergeSource = function(varDeclarations) {
const sources = _mergeSource.call(this, varDeclarations)
return sources.prepend('return (async () => {').add(' })()')
}
const handlebars = hbs.create(),
asyncCompiler = class extends hbs.JavaScriptCompiler {

handlebars.JavaScriptCompiler.prototype.appendToBuffer = function(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
constructor() {
super()
this.compiler = asyncCompiler
}

}
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

}

}
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 Down
Loading

0 comments on commit fafb106

Please sign in to comment.