Skip to content

Commit

Permalink
Merge pull request #10 from gastonrobledo/master
Browse files Browse the repository at this point in the history
Update from master fork
  • Loading branch information
gastonrobledo committed Jul 10, 2019
2 parents ed80c4b + 14407f8 commit 0f1e1b3
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 11 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Create a simple path alias to allow browserify to resolve
// the runtime on a supported path.
module.exports = require('./lib/handlebars').default
module.exports = require('./lib/handlebars')
3 changes: 2 additions & 1 deletion lib/handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ const AST = require('./handlebars/compiler/ast'),
{ parse } = Parser,
_create = runtime.create

function create() {
function create(stream) {
const hb = _create()
hb.stream = stream

hb.compile = function cmp(input, options, context) {
return compile(input, options, hb, context)
Expand Down
6 changes: 2 additions & 4 deletions lib/handlebars.runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,15 @@ async function processResult(result, output) {
}

function wrapTemplate(env, compiledFuncTemplate) {
return async(data, options) => {
return async(data) => {
/* eslint-disable no-param-reassign */
env.stream = options.stream || env.stream
try {
const dataParams = Object.assign(data, {
return await compiledFuncTemplate(data, {
async callback(item) {
const value = await processResult(item, env.stream)
return value
}
})
return await compiledFuncTemplate(dataParams)
} catch (e) {
if (env.stream) {
env.stream.emit('error', e)
Expand Down
4 changes: 2 additions & 2 deletions lib/handlebars/compiler/javascript-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ JavaScriptCompiler.prototype = {
}
source = this.source.wrap(source, location)
source.appendToBuffer = true
return `r += await (data.root.discard ? ${source} : data.root.callback(await ${source}))`
return `r += await (data.root.discard ? ${source} : container.callback(await ${source}))`

},

Expand Down Expand Up @@ -235,7 +235,7 @@ JavaScriptCompiler.prototype = {
varDeclarations += `, r = ${(this.initializeBuffer())}; \n`

if (!this.options.isPartial && !this.isChild) {
this.source.push('await data.root.callback(null)')
this.source.push('await container.callback(null)')
}
if (varDeclarations) {
this.source.prepend(`return (async () => { \nvar ${varDeclarations.substring(2)};\n`)
Expand Down
7 changes: 6 additions & 1 deletion lib/handlebars/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function checkRevision(compilerInfo) {
}
}

function template(templateSpec, env) {
function template(templateSpec, env, callback) {
/* istanbul ignore next */
if (!env) {
throw new Exception('No environment passed to template')
Expand Down Expand Up @@ -74,6 +74,7 @@ function template(templateSpec, env) {

// Just add water
const container = {
callback,
strict(obj, name) {
if (!(name in obj)) {
throw new Exception(`"${name}" not defined in ${obj}`)
Expand Down Expand Up @@ -176,8 +177,12 @@ function template(templateSpec, env) {
container.partials = options.partials
container.decorators = options.decorators
}
if (options.callback) {
container.callback = options.callback
}
}


return ret
}

Expand Down
56 changes: 54 additions & 2 deletions tests/test_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class HandlebarsStream extends Transform {

constructor(partials) {
super()
this.hbs = handlebars.create()
this.hbs = handlebars.create(this)
if (partials && partials.length) {
partials.forEach(p => this.hbs.registerPartial(p.name, p.content))
}
Expand All @@ -17,7 +17,7 @@ class HandlebarsStream extends Transform {
}

compile(content, data) {
this.result = this.hbs.compile(content)(data, { stream: this })
this.result = this.hbs.compile(content)(data)
return this
}

Expand Down Expand Up @@ -464,4 +464,56 @@ describe('Test stream results', () => {
stream.write({ name: 'pedro' })
stream.end()
})

it('test multiple instances', (done) => {
const partial = {
name: 'test',
content: '<h3>{{name}}</h3>'
},
template = `<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<body>
<div style="width: 100%">
{{#each names}}
{{> test}}
{{/each}}
</div>
</div>
</body>
</html>`,
hbs = new HandlebarsStream([partial]),
hbs1 = new HandlebarsStream([partial]),
names = [{
name: 'Gaston'
}, {
name: 'Pedro'
}]
hbs.compile(template, { names })
hbs1.compile(template, { names })
const promises = []
promises.push(new Promise((resolve, reject) => {
let result = ''
hbs.on('data', (block) => {
result += block.toString()
}).once('error', e => reject(e))
.on('end', () => {
resolve(result)
})
}))
promises.push(new Promise((resolve, reject) => {
let result = ''
hbs1.on('data', (block) => {
result += block.toString()
}).once('error', e => reject(e))
.on('end', () => {
resolve(result)
})
}))

Promise.all(promises).then((results) => {
assert(results[0].trim() === results[1].trim(), 'results are not the same')
done()
}).catch(e => done(e))
})

})

0 comments on commit 0f1e1b3

Please sign in to comment.