Skip to content

Commit

Permalink
watcher-nodejs: keep object field ordering consistent with c struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Will committed Oct 18, 2024
1 parent dffab35 commit 20ba1de
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
18 changes: 9 additions & 9 deletions watcher-nodejs/lib/watcher-napi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions watcher-nodejs/lib/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down

0 comments on commit 20ba1de

Please sign in to comment.