Closed cpq closed 5 months ago
I also use delay after setting some configurations, where I want to write a record of a parameter set (eg. calibration information) which includes multiple (approx 30) separate variables. I set a delay after one of the calibration values gets set, and after the delay I write the calibration adjustment record (trusting that setting the other values has completed in this time). If I do it once for every value that gets set I will write many records, and if I do it in only one variable callback without a delay callback the order is unknown, and some values may not have been set yet.
I guess the right way is just to document the API for delayed execution
@dvosully what do you think would be a good API for that? How about this ?
void glue_delayed_call(void (*func)(void *), void *param, unsigned long milliseconds);
// Example
glue_call_delayed_call((void (*)(void *)) reboot, NULL, 500);
That would cover all my use cases, I think that makes sense.
The other alternative is to expose the struct mg_mgr g_mgr
variable from the mongoose_impl.c
file, and use mg_timer_add()
mg_timer_add(&g_mgr, 500, 0, my_func, NULL); // Run my_func after 500 ms delay
I don't have a strong opinion there. Either would cover my use cases. Moving g_mgr to global scope would also allow the user to add their own handlers and custom protocols (in my case a POST upload) without needing to modify mongoose_impl.c which would be convenient. It feels like a small change which puts a fair bit of power back in the hands of the programmer (if they choose to use it) while also not overwhelming the novice, which feels nicely balanced to me... Either way I trust your judgement.
@dvosully exported g_mgr
, added docs snippet https://mongoose.ws/documentation/#execute-functions-after-a-delay
Please test & feedback
Thanks Sergey, tested and working for me.
@dvosully is this the only use case that requires delay, or are there any others?