daltoniam / Starscream

Websockets in swift for iOS and OSX
Apache License 2.0
8.33k stars 1.21k forks source link

Redesign an API to avoid impossible state #729

Closed daniel-abramov closed 4 years ago

daniel-abramov commented 4 years ago

I've just thought on how the API could be improved to provide more robust invariants and avoid "wrong" state by having different data types for different websocket states.

To be more precise I've encountered some issues (and workarounded them on the client code)

I made some thoughts on how to implement this API and I had in mind something similar to tungstenite-rs / tokio-tungstenite (Rust WebSockets). We can't quite get the same API due to the fact that ADT in Swift are a bit different and there is no async/futures concept in Swift (there are promises in Swift, but I'm not sure if they would help), but we can try to avoid impossible state by applying the following principles:

This probably can be applied to the rest of the API as well. What do you think?

fassko commented 4 years ago

I think it sounds good, just at the moment don't want to completely change how the API's are now as this is quite Apple like style.

Maybe you can build on top of Starscream such approach?

daniel-abramov commented 4 years ago

Perhaps. There are also Futures/Promises in Swift (they have been introduced not that far ago as far as I know), probably they would help to implement such an approach.

daltoniam commented 4 years ago

Thanks for the suggestion. I think 4.0.0 resolves a lot of this with its state management happening through enum events. It still behaves like an Apple API versus a rust one. Certainly something we will keep in mind as we make changes moving forward. Thanks again!