Conversation
| {:phoenix_pubsub, | ||
| github: "phoenixframework/phoenix_pubsub", branch: "sd-sender", override: true}, |
There was a problem hiding this comment.
TODO: replace with release
TODO: should we allow older versions and conditionally compile it?
| # we use the process dicationary so to prevent cases where a user calls | ||
| # subscribe in assign_async or a custom Task, we check and raise | ||
| # if this process is not demonstrably a LiveView | ||
| case Process.get(:"$process_label") do |
There was a problem hiding this comment.
Since we are using the process dictionary for subscriptions, we can use the process dictionary itself to detect if we are inside a LiveView?
There was a problem hiding this comment.
All we need to do is to initialize it to an empty map.
| def unsubscribe_cid(cid) do | ||
| for {{pubsub, topic}, {_ref, subscribers}} <- pubsub_subscriptions(), | ||
| Map.has_key?(subscribers, cid) do | ||
| do_unsubscribe(pubsub, topic, cid) |
There was a problem hiding this comment.
Maybe we should have a cid => topics mapping....
|
|
||
| %{} -> | ||
| # we might still have stale messages in the mailbox | ||
| # so we ignore those silently |
There was a problem hiding this comment.
This is the scenario I mentioned in the call today. We don't need to worry about it, we already handle it. However, we could use Process.aliases if we want to reduce the amount of stale messages (they can still exist but sometimes they won't be sent).
| pubsub :: module(), | ||
| topic :: binary(), | ||
| callback :: (message :: term(), socket :: Socket.t() -> Socket.t()) | ||
| ) :: Socket.t() |
There was a problem hiding this comment.
I am wondering if we should return the socket. I am thinking we should not and instead return :ok or whatever. Otherwise, someone may have the impression that, if you call subscribe but ignore the socket, nothing happens. Returning :ok makes it clearer it is a side-effect.
We should also document that you want to avoid closing over the state in the anonymous function and always use &handle_foo_bar/2 format. Perhaps an admonition block.
| """ | ||
| @spec subscribe( | ||
| socket :: Socket.t(), | ||
| pubsub :: module(), |
There was a problem hiding this comment.
Perhaps we make the pubsub an optional argument?
Requires phoenixframework/phoenix_pubsub#218.