Skip to content
This repository was archived by the owner on Jul 30, 2024. It is now read-only.

Overhaul event handler exntensions #176

Merged
merged 17 commits into from
Feb 22, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,19 @@ package object child_process {

// TODO: spawn, fork
}

implicit final class ChildProcessClassExtension(private val cp: ChildProcess) extends AnyVal {
@inline
def onClose(listener: (Int, String) => Any): ChildProcess = cp.on("close", listener)

@inline
def onDisconnect(listener: () => Any): ChildProcess = cp.on("disconnect", listener)

@inline
def onError(listener: (js.Error) => Any): ChildProcess = cp.on("error", listener)

@inline
def onMessage(listener: (js.Any, js.UndefOr[net.Socket | net.Server]) => Any): ChildProcess =
cp.on("message", listener)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ package object cluster {
* </ul>
*/
@inline
def onExit(callback: (Worker, Int, String) => Any): Worker = worker.on("exit", callback)
def onExit(callback: (Int, String) => Any): Worker = worker.on("exit", callback)

/**
* Similar to the cluster.on('listening') event, but specific to this worker.
Expand All @@ -164,7 +164,7 @@ package object cluster {
* @param callback the event handler
*/
@inline
def onMessage(callback: Message => Any): Worker = worker.on("message", callback)
def onMessage(callback: Message => Any, handle: js.UndefOr[js.Object]): Worker = worker.on("message", callback)

/**
* Similar to the cluster.on('online') event, but specific to this worker.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,11 @@ trait RemoteAddress extends js.Object {
var family: String = js.native
var port: Int = js.native
}

@js.native
trait RemoteAddressInfo extends js.Object {
var address: String = js.native
var family: String = js.native
var port: Int = js.native
var size: Int = js.native
}
13 changes: 13 additions & 0 deletions app/current/src/main/scala/io/scalajs/nodejs/dgram/package.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.scalajs.nodejs

import com.thoughtworks.enableIf
import io.scalajs.nodejs.buffer.Buffer

import scala.scalajs.js
import scala.scalajs.js.typedarray.Uint8Array
import scala.scalajs.js.|
Expand All @@ -8,4 +11,14 @@ package object dgram {
type StringMessage = String | js.Array[String]
type Message = BufferMessage | StringMessage
type BufferMessage = Uint8Array | js.Array[Uint8Array]

implicit final class SocketExtension[T <: Socket](private val instance: T) extends AnyVal {
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
@inline def onConnect(handler: () => Any): T = instance.on("connect", handler)

@inline def onClose(handler: () => Any): T = instance.on("close", handler)
@inline def onError(handler: (Error) => Any): T = instance.on("error", handler)
@inline def onListening(handler: () => Any): T = instance.on("listening", handler)
@inline def onMessage(handler: (Buffer, RemoteAddressInfo) => Any): T = instance.on("message", handler)
}
}
16 changes: 16 additions & 0 deletions app/current/src/main/scala/io/scalajs/nodejs/events/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.scalajs.nodejs

import scala.scalajs.js
import scala.scalajs.js.|

package object events {

implicit final class EventEmitterExtensions[T <: EventEmitter](private val instance: T) extends AnyVal {
@inline def onNewListener(listener: (String | js.Symbol, js.Function) => Any): T =
instance.on("newListener", listener)

@inline def onRemoveListener(listener: (String | js.Symbol, js.Function) => Any): T =
instance.on("removeListener", listener)
}

}
13 changes: 10 additions & 3 deletions app/current/src/main/scala/io/scalajs/nodejs/fs/FSWatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object FSWatcher {
/**
* File System Watcher Extensions
*/
implicit final class FSWatcherExtensions(val watcher: FSWatcher) extends AnyVal {
implicit final class FSWatcherExtensions[T <: FSWatcher](private val watcher: T) extends AnyVal {

/**
* Emitted when something changes in a watched directory or file. See more details in fs.watch().
Expand All @@ -43,15 +43,22 @@ object FSWatcher {
* @since 0.5.8
*/
@inline
def onChange(listener: (String, js.Any) => Any): watcher.type = watcher.on("change", listener)
def onChange(listener: (String, js.Any) => Any): T = watcher.on("change", listener)

/**
* Added in Node.js v10.0.0
* @see https://nodejs.org/api/fs.html#fs_event_close
*/
@inline
def onClose(listener: () => Any): T = watcher.on("close", listener)

/**
* Emitted when an error occurs.
* @param listener the event handler
* @since 0.5.8
*/
@inline
def onError(listener: Error => Any): watcher.type = watcher.on("error", listener)
def onError(listener: Error => Any): T = watcher.on("error", listener)
}
}

Expand Down
18 changes: 10 additions & 8 deletions app/current/src/main/scala/io/scalajs/nodejs/fs/ReadStream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ object ReadStream {
/**
* Read Stream Events
*/
implicit final class ReadStreamEvents(val stream: ReadStream) extends AnyVal {
implicit final class ReadStreamExtension[R <: ReadStream](private val stream: R) extends AnyVal {

/**
* Emitted when the ReadStream's underlying file descriptor has been closed using the fs.close() method.
* @param listener the event handler
* @since 0.1.93
*/
@inline
def onClose(listener: () => Any): stream.type = stream.on("close", listener)
def onClose(listener: () => Any): R = stream.on("close", listener)

/**
* Emitted when the ReadStream's file is opened.
Expand All @@ -75,13 +75,15 @@ object ReadStream {
* @since 0.1.93
*/
@inline
def onOpen(listener: FileDescriptor => Any): stream.type = stream.on("open", listener)
}
def onOpen(listener: FileDescriptor => Any): R = stream.on("open", listener)

/**
* Added in Node.js v9.11.0
* @see https://nodejs.org/api/fs.html#fs_event_ready
*/
@inline
def onReady(listener: () => Any): R = stream.on("ready", listener)

/**
* Read Stream Extensions
*/
implicit final class ReadStreamExtensions(val stream: ReadStream) extends AnyVal {
@inline
def closeFuture: Future[Unit] = promiseCallback1[Unit](stream.close)
}
Expand Down
18 changes: 10 additions & 8 deletions app/current/src/main/scala/io/scalajs/nodejs/fs/WriteStream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ object WriteStream {
/**
* Write Stream Events
*/
implicit final class WriteStreamEvents(val stream: WriteStream) extends AnyVal {
implicit final class WriteStreamExtension[T <: WriteStream](private val stream: T) extends AnyVal {

/**
* Emitted when the WriteStream's underlying file descriptor has been closed using the fs.close() method.
* @param listener the event handler
* @since 0.1.93
*/
@inline
def onClose(listener: () => Any): stream.type = stream.on("close", listener)
def onClose(listener: () => Any): T = stream.on("close", listener)

/**
* Emitted when the WriteStream's file is opened.
Expand All @@ -71,13 +71,15 @@ object WriteStream {
* @since 0.1.93
*/
@inline
def onOpen(listener: FileDescriptor => Any): stream.type = stream.on("open", listener)
}
def onOpen(listener: FileDescriptor => Any): T = stream.on("open", listener)

/**
* Added in Node.js v9.11.0
* @see https://nodejs.org/api/fs.html#fs_event_ready_1
*/
@inline
def onReady(listener: () => Any): T = stream.on("ready", listener)

/**
* Write Stream Extensions
*/
implicit final class WriteStreamExtensions(val stream: WriteStream) extends AnyVal {
@inline
def closeFuture: Future[Unit] = promiseCallback1[Unit](stream.close)
}
Expand Down
4 changes: 2 additions & 2 deletions app/current/src/main/scala/io/scalajs/nodejs/http/Agent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ object Agent {
/**
* Agent Extensions
*/
implicit final class AgentExtensions(val agent: Agent) extends AnyVal {
implicit final class AgentExtensions[T <: Agent](private val agent: T) extends AnyVal {

/**
* Produces a socket/stream to be used for HTTP requests. By default, this function is the same
Expand All @@ -109,6 +109,6 @@ object Agent {
promiseWithError1[Error, js.Any](agent.createConnection(options, _))
}

@inline def onKeylog(handler: () => Any): agent.type = agent.on("keylog", handler)
@inline def onKeylog(handler: () => Any): T = agent.on("keylog", handler)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package http

import io.scalajs.nodejs.buffer.Buffer
import io.scalajs.nodejs.net.Socket
import io.scalajs.nodejs.stream.Duplex
import io.scalajs.util.PromiseHelper._

import scala.concurrent.Future
Expand Down Expand Up @@ -109,51 +110,51 @@ class ClientRequest extends stream.Writable {
* Client Request Companion
*/
object ClientRequest {
implicit final class ClientRequestExtensions(val client: ClientRequest) extends AnyVal {
implicit final class ClientRequestExtensions[T <: ClientRequest](private val client: T) extends AnyVal {

/**
* Emitted when the request has been aborted by the client. This event is only emitted on the first call to abort().
*/
@inline
def onAbort(callback: () => Any): client.type = client.on("abort", callback)

/**
* Emitted when the request has been aborted by the server and the network socket has closed.
*/
@inline
def onAborted(callback: () => Any): client.type = client.on("aborted", callback)
def onAbort(callback: () => Any): T = client.on("abort", callback)

/**
* Emitted each time a server responds to a request with a CONNECT method. If this event is not being listened for,
* clients receiving a CONNECT method will have their connections closed.
* - response <http.IncomingMessage>
* - socket <net.Socket>
* - socket <stream.Duplex>
* - head <Buffer>
*/
@inline
def onConnect(callback: (IncomingMessage, Socket, Buffer) => Any): client.type = client.on("connect", callback)
def onConnect(callback: (IncomingMessage, Duplex, Buffer) => Any): T = client.on("connect", callback)

/**
* Emitted when the server sends a '100 Continue' HTTP response, usually because the request
* contained 'Expect: 100-continue'. This is an instruction that the client should send the request body.
*/
@inline
def onContinue(callback: () => Any): client.type = client.on("continue", callback)
def onContinue(callback: () => Any): T = client.on("continue", callback)

@inline
def onInformation(callback: Information => Any): T = client.on("information", callback)

/**
* Emitted when a response is received to this request. This event is emitted only once.
* The response argument will be an instance of http.IncomingMessage.
* - response <http.IncomingMessage>
*/
@inline
def onResponse(callback: IncomingMessage => Any): client.type = client.on("response", callback)
def onResponse(callback: IncomingMessage => Any): T = client.on("response", callback)

/**
* Emitted after a socket is assigned to this request.
* - socket <net.Socket>
*/
@inline
def onSocket(callback: Socket => Any): client.type = client.on("socket", callback)
def onSocket(callback: Duplex => Any): T = client.on("socket", callback)

@inline
def onTimeout(callback: () => Any): T = client.on("timeout", callback)

/**
* Emitted each time a server responds to a request with an upgrade. If this event isn't being listened for,
Expand All @@ -163,7 +164,7 @@ object ClientRequest {
* - head <Buffer>
*/
@inline
def onUpgrade(callback: (IncomingMessage, Socket, Buffer) => Any): client.type = client.on("upgrade", callback)
def onUpgrade(callback: (IncomingMessage, Socket, Buffer) => Any): T = client.on("upgrade", callback)

@inline
def endFuture(data: Uint8Array): Future[Unit] = {
Expand All @@ -186,3 +187,13 @@ object ClientRequest {
}
}
}

trait Information extends js.Object {
val httpVersion: String
val httpVersionMajor: Int
val httpVersionMinor: Int
val statusCode: Int
val statusMessage: String
val headers: js.Object
val rawHeaders: js.Array[String]
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,12 @@ object IncomingMessage {
/**
* Incoming Message Extensions
*/
implicit final class IncomingMessageExtensions(val message: IncomingMessage) extends AnyVal {
implicit final class IncomingMessageExtension[T <: IncomingMessage](private val message: T) extends AnyVal {
@inline
def onClose(callback: js.Function): message.type = message.on("close", callback)
def onAborted(callback: () => Any): T = message.on("aborted", callback)

@inline
def onClose(callback: () => Any): T = message.on("close", callback)

@inline
def setTimeout(duration: FiniteDuration, callback: js.Function): Unit =
Expand Down
Loading