From ca75f6128964dd49e8a6e2656cbbff54206a4d9f Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Fri, 23 Jul 2021 20:04:32 +0000 Subject: [PATCH] Check for pending events before updating the config Small optimisation that significantly reduces number of config updates. --- api.cpp | 13 ++++++++++++- ui/index.js | 7 ++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/api.cpp b/api.cpp index 9294e80..a0f585e 100644 --- a/api.cpp +++ b/api.cpp @@ -60,6 +60,16 @@ class Context { }); } + bool hasPendingEvent() { + return gpp_rethrow([=]() { + CameraEventType event_type = GP_EVENT_UNKNOWN; + gpp_unique_ptr event_data(GPP_CALL( + void *, gp_camera_wait_for_event(camera.get(), 0, &event_type, _, + context.get()))); + return event_type != GP_EVENT_TIMEOUT; + }); + } + val configToJS() { return gpp_rethrow([=]() { GPPWidget config( @@ -277,5 +287,6 @@ EMSCRIPTEN_BINDINGS(gphoto2_js_api) { .function("configToJS", &Context::configToJS) .function("setConfigValue", &Context::setConfigValue) .function("capturePreviewAsBlob", &Context::capturePreviewAsBlob) - .function("captureImageAsFile", &Context::captureImageAsFile); + .function("captureImageAsFile", &Context::captureImageAsFile) + .function("hasPendingEvent", &Context::hasPendingEvent); } diff --git a/ui/index.js b/ui/index.js index 737610e..1a7b78e 100644 --- a/ui/index.js +++ b/ui/index.js @@ -79,7 +79,12 @@ class App extends Component { type: 'Config', config }); - await new Promise(resolve => setTimeout(resolve, 100)); + do { + await new Promise(resolve => setTimeout(resolve, 100)); + } while ( + !(await this.connection.schedule(context => context.hasPendingEvent())) + ); + console.log('Event!'); } }