Skip to content

2.0.0-beta.14: Proposal - Add affects() For Action-Affected Sources #2728

@brenelz

Description

@brenelz

isPending(fn) is useful for checking whether a specific reactive read depends on pending async work, but it cannot currently distinguish or anticipate sources that an action is expected to update.

For example:

const user = createProjection(() => getUser(), {} as User)

const updateUser = action(function* (name: string) {
  yield updateUserServer(name)
  refresh(user)
})

In this case:

isPending(() => user.name)

only becomes true once refresh(user) starts. There is no way for the action to say "this source is affected by this mutation" earlier.

Proposed API

const updateUser = action(function* (name: string) {
  affects(user)

  yield updateUserServer(name)
})

Expected Behavior

  • affects(user) marks user as pending for isPending() while the action is running.
  • It does not mutate or refresh the source.
  • It clears when the action settles.
  • It should throw in dev mode if called outside an action.
  • It should accept the same refreshable targets as refresh().

Example Usage

const pending = createMemo(() =>
  isPending(() => user.name)
)

const updateUser = action(function* (name: string) {
  affects(user)

  yield updateUserServer(name)
  refresh(user)
})

This lets UI show "saving/updating" state immediately for the affected source, instead of waiting for the later refresh.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions