Skip to content

Commit 95dd480

Browse files
ryansolidclaude
andauthored
docs(signals): refresh()/until() after an await in a yielded helper wait on the action's commit (#4263) (#4288)
* docs(signals): refresh()/until() after an await in a yielded helper wait on the action's commit (#4263) Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_018LY7F9czoRqdMEvk2QwPr1 * docs(signals): call refresh() in the generator; yield it only to wait (#4263) Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_018LY7F9czoRqdMEvk2QwPr1 --------- Co-authored-by: Claude <[email protected]>
1 parent 0856fee commit 95dd480

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

‎documentation/solid-2.0/05-async-data.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,16 @@ The promise is safe to ignore — fire-and-forget `refresh()` is unchanged, and
357357
358358
Refresh granularity is the granularity of the _derive function_: there is no per-path refetch to re-run, so refresh a derived store by passing its root (`refresh(todos)`, not `refresh(todos.items[0])`) — that re-asks the whole projection, and reads through any nested node after the `await` are fresh. Inside actions, `yield refresh(x)` is the mutate-then-refetch sequencing primitive — see RFC 06.
359359
360+
Inside an action, call `refresh(x)` from the generator. It doesn't need a `yield` unless the action needs the fresh value. Don't call it after an `await`, including inside an async helper the action yields: that code runs outside the action's transaction, like any write there. Awaiting it there also hangs. The waiter waits for the action's commit, and the commit waits for the helper. So it works until `x` happens to re-run during the action, and then it hangs:
361+
362+
```js
363+
const like = action(function* () {
364+
setLikes(likes() + 1);
365+
yield saveLike(); // async helper: no refresh() inside it
366+
refresh(post); // in the generator; `yield refresh(post)` only to wait for the fresh value
367+
});
368+
```
369+
360370
#### `resource.mutate` → `createOptimisticStore` / `action`
361371
362372
In 1.x, `mutate` replaced the resource value wholesale. This had several problems: no granular updates (the entire list re-rendered), no reconciliation (identity lost on every mutation), and no protection against race conditions (concurrent mutations could clobber each other):

‎packages/signals/src/core/action.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ function inTransaction<T>(t: Transaction, q: number, fn: () => T): T {
8989
* yield until(() => todos.some(t => t.id === saved.id));
9090
* ```
9191
*
92+
* The same holds inside an async helper the action yields
93+
* (`yield (async () => { ... })()`): everything after its first `await` runs
94+
* outside the transaction. An `await refresh(x)` or `await until(...)` there
95+
* waits for this action's commit, which waits on the helper, so it hangs
96+
* whenever `x` re-ran during the action (#4263) and works otherwise. Call
97+
* `refresh(x)` in the generator once the helper returns; `yield` it only when
98+
* the action needs the fresh value.
99+
*
92100
* (For the same reason, don't call `flush()` inside an action body — it
93101
* cannot reveal the held writes and is refused.)
94102
*

0 commit comments

Comments
 (0)