Skip to content

Implement filter_map on watcher #14

@rklaehn

Description

@rklaehn

If you want to wait for a certain state, it would be neat to be able to not just map but also filter and filter_map on a watcher.

E.g. when waiting for a direct connection, this is currently only possible by manually looping through the stream:

let on_connected = |ep: Endpoint, conn: Connection| async move {
    let Ok(id) = conn.remote_node_id() else {
        return Err(io::Error::other("unable to get node id"));
    };
    let Some(watcher) = ep.conn_type(id) else {
        return Err(io::Error::other("unable to get conn_type watcher"));
    };
    let mut stream = watcher.stream();
    while let Some(status) = stream.next().await {
        if let ConnectionType::Direct { .. } = status {
            return Ok(());
        }
    }
    Err(io::Error::other("connection closed before becoming direct"))
};

Having filter_map would simplify this to:

let on_connected = |ep: Endpoint, conn: Connection| async move {
    let Ok(id) = conn.remote_node_id() else {
        return Err(io::Error::other("unable to get node id"));
    };
    let Some(watcher) = ep.conn_type(id) else {
        return Err(io::Error::other("unable to get conn_type watcher"));
    };
    watcher.filter(|t| t == ConnectionType::Direct).initialized().await;
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions