Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(socketio/state): global state implementation backed by a state::TypeMap #194

Merged
merged 11 commits into from
Dec 11, 2023

Conversation

Totodore
Copy link
Owner

@Totodore Totodore commented Dec 11, 2023

This PR adds global state management as well as a State extractor to get the state in any handlers.

Note : Before merging this PR, it is necessary to use dynamics handler for the on_disconnect function. Otherwise it wont be possible to use the state in these handlers.

Currently it is not possible to use the references in extractors because of the following problem :
In the ConnectHandler implementation below for any function with arguments that implements FromConnectParts and are specified by values. To add a lifetime to extractors we could add a lifetime on the trait FromConnectParts. The problem is that the provided lifetime should be the one declared with the for<'a> corresponding to the function and its scope is rather limited.

impl<A, F, $($ty,)*> ConnectHandler<A, (private::Sync, $($ty,)*)> for F
        where
            F: for<'a> FnOnce($($ty,)*) + Send + Sync + Clone + 'static,  // The lifetime 'a should be propagated to the `FromConnectParts` 
            A: Adapter,                                                   // ---------------------------------------
            $( $ty: FromConnectParts<'a, A> + Send, )*                    // <----- here
        {
            fn call(
                &self,
                s: Arc<Socket<A>>,
                auth: Option<String>,
                state: &Arc<dyn std::any::Any + Send + Sync>)
            {
                $(
                    let $ty = match $ty::from_connect_parts(&s, &auth,  state) {
                        Ok(v) => v,
                        Err(_e) => {
                            #[cfg(feature = "tracing")]
                            tracing::error!("Error while extracting data: {}", _e);
                            return;
                        },
                    };
                )*

                (self.clone())($($ty,)*);
            }
        }

Because of this issue the state will be static so that State extractors only contains &'static references and don't need lifetime propagation.

@Totodore Totodore enabled auto-merge December 11, 2023 15:24
@Totodore Totodore merged commit 91889b4 into main Dec 11, 2023
13 checks passed
Copy link
Contributor

@tausifcreates tausifcreates left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

@Totodore Totodore deleted the feat-state branch December 15, 2023 01:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

State Management
2 participants