File: .lake/packages/proofwidgets/ProofWidgets/Cancellable.lean

full_name: ProofWidgets.RequestId
code: abbrev RequestId 
goal: Type

full_name: ProofWidgets.CancellableTask
code: structure CancellableTask where
  task : Task (Except RequestError (LazyEncodable Json))
  /- Note: we cannot just `IO.cancel task` because it is a result of `map`.
  See https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/Should.20cancelling.20a.20purely.20mapped.20task.20cancel.20the.20original.3F -/
  cancel : IO Unit
goal: Type

full_name: ProofWidgets.CancellableTask.cancel
code: structure CancellableTask where
  task : Task (Except RequestError (LazyEncodable Json))
  /- Note: we cannot just `IO.cancel task` because it is a result of `map`.
  See https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/Should.20cancelling.20a.20purely.20mapped.20task.20cancel.20the.20original.3F -/
  cancel : IO Unit
goal: IO Unit

full_name: ProofWidgets.CancellableTask.task
code: structure CancellableTask where
  task : Task (Except RequestError (LazyEncodable Json))
  /- Note: we cannot just `IO.cancel task` because it is a result of `map`.
  See https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/Should.20cancelling.20a.20purely.20mapped.20task.20cancel.20the.20original.3F -/
  cancel : IO Unit
goal: Task (Except Lean.Server.RequestError (ProofWidgets.LazyEncodable Lean.Json))

full_name: ProofWidgets.runningRequests
code: /-- Maps the ID of each currently executing request to its task. -/
initialize runningRequests :
    IO.Ref (RequestId × HashMap RequestId CancellableTask) ←
  IO.mkRef (0, HashMap.empty)
goal: IO.Ref (ProofWidgets.RequestId × Lean.HashMap ProofWidgets.RequestId ProofWidgets.CancellableTask)

full_name: ProofWidgets.initFn._@..lake.packages.proofwidgets.ProofWidgets.Cancellable._hyg.39
code: /-- Maps the ID of each currently executing request to its task. -/
initialize runningRequests :
    IO.Ref (RequestId × HashMap RequestId CancellableTask) ←
  IO.mkRef (0, HashMap.empty)
goal: IO (IO.Ref (ProofWidgets.RequestId × Lean.HashMap ProofWidgets.RequestId ProofWidgets.CancellableTask))

full_name: ProofWidgets.mkCancellable
code: /-- Transforms a request handler returning `β`
into one that returns immediately with a `RequestId`.

The ID uniquely identifies the running request:
its results can be retrieved using `checkRequest`,
and it can be cancelled using `cancelRequest`. -/
def mkCancellable [RpcEncodable β] (handler : α → RequestM (RequestTask β)) :
    α → RequestM (RequestTask RequestId) 
goal: α → Lean.Server.RequestM (Lean.Server.RequestTask ProofWidgets.RequestId)

full_name: ProofWidgets.cancelRequest
code: /-- Cancel the request with ID `rid`.
Does nothing if `rid` is invalid. -/
@[server_rpc_method]
def cancelRequest (rid : RequestId) : RequestM (RequestTask String) 
goal: Lean.Server.RequestM (Lean.Server.RequestTask String)

full_name: ProofWidgets.CheckRequestResponse
code: /-- The status of a running cancellable request. -/
inductive CheckRequestResponse
  | running
  | done (result : LazyEncodable Json)
  deriving RpcEncodable
goal: Type

full_name: ProofWidgets.CheckRequestResponse.done
code: /-- The status of a running cancellable request. -/
inductive CheckRequestResponse
  | running
  | done (result : LazyEncodable Json)
  deriving RpcEncodable
goal: ProofWidgets.CheckRequestResponse

full_name: ProofWidgets.CheckRequestResponse.running
code: /-- The status of a running cancellable request. -/
inductive CheckRequestResponse
  | running
  | done (result : LazyEncodable Json)
  deriving RpcEncodable
goal: ProofWidgets.CheckRequestResponse

full_name: ProofWidgets.ProofWidgets.CheckRequestResponse.instFromJsonRpcEncodablePacket
code: RpcEncodable
goal: Lean.FromJson ProofWidgets.ProofWidgets.CheckRequestResponse.RpcEncodablePacket✝

full_name: ProofWidgets.ProofWidgets.CheckRequestResponse.instRpcEncodableCheckRequestResponse.dec._@..lake.packages.proofwidgets.ProofWidgets.Cancellable._hyg.336
code: RpcEncodable
goal: Lean.Json → ExceptT String (ReaderT Lean.Server.RpcObjectStore Id) ProofWidgets.CheckRequestResponse

full_name: ProofWidgets.ProofWidgets.CheckRequestResponse.instRpcEncodableCheckRequestResponse
code: RpcEncodable
goal: Lean.Server.RpcEncodable ProofWidgets.CheckRequestResponse

full_name: ProofWidgets.ProofWidgets.CheckRequestResponse.instToJsonRpcEncodablePacket
code: RpcEncodable
goal: Lean.ToJson ProofWidgets.ProofWidgets.CheckRequestResponse.RpcEncodablePacket✝

full_name: ProofWidgets.ProofWidgets.CheckRequestResponse.RpcEncodablePacket._@..lake.packages.proofwidgets.ProofWidgets.Cancellable._hyg.336
code: RpcEncodable
goal: Type

full_name: ProofWidgets.ProofWidgets.CheckRequestResponse.instRpcEncodableCheckRequestResponse.enc._@..lake.packages.proofwidgets.ProofWidgets.Cancellable._hyg.336
code: RpcEncodable
goal: ProofWidgets.CheckRequestResponse → StateM Lean.Server.RpcObjectStore Lean.Json

full_name: ProofWidgets.checkRequest
code: /-- Check whether a request has finished computing,
and return the response if so.
The request is removed from `runningRequests` the first time it is checked
and found to have finished.
Throws an error if the `rid` is invalid,
or if the request itself threw an error. -/
/- NOTE: a notification-based version would be better than this polling-based one.
But we cannot include RPC references in notifications atm;
another possible addition to the RPC protocol? -/
@[server_rpc_method]
def checkRequest (rid : RequestId) : RequestM (RequestTask CheckRequestResponse) 
goal: Lean.Server.RequestM (Lean.Server.RequestTask ProofWidgets.CheckRequestResponse)

full_name: ProofWidgets.cancellableSuffix
code: def cancellableSuffix : Name 
goal: Lean.Name

full_name: ProofWidgets.initFn._@..lake.packages.proofwidgets.ProofWidgets.Cancellable._hyg.941
code: /-- Like `server_rpc_method`, but requests for this method can be cancelled.
The method should check for that using `IO.checkCanceled`.
Cancellable methods are invoked differently from JavaScript:
see `callCancellable` in `cancellable.ts`. -/
initialize
  registerBuiltinAttribute {
    name := `server_rpc_method_cancellable
    descr := "Like `server_rpc_method`, \
    but requests for this method can be cancelled. \
    The method should check for that using `IO.checkCanceled`. \
    Cancellable methods are invoked differently from JavaScript: \
    see `callCancellable` in `cancellable.ts`."
    applicationTime := AttributeApplicationTime.afterCompilation
    add := fun decl _ _ => Prod.fst <$> MetaM.run do
      let name := decl ++ cancellableSuffix
      let value ← mkAppM ``mkCancellable #[mkConst decl]
      addAndCompile $ .defnDecl {
        name
        levelParams := []
        type := ← inferType value
        value
        hints  := .opaque
        safety := .safe
      }
      registerRpcProcedure name
  }
goal: IO Unit