Quantcast
Channel: ReScript Forum - Latest posts
Viewing all articles
Browse latest Browse all 2592

Exn to JSError.t

$
0
0

Ended up with some custom promise bindings, I now have:

@send
external thenResolve: (promise<'data>, 'data => unit) => promise<'data> = "then"

@send
external catchJSError: (promise<'data>, JsError.t => unit) => promise<'data> = "catch"

@send
external finally: (promise<'data>, unit => unit) => unit = "finally"

let withKaplayContext = (testFn: Context.t => promise<unit>): promise<unit> => {
  let k = Context.kaplay(
    ~initOptions={
      width: 160,
      height: 160,
      global: false,
      background: "#000000",
      scale: 1.,
      crisp: true,
    },
  )

  // Load assets ...

  Promise.make((resolve, reject) => {
    // https://v4000.kaplayjs.com/docs/api/ctx/onError/
    k->Context.onError((error: JsError.t) => {
      k->Context.quit
      reject(error)
    })
    k->Context.onLoad(() => {
      testFn(k)
      ->thenResolve(resolve)
      ->catchJSError(reject)
      ->finally(
        () => {
          k->Context.quit
        },
      )
      ->ignore
    })
  })
}

Ended up going with some custom bindings.

I guess one question would be if Context.onError does take a JSError.t?

@tsnobip what do you think?


Viewing all articles
Browse latest Browse all 2592

Trending Articles