UNPKG

@gear-js/api

Version:

A JavaScript library that provides functionality to connect GEAR Component APIs.

35 lines (32 loc) 1.36 kB
'use strict'; class GearMailbox { api; claimValue; constructor(api) { this.api = api; this.claimValue = api.claimValueFromMailbox; } async read(accountId, messageIdOrNumberOfMessages) { const [messageId, numberOfMessages] = typeof messageIdOrNumberOfMessages === 'string' ? [messageIdOrNumberOfMessages, undefined] : [undefined, messageIdOrNumberOfMessages || 1000]; if (messageId) { const mailbox = await this.api.query.gearMessenger.mailbox(accountId, messageId); const typedMailbox = this.api.createType('Option<(UserStoredMessage, Interval)>', mailbox); return typedMailbox.unwrapOr(null); } else { const keyPrefixes = this.api.query.gearMessenger.mailbox.keyPrefix(accountId); const keysPaged = await this.api.rpc.state.getKeysPaged(keyPrefixes, numberOfMessages, keyPrefixes); if (keysPaged.length === 0) { return []; } const mailbox = (await this.api.rpc.state.queryStorageAt(keysPaged)); return mailbox.map((item) => { const typedItem = this.api.createType('Option<(UserStoredMessage, Interval)>', item); return typedItem.unwrapOr(null); }); } } } exports.GearMailbox = GearMailbox;