From c44e23110385267b26e25689c571d7b7d6e64cb6 Mon Sep 17 00:00:00 2001 From: Andrew Herron Date: Mon, 11 Dec 2023 03:46:18 +1100 Subject: [PATCH] Added timers/promises. Resolves #21 --- .changes/unreleased/Added-20231211-034512.yaml | 5 +++++ lib/js/src/Timers.bs.js | 3 +++ lib/js/test/atomic/Timers.test.bs.js | 18 ++++++++++++++++++ src/Timers.res | 10 ++++++++++ test/atomic/Timers.test.res | 12 ++++++++++++ 5 files changed, 48 insertions(+) create mode 100644 .changes/unreleased/Added-20231211-034512.yaml create mode 100644 lib/js/test/atomic/Timers.test.bs.js create mode 100644 test/atomic/Timers.test.res diff --git a/.changes/unreleased/Added-20231211-034512.yaml b/.changes/unreleased/Added-20231211-034512.yaml new file mode 100644 index 0000000..5464a78 --- /dev/null +++ b/.changes/unreleased/Added-20231211-034512.yaml @@ -0,0 +1,5 @@ +kind: Added +body: Timers.Promises from NodeJS 16 +time: 2023-12-11T03:45:12.916076+11:00 +custom: + Github Issue: "21" diff --git a/lib/js/src/Timers.bs.js b/lib/js/src/Timers.bs.js index c63f6b6..ae47576 100644 --- a/lib/js/src/Timers.bs.js +++ b/lib/js/src/Timers.bs.js @@ -6,6 +6,9 @@ var Immediate = {}; var Timeout = {}; +var Promises = {}; + exports.Immediate = Immediate; exports.Timeout = Timeout; +exports.Promises = Promises; /* No side effect */ diff --git a/lib/js/test/atomic/Timers.test.bs.js b/lib/js/test/atomic/Timers.test.bs.js new file mode 100644 index 0000000..1999ea1 --- /dev/null +++ b/lib/js/test/atomic/Timers.test.bs.js @@ -0,0 +1,18 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE +'use strict'; + +var Zora = require("zora"); +var Promises = require("node:timers/promises"); + +Zora.test("Timers (promises)", (async function (t) { + t.test("setTimeout", (async function (t) { + await Promises.setTimeout(100); + t.ok(true, ""); + })); + t.test("setImmediate", (async function (t) { + var yes = await Promises.setImmediate("yes"); + t.equal(yes, "yes", ""); + })); + })); + +/* Not a pure module */ diff --git a/src/Timers.res b/src/Timers.res index 93e2b79..c314d63 100644 --- a/src/Timers.res +++ b/src/Timers.res @@ -21,3 +21,13 @@ external setInterval: (unit => unit, int) => Timeout.t = "setInterval" @val external clearImmediate: Immediate.t => unit = "clearImmediate" @val external clearTimeout: Timeout.t => unit = "clearTimeout" @val external clearInterval: Timeout.t => unit = "clearInterval" + +module Promises = { + @module("node:timers/promises") + external setTimeout: int => Js.Promise.t = "setTimeout" + @module("node:timers/promises") + external setImmediate: 'a => Js.Promise.t<'a> = "setImmediate" + // setInterval is not a promise, it's an async iterator + // @module("node:timers/promises") + // external setInterval: (int, 'a) => Js.Promise.t<'a> = "setTimeout" +} diff --git a/test/atomic/Timers.test.res b/test/atomic/Timers.test.res new file mode 100644 index 0000000..13bda61 --- /dev/null +++ b/test/atomic/Timers.test.res @@ -0,0 +1,12 @@ +open Zora + +zora("Timers (promises)", async t => { + t->test("setTimeout", async t => { + let () = await Timers.Promises.setTimeout(100) + t->ok(true, "") + }) + t->test("setImmediate", async t => { + let yes = await Timers.Promises.setImmediate("yes") + t->equal(yes, "yes", "") + }) +})