Skip to content

Commit

Permalink
feat: add support for removing inline registered templates
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Feb 21, 2021
1 parent fd891f0 commit 148d7c9
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 24 deletions.
3 changes: 1 addition & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"extends": [
"plugin:adonis/typescriptPackage",
"prettier",
"prettier/@typescript-eslint"
"prettier"
],
"plugins": [
"prettier"
Expand Down
44 changes: 22 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/CacheManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,15 @@ export class CacheManager implements CacheManagerContract {

this.cacheStore.set(absPath, payload)
}

/**
* Delete template from the compiled cache
*/
public delete(absPath: string) {
if (!this.enabled) {
return
}

this.cacheStore.delete(absPath)
}
}
11 changes: 11 additions & 0 deletions src/Contracts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ export interface LoaderContract {
* Register in memory template and presenter
*/
register(templatePath: string, contents: LoaderTemplate): void

/**
* Remove the pre-registered template
*/
remove(templatePath: string): void
}

/**
Expand Down Expand Up @@ -95,6 +100,7 @@ export interface CacheManagerContract {
get(templatePath: string): undefined | LoaderTemplate
set(templatePath: string, compiledOutput: LoaderTemplate): void
has(templatePath: string): boolean
delete(templatePath: string): void
}

/**
Expand Down Expand Up @@ -320,6 +326,11 @@ export interface EdgeContract {
*/
registerTemplate(templatePath: string, contents: LoaderTemplate): this

/**
* Remove the template registered using the "registerTemplate" method
*/
removeTemplate(templatePath: string): this

/**
* Register a global value
*/
Expand Down
10 changes: 10 additions & 0 deletions src/Edge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,16 @@ export class Edge implements EdgeContract {
return this
}

/**
* Remove the template registered using the "registerTemplate" method
*/
public removeTemplate(templatePath: string): this {
this.loader.remove(templatePath)
this.compiler.cacheManager.delete(templatePath)
this.asyncCompiler.cacheManager.delete(templatePath)
return this
}

/**
* Returns a new instance of edge. The instance
* can be used to define locals.
Expand Down
7 changes: 7 additions & 0 deletions src/Loader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,11 @@ export class Loader implements LoaderContract {

this.preRegistered.set(templatePath, contents)
}

/**
* Remove registered template
*/
public remove(templatePath: string) {
this.preRegistered.delete(templatePath)
}
}
17 changes: 17 additions & 0 deletions test/edge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,23 @@ test.group('Edge', (group) => {
assert.equal(edge.render('hello::foo', { username: 'virk' }).trim(), 'Hello virk')
})

test('clear compiled cache when template is removed', async (assert) => {
const edge = new Edge({ cache: true })

edge.registerTemplate('foo', {
template: 'Hello {{ username }}',
})
assert.equal(edge.render('foo', { username: 'virk' }).trim(), 'Hello virk')
assert.equal((await edge.renderAsync('foo', { username: 'virk' })).trim(), 'Hello virk')

edge.removeTemplate('foo')
edge.registerTemplate('foo', {
template: 'Hi {{ username }}',
})
assert.equal(edge.render('foo', { username: 'virk' }).trim(), 'Hi virk')
assert.equal((await edge.renderAsync('foo', { username: 'virk' })).trim(), 'Hi virk')
})

test('pass absolute path of template to lexer errors', async (assert) => {
assert.plan(1)
await fs.add('foo.edge', '@if(1 + 1)')
Expand Down
17 changes: 17 additions & 0 deletions test/loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,23 @@ test.group('Loader', (group) => {
assert.equal(template.trim(), 'Hello world')
})

test('remove registered template', async (assert) => {
const loader = new Loader()
loader.register('my-view', {
template: 'Hello world',
})
loader.mount('default', __dirname)

const { template } = loader.resolve('my-view')
assert.equal(template.trim(), 'Hello world')

loader.remove('my-view')
assert.throw(
() => loader.resolve('my-view'),
`Cannot resolve "${join(__dirname, 'my-view.edge')}". Make sure the file exists`
)
})

test('pre registering duplicate templates must raise an error', async (assert) => {
const loader = new Loader()
loader.register('my-view', { template: 'Hello world' })
Expand Down

0 comments on commit 148d7c9

Please sign in to comment.