Skip to content

Commit

Permalink
PKG -- [fcl] Make errors accessible to subscribers from fcl.tx pollin…
Browse files Browse the repository at this point in the history
…g and throw for onceSealed, onceExecuted, onceFinalized
  • Loading branch information
jribbink authored Jun 14, 2022
1 parent d09ba0f commit 4ec2bdc
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-shoes-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@onflow/fcl": minor
---

Make errors accessible to subscribers from fcl.tx polling (second argument of callback) and throw error for onceSealed, onceExecuted, onceFinalized promises. Also removed retried polling requests as they are a redundancy already implemented by @onflow/transport-http
5 changes: 5 additions & 0 deletions .changeset/tender-jobs-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@onflow/util-actor": minor
---

Add error handling to actors. Second argument of callback is now an error object and fatal errors can be thrown with ctx.fatalError(e).
16 changes: 7 additions & 9 deletions packages/fcl/src/transaction/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
INIT,
SUBSCRIBE,
UNSUBSCRIBE,
ERROR,
} from "@onflow/util-actor"
import {send as fclSend, decode, getTransactionStatus} from "@onflow/sdk"

Expand All @@ -32,9 +33,7 @@ const isDiff = (cur, next) => {

const HANDLERS = {
[INIT]: async ctx => {
const tx = await fetchTxStatus(ctx.self())
if (!isSealed(tx)) setTimeout(() => ctx.sendSelf(POLL), RATE)
ctx.merge(tx)
ctx.sendSelf(POLL)
},
[SUBSCRIBE]: (ctx, letter) => {
ctx.subscribe(letter.from)
Expand All @@ -51,10 +50,9 @@ const HANDLERS = {
try {
tx = await fetchTxStatus(ctx.self())
} catch (e) {
console.error(e)
setTimeout(() => ctx.sendSelf(POLL), RATE)
return
return ctx.fatalError(e)
}

if (!isSealed(tx)) setTimeout(() => ctx.sendSelf(POLL), RATE)
if (isDiff(ctx.all(), tx)) ctx.broadcast(UPDATED, tx)
ctx.merge(tx)
Expand Down Expand Up @@ -85,9 +83,9 @@ export function transaction(transactionId) {
return function innerOnce(opts = {}) {
const suppress = opts.suppress || false
return new Promise((resolve, reject) => {
const unsub = subscribe(txStatus => {
if (txStatus.statusCode && !suppress) {
reject(txStatus.errorMessage)
const unsub = subscribe((txStatus, error) => {
if ((error || txStatus.statusCode) && !suppress) {
reject(error || txStatus.errorMessage)
unsub()
} else if (predicate(txStatus)) {
resolve(txStatus)
Expand Down
14 changes: 13 additions & 1 deletion packages/util-actor/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export const spawn = (fn, addr = null) => {
mailbox: createMailbox(),
subs: new Set(),
kvs: {},
error: null,
}

const ctx = {
Expand Down Expand Up @@ -143,6 +144,10 @@ export const spawn = (fn, addr = null) => {
key => (root.FCL_REGISTRY[addr].kvs[key] = data[key])
)
},
fatalError: error => {
root.FCL_REGISTRY[addr].error = error
for (let to of root.FCL_REGISTRY[addr].subs) send(to, UPDATED)
},
}

if (typeof fn === "object") fn = fromHandlers(fn)
Expand Down Expand Up @@ -170,11 +175,18 @@ export function subscriber(address, spawnFn, callback) {
ctx.send(address, SUBSCRIBE)
while (1) {
const letter = await ctx.receive()
const error = root.FCL_REGISTRY[address].error
if (letter.tag === EXIT) {
ctx.send(address, UNSUBSCRIBE)
return
}
callback(letter.data)
if (error) {
callback(null, error)
ctx.send(address, UNSUBSCRIBE)
return
}

callback(letter.data, null)
}
})
return () => send(self, EXIT)
Expand Down

0 comments on commit 4ec2bdc

Please sign in to comment.