mirror of
https://github.com/codex-storage/nim-codex.git
synced 2025-02-23 10:08:17 +00:00
49 lines
1.4 KiB
Nim
49 lines
1.4 KiB
Nim
import std/sequtils
|
|
import ./cancelled
|
|
import ./failed
|
|
import ./proving
|
|
import ./errored
|
|
import ../salesagent
|
|
import ../statemachine
|
|
import ../../market
|
|
|
|
type
|
|
SaleDownloading* = ref object of SaleState
|
|
failedSubscription: ?market.Subscription
|
|
hasCancelled: ?Future[void]
|
|
SaleDownloadingError* = object of SaleError
|
|
|
|
method `$`*(state: SaleDownloading): string = "SaleDownloading"
|
|
|
|
method onCancelled*(state: SaleDownloading, request: StorageRequest) {.async.} =
|
|
await state.switchAsync(SaleCancelled())
|
|
|
|
method onFailed*(state: SaleDownloading, request: StorageRequest) {.async.} =
|
|
await state.switchAsync(SaleFailed())
|
|
|
|
method enterAsync(state: SaleDownloading) {.async.} =
|
|
without agent =? (state.context as SalesAgent):
|
|
raiseAssert "invalid state"
|
|
|
|
try:
|
|
without onStore =? agent.sales.onStore:
|
|
raiseAssert "onStore callback not set"
|
|
|
|
without slotIndex =? agent.slotIndex:
|
|
raiseAssert "no slot selected"
|
|
|
|
without request =? agent.request:
|
|
raiseAssert "no sale request"
|
|
|
|
await onStore(request, slotIndex, agent.availability)
|
|
await state.switchAsync(SaleProving())
|
|
|
|
except CancelledError:
|
|
discard
|
|
|
|
except CatchableError as e:
|
|
let error = newException(SaleDownloadingError,
|
|
"unknown sale downloading error",
|
|
e)
|
|
await state.switchAsync(SaleErrored(error: error))
|