Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assign_async and start_async #2763

Merged
merged 63 commits into from
Aug 31, 2023
Merged

Add assign_async and start_async #2763

merged 63 commits into from
Aug 31, 2023

Conversation

chrismccord
Copy link
Member

Still a WIP but ready for initial review to help finalize some things

lib/phoenix_live_view.ex Outdated Show resolved Hide resolved
lib/phoenix_live_view.ex Outdated Show resolved Hide resolved
cancel_existing_async(socket, :preview)
cancel_existing_async(socket, socket.assigns.preview)
"""
def cancel_existing_async(socket, async_or_keys, reason \\ :cancel) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same about the default reason here.

end
end

def cancel_async(%Socket{} = socket, key, _reason) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we support a list of keys? I don't see where that is handled. :( But also note that if I do:

cancel_async(socket, :key, ...)

We won't set the async_result under socket.assign.key to failed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the "key" is whatever the user passes to start_async, so :foo or [:foo, :bar, :baz]. So the list of keys is supported because the key is a list of keys in some cases. I will bump the docs around this.

@aj-foster
Copy link

Love to see support for this in LiveView. ❤️

Just a thought (sorry if this should be shared elsewhere): While messing with asynchronous assignments in the past, I occasionally found it helpful to perform atomic updates (via Phoenix.Component.update/3 via asynchronous tasks. It looks like this will be supported through start_async. Another option might be to allow a tagged tuple (ex. {:update, fn current -> current + 1 end}) as an alternative return type for the asynchronous function.

@chrismccord chrismccord merged commit 56697d3 into main Aug 31, 2023
4 of 6 checks passed
@chrismccord chrismccord deleted the cm-assign-async branch August 31, 2023 14:01
chrismccord added a commit that referenced this pull request Sep 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants