I try to write a binding for socket.io.
I am having trouble with a function (next()
in my example code at the bottom), that either takes no argument or a error object (Js.Exn.raiseError("ERROR!")
).
I can't find a way to define a function signature that can take both types of argument as the first value.
I am not even sure, if what I am asking for is possible in rescript, any help to solve that problem in the proper rescript way, would be appreciated.
My current implementation looks like this:
type server
type socket
type next = (. unit) => unit
@new @module("socket.io") external socketIO: unit => server = "Server"
@send external use: (server, (socket, next) => unit) => unit = "use"
@send external listen: (server, int) => unit = "listen"
@send external on: (server, @string [ #connection(socket => unit) ] ) => unit = "on"
let io = socketIO()
io->use((socket, next) => {
Js.log("FIRST")
next(.)
})
io->use((socket, next) => {
Js.log("SECOND")
next(.)
})
io->on(#connection(socket =>
Js.log("CONNECT")
))
io->listen(3000)