Skip to content

Conversation

@danielwegener
Copy link
Contributor

This PR adds interfaces for the following Web Workers API interfaces (all in the experimental package for now - even they are not really tagged as experimental in MDN):

  • NavigatorLanguage (mixed into Navigator)
  • WorkerNavigator
  • WorkerLocation
  • WorkerGlobalScope
  • DedicatedWorkerGlobalScope
  • SharedWorkerGlobalScope
  • ServiceWorkerGlobalScope

The following interfaces for ServiceWorkers are added

  • ClientType
  • ClientQueryOptions
  • Clients

The idea is that a user can create a serviceWorker.js with scala.js (with a wrapper that directly calls the main method) like this (note that the message event comes from ServiceWorker):

@JSExport object SharedServiceWorker { val self = SharedWorkerGlobalScope.self @JSExport def main(): Unit = { self.onconnect = (m:MessageEvent) => { m.ports(0).postMessage(s"hello client! I am ${self.name}") } } }
@danielwegener
Copy link
Contributor Author

Disclaimer: Untested yet, I am on it

@mseddon
Copy link
Contributor

mseddon commented Dec 13, 2015

👍 I'll take a proper look tomorrow. It would be good to pull ServiceWorker and friends into the experimental package in this PR too if you're okay with that, but if not I can address that later.

@danielwegener
Copy link
Contributor Author

Okay, gonna pick up #180 (comment) and move them to the experimental package. Would you be fine with merging all worker related api into a WebWorkers.scala (like ServiceWorker.scala and my new ChannelMessagingApi.scala - which is misleading anyway)?

@mseddon
Copy link
Contributor

mseddon commented Dec 13, 2015

Yes, I think that'll work. From a user perspective that won't have much impact.

@mseddon
Copy link
Contributor

mseddon commented Dec 13, 2015

Thanks :)

@danielwegener
Copy link
Contributor Author

NavigatorID should stay where it is but Navigator.serviceWorker should go into the experimental package.
Thought about creating an

implicit class NavigatorOps(private val navigator:Navigator) extends AnyVal { val serviceWorker: ServiceWorkerContainer = js.native } 

in experimental/package.scala. Thoughts on that?

I'll also keep WebWorkers and ServiceWorker separated (different W3C standards) since WebWorkers should already be pretty stable but ServiceWorker probably still will get some changes.

(There is one weird dependency from WebWorkers to ServiceWorkers left, MDN declares WorkerGlobalScope.caches, but those caches are Part of ServiceWorkers spec. Just made it js.Any in WebWorkers)

@danielwegener danielwegener force-pushed the wip-186-shared-worker branch 6 times, most recently from fbeedd8 to e3a9647 Compare December 14, 2015 08:15
@mseddon
Copy link
Contributor

mseddon commented Dec 14, 2015

Actually, I'd put it in a subpackage specifically for workers- that way people can import just the features that they want without accidentally pulling in all experimental apis (and getting tripped up believing they're accessing a standard method in an unrelated api).

Rather than deleting the currently exposed types from the dom package object, for now we should probably leave a type declaration forwarding to their new location, but with a @deprecated annotation mentioning they should now be imported from the new location. Unsure how many people are actually using it from the old (incorrect) location, but it's nicer to just follow the deprecation warnings (or choose to ignore them) if you have to upgrade your dependencies on a tight deadline. :)

@danielwegener
Copy link
Contributor Author

Yeah thats what i thought too - so we'd also need a org.scalajs.dom.raw package-object that has @deprecated type-aliases pointing to the new location in experimental.worker.

@mseddon
Copy link
Contributor

mseddon commented Dec 14, 2015

Yep, This is what happens when things go in the wrong place. Sorry about that :/

@danielwegener
Copy link
Contributor Author

One could argue that the types in the raw-package are non-public anyway and one should only rely on types on org.scalajs.dom ;)

@mseddon
Copy link
Contributor

mseddon commented Dec 14, 2015

You could. Unfortunately Intellij sucks classes from their package of definition when using autocomplete/import, so people are probably already doomed :)

(And actually, I think there are some classes that are only exposed in dom.raw).

@mseddon
Copy link
Contributor

mseddon commented Dec 14, 2015

Oh, definitely think you should use something like dom.experimental.worker (perhaps you can come up with a better name) to keep experimental features separated.

@danielwegener
Copy link
Contributor Author

Thats what I ment - if they only existed in dom.raw - they never have been public and we could move them freely - but i know what you mean ;) I'd prefer the original spec name (webworkers, serviceworkers -maybe to longish?) - think it is a good idea anyway to roughly split our packages by w3c api - or feature - just tried to stick with the existing convention.

@mseddon
Copy link
Contributor

mseddon commented Dec 14, 2015

I definitely agree with classification by specification. As for 'convention', apart from a few apis, it has generally been "chuck it in dom.raw", something I'm trying to incrementally move us away from :)

I notice actually that Channel Messaging is flagged here as 'very compatible'. But Service Workers are (extremely) experimental. For now we can probably keep them in experimental though.

@danielwegener danielwegener changed the title Add ChannelMessagingApi (SharedWorker, WorkerScopes) (fixes #186) Add Web Workers (SharedWorker, WorkerScopes) (fixes #186) Dec 14, 2015
@danielwegener danielwegener changed the title Add Web Workers (SharedWorker, WorkerScopes) (fixes #186) Add Web Workers API (SharedWorker, WorkerScopes) (fixes #186) Dec 14, 2015
@danielwegener
Copy link
Contributor Author

(renamed PR Channel Messaging API to Web Workers API - but that one looks stable too - but not fully supported by IE)

@danielwegener danielwegener force-pushed the wip-186-shared-worker branch 4 times, most recently from 8b6eb8b to d194db0 Compare December 14, 2015 17:31
@danielwegener
Copy link
Contributor Author

Okay - I came up with the following solution:

  • WebWorkers (except their SharedWorker part) are stable and available. Hence I'll put them into org.scalajs.dom.webworkers package. We need no raw package, because WebWorker classes are not prefixed.
  • SharedWorkers are experimental and moved into org.scalajs.dom.experimental.sharedworkers (I am not quite sure if the contained classes should all be within one file)
  • ServiceWorkers are experimental and moved into org.scalajs.dom.experimental.serviceworkers
    • ServiceWorker specifics are moved from org.scalajs.dom and org.scalajs.dom.raw into this package
  • All moved classes have type aliases with deprecation-marker at the place where they were removed

Caveats:

  • now we have two Transferrables, one in dom.webworkers and one in dom.experimental.serviceworkers

Sorry for this back and forth noise (and those tons of builds)

Copy link
Member

Choose a reason for hiding this comment

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

Drop @return. (the same thing happens elsewhere)

@sjrd
Copy link
Member

sjrd commented Jan 20, 2016

That's all.

@danielwegener danielwegener force-pushed the wip-186-shared-worker branch 2 times, most recently from dd3885c to c119d74 Compare January 20, 2016 17:55
@danielwegener
Copy link
Contributor Author

@sjrd cheers. all done. I am off writing a scalariform config. or trying to push this into the idea formatter. that was a bit cruel :) (no doubt, reviewing for code style must be no fun either)

@mseddon
Copy link
Contributor

mseddon commented Jan 20, 2016

stoopid travis lost network, so that test failed. :/

@mseddon
Copy link
Contributor

mseddon commented Jan 21, 2016

LGTM

@danielwegener
Copy link
Contributor Author

@mseddon squashed!

@mseddon
Copy link
Contributor

mseddon commented Jan 21, 2016

Thanks! @sjrd okay to merge?

@sjrd
Copy link
Member

sjrd commented Jan 21, 2016

LGTM

sjrd added a commit that referenced this pull request Jan 21, 2016
Fix #186: Add Web Workers API (SharedWorker, WorkerScopes, ServiceWorker)
@sjrd sjrd merged commit 6cf1222 into scala-js:master Jan 21, 2016
@danielwegener danielwegener deleted the wip-186-shared-worker branch February 4, 2016 17:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

5 participants