Calling MainComponent method from external library callback

Hey,

I have this library that notifies me about different events. So from time to time I’ll get a callback like:

void onEvent(Type type, int id)
{
   //here I want to update something in MainComponent
}

how can I do something like:
MyJuceApplication::getMainComponent()->updateView(type, id);

Thanks
A

Typically you create a listener interface in your library that the MainComponent implements or you use a lambda.

class Library
{
public:
    struct Listener
    {
        virtual void libraryDidSomething() = 0;
    };
}

Or lambda:

class Library
{
public:
    std::function<void()> onEvent;
};

// call it
if (onEvent) onEvent();

// set it
library.onEvent = [this] 
{
    // call whatever you need. Don't forget to clear the lambda in your destructor!
};

Hope that helps