From fd28bee4795b5bb395ef8baf8c6050391c30dc66 Mon Sep 17 00:00:00 2001 From: Tim Branyen Date: Fri, 14 Jul 2023 09:25:04 -0700 Subject: [PATCH] Allow previously diffed elements to be updated --- packages/diffhtml/lib/transaction.js | 6 ++++++ packages/diffhtml/test/integration/inner.js | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/packages/diffhtml/lib/transaction.js b/packages/diffhtml/lib/transaction.js index abdf7e5b..91f02a22 100644 --- a/packages/diffhtml/lib/transaction.js +++ b/packages/diffhtml/lib/transaction.js @@ -149,6 +149,12 @@ export default class Transaction { this.endedCallbacks = new Set(); StateCache.set(mount, this.state); + + // When a previous mount is injected into a new location, remove the + // state held for it, this allows previously diffed elements to be updated. + if (StateCache.has(input)) { + StateCache.delete(input); + } } /** diff --git a/packages/diffhtml/test/integration/inner.js b/packages/diffhtml/test/integration/inner.js index d250abee..559a8a88 100644 --- a/packages/diffhtml/test/integration/inner.js +++ b/packages/diffhtml/test/integration/inner.js @@ -354,6 +354,24 @@ describe('Integration: innerHTML', function() { diff.innerHTML(this.fixture, diff.html`
${domNode}

after

`); assert.equal(this.fixture.innerHTML, '
test

after

'); }); + + it("will diff an element when element's children have been diffed before", function (cb) { + const p = document.createElement("p"); + diff.innerHTML(p, "Test"); + // this.fixture is
+ diff.innerHTML(this.fixture, p); //

Test

+ + // diff element p child span + diff.innerHTML(this.fixture.querySelector("span"), "Test 2"); // this works:

Test 2

+ + // our test case: diff element p when child span has been diffed previously + diff.innerHTML(this.fixture.querySelector("p"), "Test 3"); // this doesn't work - still

Test 2

+ + setTimeout(() => { + assert.equal(this.fixture.querySelector("span").innerHTML, "Test 3"); // fails because it's still Test 2 + cb(); + }); + }); }); describe('Comments', function() {