When WebSharper compiles F# to JavaScript it preserves
namespaces, module and class nesting to make it easy to navigate
the compiled code from JavaScript shell. Roughly speaking,
A.B.C.D.E
identifier in F# can be found by typing
A.B.C.D.E
in JavaScript.
This poses a challenge: as you can imagine, emitting long
qualified identifiers everywhere is not a good idea for compact
code generation. To save space WebSharper 2.4 does class/module
interning. The basic idea is to say
L=Microsoft.FSharp.Core.ListModule
once and then say
L.ofArray
at every use site.
An example of this in action can be seen below:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Website | |
module Client = | |
open IntelliFactory.WebSharper | |
open IntelliFactory.WebSharper.Html | |
[<JavaScript>] | |
let SayHello () = | |
JavaScript.Alert("HELLO!") | |
[<JavaScript>] | |
let MakeHelloDiv () = | |
SayHello () | |
Div [ Text "HELLO THERE" ] | |
module Controls = | |
open IntelliFactory.WebSharper | |
type HelloControl() = | |
inherit Web.Control() | |
[<JavaScript>] | |
override this.Body = Client.MakeHelloDiv() :> _ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() | |
{ | |
var Global=this,Runtime=this.IntelliFactory.Runtime,Website,Client,WebSharper,Html,Default,List,alert; | |
Runtime.Define(Global,{ | |
Website:{ | |
Client:{ | |
MakeHelloDiv:function() | |
{ | |
Client.SayHello(); | |
return Default.Div(List.ofArray([Default.Text("HELLO THERE")])); | |
}, | |
SayHello:function() | |
{ | |
return alert("HELLO!"); | |
} | |
}, | |
Controls:{ | |
HelloControl:Runtime.Class({ | |
get_Body:function() | |
{ | |
return Client.MakeHelloDiv(); | |
} | |
}) | |
} | |
} | |
}); | |
Runtime.OnInit(function() | |
{ | |
Website=Runtime.Safe(Global.Website); | |
Client=Runtime.Safe(Website.Client); | |
WebSharper=Runtime.Safe(Global.IntelliFactory.WebSharper); | |
Html=Runtime.Safe(WebSharper.Html); | |
Default=Runtime.Safe(Html.Default); | |
List=Runtime.Safe(WebSharper.List); | |
return alert=Runtime.Safe(Global.alert); | |
}); | |
Runtime.OnLoad(function() | |
{ | |
}); | |
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){var $$=this,$=this.IntelliFactory.Runtime,a,b,c,d,e,f,g;$.Define($$,{Website:{Client:{MakeHelloDiv:function(){b.SayHello();return e.Div(f.ofArray([e.Text("HELLO THERE")]));},SayHello:function(){return g("HELLO!");}},Controls:{HelloControl:$.Class({get_Body:function(){return b.MakeHelloDiv();}})}}});$.OnInit(function(){a=$.Safe($$.Website);b=$.Safe(a.Client);c=$.Safe($$.IntelliFactory.WebSharper);d=$.Safe(c.Html);e=$.Safe(d.Default);f=$.Safe(c.List);return g=$.Safe($$.alert);});$.OnLoad(function(){});}()); |
No comments:
Post a Comment