|
5 | 5 | createRenderEffect, |
6 | 6 | createRoot, |
7 | 7 | createSignal, |
8 | | - flush |
| 8 | + createStore, |
| 9 | + flush, |
| 10 | + refresh |
9 | 11 | } from "../src/index.js"; |
10 | 12 |
|
11 | 13 | afterEach(() => flush()); |
@@ -578,6 +580,89 @@ describe("action", () => { |
578 | 580 | }); |
579 | 581 | }); |
580 | 582 |
|
| 583 | + describe("refresh(store) inside an action (#3026)", () => { |
| 584 | + // A setStore on a derived store masks its recompute for the tick |
| 585 | + // (REACTIVE_MANUAL_WRITE, #2692). Inside an action the mask survives |
| 586 | + // until the transaction commits, so a later explicit refresh() was |
| 587 | + // silently swallowed and the store source never re-ran. refresh() now |
| 588 | + // lifts a mask stamped in an earlier tick. |
| 589 | + it("re-runs the source after a setStore absorbed into the action transaction", async () => { |
| 590 | + const data = { messages: [] as number[] }; |
| 591 | + let evals = 0; |
| 592 | + const [store, setStore] = createRoot(() => { |
| 593 | + const [s, sStore] = createStore( |
| 594 | + () => { |
| 595 | + evals++; |
| 596 | + return data; |
| 597 | + }, |
| 598 | + { messages: [] as number[] } |
| 599 | + ); |
| 600 | + createRenderEffect( |
| 601 | + () => JSON.stringify(s.messages), |
| 602 | + () => {} |
| 603 | + ); |
| 604 | + return [s, sStore] as const; |
| 605 | + }); |
| 606 | + flush(); |
| 607 | + expect(evals).toBe(1); |
| 608 | + |
| 609 | + const myAction = action(function* () { |
| 610 | + yield new Promise(r => setTimeout(r, 10)); |
| 611 | + refresh(store); |
| 612 | + }); |
| 613 | + |
| 614 | + const p = myAction(); |
| 615 | + // The issue shape: a setStore write while the action's transition is |
| 616 | + // still ambient, plus an external source mutation. |
| 617 | + setStore(s => { |
| 618 | + s.messages.push(1); |
| 619 | + }); |
| 620 | + data.messages.push(2); |
| 621 | + await p; |
| 622 | + flush(); |
| 623 | + expect(evals).toBe(2); |
| 624 | + }); |
| 625 | + |
| 626 | + it("reconciles a fresh source result over the draft write at action commit", async () => { |
| 627 | + let fetched = { messages: [10] as number[] }; |
| 628 | + let evals = 0; |
| 629 | + const [store, setStore] = createRoot(() => { |
| 630 | + const [s, sStore] = createStore( |
| 631 | + () => { |
| 632 | + evals++; |
| 633 | + return JSON.parse(JSON.stringify(fetched)) as typeof fetched; |
| 634 | + }, |
| 635 | + { messages: [] as number[] } |
| 636 | + ); |
| 637 | + createRenderEffect( |
| 638 | + () => JSON.stringify(s.messages), |
| 639 | + () => {} |
| 640 | + ); |
| 641 | + return [s, sStore] as const; |
| 642 | + }); |
| 643 | + flush(); |
| 644 | + expect(evals).toBe(1); |
| 645 | + expect(store.messages).toEqual([10]); |
| 646 | + |
| 647 | + const myAction = action(function* () { |
| 648 | + yield new Promise(r => setTimeout(r, 10)); |
| 649 | + refresh(store); |
| 650 | + }); |
| 651 | + |
| 652 | + const p = myAction(); |
| 653 | + setStore(s => { |
| 654 | + s.messages.push(1); |
| 655 | + }); |
| 656 | + fetched = { messages: [10, 20] }; |
| 657 | + await p; |
| 658 | + flush(); |
| 659 | + // refresh() came after the setStore in program order: the re-derived |
| 660 | + // result wins over the transaction draft. |
| 661 | + expect(evals).toBe(2); |
| 662 | + expect(store.messages).toEqual([10, 20]); |
| 663 | + }); |
| 664 | + }); |
| 665 | + |
581 | 666 | describe("edge cases", () => { |
582 | 667 | it("should handle empty generator (immediate return)", () => { |
583 | 668 | const executed = vi.fn(); |
|
0 commit comments