Thank you! I refactored some code so that I have client-side code spawning smaller incremental RPC calls. It is simply impressive to dispatch 480ish async calls concurrently in a clean and orderly fashion.

By on 12/7/2016 3:05 PM ()

Hi,

This is not implemented in WebSharper as it would be too specific behavior, but doable in your own project if you want to reduce server load on some expensive RPCs. I will be assuming now that you are running the web app from a single server, if scaling out it would be needed to switch to websockets so that you can have stateful server asociated with a client.

Some ideas:

  • Generate a unique key before calling long-running RPCs (using NewGuid on the client or set up another small RPC to return increasing numbers)
  • Pass this key to the long-running RPC which can create a CancellationToken for its async computation, and store it in a global dictionary with the key
  • Another RPC would accept a key and cancel the corresponding computation
  • On the client, you can use async cancellation too. Wehenever an RPC call async is cancelled on the client, it is discarding the usual continuation which would handle the ajax result (and executing cancellation path instead), but it is not sending any cancellation notification to the server by itself. So you can do an Async.OnCancel around the RPC call to call your cancelling RPC with the key.
  • Then you still need to run Async.CancelDefaultToken on a window.onunload event handler. One extra catch here: you will want your pending asyncs to be forced to finish. WebSharper.Tests use this trick to do this, a bit ugly but works, maybe we will expose it in a more safe way:
1
2
3
4
5
6
7
8
9
[<Inline "WebSharper.Concurrency.scheduler().tick()">]
let tick() = ()

[<Inline "WebSharper.Concurrency.scheduler().idle">]
let isIdle() = true

[<JavaScript>]
let forceAsync() =
    while not (isIdle()) do tick()
By on 12/6/2016 3:13 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper