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

Codec type for WebStorage, FetchStream, etc. #136

Open
raquo opened this issue Dec 7, 2024 · 3 comments
Open

Codec type for WebStorage, FetchStream, etc. #136

raquo opened this issue Dec 7, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@raquo
Copy link
Owner

raquo commented Dec 7, 2024

Currently, FetchStream and WebStorage offer withCodec(encodeFn, decodeFn) builders.

See if we can offer a Codec[A, B] typeclass that users could automatically derive from their existing JSON codecs (once per JSON lib, not once per type).

Our APIs should require (Encoder[A, B], Decoder[A, B]) for maximum flexibility

Note that FetchStream needs some weird types, but we should be able to wrangle them into Encoder & Decoder

Consider all applications of these codecs across Airstream and Laminar – where do we want to use them? Do we have decode return a Try, or throw instead? Should encode or decode be allowed to throw?

Ideally we should keep the current withCodec et al APIs because I think for many cases this would work just as well, without requiring the concept of typeclasses.

@raquo raquo added the enhancement New feature or request label Dec 7, 2024
@yurique
Copy link
Contributor

yurique commented Dec 7, 2024

I think in cases like this, we should not fret too much about typeclasses :), especially now that they are first class citizens in Scala 3.

A more substantial question I have is - why two type parameters? (I might be missing the idea here)
Also, if we make it Codec instead of Encoder + Decoder - I'm pretty sure it will have to be invariant, right? (which can be annoying in some cases)

@yurique
Copy link
Contributor

yurique commented Dec 7, 2024

Circe has all three (encoders, decoders, codecs) and they can be automatically derived both ways: encoder+decoder <-> codec (and codec is just a convenience)

@raquo
Copy link
Owner Author

raquo commented Dec 8, 2024

Yes, encoders and decoders will be separate. Codec would just be a bundle of both that the user may choose to provide as a single instance, as is usual in json libs.

WebStorageVar needs to encode/decode values between A and String, which aligns well with a single-param Codec[A], but FetchStream is more complicated. It needs to encode data from your Input to type dom.BodyInit = Blob | BufferSource | FormData | String | ReadableStream[Uint8Array] | URLSearchParams, and needs to decode data from dom.Response into your EventStream[Output] (allowing for a streaming codec to read the response.body stream).

So... I'm not yet sure if FetchStream would be able to use json-style codecs that can only encode/decode into String – those aren't enough to describe its entire functionality. But... maybe I'll just make the new Codec-based API more limited, intended for simple, non-streaming JSON codecs. And if someone needs to e.g. implement a streaming codec to parse the response, or submit FormData instead of a String, they could use the existing non-typeclass methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants