dotnet-websharper / core

WebSharper - Full-stack, functional, reactive web apps and microservices in F# and C#
https://websharper.com
Apache License 2.0
595 stars 50 forks source link

Problem with sending remote Array2D to client #382

Open fjoppe opened 9 years ago

fjoppe commented 9 years ago

Creating a client/server app with a grid, I'd like to fill an Array2D at the Server side (easier calculations, better support DateTime), request and visualize it at the client side.

Array2D is supported both at client and server, but it doesn't get transfered from server to client.

This code visualizes correctly:

    [<JavaScript>]
    let Main () =
        let myData = Array2D.init 4 4 (fun x y ->  x * y)
        Div [
            for x in [0..3] do
                yield Div [
                    for y in [0..3] do
                        yield Input [Attr.Type "Text"; Value (sprintf "%d" myData.[x,y])]
                ]
        ]

This code compiles and runs, but doesn't display anything (I think it causes a Javascript exception):

    [<Rpc>]
    let data() = Array2D.init 4 4 (fun x y ->  x * y)

    [<JavaScript>]
    let Main () =
        let myData = data()
        Div [
            for x in [0..3] do
                yield Div [
                    for y in [0..3] do
                        yield Input [Attr.Type "Text"; Value (sprintf "%d" myData.[x,y])]
                ]
        ]

Wrapping an Array2D in an F# Record displays the same problem. The client receives nothing at all.

Websharper version: 3.0.46.130-rc1,

Jand42 commented 9 years ago

Thanks for the report.

2D arrays currently have no RPC de/serializers. When using it as an RPC parameter it gives a compile warning, but not when used as a return type.

We will implement: RPC de/serializers for 2D arrays. Give compile warning on higher dimensional arrays not supported by WebSharper client side.

granicz commented 9 years ago

As a temp workaround, you can use double arrays ([][]) to ship data between tiers.

Jand42 commented 9 years ago

As of WebSharper 3.1: compiler now gives warnings on unsupported multi-dimensional arrays used in RPC signatures.

But 2D array proxy hasn't been changed. it is a jagged JS array with an extra property which is not serializable to JSON, so converting it to [][] is still necessary for client-server interaction.