Skip to content

Commit

Permalink
feat(bindings): add messageCount API (#340)
Browse files Browse the repository at this point in the history
* feat(bindings): add messageCount API

* fix: fmt
  • Loading branch information
lucasfernog authored Feb 22, 2021
1 parent a164f4d commit ed74aaf
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changes/message-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"nodejs-binding": patch
---

Adds a `messageCount` function on the Account class.
1 change: 1 addition & 0 deletions bindings/nodejs/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export declare class Account {
index(): number;
alias(): string;
balance(): AccountBalance;
messageCount(): number;
listMessages(count?: number, from?: number, messageType?: MessageType): Message[]
listAddresses(unspent?: boolean): Address[]
sync(options?: SyncOptions): Promise<SyncedAccount>
Expand Down
11 changes: 11 additions & 0 deletions bindings/nodejs/native/src/classes/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ declare_types! {
Ok(neon_serde::to_value(&mut cx, &balance)?.upcast())
}

method messageCount(mut cx) {
let this = cx.this();
let id = cx.borrow(&this, |r| r.0.clone());
crate::block_on(async move {
let account_handle = crate::get_account(&id).await;
let account = account_handle.read().await;
let count = account.messages().len();
Ok(cx.number(count as f64).upcast())
})
}

method listMessages(mut cx) {
let count = match cx.argument_opt(0) {
Some(arg) => arg.downcast::<JsNumber>().or_throw(&mut cx)?.value() as usize,
Expand Down
1 change: 1 addition & 0 deletions bindings/nodejs/tests/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ async function run() {
}
})
console.log('messages', account.listMessages(0, 0, MessageType.Failed))
console.log(account.messageCount())
account.setAlias('new alias')

const savedAccount = manager.getAccount('new alias')
Expand Down
4 changes: 4 additions & 0 deletions bindings/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,10 @@ Updates the account's client options.
| ------- | -------------------------------------------- | ---------------------- | ------------------------- |
| options | <code>[ClientOptions](#clientoptions)</code> | <code>undefined</code> | The client options to set |

#### message_count(): int

Returns the number of messages associated with the account.

#### list_messages(count, from, message_type (optional)): list([WalletMessage](#walletmessage))

Get the list of messages of this account.
Expand Down
5 changes: 5 additions & 0 deletions bindings/python/native/src/classes/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ impl AccountHandle {
})?)
}

/// The number of messages associated with the account.
fn message_count(&self) -> usize {
crate::block_on(async { self.account_handle.read().await.messages().len() })
}

/// Bridge to [Account#list_messages](struct.Account.html#method.list_messages).
/// This method clones the account's messages so when querying a large list of messages
/// prefer using the `read` method to access the account instance.
Expand Down
4 changes: 4 additions & 0 deletions docs/libraries/nodejs/api_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ Returns the account's balance information object.

Balance object: { total: number, available: number, incoming: number, outgoing: number }

#### messageCount()

Returns the number of messages associated with the account.

#### listMessages([count, from, type])

Returns the account's messages.
Expand Down

0 comments on commit ed74aaf

Please sign in to comment.