The only example I can find for using a web worker with WebSharper is this old demo:

https://bitbucket.org/IntelliFactory/websharper.demos.webworker/src/53a08c18e9f9e62ba494fb154f05751738d26986/websharper.demos.webworker/Client.fs?at=default&fileviewer=file-view-default

With a single page application this appears to work, because all the JavaScript required is put into a single js file. But with my regular application, feeding the js file of the current project to the web worker fails because there are dependencies on other js files, including WebSharper.Core.JavaScript/Runtime.js.

  • github

    Having explicit support for targeting a web worker from WebSharper would be useful. What I'm currently trialing in WebSharper 4.x/Zafir is:

    Configure projects as libraries Set

    1
    
    <WebProjectOutputDir>Content</WebProjectOutputDir>

    (previously this was <WebSharperProject>Bundle</WebSharperProject>)

    that outputs everything into \Content\Scripts\WebSharper

    You can then manually determine the libaries you need and import them (and bundle as desired):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
        importScripts("build/WebSharper.Core.JavaScript/Runtime.js");
        importScripts("build/WebSharper.Main.js");
        importScripts("build/WebSharper.Collections.js");
        importScripts("build/WebSharper.Web.js");
    
        importScripts("build/ourlibrary.js");
        importScripts("build/anotherlibary.js");
        importScripts("build/yetmorelibary.js");
        importScripts("build/ourapp.js");

    Hope this helps, having something like this route documented would be really useful! In WebSharper 3.x this was a bit easier as you can get everything in concatenated form.

  • github

    Unfortunately, this seems to have gotten more awkward, possibly with recent versions (using Zafir.4.0.159.36-beta6) as the generated code directly looks for Global on window. I've been able to workaround this by modifying the equivalent global selfin web workers around the import:

    1
    2
    3
    4
    5
    
    eval("self.window = self");
    // Load the transcompiled F# code.
    importScripts("websharperlibs.js");
    importScripts("ourtranspiledlib.js");
    eval("delete self.window");

    note: I used eval as TypeScript sensibly marks self as read-only so this may not work indefinitely but you might also be able to do without that.

    It would be great if WebSharper could look for its global on selfas well (or avoid globals).

  • github

    Any word on this? We mostly use W# to do the lengthy math operations as F# is nicer for that kind of stuff, and they need to go in a worker.