Commit 5f44c19
committed
Fix infinite loop and page freeze caused by async Alpine.effect
WHY:
Since version 1.1.19 (Feb 16, 2026), Alpine.store('LocalStorage').get()
became asynchronous, but the Message store was using it incorrectly inside
Alpine.effect(), causing:
1. Infinite reactivity loops - Alpine.effect() expects synchronous functions
for proper dependency tracking. Using async causes the effect to return
a Promise immediately, preventing Alpine from tracking reactive dependencies.
2. The effect modifies this.messages inside itself, triggering re-runs
infinitely as Alpine tries to track the dependency on this.messages.
3. Page freezes and high CPU usage as the browser gets stuck in the loop.
4. saveMessage() was calling getMessagesFromStore() without await, causing
it to try pushing to a Promise instead of an array.
HOW:
1. Replaced Alpine.effect(async () => {...}) with a single-run event listener:
- Listen for 'loki:init:localstorage-store' event (dispatched by
LocalStorage store when initialized)
- Use { once: true } to ensure it only runs once
- Load messages asynchronously without reactive tracking issues
- No infinite loops since we're not inside Alpine's reactivity system
2. Made saveMessage() async and added await before getMessagesFromStore()
to properly handle the Promise returned by the async method.
RESULT:
- Messages load correctly from LocalStorage once on initialization
- No infinite loops or page freezes
- Proper async/await pattern throughout
- Compatible with async LocalStorage.get() from base 1.1.19+1 parent ceeaee5 commit 5f44c19
1 file changed
Lines changed: 5 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
| 27 | + | |
| 28 | + | |
28 | 29 | | |
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
33 | | - | |
| 34 | + | |
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
| |||
63 | 64 | | |
64 | 65 | | |
65 | 66 | | |
66 | | - | |
67 | | - | |
| 67 | + | |
| 68 | + | |
68 | 69 | | |
69 | 70 | | |
70 | 71 | | |
| |||
0 commit comments