Skip to content

Commit

Permalink
fix(dirmonitor): avoid calling the change callback multiple times in …
Browse files Browse the repository at this point in the history
…the same notification
  • Loading branch information
Guldoman committed Jun 22, 2024
1 parent 290c7bc commit 5032037
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/api/dirmonitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,19 @@ int get_mode_dirmonitor();


static int f_check_dir_callback(int watch_id, const char* path, void* L) {
// using absolute indices from f_dirmonitor_check (2: callback, 3: error_callback)
// using absolute indices from f_dirmonitor_check (2: callback, 3: error_callback, 4: watch_id notified table)

// Check if we already notified about this watch
lua_geti(L, 4, watch_id);
bool skip = !lua_isnoneornil(L, -1);
lua_pop(L, 1);
if (skip) return 0;

// Set watch as notified
lua_pushboolean(L, true);
lua_seti(L, 4, watch_id);

// Prepare callback call
lua_pushvalue(L, 2);
if (path)
lua_pushlstring(L, path, watch_id);
Expand Down Expand Up @@ -117,6 +129,9 @@ static int f_dirmonitor_check(lua_State* L) {
if (monitor->length < 0)
lua_pushnil(L);
else if (monitor->length > 0) {
// Create a table for keeping track of what watch ids were notified in this check,
// so that we avoid notifying multiple times.
lua_newtable(L);
if (translate_changes_dirmonitor(monitor->internal, monitor->buffer, monitor->length, f_check_dir_callback, L) == 0)
monitor->length = 0;
lua_pushboolean(L, 1);
Expand Down

0 comments on commit 5032037

Please sign in to comment.