Skip to main content

plexus_core/plexus/
context.rs

1//! Global Plexus RPC context for sharing state with activations
2//!
3//! This module provides a thread-safe way to share the Plexus RPC server hash
4//! with all activations. The hash is set once during hub initialization
5//! and can be read by any activation when creating stream items.
6
7use std::sync::OnceLock;
8
9/// Global Plexus RPC context singleton
10static PLEXUS_CONTEXT: OnceLock<PlexusContext> = OnceLock::new();
11
12/// Shared Plexus RPC context accessible to all activations
13#[derive(Debug, Clone)]
14pub struct PlexusContext {
15    /// Hash of all activations for cache invalidation
16    pub hash: String,
17}
18
19impl PlexusContext {
20    /// Initialize the global Plexus RPC context
21    ///
22    /// This should be called once during hub initialization.
23    /// Subsequent calls will be ignored.
24    pub fn init(hash: String) {
25        let _ = PLEXUS_CONTEXT.set(PlexusContext { hash });
26    }
27
28    /// Get the global Plexus RPC server hash
29    ///
30    /// Returns an empty string if the context hasn't been initialized yet.
31    pub fn hash() -> String {
32        PLEXUS_CONTEXT
33            .get()
34            .map(|ctx| ctx.hash.clone())
35            .unwrap_or_default()
36    }
37}