[sales] onSale() callback is optional

This commit is contained in:
Mark Spanbroek 2022-03-31 11:41:45 +02:00 committed by markspanbroek
parent 11f7b4b4d8
commit 5d434f20d3
1 changed files with 6 additions and 3 deletions

View File

@ -16,7 +16,7 @@ type
subscription: ?Subscription
available*: seq[Availability]
offerExpiryInterval*: UInt256
onSale*: OnSale
onSale: ?OnSale
Availability* = object
id*: array[32, byte]
size*: uint64
@ -35,6 +35,9 @@ proc init*(_: type Availability,
doAssert randomBytes(id) == 32
Availability(id: id, size: size, duration: duration, minPrice: minPrice)
proc `onSale=`*(sales: Sales, callback: OnSale) =
sales.onSale = some callback
func add*(sales: Sales, availability: Availability) =
sales.available.add(availability)
@ -70,8 +73,8 @@ proc handleRequest(sales: Sales, request: StorageRequest) {.async.} =
proc onSelect(offerId: array[32, byte]) {.gcsafe, upraises:[].} =
if subscription =? subscription:
asyncSpawn subscription.unsubscribe()
if offer.id == offerId:
sales.onSale(offer)
if onSale =? sales.onSale and offer.id == offerId:
onSale(offer)
subscription = some await sales.market.subscribeSelection(request.id, onSelect)
proc start*(sales: Sales) =