Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ jobs:
- name: install
if: ${{ matrix.os.name != 'windows-latest' }}
run: |
curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash -s bleeding
curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash -s # bleeding
echo "$HOME/.moon/bin" >> $GITHUB_PATH

- name: install on windows
env:
MOONBIT_INSTALL_VERSION: bleeding
# env:
# MOONBIT_INSTALL_VERSION: bleeding
if: ${{ matrix.os.name == 'windows-latest' }}
run: |
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser; irm https://cli.moonbitlang.com/install/powershell.ps1 | iex
Expand Down
2 changes: 1 addition & 1 deletion moon.mod.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rami3l/js-ffi",
"version": "0.2.4",
"version": "0.3.0",
"readme": "README.md",
"repository": "",
"license": "Apache-2.0",
Expand Down
24 changes: 10 additions & 14 deletions src/js/async.mbt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
///|
pub async fn suspend[T, E : Error](
pub async fn[T, E : Error] suspend(
f : ((T) -> Unit, (E) -> Unit) -> Unit
) -> T!E = "%async.suspend"

///|
pub fn async_run(f : () -> Unit!Async) -> Unit = "%async.run"
pub fn async_run(f : async () -> Unit) -> Unit = "%async.run"

///|
/// # Safety
Expand Down Expand Up @@ -38,20 +38,18 @@ pub async fn Promise::wait(self : Promise) -> Value! {
/// This makes sure that when `op` errs out, the error is caught by the MoonBit runtime.
///
/// If you don't care about the result of the operation, you can use `spawn_detach` instead.
pub fn Promise::unsafe_new[T](op : () -> T!Error + Async) -> Promise {
pub fn[T] Promise::unsafe_new(op : async () -> T!) -> Promise {
Promise::new_ffi(fn() { Value::cast_from(op!()) })
}

///|
extern "js" fn Promise::new_ffi(op : () -> Value!Error + Async) -> Promise =
extern "js" fn Promise::new_ffi(op : async () -> Value!) -> Promise =
#| (op) => new Promise((k, ke) => op(k, ke))

///|
pub fn spawn_detach[T, E : Error](op : () -> T!E + Async) -> Unit {
pub fn[T, E : Error] spawn_detach(op : async () -> T!E) -> Unit {
async_run(fn() {
try {
op!() |> ignore
} catch {
try op!() |> ignore catch {
_ => ()
}
})
Expand All @@ -64,23 +62,21 @@ pub extern "js" fn Promise::all(promises : Array[Promise]) -> Promise = "(ps) =>

///|
/// Wraps each given `async fn` in a `Promise` and waits for all of them to resolve.
pub async fn async_all![T](ops : Array[() -> T!Error + Async]) -> Array[T] {
pub async fn[T] async_all!(ops : Array[async () -> T!]) -> Array[T] {
async_all_raw!(ops.map(fn(op) { async fn!() { Value::cast_from(op!()) } })).map(
Value::cast,
)
}

///|
async fn async_all_raw!(ops : Array[() -> Value!Error + Async]) -> Array[Value] {
async fn async_all_raw!(ops : Array[async () -> Value!]) -> Array[Value] {
Promise::all(ops.map(Promise::unsafe_new)).wait!().cast()
}

///|
pub fn async_test(op : () -> Unit!Error + Async) -> Unit {
pub fn async_test(op : async () -> Unit!) -> Unit {
async_run(async fn() {
try {
op!()
} catch {
try op!() catch {
e => {
println("ERROR in `async_test`: \{e}")
panic()
Expand Down
28 changes: 0 additions & 28 deletions src/js/async_deprecated.mbt

This file was deleted.

2 changes: 1 addition & 1 deletion src/js/error.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extern "js" fn Error_::wrap_ffi(
#| (op, on_ok, on_error) => { try { on_ok(op()); } catch (e) { on_error(e); } }

///|
pub fn Error_::wrap[T](
pub fn[T] Error_::wrap(
op : () -> Value,
map_ok~ : (Value) -> T = Value::cast
) -> T!Error_ {
Expand Down
Loading