2022-09-27 14:27:40 +00:00
|
|
|
import ../statemachine
|
2022-11-08 07:10:17 +00:00
|
|
|
import ./error
|
|
|
|
import ./finished
|
|
|
|
import ./failed
|
2022-09-27 14:27:40 +00:00
|
|
|
|
|
|
|
type PurchaseStarted* = ref object of PurchaseState
|
|
|
|
|
2022-11-08 07:10:17 +00:00
|
|
|
method enterAsync*(state: PurchaseStarted) {.async.} =
|
2022-09-27 14:27:40 +00:00
|
|
|
without purchase =? (state.context as Purchase):
|
|
|
|
raiseAssert "invalid state"
|
|
|
|
|
2022-11-08 07:10:17 +00:00
|
|
|
let clock = purchase.clock
|
|
|
|
let market = purchase.market
|
|
|
|
|
|
|
|
let failed = newFuture[void]()
|
|
|
|
proc callback(_: RequestId) =
|
|
|
|
failed.complete()
|
|
|
|
let subscription = await market.subscribeRequestFailed(purchase.requestId, callback)
|
|
|
|
|
|
|
|
let ended = clock.waitUntil(await market.getRequestEnd(purchase.requestId))
|
|
|
|
try:
|
|
|
|
let fut = await one(ended, failed)
|
|
|
|
if fut.id == failed.id:
|
|
|
|
state.switch(PurchaseFailed())
|
|
|
|
else:
|
|
|
|
state.switch(PurchaseFinished())
|
|
|
|
await subscription.unsubscribe()
|
|
|
|
except CatchableError as error:
|
|
|
|
state.switch(PurchaseErrored(error: error))
|
|
|
|
|
|
|
|
method description*(state: PurchaseStarted): string =
|
|
|
|
"started"
|