feat: stickers spawnAndSend to threadpool task

Move the remaining stickers spawnAndSend (obtainAvailableStickerPacks) into a threadpool task.

refactor: create a base class for tasks models to inherit from

NOTE: this branch is based off of `experiment/tasks-3` and should be rebased on master once that branch is merged.
This commit is contained in:
Eric Mastro 2021-03-18 22:54:25 +11:00 committed by Iuri Matias
parent 98ef76d3c8
commit b7124372e9
6 changed files with 54 additions and 29 deletions

View File

@ -65,12 +65,7 @@ QtObject:
self.transactionWasSent(response)
proc obtainAvailableStickerPacks*(self: StickersView) =
spawnAndSend(self, "setAvailableStickerPacks") do:
let availableStickerPacks = status_stickers.getAvailableStickerPacks()
var packs: seq[StickerPack] = @[]
for packId, stickerPack in availableStickerPacks.pairs:
packs.add(stickerPack)
$(%*(packs))
self.status.taskManager.threadPool.stickers.obtainAvailableStickerPacks(cast[pointer](self.vptr), "setAvailableStickerPacks")
proc stickerPacksLoaded*(self: StickersView) {.signal.}

View File

@ -107,7 +107,7 @@ proc getInstalledStickerPacks*(self: StickersModel): Table[int, StickerPack] =
self.installedStickerPacks = status_stickers.getInstalledStickerPacks()
result = self.installedStickerPacks
proc getAvailableStickerPacks*(self: StickersModel): Table[int, StickerPack] = status_stickers.getAvailableStickerPacks()
proc getAvailableStickerPacks*(): Table[int, StickerPack] = status_stickers.getAvailableStickerPacks()
proc getRecentStickers*(self: StickersModel): seq[Sticker] =
result = status_stickers.getRecentStickers()

View File

@ -0,0 +1,17 @@
import
json_serialization, NimQml, task_runner
type
BaseTasks* = ref object of RootObj
chanSendToPool*: AsyncChannel[ThreadSafeString]
BaseTask* = ref object of RootObj
vptr*: ByteAddress
slot*: string
proc start*[T: BaseTask](self: BaseTasks, task: T) =
let payload = task.toJson(typeAnnotations = true)
self.chanSendToPool.sendSync(payload.safe)
proc finish*[T](task: BaseTask, payload: T) =
let resultPayload = Json.encode(payload)
signal_handler(cast[pointer](task.vptr), resultPayload, task.slot)

View File

@ -1,40 +1,50 @@
import
chronos, NimQml, json_serialization, task_runner
import # nim libs
tables
import
../stickers
import # vendor libs
chronos, NimQml, json, json_serialization, task_runner
import # status-desktop libs
./common, ../libstatus/types, ../stickers
type
StickerPackPurchaseGasEstimate* = object
vptr*: ByteAddress
slot*: string
StickerPackPurchaseGasEstimate* = ref object of BaseTask
packId*: int
address*: string
price*: string
uuid*: string
StickersTasks* = ref object
chanSendToPool: AsyncChannel[ThreadSafeString]
ObtainAvailableStickerPacks* = ref object of BaseTask
StickersTasks* = ref object of BaseTasks
proc newStickersTasks*(chanSendToPool: AsyncChannel[ThreadSafeString]): StickersTasks =
new(result)
result.chanSendToPool = chanSendToPool
proc runTask*(stickerPackPurchaseGasEstimate: StickerPackPurchaseGasEstimate) =
proc run*(task: StickerPackPurchaseGasEstimate) =
var success: bool
var estimate = estimateGas(
stickerPackPurchaseGasEstimate.packId,
stickerPackPurchaseGasEstimate.address,
stickerPackPurchaseGasEstimate.price,
task.packId,
task.address,
task.price,
success
)
if not success:
estimate = 325000
let result: tuple[estimate: int, uuid: string] = (estimate, stickerPackPurchaseGasEstimate.uuid)
let resultPayload = Json.encode(result)
let result: tuple[estimate: int, uuid: string] = (estimate, task.uuid)
task.finish(result)
signal_handler(cast[pointer](stickerPackPurchaseGasEstimate.vptr), resultPayload, stickerPackPurchaseGasEstimate.slot)
proc run*(task: ObtainAvailableStickerPacks) =
var success: bool
let availableStickerPacks = getAvailableStickerPacks()
var packs: seq[StickerPack] = @[]
for packId, stickerPack in availableStickerPacks.pairs:
packs.add(stickerPack)
task.finish(%*(packs))
proc stickerPackPurchaseGasEstimate*(self: StickersTasks, vptr: pointer, slot: string, packId: int, address: string, price: string, uuid: string) =
let task = StickerPackPurchaseGasEstimate(vptr: cast[ByteAddress](vptr), slot: slot, packId: packId, address: address, price: price, uuid: uuid)
let payload = task.toJson(typeAnnotations = true)
self.chanSendToPool.sendSync(payload.safe)
self.start(task)
proc obtainAvailableStickerPacks*(self: StickersTasks, vptr: pointer, slot: string) =
let task = ObtainAvailableStickerPacks(vptr: cast[ByteAddress](vptr), slot: slot)
self.start(task)

View File

@ -3,7 +3,7 @@ import
task_runner
import
./stickers
./common, ./stickers
export
stickers
@ -89,9 +89,12 @@ proc task(arg: TaskThreadArg) {.async.} =
try:
case messageType
of "StickerPackPurchaseGasEstimate":
of "StickerPackPurchaseGasEstimate:ObjectType":
let decoded = Json.decode(received, StickerPackPurchaseGasEstimate, allowUnknownFields = true)
decoded.runTask()
decoded.run()
of "ObtainAvailableStickerPacks:ObjectType":
let decoded = Json.decode(received, ObtainAvailableStickerPacks, allowUnknownFields = true)
decoded.run()
else:
error "[threadpool task thread] unknown message", message=received
except Exception as e:

View File

@ -215,7 +215,7 @@ proc getStickers*(address: Address): string =
if (purchasedStickerPacks.len == 0):
return $(%*stickers)
# TODO find a way to keep those in memory so as not to reload it each time
let availableStickerPacks = status_stickers.getAvailableStickerPacks()
let availableStickerPacks = getAvailableStickerPacks()
var index = 0
for stickerId in purchasedStickerPacks: