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

View File

@ -107,7 +107,7 @@ proc getInstalledStickerPacks*(self: StickersModel): Table[int, StickerPack] =
self.installedStickerPacks = status_stickers.getInstalledStickerPacks() self.installedStickerPacks = status_stickers.getInstalledStickerPacks()
result = self.installedStickerPacks 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] = proc getRecentStickers*(self: StickersModel): seq[Sticker] =
result = status_stickers.getRecentStickers() 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 import # nim libs
chronos, NimQml, json_serialization, task_runner tables
import import # vendor libs
../stickers chronos, NimQml, json, json_serialization, task_runner
import # status-desktop libs
./common, ../libstatus/types, ../stickers
type type
StickerPackPurchaseGasEstimate* = object StickerPackPurchaseGasEstimate* = ref object of BaseTask
vptr*: ByteAddress
slot*: string
packId*: int packId*: int
address*: string address*: string
price*: string price*: string
uuid*: string uuid*: string
StickersTasks* = ref object ObtainAvailableStickerPacks* = ref object of BaseTask
chanSendToPool: AsyncChannel[ThreadSafeString] StickersTasks* = ref object of BaseTasks
proc newStickersTasks*(chanSendToPool: AsyncChannel[ThreadSafeString]): StickersTasks = proc newStickersTasks*(chanSendToPool: AsyncChannel[ThreadSafeString]): StickersTasks =
new(result) new(result)
result.chanSendToPool = chanSendToPool result.chanSendToPool = chanSendToPool
proc runTask*(stickerPackPurchaseGasEstimate: StickerPackPurchaseGasEstimate) = proc run*(task: StickerPackPurchaseGasEstimate) =
var success: bool var success: bool
var estimate = estimateGas( var estimate = estimateGas(
stickerPackPurchaseGasEstimate.packId, task.packId,
stickerPackPurchaseGasEstimate.address, task.address,
stickerPackPurchaseGasEstimate.price, task.price,
success success
) )
if not success: if not success:
estimate = 325000 estimate = 325000
let result: tuple[estimate: int, uuid: string] = (estimate, stickerPackPurchaseGasEstimate.uuid) let result: tuple[estimate: int, uuid: string] = (estimate, task.uuid)
let resultPayload = Json.encode(result) 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) = 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 task = StickerPackPurchaseGasEstimate(vptr: cast[ByteAddress](vptr), slot: slot, packId: packId, address: address, price: price, uuid: uuid)
let payload = task.toJson(typeAnnotations = true) self.start(task)
self.chanSendToPool.sendSync(payload.safe)
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 task_runner
import import
./stickers ./common, ./stickers
export export
stickers stickers
@ -89,9 +89,12 @@ proc task(arg: TaskThreadArg) {.async.} =
try: try:
case messageType case messageType
of "StickerPackPurchaseGasEstimate": of "StickerPackPurchaseGasEstimate:ObjectType":
let decoded = Json.decode(received, StickerPackPurchaseGasEstimate, allowUnknownFields = true) 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: else:
error "[threadpool task thread] unknown message", message=received error "[threadpool task thread] unknown message", message=received
except Exception as e: except Exception as e:

View File

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