Common Pitfalls
The API aims to be easy to use and hard to misuse, however there are still some pitfalls saucer can't detect.
This page tries to document the most common mistakes and how to avoid them.
Execution Order Matters
The order in which you call some methods matters!
Common mistakes that fall under this category are those, in which you call set_url
(or serve
) before expose
.
#include <saucer/smartview.hpp>
int main()
{
auto app = saucer::application::acquire({
.id = "pitfalls",
});
saucer::smartview webview{{
.application = app,
}};
webview.set_size(500, 600);
webview.set_title("Hello World!");
webview.set_url("https://github.com");
webview.expose(
"add_random",
[&](float number) {
auto random = webview.evaluate<float>("Math.random()").get();
return number + random;
},
saucer::launch::async);
webview.show();
app->run();
return 0;
}
In this case, it is not guaranteed that the function add_random
is available to the web-page immediately.
To fix this issue simply re-order the code.
int main()
{
auto app = saucer::application::acquire({
.id = "pitfalls",
});
saucer::smartview webview{{
.application = app,
}};
webview.set_size(500, 600);
webview.set_title("Hello World!");
webview.set_url("https://github.com");
webview.expose(
"add_random",
[&](float number) {
auto random = webview.evaluate<float>("Math.random()").get();
return number + random;
},
saucer::launch::async);
webview.set_url("https://github.com");
webview.show();
app->run();
return 0;
}
Run is blocking
A call to run()
is blocking.
All code that comes after it will only be executed once the last window is closed.
Check the attributes of the public interface.
They may contain useful information, like in this case, that run is potentially blocking:
template <bool Blocking = true>
[[sc::may_block]] void run() const;
int main()
{
auto app = saucer::application::acquire({
.id = "pitfalls",
});
saucer::smartview webview{{
.application = app,
}};
webview.set_size(500, 600);
webview.set_title("Hello World!");
webview.show();
app->run();
webview.set_url("https://github.com");
return 0;
}
The fix for this should be obvious:
int main()
{
auto app = saucer::application::acquire({
.id = "pitfalls",
});
saucer::smartview webview{{
.application = app,
}};
webview.set_size(500, 600);
webview.set_title("Hello World!");
webview.set_url("https://github.com");
webview.show();
app->run();
webview.set_url("https://github.com");
return 0;
}