1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::errors::JsonRpcError;
use std::sync::Arc;
use storage_interface::MoveDbReader;
use tokio::task::JoinHandle;

use crate::stream_rpc::{
    connection::ClientConnection,
    subscription_types::{Subscription, SubscriptionHelper},
    subscriptions::{EventsSubscription, TransactionsSubscription},
};
use diem_json_rpc_types::{stream::request::StreamMethodRequest, Id};

pub struct CallableStreamMethod(pub StreamMethodRequest);

impl CallableStreamMethod {
    pub fn call_method(
        self,
        db: Arc<dyn MoveDbReader>,
        client: ClientConnection,
        jsonrpc_id: Id,
    ) -> Result<JoinHandle<()>, JsonRpcError> {
        let method = self.0.method();
        let helper = SubscriptionHelper::new(db, client, jsonrpc_id, method);
        match self.0 {
            StreamMethodRequest::SubscribeToTransactions(params) => {
                TransactionsSubscription::default().run(helper, params)
            }
            StreamMethodRequest::SubscribeToEvents(params) => {
                EventsSubscription::default().run(helper, params)
            }
            // This is handled in the `handle_rpc_request` function, as we don't spawn a task
            StreamMethodRequest::Unsubscribe => unreachable!(),
        }
    }
}