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

Commit 6f06436

Browse files
authored
Merge pull request #179 from exoego/package-object
Move implicit classes to package object so companion can be js.Object
2 parents ab67aa2 + 6a8972a commit 6f06436

20 files changed

+561
-728
lines changed

app/current/src/main/scala/io/scalajs/nodejs/fs/FSWatcher.scala

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,49 +19,6 @@ trait FSWatcher extends IEventEmitter {
1919
def close(): Unit = js.native
2020
}
2121

22-
/**
23-
* File System Watcher Companion
24-
*/
25-
object FSWatcher {
26-
27-
/**
28-
* File System Watcher Extensions
29-
*/
30-
implicit final class FSWatcherExtensions[T <: FSWatcher](private val watcher: T) extends AnyVal {
31-
32-
/**
33-
* Emitted when something changes in a watched directory or file. See more details in fs.watch().
34-
*
35-
* The filename argument may not be provided depending on operating system support. If filename is provided,
36-
* it will be provided as a Buffer if fs.watch() is called with it's encoding option set to 'buffer', otherwise
37-
* filename will be a string.
38-
* @param listener the event handler
39-
* <ul>
40-
* <li>event: String - The type of fs change</li>
41-
* <li>filename: String> | Buffer - The filename that changed (if relevant/available)</li>
42-
* </ul>
43-
* @since 0.5.8
44-
*/
45-
@inline
46-
def onChange(listener: (String, js.Any) => Any): T = watcher.on("change", listener)
47-
48-
/**
49-
* Added in Node.js v10.0.0
50-
* @see https://nodejs.org/api/fs.html#fs_event_close
51-
*/
52-
@inline
53-
def onClose(listener: () => Any): T = watcher.on("close", listener)
54-
55-
/**
56-
* Emitted when an error occurs.
57-
* @param listener the event handler
58-
* @since 0.5.8
59-
*/
60-
@inline
61-
def onError(listener: Error => Any): T = watcher.on("error", listener)
62-
}
63-
}
64-
6522
/**
6623
* FS Watcher Options
6724
* @param encoding Specifies the character encoding to be used for the filename passed to the listener (default: "utf8")

app/current/src/main/scala/io/scalajs/nodejs/fs/ReadStream.scala

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ package fs
33

44
import com.thoughtworks.enableIf
55
import io.scalajs.nodejs.buffer.Buffer
6-
import io.scalajs.nodejs.stream
7-
import io.scalajs.util.PromiseHelper.promiseCallback1
86

9-
import scala.concurrent.Future
107
import scala.scalajs.js
118
import scala.scalajs.js.annotation.JSImport
129
import scala.scalajs.js.|
@@ -47,44 +44,3 @@ class ReadStream(path: Path) extends stream.Readable {
4744
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
4845
val pending: Boolean = js.native
4946
}
50-
51-
/**
52-
* Read Stream Companion
53-
*/
54-
object ReadStream {
55-
56-
/**
57-
* Read Stream Events
58-
*/
59-
implicit final class ReadStreamExtensions[R <: ReadStream](private val stream: R) extends AnyVal {
60-
61-
/**
62-
* Emitted when the ReadStream's underlying file descriptor has been closed using the fs.close() method.
63-
* @param listener the event handler
64-
* @since 0.1.93
65-
*/
66-
@inline
67-
def onClose(listener: () => Any): R = stream.on("close", listener)
68-
69-
/**
70-
* Emitted when the ReadStream's file is opened.
71-
* @param listener the event handler
72-
* <ul>
73-
* <li>fd: Integer - file descriptor used by the ReadStream.</li>
74-
* </ul>
75-
* @since 0.1.93
76-
*/
77-
@inline
78-
def onOpen(listener: FileDescriptor => Any): R = stream.on("open", listener)
79-
80-
/**
81-
* Added in Node.js v9.11.0
82-
* @see https://nodejs.org/api/fs.html#fs_event_ready
83-
*/
84-
@inline
85-
def onReady(listener: () => Any): R = stream.on("ready", listener)
86-
87-
@inline
88-
def closeFuture: Future[Unit] = promiseCallback1[Unit](stream.close)
89-
}
90-
}

app/current/src/main/scala/io/scalajs/nodejs/fs/WriteStream.scala

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ package io.scalajs.nodejs
22
package fs
33

44
import io.scalajs.nodejs.buffer.Buffer
5-
import io.scalajs.nodejs.stream
6-
import io.scalajs.util.PromiseHelper._
75

8-
import scala.concurrent.Future
96
import scala.scalajs.js
107
import scala.scalajs.js.annotation.JSImport
118
import scala.scalajs.js.|
@@ -43,44 +40,3 @@ class WriteStream(path: Path) extends stream.Writable {
4340
*/
4441
def close(callback: js.Function1[Unit, Any]): Unit = js.native
4542
}
46-
47-
/**
48-
* Write Stream Companion
49-
*/
50-
object WriteStream {
51-
52-
/**
53-
* Write Stream Events
54-
*/
55-
implicit final class WriteStreamExtensions[T <: WriteStream](private val stream: T) extends AnyVal {
56-
57-
/**
58-
* Emitted when the WriteStream's underlying file descriptor has been closed using the fs.close() method.
59-
* @param listener the event handler
60-
* @since 0.1.93
61-
*/
62-
@inline
63-
def onClose(listener: () => Any): T = stream.on("close", listener)
64-
65-
/**
66-
* Emitted when the WriteStream's file is opened.
67-
* @param listener the event handler
68-
* <ul>
69-
* <li>fd: Integer - file descriptor used by the ReadStream.</li>
70-
* </ul>
71-
* @since 0.1.93
72-
*/
73-
@inline
74-
def onOpen(listener: FileDescriptor => Any): T = stream.on("open", listener)
75-
76-
/**
77-
* Added in Node.js v9.11.0
78-
* @see https://nodejs.org/api/fs.html#fs_event_ready_1
79-
*/
80-
@inline
81-
def onReady(listener: () => Any): T = stream.on("ready", listener)
82-
83-
@inline
84-
def closeFuture: Future[Unit] = promiseCallback1[Unit](stream.close)
85-
}
86-
}

app/current/src/main/scala/io/scalajs/nodejs/fs/package.scala

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,4 +483,108 @@ package object fs {
483483
promiseWithError0[js.Error](instance.close _)
484484
}
485485
}
486+
487+
/**
488+
* File System Watcher Extensions
489+
*/
490+
implicit final class FSWatcherExtensions[T <: FSWatcher](private val watcher: T) extends AnyVal {
491+
492+
/**
493+
* Emitted when something changes in a watched directory or file. See more details in fs.watch().
494+
*
495+
* The filename argument may not be provided depending on operating system support. If filename is provided,
496+
* it will be provided as a Buffer if fs.watch() is called with it's encoding option set to 'buffer', otherwise
497+
* filename will be a string.
498+
* @param listener the event handler
499+
* <ul>
500+
* <li>event: String - The type of fs change</li>
501+
* <li>filename: String> | Buffer - The filename that changed (if relevant/available)</li>
502+
* </ul>
503+
* @since 0.5.8
504+
*/
505+
@inline
506+
def onChange(listener: (String, js.Any) => Any): T = watcher.on("change", listener)
507+
508+
/**
509+
* Added in Node.js v10.0.0
510+
* @see https://nodejs.org/api/fs.html#fs_event_close
511+
*/
512+
@inline
513+
def onClose(listener: () => Any): T = watcher.on("close", listener)
514+
515+
/**
516+
* Emitted when an error occurs.
517+
* @param listener the event handler
518+
* @since 0.5.8
519+
*/
520+
@inline
521+
def onError(listener: Error => Any): T = watcher.on("error", listener)
522+
}
523+
524+
implicit final class ReadStreamExtensions[R <: ReadStream](private val stream: R) extends AnyVal {
525+
526+
/**
527+
* Emitted when the ReadStream's underlying file descriptor has been closed using the fs.close() method.
528+
* @param listener the event handler
529+
* @since 0.1.93
530+
*/
531+
@inline
532+
def onClose(listener: () => Any): R = stream.on("close", listener)
533+
534+
/**
535+
* Emitted when the ReadStream's file is opened.
536+
* @param listener the event handler
537+
* <ul>
538+
* <li>fd: Integer - file descriptor used by the ReadStream.</li>
539+
* </ul>
540+
* @since 0.1.93
541+
*/
542+
@inline
543+
def onOpen(listener: FileDescriptor => Any): R = stream.on("open", listener)
544+
545+
/**
546+
* Added in Node.js v9.11.0
547+
* @see https://nodejs.org/api/fs.html#fs_event_ready
548+
*/
549+
@inline
550+
def onReady(listener: () => Any): R = stream.on("ready", listener)
551+
552+
@inline
553+
def closeFuture: Future[Unit] = promiseCallback1[Unit](stream.close)
554+
}
555+
556+
/**
557+
* Write Stream Events
558+
*/
559+
implicit final class WriteStreamExtensions[T <: WriteStream](private val stream: T) extends AnyVal {
560+
561+
/**
562+
* Emitted when the WriteStream's underlying file descriptor has been closed using the fs.close() method.
563+
* @param listener the event handler
564+
* @since 0.1.93
565+
*/
566+
@inline
567+
def onClose(listener: () => Any): T = stream.on("close", listener)
568+
569+
/**
570+
* Emitted when the WriteStream's file is opened.
571+
* @param listener the event handler
572+
* <ul>
573+
* <li>fd: Integer - file descriptor used by the ReadStream.</li>
574+
* </ul>
575+
* @since 0.1.93
576+
*/
577+
@inline
578+
def onOpen(listener: FileDescriptor => Any): T = stream.on("open", listener)
579+
580+
/**
581+
* Added in Node.js v9.11.0
582+
* @see https://nodejs.org/api/fs.html#fs_event_ready_1
583+
*/
584+
@inline
585+
def onReady(listener: () => Any): T = stream.on("ready", listener)
586+
587+
@inline
588+
def closeFuture: Future[Unit] = promiseCallback1[Unit](stream.close)
589+
}
486590
}

app/current/src/main/scala/io/scalajs/nodejs/http/Agent.scala

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package io.scalajs.nodejs
22
package http
33

44
import io.scalajs.nodejs.events.IEventEmitter
5-
import io.scalajs.util.PromiseHelper._
65

7-
import scala.concurrent.Future
86
import scala.scalajs.js
97
import scala.scalajs.js.annotation.JSImport
108

@@ -88,27 +86,3 @@ class Agent(options: AgentOptions = js.native) extends IEventEmitter {
8886

8987
def getName(options: GetNameOptions): String = js.native
9088
}
91-
92-
/**
93-
* Agent Companion
94-
*/
95-
object Agent {
96-
97-
/**
98-
* Agent Extensions
99-
*/
100-
implicit final class AgentExtensions[T <: Agent](private val agent: T) extends AnyVal {
101-
102-
/**
103-
* Produces a socket/stream to be used for HTTP requests. By default, this function is the same
104-
* as net.createConnection(). However, custom Agents may override this method in case greater
105-
* flexibility is desired.
106-
*/
107-
@inline
108-
def createConnectionFuture(options: ConnectionOptions): Future[js.Any] = {
109-
promiseWithError1[Error, js.Any](agent.createConnection(options, _))
110-
}
111-
112-
@inline def onKeylog(handler: () => Any): T = agent.on("keylog", handler)
113-
}
114-
}

0 commit comments

Comments
 (0)