Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #167: Add Fetch API + Streams API #177

Merged
merged 1 commit into from
Dec 4, 2015
Merged

Conversation

bblfish
Copy link
Contributor

@bblfish bblfish commented Nov 25, 2015

I have been using the Fetch API for my rww-scala-js akka branch. The code that I have tested and used works. So I thought I'd push this over before someone goes through the same work again.

Probably still a lot to be improved.

This would close #167

@bblfish
Copy link
Contributor Author

bblfish commented Nov 25, 2015

note: I am quite sure there will be improvements needed to the formatting. But before going through all that, I thought I'd first check if this is of interest.

Btw. is there a script to get the formatting just right for this repo?

* @tparam A
*/
@js.native
trait JSIterable[A] extends js.Object {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure actually where this should go. But it has proven useful in my code to be able to iterate through the headers.

@bblfish
Copy link
Contributor Author

bblfish commented Nov 25, 2015

Not sure what the travis failures are related to.

@sjrd
Copy link
Member

sjrd commented Nov 25, 2015

[error] /home/travis/build/scala-js/scala-js-dom/src/main/scala/org/scalajs/dom/experimental/package.scala:12:
  Classes, traits and objects inheriting from js.Any should be annotated with @js.native,
  unless they have @ScalaJSDefined. The default will switch to Scala.js-defined
  in the next major version of Scala.js.
[error] package object experimental extends js.GlobalScope {
[error]                ^
[error] one error found
[error] (root/compile:compile) Compilation failed

@bblfish
Copy link
Contributor Author

bblfish commented Nov 25, 2015

yes but when I add @js.native then I get

> compile
[info] Compiling 6 Scala sources to /Volumes/Dev/Programming/ScalaJS/scala-js-dom/target/scala-2.11/classes...
[error] /Volumes/Dev/Programming/ScalaJS/scala-js-dom/src/main/scala/org/scalajs/dom/experimental/package.scala:13: expected start of definition
[error] package object experimental extends js.GlobalScope {
[error] ^
[error] one error found

what I don't understand is that my own code seems to compile.

@sjrd
Copy link
Member

sjrd commented Nov 25, 2015

Yep, native package objects are sort of deprecated (scala-js/scala-js#1892). This build fails because we have -Xfatal-warnings.

@bblfish
Copy link
Contributor Author

bblfish commented Nov 25, 2015

What do you recommend I do instead?

It looks like the documentation on facade-types needs to be changed. It allows package object without the @js.native

@sjrd
Copy link
Member

sjrd commented Nov 25, 2015

Yes, we should change it.

Use a named object, like @js.native object Fetch extends js.GlobalScope.

@sjrd
Copy link
Member

sjrd commented Nov 25, 2015

If you absolutely want the fetch method visible at the top-level of the package (which I'm sure is a good idea), you can write a Scala forwarder.

@bblfish
Copy link
Contributor Author

bblfish commented Nov 25, 2015

Ok, it compiles now. I did not add fetch to the package object because I am not sure how useful that is and how much thought I had put into that.

/**
* The locked getter returns whether or not the readable stream is locked to a reader.
* throws scala.scalajs.js.TypeError if the stream is not readable
*/
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would not publishLocal with @throws

@bblfish
Copy link
Contributor Author

bblfish commented Nov 27, 2015

I tried to get as close to the preferred formatting of this project in the last commit. I also took out some classes related to streaming to a separate file. I worked on this before my previous commits, so the code may not be as good as my last one. But it is quite a lot of code.

@sjrd
Copy link
Member

sjrd commented Nov 27, 2015

I don't have the bandwidth to process this these days. Could someone else review this? @mseddon? @nicolasstucki?

trait JSIterator[+A] extends js.Object {
def next(): JSIterator.Entry[A] = js.native
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose this one is for @sjrd . I added this here as there is none available yet in the standard library. Is that ok?
See scala-js/scala-js#1141

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to submit this as a PR to scala-js/scala-js.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll do that too. Is there a package in which it should be placed there?

@mseddon
Copy link
Contributor

mseddon commented Nov 28, 2015

@sjrd @bblfish I have a bit of free time tomorrow, so I can pick this up from then.

*
* bblfish: not clear from the definition if this is a number of traits that
* may be implemented by the Response or not
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unclear what you mean by this- certainly body is a trait that is implemented by Response with this signature.

* method is intentionally generic; it does not require that its this
* value be a ReadableStream object.
*/
def tee(): js.Array[ReadableStream[T]] = js.native
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc mentions a "given writable" stream", but the signature doesn't. That's suspicious.

@bblfish
Copy link
Contributor Author

bblfish commented Dec 3, 2015

I fixed a few of the points with JSIterable and JSIterator, but quite a few of the methods do need a js.native, in order to access the underlying JS functions. I have tested it with my application read-write-web/rww-scala-js@ebb8b7e

@sjrd
Copy link
Member

sjrd commented Dec 3, 2015

but quite a few of the methods do need a js.native, in order to access the underlying JS functions.

What makes you think that?

trait Entry[+A] extends js.Object {
def done: Boolean = js.native
def value: A = js.native
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value and done are two js.native functions, so Entry can't be @ScalaJSDefined right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps not. I suppose it's such a basic class...

@bblfish
Copy link
Contributor Author

bblfish commented Dec 3, 2015

Is that better?


def integrity: js.UndefOr[String]

def window: js.UndefOr[Null]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

js.UndefOr[Null] looks suspicious ^^

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, but it's actually what they seem to say in the docs. "can only be null" .
That's apparently to allow you to use it like ServiceWorkers from a window.
The idea is you can say you don't want to have a window. Don't really understand it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol. OK, then.

@bblfish
Copy link
Contributor Author

bblfish commented Dec 4, 2015

No more comments left in the code tab. Ready to go?

* branches. (Let us know if you think we should add an option to tee that
* creates structured clones of the chunks for each branch.)
*/
def tee(): js.Array[ReadableStream[T]] = js.native
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see this was blocking the covariant thing.
Would js.Array[_ <: ReadableStream[T]] allow you to make T covariant?

Ideally this should even be a js.Tuple2[ReadableStream[T], ReadableStream[T]], which would also solve the covariant issue.

@bblfish
Copy link
Contributor Author

bblfish commented Dec 4, 2015

done.

@sjrd
Copy link
Member

sjrd commented Dec 4, 2015

Good!

Last step: could you squash all the commits, please?

@bblfish bblfish changed the title add Fetch API Fix #167: Add Fetch API + Streams API Dec 4, 2015
@bblfish
Copy link
Contributor Author

bblfish commented Dec 4, 2015

commits squashed. I learned a lot with the PR. :-)

@mseddon
Copy link
Contributor

mseddon commented Dec 4, 2015

👍 whew! Thanks for that @bblfish and @sjrd, good work!

@sjrd
Copy link
Member

sjrd commented Dec 4, 2015

LGTM, thanks!

sjrd added a commit that referenced this pull request Dec 4, 2015
Fix #167: Add Fetch API + Streams API
@sjrd sjrd merged commit dad18b7 into scala-js:master Dec 4, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fetch and Streaming API
3 participants