Skip to content

Commit

Permalink
Use native teardown API (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager authored Nov 26, 2024
1 parent 156d32e commit 8f5ec5e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 68 deletions.
4 changes: 0 additions & 4 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,3 @@ BinPackParameters: false
ContinuationIndentWidth: 2
ColumnLimit: 0
SpaceAfterCStyleCast: true
SpaceBeforeParens: Custom
SpaceBeforeParensOptions:
AfterFunctionDeclarationName: true
AfterFunctionDefinitionName: true
93 changes: 40 additions & 53 deletions binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@ typedef struct {
int active_handles;

js_env_t *env;
js_ref_t *ctx;
js_ref_t *on_timer;
js_ref_t *on_check;

int64_t next_delay;

js_deferred_teardown_t *teardown;
} bare_timer_t;

static void
bare_timers__on_idle (uv_idle_t *handle) {}
bare_timers__on_idle(uv_idle_t *handle) {}

static void
bare_timers__on_check (uv_check_t *handle) {
bare_timers__on_check(uv_check_t *handle) {
int err;

bare_timer_t *self = (bare_timer_t *) handle->data;
Expand Down Expand Up @@ -54,7 +57,7 @@ bare_timers__on_check (uv_check_t *handle) {
}

static void
bare_timers__on_timer (uv_timer_t *handle) {
bare_timers__on_timer(uv_timer_t *handle) {
int err;

bare_timer_t *self = (bare_timer_t *) handle->data;
Expand Down Expand Up @@ -99,7 +102,7 @@ bare_timers__on_timer (uv_timer_t *handle) {
}

static void
bare_timers__on_close (uv_handle_t *handle) {
bare_timers__on_close(uv_handle_t *handle) {
int err;

bare_timer_t *self = (bare_timer_t *) handle->data;
Expand All @@ -111,10 +114,25 @@ bare_timers__on_close (uv_handle_t *handle) {

err = js_delete_reference(self->env, self->on_check);
assert(err == 0);

err = js_delete_reference(self->env, self->ctx);
assert(err == 0);

err = js_finish_deferred_teardown_callback(self->teardown);
assert(err == 0);
}

static void
bare_timers__on_teardown(js_deferred_teardown_t *handle, void *data) {
bare_timer_t *self = (bare_timer_t *) data;

uv_close((uv_handle_t *) &self->timer, bare_timers__on_close);
uv_close((uv_handle_t *) &self->check, bare_timers__on_close);
uv_close((uv_handle_t *) &self->idle, bare_timers__on_close);
}

static js_value_t *
bare_timers_init (js_env_t *env, js_callback_info_t *info) {
bare_timers_init(js_env_t *env, js_callback_info_t *info) {
int err;

size_t argc = 2;
Expand Down Expand Up @@ -143,67 +161,37 @@ bare_timers_init (js_env_t *env, js_callback_info_t *info) {
self->idle.data = self;

err = uv_timer_init(loop, &self->timer);
if (err < 0) {
js_throw_error(env, uv_err_name(err), uv_strerror(err));
return NULL;
}

self->active_handles++;
assert(err == 0);

err = uv_check_init(loop, &self->check);
if (err < 0) {
js_throw_error(env, uv_err_name(err), uv_strerror(err));
return NULL;
}

self->active_handles++;
assert(err == 0);

err = uv_idle_init(loop, &self->idle);
if (err < 0) {
js_throw_error(env, uv_err_name(err), uv_strerror(err));
return NULL;
}
assert(err == 0);

self->active_handles++;
self->active_handles = 3;

uv_unref((uv_handle_t *) &self->timer);
uv_unref((uv_handle_t *) &self->check);
uv_unref((uv_handle_t *) &self->idle);

err = js_create_reference(env, argv[0], 1, &self->on_timer);
err = js_add_deferred_teardown_callback(env, bare_timers__on_teardown, (void *) self, &self->teardown);
assert(err == 0);

err = js_create_reference(env, argv[1], 1, &self->on_check);
err = js_create_reference(env, handle, 1, &self->ctx);
assert(err == 0);

return handle;
}

static js_value_t *
bare_timers_destroy (js_env_t *env, js_callback_info_t *info) {
int err;

size_t argc = 1;
js_value_t *argv[1];

err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
err = js_create_reference(env, argv[0], 1, &self->on_timer);
assert(err == 0);

assert(argc == 1);

bare_timer_t *self;
err = js_get_arraybuffer_info(env, argv[0], (void **) &self, NULL);
err = js_create_reference(env, argv[1], 1, &self->on_check);
assert(err == 0);

uv_close((uv_handle_t *) &self->timer, bare_timers__on_close);
uv_close((uv_handle_t *) &self->check, bare_timers__on_close);
uv_close((uv_handle_t *) &self->idle, bare_timers__on_close);

return NULL;
return handle;
}

static js_value_t *
bare_timers_pause (js_env_t *env, js_callback_info_t *info) {
bare_timers_pause(js_env_t *env, js_callback_info_t *info) {
int err;

size_t argc = 1;
Expand Down Expand Up @@ -232,7 +220,7 @@ bare_timers_pause (js_env_t *env, js_callback_info_t *info) {
}

static js_value_t *
bare_timers_resume (js_env_t *env, js_callback_info_t *info) {
bare_timers_resume(js_env_t *env, js_callback_info_t *info) {
int err;

size_t argc = 3;
Expand Down Expand Up @@ -273,7 +261,7 @@ bare_timers_resume (js_env_t *env, js_callback_info_t *info) {
}

static js_value_t *
bare_timers_ref (js_env_t *env, js_callback_info_t *info) {
bare_timers_ref(js_env_t *env, js_callback_info_t *info) {
int err;

size_t argc = 1;
Expand All @@ -296,7 +284,7 @@ bare_timers_ref (js_env_t *env, js_callback_info_t *info) {
}

static js_value_t *
bare_timers_unref (js_env_t *env, js_callback_info_t *info) {
bare_timers_unref(js_env_t *env, js_callback_info_t *info) {
int err;

size_t argc = 1;
Expand All @@ -319,7 +307,7 @@ bare_timers_unref (js_env_t *env, js_callback_info_t *info) {
}

static js_value_t *
bare_timers_start (js_env_t *env, js_callback_info_t *info) {
bare_timers_start(js_env_t *env, js_callback_info_t *info) {
int err;

size_t argc = 2;
Expand Down Expand Up @@ -350,7 +338,7 @@ bare_timers_start (js_env_t *env, js_callback_info_t *info) {
}

static js_value_t *
bare_timers_stop (js_env_t *env, js_callback_info_t *info) {
bare_timers_stop(js_env_t *env, js_callback_info_t *info) {
int err;

size_t argc = 1;
Expand All @@ -375,7 +363,7 @@ bare_timers_stop (js_env_t *env, js_callback_info_t *info) {
}

static js_value_t *
bare_timers_immediate (js_env_t *env, js_callback_info_t *info) {
bare_timers_immediate(js_env_t *env, js_callback_info_t *info) {
int err;

size_t argc = 1;
Expand Down Expand Up @@ -406,7 +394,7 @@ bare_timers_immediate (js_env_t *env, js_callback_info_t *info) {
}

static js_value_t *
bare_timers_exports (js_env_t *env, js_value_t *exports) {
bare_timers_exports(js_env_t *env, js_value_t *exports) {
int err;

#define V(name, fn) \
Expand All @@ -419,7 +407,6 @@ bare_timers_exports (js_env_t *env, js_value_t *exports) {
}

V("init", bare_timers_init)
V("destroy", bare_timers_destroy)
V("ref", bare_timers_ref)
V("unref", bare_timers_unref)
V("start", bare_timers_start)
Expand Down
13 changes: 3 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,14 @@ const immediates = new TimerList(0)

const handle = binding.init(ontimer, onimmediate)

Bare.on('idle', pause).on('resume', resume).on('exit', destroy)
Bare.on('idle', pause).on('resume', resume)

let refs = 0
let garbage = 0
let nextExpiry = 0
let ticks = 1
let triggered = 0
let paused = false
let destroyed = false

function destroy() {
if (destroyed) return
binding.destroy(handle)
destroyed = true
}

function pause() {
if (paused) return
Expand Down Expand Up @@ -178,12 +171,12 @@ function tick() {
}

function cancelTimer() {
if (paused || destroyed || ticks === triggered) return
if (paused || ticks === triggered) return
binding.stop(handle)
}

function updateTimer(ms) {
if (paused || destroyed || ticks === triggered) return
if (paused || ticks === triggered) return
binding.start(handle, ms)
}

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bare-timers",
"version": "2.2.0",
"version": "3.0.0-0",
"description": "Native timers for Javascript",
"exports": {
".": "./index.js",
Expand Down Expand Up @@ -29,6 +29,9 @@
"url": "https://github.com/holepunchto/bare-timers/issues"
},
"homepage": "https://github.com/holepunchto/bare-timers#readme",
"engines": {
"bare": ">=1.7.0"
},
"dependencies": {
"tiny-binary-heap": "^1.1.0"
},
Expand Down

0 comments on commit 8f5ec5e

Please sign in to comment.