-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f95ccfe
commit 1d25616
Showing
6 changed files
with
196 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package scommons.nodejs | ||
|
||
import scommons.nodejs.raw.Readable | ||
|
||
import scala.concurrent.{Future, Promise} | ||
import scala.scalajs.js | ||
import scala.scalajs.js.typedarray.Uint8Array | ||
|
||
class StreamReader(readable: Readable) { | ||
|
||
import scala.concurrent.ExecutionContext.Implicits.global | ||
|
||
private var ready = Promise[Unit]() | ||
private var isEnd = false | ||
|
||
private val readableListener: js.Function = { () => | ||
ready.trySuccess(()) | ||
} | ||
private val errorListener: js.Function = { error: js.Error => | ||
ready.tryFailure(js.JavaScriptException(error)) | ||
} | ||
private val endListener: js.Function0[Unit] = { () => | ||
isEnd = true | ||
ready.trySuccess(()) | ||
readable.removeListener("readable", readableListener) | ||
readable.removeListener("error", errorListener) | ||
} | ||
readable.on("readable", readableListener) | ||
readable.on("error", errorListener) | ||
readable.once("end", endListener) | ||
readable.once("close", endListener) | ||
|
||
def readNextBytes(size: Int): Future[Option[Uint8Array]] = { | ||
|
||
def loop(): Future[Option[Uint8Array]] = { | ||
ready.future.flatMap { _ => | ||
if (isEnd) Future.successful(None) | ||
else { | ||
val content = readable.read(size) | ||
if (content != null) Future.successful(Some(content)) | ||
else { | ||
ready = Promise[Unit]() | ||
loop() | ||
} | ||
} | ||
} | ||
} | ||
|
||
loop() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters