From 20ba1de685ce4a355cfbc0eb00c898db07c1b0ea Mon Sep 17 00:00:00 2001 From: Will Date: Thu, 17 Oct 2024 22:19:18 -0400 Subject: [PATCH] watcher-nodejs: keep object field ordering consistent with c struct --- watcher-nodejs/lib/watcher-napi.cpp | 18 +++++++++--------- watcher-nodejs/lib/watcher.ts | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/watcher-nodejs/lib/watcher-napi.cpp b/watcher-nodejs/lib/watcher-napi.cpp index f47360b3..83460553 100644 --- a/watcher-nodejs/lib/watcher-napi.cpp +++ b/watcher-nodejs/lib/watcher-napi.cpp @@ -25,26 +25,26 @@ struct watcher_lifetime { static napi_value event_to_js_obj(napi_env env, wtr_watcher_event* event) { napi_value event_obj = NULL; + napi_value effect_time = NULL; napi_value path_name = NULL; + napi_value associated_path_name = NULL; napi_value effect_type = NULL; napi_value path_type = NULL; - napi_value effect_time = NULL; - napi_value associated_path_name = NULL; napi_create_object(env, &event_obj); - napi_create_string_utf8(env, event->path_name, NAPI_AUTO_LENGTH, &path_name); - napi_create_int32(env, event->effect_type, &effect_type); - napi_create_int32(env, event->path_type, &path_type); napi_create_bigint_int64(env, event->effect_time, &effect_time); + napi_create_string_utf8(env, event->path_name, NAPI_AUTO_LENGTH, &path_name); if (event->associated_path_name) { napi_create_string_utf8(env, event->associated_path_name, NAPI_AUTO_LENGTH, &associated_path_name); } else { napi_get_null(env, &associated_path_name); } + napi_create_int32(env, event->effect_type, &effect_type); + napi_create_int32(env, event->path_type, &path_type); + napi_set_named_property(env, event_obj, "effectTime", effect_time); napi_set_named_property(env, event_obj, "pathName", path_name); + napi_set_named_property(env, event_obj, "associatedPathName", associated_path_name); napi_set_named_property(env, event_obj, "effectType", effect_type); napi_set_named_property(env, event_obj, "pathType", path_type); - napi_set_named_property(env, event_obj, "effectTime", effect_time); - napi_set_named_property(env, event_obj, "associatedPathName", associated_path_name); return event_obj; } @@ -74,11 +74,11 @@ static void callback_bridge(struct wtr_watcher_event event_view, void* ctx) struct watcher_lifetime* w = (struct watcher_lifetime*)ctx; #if WITH_TSFN_NONBLOCKING wtr_watcher_event* event_owned = (wtr_watcher_event*)malloc(sizeof(wtr_watcher_event)); + event_owned->effect_time = event_view.effect_time; event_owned->path_name = event_view.path_name ? strdup(event_view.path_name) : NULL; + event_owned->associated_path_name = event_view.associated_path_name ? strdup(event_view.associated_path_name) : NULL; event_owned->effect_type = event_view.effect_type; event_owned->path_type = event_view.path_type; - event_owned->effect_time = event_view.effect_time; - event_owned->associated_path_name = event_view.associated_path_name ? strdup(event_view.associated_path_name) : NULL; if (w->tsfn) { napi_call_threadsafe_function(w->tsfn, event_owned, napi_tsfn_nonblocking); } diff --git a/watcher-nodejs/lib/watcher.ts b/watcher-nodejs/lib/watcher.ts index a227a041..5b1cb222 100644 --- a/watcher-nodejs/lib/watcher.ts +++ b/watcher-nodejs/lib/watcher.ts @@ -28,18 +28,18 @@ export interface Event { interface CEvent { pathName: string; + associatedPathName: string | null; effectType: number; pathType: number; - associatedPathName: string | null; } export const watch = (path: string, cb: (event: Event) => void): { close: () => boolean } => { let typedCb: null | ((_: CEvent) => void) = (cEvent) => { let event: Event = { pathName: cEvent.pathName, + associatedPathName: cEvent.associatedPathName, effectType: Object.keys(EffectType)[cEvent.effectType] as EffectType, pathType: Object.keys(PathType)[cEvent.pathType] as PathType, - associatedPathName: cEvent.associatedPathName, }; cb(event); };