Skip to content

Implement .as_weak() and .upgrade() for global component instances #9389

@bennysj

Description

@bennysj

Feature Description

Having the following global component:

export global BLogicAAPI {
    // A lot properties and etc ...

    callback update-data(bdata: BData);
}

From the application main.rs it's accesed via:

fn main() -> Result<(), Box<dyn Error>> {
    let ui = AppWindow::new()?;

    let blociga_api = ui.global::<blogica::backend::BLogicAAPI>();
    blogica::backend::init(&blociga_api);
}

Due to that the business logic of BLogicAAPI is implemented in it's own crate / module it does not know the AppWindow, and can't do ui.global::<blogica::backend::BLogicAAPI>(), so in lib.rs we would like to do something like:

    pub fn init(blogica_api: &BLogicAAPI) {
        blogica_api.on_update_data({
            let blogica_api_weak = blogica_api.as_weak();
            |bdata| {
                let blogica_api = blogica_api_weak.upgrade().unwrap();
                // Do stuff here with blogica_api
            }
        });
    }

Internally the global components are already Rc instances, so proving .as_weak() and .upgrade() functionality should be techincally possible, (just like with the AppWindow)

Relates to issue #7060

Product Impact

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions