Skip to content

Commit 09d8fba

Browse files
committed
Update the implementation of OptimizedCounter to match the hydration example
1 parent 2b9d487 commit 09d8fba

File tree

3 files changed

+35
-19
lines changed

3 files changed

+35
-19
lines changed

test/van.test.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,17 @@ const runTests = async (van, msgDom, { debug }) => {
826826
await sleep(waitMsOnDomUpdates);
827827
assertEq(hiddenDom.innerHTML, "");
828828
}),
829+
hydrate_0NotToRemoveDom: withHiddenDom(async (hiddenDom) => {
830+
van.add(hiddenDom, div(), div());
831+
const s = van.state(0);
832+
const [dom1, dom2] = hiddenDom.querySelectorAll("div");
833+
van.hydrate(dom1, (() => s.val));
834+
van.hydrate(dom2, (() => 1 - s.val));
835+
assertEq(hiddenDom.innerHTML, "01");
836+
s.val = 1;
837+
await sleep(waitMsOnDomUpdates);
838+
assertEq(hiddenDom.innerHTML, "10");
839+
}),
829840
};
830841
const debugTests = {
831842
escape_nonFuncArg: () => {
@@ -1006,14 +1017,12 @@ const runTests = async (van, msgDom, { debug }) => {
10061017
const counter = van.state(init);
10071018
return div(Object.assign(Object.assign({}, (id ? { id } : {})), { "data-counter": counter }), "❤️ ", counter, " ", button({ onclick: () => ++counter.val }, up), button({ onclick: () => --counter.val }, down));
10081019
};
1009-
const OptimizedCounter = ({ van, id, init = 0, buttonStyle = "👍👎", }) => div((dom) => {
1020+
const OptimizedCounter = ({ van: { state, derive, val, tags: { button, div } }, id, init = 0, buttonStyle = "👍👎", }) => div((dom) => {
10101021
if (dom)
10111022
return dom;
1012-
const { button, div } = van.tags;
1013-
const counter = van.state(init);
1014-
const up = van.state(undefined);
1015-
const down = van.state(undefined);
1016-
van.derive(() => [up.val, down.val] = [...van.val(buttonStyle)]);
1023+
const counter = state(init);
1024+
const up = state(undefined), down = state(undefined);
1025+
derive(() => [up.val, down.val] = [...val(buttonStyle)]);
10171026
return div(Object.assign(Object.assign({}, (id ? { id } : {})), { "data-counter": counter }), "❤️ ", counter, " ", button({ onclick: () => ++counter.val }, up), button({ onclick: () => --counter.val }, down));
10181027
}).firstChild;
10191028
const hydrateExample = (Counter) => withHiddenDom(async (hiddenDom) => {

test/van.test.nomodule.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,17 @@
764764
s.val = 2;
765765
await sleep(waitMsOnDomUpdates);
766766
assertEq(hiddenDom.innerHTML, "");
767+
}),
768+
hydrate_0NotToRemoveDom: withHiddenDom(async (hiddenDom) => {
769+
van2.add(hiddenDom, div2(), div2());
770+
const s = van2.state(0);
771+
const [dom1, dom2] = hiddenDom.querySelectorAll("div");
772+
van2.hydrate(dom1, () => s.val);
773+
van2.hydrate(dom2, () => 1 - s.val);
774+
assertEq(hiddenDom.innerHTML, "01");
775+
s.val = 1;
776+
await sleep(waitMsOnDomUpdates);
777+
assertEq(hiddenDom.innerHTML, "10");
767778
})
768779
};
769780
const debugTests = {
@@ -940,14 +951,12 @@
940951
const counter = van3.state(init);
941952
return div3(Object.assign(Object.assign({}, id ? { id } : {}), { "data-counter": counter }), "\u2764\uFE0F ", counter, " ", button2({ onclick: () => ++counter.val }, up), button2({ onclick: () => --counter.val }, down));
942953
};
943-
const OptimizedCounter = ({ van: van3, id, init = 0, buttonStyle = "\u{1F44D}\u{1F44E}" }) => div2((dom) => {
954+
const OptimizedCounter = ({ van: { state, derive, val, tags: { button: button2, div: div3 } }, id, init = 0, buttonStyle = "\u{1F44D}\u{1F44E}" }) => div3((dom) => {
944955
if (dom)
945956
return dom;
946-
const { button: button2, div: div3 } = van3.tags;
947-
const counter = van3.state(init);
948-
const up = van3.state(void 0);
949-
const down = van3.state(void 0);
950-
van3.derive(() => [up.val, down.val] = [...van3.val(buttonStyle)]);
957+
const counter = state(init);
958+
const up = state(void 0), down = state(void 0);
959+
derive(() => [up.val, down.val] = [...val(buttonStyle)]);
951960
return div3(Object.assign(Object.assign({}, id ? { id } : {}), { "data-counter": counter }), "\u2764\uFE0F ", counter, " ", button2({ onclick: () => ++counter.val }, up), button2({ onclick: () => --counter.val }, down));
952961
}).firstChild;
953962
const hydrateExample = (Counter2) => withHiddenDom(async (hiddenDom) => {

test/van.test.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,16 +1333,14 @@ const runTests = async (van: VanForTesting, msgDom: Element, {debug}: BundleOpti
13331333
}
13341334

13351335
const OptimizedCounter = ({
1336-
van, id, init = 0, buttonStyle = "👍👎",
1336+
van: {state, derive, val, tags: {button, div}},
1337+
id, init = 0, buttonStyle = "👍👎",
13371338
}: CounterProps) => <HTMLDivElement>div((dom: Node) => {
13381339
if (dom) return dom
13391340

1340-
const {button, div} = van.tags
1341-
1342-
const counter = van.state(init)
1343-
const up = van.state(<string | undefined>undefined)
1344-
const down = van.state(<string | undefined>undefined)
1345-
van.derive(() => [up.val, down.val] = [...van.val(buttonStyle)])
1341+
const counter = state(init)
1342+
const up = state(<string | undefined>undefined), down = state(<string | undefined>undefined)
1343+
derive(() => [up.val, down.val] = [...val(buttonStyle)])
13461344

13471345
return div({...(id ? {id} : {}), "data-counter": counter},
13481346
"❤️ ", counter, " ",

0 commit comments

Comments
 (0)