As per the headline, I hereby use the opportunity to thank the LiveView team for finally introducing (as of v0.18.12) the long awaited mechanism for managing the lists - streams.
So far, the only way to do it was with client-side hooks or (far better) Alpine, but still, component-based nested lists kept on having bugs I hope the stream mechanism overcomes completely (as in by design).
Judging by the documentation, itās gonna be your code that provides the index and if the relative reference is an id, then it means youāll probably end up maintain an id => index map yourself, but thatās not a big deal in comparison to what was needed before this (the temporary assigns, JS sorting code, etc.)
Does anyone know if streams only work in live views and not in live components?
Iām getting the error: lifecycle hooks are not supported on stateful components. when trying to use stream in the update function of a live component.
Thatās awesome! Glad to see support for streams in live components. Iāll test this out soon.
But for now onto my next issue with streams It looks like itās not possible to reset an entire stream.
My use case is a chat app which has the typical layout of a list of conversations on the left and when you click conversation it loads all the messages on the right. Because I couldnāt use streams in a live component I was just using a live view. On mount I would create a stream for the messages and then in handle params I tried to reset the stream with stream(socket, :messages, get_messages[new_conversation_id]) but I get the error existing hook :messages already attached on :after_render.
True, thereās no delete_all and the docs states āAssigns a NEW stream to the socketā.
Now, this is just me thinking aloud, but maybe try removing the assign manually (as in: assign( socket, :name, nil)) and then setting the stream anew OR now that live components support streams, simply reinitialize the entire live component (replace it in its parent template with a new instance of the same component).
Is there any way to keep the stream at a fixed size? Iām thinking about the use case of streaming some logs, where the server doesnāt want to keep the logs in the live view gen server, but you donāt want the client to have the logs keep growing, forever.
Now that Iāve migrated completely, I found myself with the same problem. The short answer is you canāt, at least not in the v0.18.18.
However, judging by the most recent changes of the LiveView code, theyāre adding this feature as we speak. There is going to be a reset: true option to reset the stream to a new list of items.
They also realized it was clumsy to have just a single stream/4 function for in most cases you donāt want the stream/4 in mount but in update, so they added a stream_configure/3 function as well.