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

Lifecycle operators (restartWith?) #139

Open
raquo opened this issue Feb 11, 2025 · 0 comments
Open

Lifecycle operators (restartWith?) #139

raquo opened this issue Feb 11, 2025 · 0 comments

Comments

@raquo
Copy link
Owner

raquo commented Feb 11, 2025

Sometimes I run into a situation where I need to reset a signal's state to some empty or default value when the signal is re-started. This is usually because the same signal is used to hold data for e.g. a currently selected product, as the user switches between products, and it usually involves asynchrony, e.g. from network requests.

For streams, we at least have this:

val resettingStream = EventStream.merge(
  EventStream.fromValue(0),
  parentStream
)

This will emit 0 every time that resettingStream is started.

However, we don't really have the same functionality for signals. Yes, we have composeChanges, but that only works for subsequent events, it's not suitable for general-purpose resetting logic (and it also introduces streams into a signal pipeline, which may have other issues related to re-syncing).

I think we could have operators like:

signal.restartWith(: => value)
stream.alwaysStartWith(: => value) // probably returns `resettingStream` above? Or maybe `stream.startWith(value).restartWith(value)`?

Naming could be a problem. We already use stream.startWith(initial) to convert streams to signals. This operator is very old and may even be in my laminar video, so I probably wouldn't want it to subtly change meaning.

Similar to debgugging methods, perhaps we could have a single method used like signal.lifecycle(_.restartWith(0)), but I don't really know what other hooks could be useful. Perhaps _.stopWith(value) to unload big data structures from memory when the signal is stopped? Nobody would see the new value event until the signal is restarted. We proooobably can implement that.

Welp, not sure yet. Just writing this down in case anyone wants to share similar needs or solutions.

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

No branches or pull requests

1 participant