From 3c48177ea6b65713b9c1a5de832ebc0d8717b953 Mon Sep 17 00:00:00 2001
From: librelois <c@elo.tf>
Date: Fri, 18 Dec 2020 01:19:00 +0100
Subject: [PATCH] [ref] gva: fusion sub newBlockMeta with newBlock

---
 .../gva/gql/src/subscriptions/new_blocks.rs   | 107 ++++++++++--------
 1 file changed, 59 insertions(+), 48 deletions(-)

diff --git a/rust-libs/modules/gva/gql/src/subscriptions/new_blocks.rs b/rust-libs/modules/gva/gql/src/subscriptions/new_blocks.rs
index c61008b60..e6e2a53f1 100644
--- a/rust-libs/modules/gva/gql/src/subscriptions/new_blocks.rs
+++ b/rust-libs/modules/gva/gql/src/subscriptions/new_blocks.rs
@@ -16,63 +16,74 @@
 use super::create_subscription;
 use crate::*;
 use duniter_dbs::databases::cm_v1::{CmV1DbReadable, CurrentBlockEvent, CurrentBlockMetaEvent};
+use futures::future::Either;
 
 #[derive(Clone, Copy, Default)]
 pub struct NewBlocksSubscription;
 
 #[async_graphql::Subscription]
 impl NewBlocksSubscription {
-    async fn new_blocks_meta(
-        &self,
-        ctx: &async_graphql::Context<'_>,
-    ) -> impl Stream<Item = async_graphql::Result<Vec<BlockMeta>>> {
-        create_subscription(
-            ctx,
-            |dbs| dbs.cm_db.current_block_meta(),
-            |events| {
-                let mut blocks_meta = Vec::new();
-                for event in events.deref() {
-                    if let CurrentBlockMetaEvent::Upsert {
-                        value: ref block_meta,
-                        ..
-                    } = event
-                    {
-                        blocks_meta.push(BlockMeta::from(block_meta));
-                    }
-                }
-                if blocks_meta.is_empty() {
-                    futures::future::ready(None)
-                } else {
-                    futures::future::ready(Some(Ok(blocks_meta)))
-                }
-            },
-        )
-        .await
-    }
     async fn new_blocks(
         &self,
         ctx: &async_graphql::Context<'_>,
     ) -> impl Stream<Item = async_graphql::Result<Vec<Block>>> {
-        create_subscription(
-            ctx,
-            |dbs| dbs.cm_db.current_block(),
-            |events| {
-                let mut blocks = Vec::new();
-                for event in events.deref() {
-                    if let CurrentBlockEvent::Upsert {
-                        value: ref block, ..
-                    } = event
-                    {
-                        blocks.push(Block::from(&block.0));
-                    }
-                }
-                if blocks.is_empty() {
-                    futures::future::ready(None)
-                } else {
-                    futures::future::ready(Some(Ok(blocks)))
-                }
-            },
-        )
-        .await
+        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(
+                    ctx,
+                    |dbs| dbs.cm_db.current_block_meta(),
+                    |events| {
+                        let mut blocks = Vec::new();
+                        for event in events.deref() {
+                            if let CurrentBlockMetaEvent::Upsert {
+                                value: ref block_meta,
+                                ..
+                            } = event
+                            {
+                                blocks.push(Block::from(block_meta));
+                            }
+                        }
+                        if blocks.is_empty() {
+                            futures::future::ready(None)
+                        } else {
+                            futures::future::ready(Some(Ok(blocks)))
+                        }
+                    },
+                )
+                .await,
+            )
+        } else {
+            Either::Right(
+                create_subscription(
+                    ctx,
+                    |dbs| dbs.cm_db.current_block(),
+                    |events| {
+                        let mut blocks = Vec::new();
+                        for event in events.deref() {
+                            if let CurrentBlockEvent::Upsert {
+                                value: ref block, ..
+                            } = event
+                            {
+                                blocks.push(Block::from(&block.0));
+                            }
+                        }
+                        if blocks.is_empty() {
+                            futures::future::ready(None)
+                        } else {
+                            futures::future::ready(Some(Ok(blocks)))
+                        }
+                    },
+                )
+                .await,
+            )
+        }
     }
 }
-- 
GitLab