dotnet-websharper / core

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

Use symmetric JSON de/serialization for Web.Control initialization #1346

Closed Jand42 closed 10 months ago

Jand42 commented 1 year ago

RPCs now use the symmetric (no type tags) JSON format for remoting: #930

Same format could be used for Web.Control initialization - passing down data from the server to client to set up the page. This would make current dynamic WebSharper.Json.Activate deprecated. For WS7, this already gets all necessary type constructors passed down to it from the startup code snippet generated by Sitelets runtime. This piece could be simplified if compiler pre-generates the JSON de-serialization and DOM replacement function for each Web.Control type.

granicz commented 1 year ago

Can you add some pseudo code for what this would look like for each Web.Control?

Jand42 commented 11 months ago

For normal Web.Controls, we can generate startup code like this:

DecodeJson_MyControl({ ... data ...}).Body.ReplaceInDom(document.getElementById("ControlID"))

For InlineControls, we need custom deserializing where we can pass import/imports, maybe new Interface IImportingControl, so we know at runtime easily, however, we need static method for client-side instantiation

MyInlineControl.Decode({ ... data ...}, funcImport, dataDeserializers...).Body.ReplaceInDom(document.getElementById("ControlID"))

And for IInitializer, we need to instantiate it and call the 3 methods in right order:

let x = DecodeJson_MyInitializer({ ... data ... })
x.PreInitialize("ControlID")
//...
x.Initialize("ControlID")
//...
x.PostInitialize("ControlID")