Skip to content

Introduce Custom shared data structure #427

Description

@kitgxrl

This idea was gotten from serenity-rs/serenity, while their implementation is much more nuanced I believe this can be simplified fairly heavily for most use cases.

The idea is to have some data structure that is shared amongst events, possibly accessed via socket.data() or something similar. This can indirectly solve #363 and #425

Some example code may be as such, taking example of #363:

struct MyData {
    tx: Sender<String>
}

fn main() {
    let (tx, rx) = mpsc::channel::<String>():

    let my_data = Arc::new(MyData {
        tx
    });

    let callback1 = |payload: Payload, socket: RawClient| {
        let data: Arc<MyData> = socket.data();

        data.tx.send("...".to_string());
    };

    let callback2 = |payload: Payload, socket: RawClient| {
        let data: Arc<MyData> = socket.data();

        data.tx.send("...".to_string());
    };

    let socket = ClientBuilder::new("...")
        .on("event1", callback1)
        .on("event2", callback2)
        .data(my_data)
        .connect()
        .expect("Connection failed");
}

I am absolutely willing to work on this, it is exam week at my college so it might be a week or two before I can get around to it but this implementation would greatly help in cases where shared data is absolutely required.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions