Skip to content

Commit

Permalink
feat: clone edge renderer instance with shared data
Browse files Browse the repository at this point in the history
Fixes: #156
  • Loading branch information
thetutlage committed Sep 18, 2024
1 parent 5714054 commit 0cddc8d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/edge/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ export class EdgeRenderer {
this.#globals = globals
}

/**
* Clone renderer instance with shared data
*/
clone() {
const renderer = new EdgeRenderer(
this.#compiler,
this.#asyncCompiler,
this.#processor,
this.#globals
)
return renderer.share(this.#locals)
}

/**
* Share local variables with the template. They will overwrite the
* globals
Expand Down
19 changes: 19 additions & 0 deletions tests/edge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,25 @@ test.group('Edge', () => {
const output1 = await edge.render('foo', { username: 'virk' })
assert.equal(output1.trim(), '@!foo({ username })')
})

test('clone renderer with shared data', async ({ assert, fs }) => {
const edge = new Edge()
await fs.create('foo.edge', "Hello {{ username || 'guest' }}")

edge.mount(fs.basePath)

const tmpl = edge.createRenderer()
tmpl.share({ username: 'nikk' })
const tmpl1 = tmpl.clone()

const output = await tmpl.render('foo', {})
const output1 = await tmpl1.render('foo', {})
const output2 = await edge.render('foo', {})

assert.equal(output.trim(), 'Hello nikk')
assert.equal(output1.trim(), 'Hello nikk')
assert.equal(output2.trim(), 'Hello guest')
})
})

test.group('Edge | regression', () => {
Expand Down

0 comments on commit 0cddc8d

Please sign in to comment.