kino
                                
                                
                                
                                    kino copied to clipboard
                            
                            
                            
                        Serialize `Kino.JS` payloads upfront
Currently payloads are serialized on the Livebook side (LivebookWeb.JSViewChannel), so structs from dependencies are not supported.

One way to address this is to serialize before sending messages to Livebook (as a new payload type). For serialization we need jason, but we don't want to depend on it to keep kino lightweight, so we can serialize upfront only when jason is already available.
For decimal specifically, we could add it as a dependency in Livebook, but I'm still not convinced on that :)
For more context, the only reason we would add Decimal to Livebook is because Jason has an optional dependency on it, so using it gives us a UndefinedFunctionError for Decimal.to_string instead of the expected Protocol.UndefinedErrror. I guess an alternative is to also detected UndefinedFunctionError for Decimal.to_string and raise a good error message for it.
Another idea is to try to encode to JSON and, if that fails, convert to_string on the client itself. Since JSON encoding is recursive, we may need additional features on Jason to make it work (like a per encoding :fallback_to_any callback).
- [x] Add Decimal as a dependency to Livebook (only to improve UX given Jason depends on it already)
 - [ ] Allow Kino to sent either encoded data (if Jason is available) or rich data (as today)
 - [x] Livebook should detect protocol undefined error and improve the error message
 
Livebook depends on Decimal via Ecto now, and the error message is already specialised.
I can't think of cases where we would actually just pass random structs, so for now we are closing and we can revisit the optional Jason approach if a relevant use case comes up.