Skip to content
Snippets Groups Projects
Commit 3c48177e authored by Éloïs's avatar Éloïs
Browse files

[ref] gva: fusion sub newBlockMeta with newBlock

parent 339cf597
No related branches found
No related tags found
No related merge requests found
...@@ -16,43 +16,52 @@ ...@@ -16,43 +16,52 @@
use super::create_subscription; use super::create_subscription;
use crate::*; use crate::*;
use duniter_dbs::databases::cm_v1::{CmV1DbReadable, CurrentBlockEvent, CurrentBlockMetaEvent}; use duniter_dbs::databases::cm_v1::{CmV1DbReadable, CurrentBlockEvent, CurrentBlockMetaEvent};
use futures::future::Either;
#[derive(Clone, Copy, Default)] #[derive(Clone, Copy, Default)]
pub struct NewBlocksSubscription; pub struct NewBlocksSubscription;
#[async_graphql::Subscription] #[async_graphql::Subscription]
impl NewBlocksSubscription { impl NewBlocksSubscription {
async fn new_blocks_meta( async fn new_blocks(
&self, &self,
ctx: &async_graphql::Context<'_>, ctx: &async_graphql::Context<'_>,
) -> impl Stream<Item = async_graphql::Result<Vec<BlockMeta>>> { ) -> impl Stream<Item = async_graphql::Result<Vec<Block>>> {
let meta_only = !(ctx.look_ahead().field("identities").exists()
|| ctx.look_ahead().field("joiners").exists()
|| ctx.look_ahead().field("actives").exists()
|| ctx.look_ahead().field("leavers").exists()
|| ctx.look_ahead().field("revoked").exists()
|| ctx.look_ahead().field("excluded").exists()
|| ctx.look_ahead().field("certifications").exists()
|| ctx.look_ahead().field("transactions").exists());
if meta_only {
Either::Left(
create_subscription( create_subscription(
ctx, ctx,
|dbs| dbs.cm_db.current_block_meta(), |dbs| dbs.cm_db.current_block_meta(),
|events| { |events| {
let mut blocks_meta = Vec::new(); let mut blocks = Vec::new();
for event in events.deref() { for event in events.deref() {
if let CurrentBlockMetaEvent::Upsert { if let CurrentBlockMetaEvent::Upsert {
value: ref block_meta, value: ref block_meta,
.. ..
} = event } = event
{ {
blocks_meta.push(BlockMeta::from(block_meta)); blocks.push(Block::from(block_meta));
} }
} }
if blocks_meta.is_empty() { if blocks.is_empty() {
futures::future::ready(None) futures::future::ready(None)
} else { } else {
futures::future::ready(Some(Ok(blocks_meta))) futures::future::ready(Some(Ok(blocks)))
} }
}, },
) )
.await .await,
} )
async fn new_blocks( } else {
&self, Either::Right(
ctx: &async_graphql::Context<'_>,
) -> impl Stream<Item = async_graphql::Result<Vec<Block>>> {
create_subscription( create_subscription(
ctx, ctx,
|dbs| dbs.cm_db.current_block(), |dbs| dbs.cm_db.current_block(),
...@@ -73,6 +82,8 @@ impl NewBlocksSubscription { ...@@ -73,6 +82,8 @@ impl NewBlocksSubscription {
} }
}, },
) )
.await .await,
)
}
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment