anchor.diff:  open IntelliFactory.WebSharper
   open System.Threading
   
  -[<JavaScript>]                                
   [<Proxy(typeof<AsyncReplyChannel<_>>)>]
  -[<Name "AsyncReplyChannel">]
  -type private ChannelProxy<'T>(reply: 'T -> unit) =
  -    member this.Reply(res) = reply res
  +type private ChannelProxy<'T> =
  +    [<Inline "$this($res)">]
  +    member this.Reply(res: 'T) = X<unit>
  +
  +[<Proxy(typeof<System.TimeoutException>)>]
  +type TimeoutExceptionProxy =
  +    [<Inline "new Error(\"TimeoutException\")">]
  +    new () = {}
   
   [<JavaScript>]
   [<Proxy(typeof<MailboxProcessor<_>>)>]
  
positive.code:     let errorEvent = Event<exn>()
      let mailbox = LinkedList<'T>()
      let mutable savedCont = None
      
      let dequeue() =
          let f = mailbox.First.Value
          mailbox.RemoveFirst()
          f
  
      member this.Error = errorEvent.Publish
  
      member this.add_Error handler = this.Error.AddHandler handler
      member this.remove_Error handler = this.Error.RemoveHandler handler
  
      member this.Start() =
          if started then
  
negative.code:             async {
                  try do! initial (As<MailboxProcessor<'T>> this)
                  with err -> errorEvent.Trigger err
              } |> startAsync
  
      static member Start(initial, ?token: CancellationToken) =
          let mb = new MailboxProcessor<'T>(initial, ?cancellationToken = token)
          mb.Start()
          mb
  
      [<Inline>]
      member this.Post(msg: 'T) =
          mailbox.AddLast msg |> ignore
          resume()
  
      member this.TryReceive(?timeout: int) =        
          let timeout = defaultArg timeout -1
          Async.FromContinuations <| fun (ok, _, _) ->
              if mailbox.First = null then
                  if timeout < 0 then
                      savedCont <- Some <| async { dequeue() |> Some |> ok }
                  else  
                      let waiting = ref true
                      let pending = 
                          JavaScript.SetTimeout(fun () ->
                              if !waiting then
                                  waiting := false
                                  savedCont <- None
                                  ok None
                          ) timeout 
                      savedCont <- Some <| async { 
                          if !waiting then do
                              waiting := false  
                              JavaScript.ClearTimeout pending
                              dequeue() |> Some |> ok 
                      }
              else dequeue() |> Some |> ok
  
      member this.Receive(?timeout: int) =
          async {
              let! x = this.TryReceive(?timeout = timeout)
              return
                  match x with
                  | None -> raise (System.TimeoutException())
                  | Some x -> x
          }
                                
      member this.CurrentQueueLength = mailbox.Count
  
      member this.PostAndTryAsyncReply(msgf: AsyncReplyChannel<'R> -> 'T, ?timeout: int) : Async<'R option> =
          let timeout = defaultArg timeout -1
          Async.FromContinuations <| fun (ok, _, _) ->
              if timeout < 0 then
                  As (Some >> ok) |> msgf |> this.Post
              else
                  let waiting = ref true
                  As (fun res ->
                      if !waiting then
                          waiting := false
                          ok (Some res)
                  ) |> msgf |> this.Post
                  JavaScript.SetTimeout (fun () ->
                      if !waiting then
                          waiting := false
                          ok None        
                  ) timeout |> ignore               
  
      member this.PostAndAsyncReply(msgf: AsyncReplyChannel<'R> -> 'T, ?timeout: int) : Async<'R> =
          async {
              let! x = this.PostAndTryAsyncReply(msgf, ?timeout = timeout)
              return
                  match x with
                  | None -> raise (System.TimeoutException())
                  | Some x -> x
          }
  
      member this.TryScan(scanner, ?timeout: int) =
          let timeout = defaultArg timeout -1
          async {
              let scanInbox() =
                  let mutable m = mailbox.First
                  let mutable found = None
                  while m <> null do 
                      match scanner m.Value with
                      | None ->
                          m <- m.Next
                      | _ as a ->
                          mailbox.Remove m
                          m <- null
                          found <- a
                  found
              match scanInbox() with
              | Some found -> 
                  let! res = found
                  return Some res
              | _ -> return! Async.FromContinuations <| fun (ok, _, _) -> 
                  if timeout < 0 then
                      let rec scanNext() =
                          savedCont <- Some <| async {
                              match scanner mailbox.First.Value with
                              | None -> scanNext()
                              | Some c ->
                                  mailbox.RemoveFirst()
                                  let! res = c
                                  ok (Some res)
                          }
                      scanNext()
                  else
                      let waiting = ref true
                      let pending = 
                          JavaScript.SetTimeout(fun () ->
                              if !waiting then
                                  waiting := false
                                  savedCont <- None
                                  ok None
                          ) timeout
                      let rec scanNext() =
                          savedCont <- Some <| async {
                              match scanner mailbox.First.Value with
                              | None -> scanNext()
                              | Some c ->
                                  mailbox.RemoveFirst()
                                  let! res = c
                                  if !waiting then do
                                      waiting := false
                                      JavaScript.ClearTimeout pending
                                      ok (Some res)
                          }
                      scanNext()
          }
          
      member this.Scan(scanner, ?timeout: int) =
          async {
              let! x = this.TryScan(scanner, ?timeout = timeout)
              return
                  match x with
                  | None -> raise (System.TimeoutException())
                  | Some x -> x
          }
  