diff --git a/src/client/parts.ts b/src/client/parts.ts index 8e561a3e..9168032c 100644 --- a/src/client/parts.ts +++ b/src/client/parts.ts @@ -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 } } diff --git a/src/client/tests/directives.test.ts b/src/client/tests/directives.test.ts index 11888072..b324ca52 100644 --- a/src/client/tests/directives.test.ts +++ b/src/client/tests/directives.test.ts @@ -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`