Skip to content

Commit

Permalink
fix: improve cached value handling
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Apr 18, 2023
1 parent bfb62c3 commit b6bc25d
Show file tree
Hide file tree
Showing 23 changed files with 371 additions and 137 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-dancers-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@marko/tags-api-preview": minor
---

Improve caching to be more like Marko 6. Fixes an issue with conditionally caching functions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div>
1
</div>
<button>
Increment
</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div>
1
</div>
<button>
Increment
</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div>
2
</div>
<button>
Increment
</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div>
3
</div>
<button>
Increment
</button>
9 changes: 9 additions & 0 deletions src/__tests__/fixtures/let/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ describe(
])
);

describe(
"<let> with cacheable value",
fixture("./templates/with-cacheable-value.marko", [
{ value: 1 },
increment,
increment,
])
);

function click(text: string) {
return async ({ fireEvent, screen }: FixtureHelpers) =>
await fireEvent.click(screen.getByText(text));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<attrs/{ value }/>
<let/getCount() { return value; }/>

<div>${getCount()}</div>
<button onClick=(() => {
const newCount = getCount() + 1;
getCount = () => newCount;
})>Increment</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div>
Count: 0
</div>
<div>
Message:
</div>
<button>
Increment
</button>
<button>
Toggle
</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div>
Count: 0
</div>
<div>
Message:
</div>
<button>
Increment
</button>
<button>
Toggle
</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div>
Count: 1
</div>
<div>
Message:
</div>
<button>
Increment
</button>
<button>
Toggle
</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div>
Count: 1
</div>
<div>
Message: hello 1
</div>
<button>
Increment
</button>
<button>
Toggle
</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div>
Count: 1
</div>
<div>
Message:
</div>
<button>
Increment
</button>
<button>
Toggle
</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div>
Count: 2
</div>
<div>
Message:
</div>
<button>
Increment
</button>
<button>
Toggle
</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div>
Count: 2
</div>
<div>
Message: hello 2
</div>
<button>
Increment
</button>
<button>
Toggle
</button>
20 changes: 20 additions & 0 deletions src/__tests__/fixtures/misc/cached-conditional-value/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import fixture, { FixtureHelpers } from "../../../fixture";
const increment = click("Increment");
const toggle = click("Toggle");

describe(
"cached conditional value",
fixture("./templates/basic.marko", [
{},
increment,
toggle,
toggle,
increment,
toggle,
])
);

function click(text: string) {
return async ({ fireEvent, screen }: FixtureHelpers) =>
await fireEvent.click(screen.getByText(text));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<let/enabled = false/>
<let/count = 0/>
<const/dynamicMessage = enabled ? () => `hello ${count}` : undefined/>
<div>Count: ${count}</div>
<div>Message: ${dynamicMessage?.()}</div>
<button onClick=(() => count++)>Increment</button>
<button onClick=(() => enabled = !enabled)>Toggle</button>
132 changes: 0 additions & 132 deletions src/transform/cached-function/transform.ts

This file was deleted.

File renamed without changes.
File renamed without changes.
Loading

0 comments on commit b6bc25d

Please sign in to comment.