diff --git a/src/stores/App.js b/src/stores/App.js index 5857658..c5cee15 100644 --- a/src/stores/App.js +++ b/src/stores/App.js @@ -1,6 +1,8 @@ import {defineStore} from 'pinia' import {useSettingsStore} from 'stores/Settings' import {useNostrStore} from 'src/nostr/NostrStore' +import {markRaw} from 'vue' +import JobQueue from 'src/utils/JobQueue' export const useAppStore = defineStore('app', { state: () => ({ @@ -13,6 +15,7 @@ export const useAppStore = defineStore('app', { open: false, params: {}, }, + queue: markRaw(new JobQueue()), }), getters: { activeAccount() { @@ -28,11 +31,13 @@ export const useAppStore = defineStore('app', { }, actions: { signIn(fragment = 'welcome') { - return new Promise(resolve => { - this.signInDialog.callback = resolve - this.signInDialog.fragment = fragment - this.signInDialog.open = true - }) + return this.queue.push( + () => new Promise(resolve => { + this.signInDialog.callback = resolve + this.signInDialog.fragment = fragment + this.signInDialog.open = true + }) + ) }, signInIfNeeded(fragment = 'welcome') { if (this.isSignedIn) return Promise.resolve(true) @@ -52,17 +57,17 @@ export const useAppStore = defineStore('app', { async signEvent(event) { if (!await this.signInIfNeeded()) return if (!this.activeAccount.canSign() && !await this.signIn('private-key')) return - return this.activeAccount.sign(event) + return await this.activeAccount.sign(event) }, async decryptMessage(pubkey, content) { if (!await this.signInIfNeeded()) return if (!this.activeAccount.canDecrypt() && !await this.signIn('private-key')) return - return this.activeAccount.decrypt(pubkey, content) + return await this.activeAccount.decrypt(pubkey, content) }, async encryptMessage(pubkey, content) { if (!await this.signInIfNeeded()) return if (!this.activeAccount.canEncrypt() && !await this.signIn('private-key')) return - return this.activeAccount.encrypt(pubkey, content) + return await this.activeAccount.encrypt(pubkey, content) }, }, }) diff --git a/src/utils/JobQueue.js b/src/utils/JobQueue.js new file mode 100644 index 0000000..ecca078 --- /dev/null +++ b/src/utils/JobQueue.js @@ -0,0 +1,29 @@ +export default class JobQueue { + constructor() { + this.queue = [] + this.working = false + } + + push(fn) { + return new Promise((resolve, reject) => { + this.queue.push({fn: fn, resolve: resolve, reject: reject}) + if (!this.working) { + this.work().catch(e => console.error(e)) + } + }) + } + + async work() { + this.working = true + while (this.queue.length > 0) { + const job = this.queue.shift() + try { + const result = await job.fn() + job.resolve(result) + } catch (e) { + if (job.reject) job.reject(e) + } + } + this.working = false + } +} diff --git a/src/utils/Nip07.js b/src/utils/Nip07.js index cf6ff2c..080e672 100644 --- a/src/utils/Nip07.js +++ b/src/utils/Nip07.js @@ -1,3 +1,5 @@ +import JobQueue from 'src/utils/JobQueue' + export default class Nip07 { static isAvailable() { return !!window.nostr @@ -9,21 +11,22 @@ export default class Nip07 { static getPublicKey() { Nip07.enforceAvailable() - return window.nostr.getPublicKey() + return Nip07.queue.push(() => window.nostr.getPublicKey()) } static signEvent(event) { Nip07.enforceAvailable() - return window.nostr.signEvent(event) + return Nip07.queue.push(() => window.nostr.signEvent(event)) } static encrypt(pubkey, plaintext) { Nip07.enforceAvailable() - return window.nostr.nip04.encrypt(pubkey, plaintext) + return Nip07.queue.push(() => window.nostr.nip04.encrypt(pubkey, plaintext)) } static decrypt(pubkey, ciphertext) { Nip07.enforceAvailable() - return window.nostr.nip04.decrypt(pubkey, ciphertext) + return Nip07.queue.push(() => window.nostr.nip04.decrypt(pubkey, ciphertext)) } } +Nip07.queue = new JobQueue()