Use TaskQueue for unwrapping

This commit is contained in:
Jon Staab
2026-02-09 11:17:35 -08:00
parent 119cb6248b
commit 628f4595f1
+20 -23
View File
@@ -1,6 +1,6 @@
import {Client, ClientOptions, PomadeSigner} from "@pomade/core" import {Client, ClientOptions, PomadeSigner} from "@pomade/core"
import {derived, writable} from "svelte/store" import {derived, writable} from "svelte/store"
import {cached, randomId, append, omit, equals, assoc} from "@welshman/lib" import {cached, randomId, append, omit, equals, assoc, TaskQueue} from "@welshman/lib"
import {withGetter} from "@welshman/store" import {withGetter} from "@welshman/store"
import { import {
Wallet, Wallet,
@@ -301,28 +301,10 @@ export const shouldUnwrap = withGetter(writable(false))
export const failedUnwraps = new Set<string>() export const failedUnwraps = new Set<string>()
export const unwrapAndStore = async (wrap: SignedEvent) => { export const wrapQueue = new TaskQueue({
if (wrap.kind !== WRAP) { batchSize: 5,
throw new Error("Tried to unwrap an invalid event") batchDelay: 30,
} processItem: async (wrap: SignedEvent) => {
if (!shouldUnwrap.get()) {
throw new Error("Discarding wrapped event because `shouldUnwrap` is not enabled")
}
// Check to see if we already tried to unwrap but failed
if (failedUnwraps.has(wrap.id)) {
return
}
// Check index and repository
const cached = wrapManager.getRumor(wrap.id)
if (cached) {
return cached
}
// Next, try to decrypt as the recipient
for (const recipient of getPubkeyTagValues(wrap.tags)) { for (const recipient of getPubkeyTagValues(wrap.tags)) {
const signer = getSignerFromPubkey(recipient) const signer = getSignerFromPubkey(recipient)
@@ -338,4 +320,19 @@ export const unwrapAndStore = async (wrap: SignedEvent) => {
} }
} }
} }
},
})
export const unwrapAndStore = async (wrap: SignedEvent) => {
if (wrap.kind !== WRAP) {
throw new Error("Tried to unwrap an invalid event")
}
if (!shouldUnwrap.get()) {
throw new Error("Discarding wrapped event because `shouldUnwrap` is not enabled")
}
if (!failedUnwraps.has(wrap.id) && !wrapManager.getRumor(wrap.id)) {
wrapQueue.push(wrap)
}
} }