Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/client/parts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,11 @@ export function create_directive_part(node: Node): Part {
return fn => {
if (prev_fn === fn) return
assert(typeof fn === 'function' || fn == null)
cleanup?.()
cleanup = fn?.(node)
try {
cleanup?.()
} finally {
cleanup = fn?.(node)
}
prev_fn = fn
}
}
Expand Down
28 changes: 28 additions & 0 deletions src/client/tests/directives.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,31 @@ test('same directive function is not re-invoked or cleaned up', () => {
root.render(template(null, null))
assert_deep_eq(sequence, ['stable cleanup', 'unstable cleanup'])
})

test('cleanup can throw and next directive will still run', () => {
const { root, el } = setup()

const template = (d: Directive | null) => html`<div ${d}>Hello, world!</div>`

const oops = new Error('oops')

root.render(
template(() => () => {
throw oops
}),
)

let caught
try {
root.render(
template(node => {
;(node as HTMLElement).style.color = 'red'
}),
)
} catch (error) {
caught = error
}

assert_eq(caught, oops)
assert_eq(el.querySelector('div')!.style.cssText, 'color: red;')
})