Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/zjs_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#define DEFAULT_MAX_LISTENERS 10

static jerry_value_t zjs_event_emitter_prototype = 0;
zjs_callback_id emit_id = -1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

making this global doesn't it mean that it can be overwritten with multiple events from different modules?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was already global... I just moved it up from down below because it's lifetime is now tied to that to that of the prototype defined just above.

However, it could probably be static...


typedef struct listener {
jerry_value_t func;
Expand Down Expand Up @@ -43,6 +44,8 @@ static void free_listener(void *ptr)
static void zjs_event_proto_free_cb(void *native)
{
zjs_event_emitter_prototype = 0;
zjs_remove_callback(emit_id);
emit_id = -1;
}

static const jerry_object_native_info_t event_proto_type_info = {
Expand Down Expand Up @@ -308,8 +311,6 @@ static ZJS_DECL_FUNC(get_listeners)
return rval;
}

zjs_callback_id emit_id = -1;

typedef struct emit_event {
jerry_value_t obj;
zjs_pre_emit pre;
Expand Down Expand Up @@ -468,16 +469,17 @@ static void zjs_event_init_prototype() {
};
zjs_event_emitter_prototype = zjs_create_object();
zjs_obj_add_functions(zjs_event_emitter_prototype, array);
jerry_set_object_native_pointer(zjs_event_emitter_prototype, NULL, &event_proto_type_info);
jerry_set_object_native_pointer(zjs_event_emitter_prototype, NULL,
&event_proto_type_info);
jerry_release_value(zjs_event_emitter_prototype);
emit_id = zjs_add_c_callback(NULL, emit_event_callback);
}
}

void zjs_make_emitter(jerry_value_t obj, jerry_value_t prototype,
void *user_data, zjs_event_free free_cb)
{
zjs_event_init_prototype();
emit_id = zjs_add_c_callback(NULL, emit_event_callback);
jerry_value_t proto = zjs_event_emitter_prototype;
if (jerry_value_is_object(prototype)) {
jerry_set_prototype(prototype, proto);
Expand Down