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

Draft / Suggestion: Bot shared state available via context #57

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ivan-jukic
Copy link
Contributor

Allows bots to hold shared state on the bot context.

Commands would be able to access this state within their execute handlers as ctx.shared_state. Command definition structs would only need to reference local state.

This would provide SDK support for shared state, though making the type signatures somewhat more complex. For not shared state we'd use unit type ().

(breaking change)

---
Allows bots to hold shared state on the bot context. Commands would be
able to access this state within their `execute` handlers as
`ctx.shared_state`.

This would provide SDK support for shared state, though making the type
signatures somewhat more complex.
@@ -55,8 +55,10 @@ impl<R> CommandHandlerRegistry<R> {
jwt: &str,
public_key: &str,
now: TimestampMillis,
shared_state: S,
Copy link
Contributor

Choose a reason for hiding this comment

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

If we have to pass in the shared state every time that would look messy for the majority of use cases where the shared state will be ().
So maybe we instead hold the shared state in the CommandHandlerRegistry?
Then it automatically passes it through to command_handler.execute.
Or are you envisioning potentially passing in different instances, but the type always remains the same?

on_set_api_key: Option<Box<dyn Fn(String) -> CommandResponse + Send + Sync + 'static>>,
oc_client_factory: Arc<ClientFactory<R>>,
}

static SET_API_KEY_PARAMS: LazyLock<Vec<BotCommandParam>> = LazyLock::new(set_api_key_params);

impl<R> CommandHandlerRegistry<R> {
pub fn new(oc_client_factory: Arc<ClientFactory<R>>) -> CommandHandlerRegistry<R> {
impl<R, S> CommandHandlerRegistry<R, S> {
Copy link
Contributor

Choose a reason for hiding this comment

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

If we were to hold the state on the CommandHandlerRegistry then we could set a default for S as S = (), and then expose a with_shared_state which would hand the value to CommandHandlerRegistry and also set the S generic type on the returned instance.

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.

2 participants