vaadin / flow

Vaadin Flow is a Java framework binding Vaadin web components to Java. This is part of Vaadin 10+.
Apache License 2.0
623 stars 167 forks source link

JS Typed Arrays (like Uint8Array) should be supported for `@ClientCallable` #9435

Open mvysny opened 4 years ago

mvysny commented 4 years ago

When transferring primitive array-like data using @ClientCallable (or other Flow data passing mechanisms), I want primitive Java arrays like byte[] to be mapped to typed JS arrays like UInt8Array, so that (a) the transfer is more efficient in terms of how many bytes are sent over the wire and (b) I do not have to write the boilerplate code to convert from a typed array into an regular JS array of numbers.

Similar to vaadin/hilla#350 but this ticket relates to @ClientCallable.

mstahv commented 3 months ago

Slightly related. I have recently started to use CustomEvent more than @ClientCallable methods for client->server communication (supports throttling/debouncing). For my helper I just added byte[] support.

https://github.com/parttio/velocity-component/blob/07c69400984d97612e539e73197438721fa4b298/src/main/java/org/vaadin/addons/velocitycomponent/VElement.java#L109

Usage example from the client side:

https://github.com/mstahv/hr-monitor/blob/ca5a5a4581508fa69af8bae7de0fc3ce92cbef2c/src/main/frontend/h10tooling.js#L38

The target.value on the piggybacked event is DataView (which contains buffer), base64 encoded like this:

// base64 encoded DataView
const b64 = b => btoa(String.fromCharCode(...new Uint8Array(b.buffer)));

This is probably "good enough" for most cases. If there are really big binaries, sending them as raw http request probably is the way to go, a "bigger" framework feature for that would be needed.