Skip to content

Commit

Permalink
Remove onread options in favor of data callback
Browse files Browse the repository at this point in the history
  • Loading branch information
05nelsonm committed Apr 8, 2024
1 parent 24f328e commit 6376f97
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,27 +163,6 @@ public actual interface TorCtrl : Destroyable, TorEvent.Processor, TorCmd.Privil
}
}

fun callback(nread: Int, buf: dynamic) {
val readBuf = try {
val jsBuf = Buffer.wrap(buf)
ReadBuffer.of(jsBuf)
} catch (_: Throwable) {
return
}

feed.onData(readBuf, nread)
}

val buf = ReadBuffer.allocate()

run {
val onReadOptions = js("{}")
onReadOptions["buffer"] = buf.buf.unwrap()
onReadOptions["callback"] = ::callback

options["onread"] = onReadOptions
}

val socket = run {
var isConnected = false
val socket = try {
Expand All @@ -197,7 +176,16 @@ public actual interface TorCtrl : Destroyable, TorEvent.Processor, TorCmd.Privil
threw = IOException("$error")
}

socket.onceClose { feed.close(); buf.buf.fill() }
socket.onData { buffer ->
val jsBuf = try {
Buffer.wrap(buffer)
} catch (_: IllegalArgumentException) {
return@onData
}

feed.onData(ReadBuffer.of(jsBuf), jsBuf.length.toInt())
}
socket.onceClose { feed.close() }

withContext(NonCancellable) {
val mark = TimeSource.Monotonic.markNow()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,11 @@ internal inline fun net_Socket.onceClose(
once("close", listener)
return this
}

@OptIn(InternalProcessApi::class)
internal inline fun net_Socket.onData(
noinline listener: (data: dynamic) -> Unit
): net_Socket {
on("data", listener)
return this
}

0 comments on commit 6376f97

Please sign in to comment.