Synphonyte / leptos-use

Collection of essential Leptos utilities inspired by React-Use / VueUse / SolidJS-USE
https://leptos-use.rs/
Apache License 2.0
309 stars 66 forks source link

Allow for lazy protocols with use_websocket #164

Closed zakstucke closed 2 weeks ago

zakstucke commented 2 weeks ago

I have a need to use the protocols option of websockets, but I can only get them asyncronously. This PR provides a way to have "async" protocols by wrapping Option<Vec<String>> in MaybeSignal. Combining e.g. an async resource as the protocol signal with opts.immediate(false) and manually calling ws.open() once the resource has loaded solves the issue.

This is definitely a weird implementation though, if you have a better way to do it I can update the PR?

This shouldn't be a breaking change, all previous config of the protocol option should be unaffected, due to the impl Into<MaybeSignal<...>>

zakstucke commented 2 weeks ago

Second commit is adding web-sys as a dep to the use_websocket Cargo.toml feature, compilation fails otherwise when it's the only enabled feature.

maccesch commented 2 weeks ago

Thanks for this!

I get the need for this and I agree, it feels a bit weird. However I can't find a better solution 😂 Could you maybe add docs to this option, that it can be a signal but is only read when open is called?

zakstucke commented 2 weeks ago

Okay cool, I've added some comments to the protocols option.

maccesch commented 2 weeks ago

Perfect, thanks!