Skip to content

Add WorkerOptions to [Shared]Worker constructors #691

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

Merged
merged 9 commits into from
Apr 13, 2022
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
7 changes: 7 additions & 0 deletions api-reports/2_12.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24057,6 +24057,7 @@ SharedWorker[JC] var onerror: js.Function1[ErrorEvent, _]
SharedWorker[JC] def port: MessagePort
SharedWorker[JC] def removeEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], options: EventListenerOptions): Unit
SharedWorker[JC] def removeEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], useCapture: Boolean?): Unit
SharedWorker[SO]
SharedWorkerGlobalScope[JO] def self: SharedWorkerGlobalScope
SharedWorkerGlobalScope[JT] def addEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], options: EventListenerOptions): Unit
SharedWorkerGlobalScope[JT] def addEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], useCapture: Boolean?): Unit
Expand Down Expand Up @@ -25351,6 +25352,12 @@ WorkerNavigator[JT] def onLine: Boolean
WorkerNavigator[JT] def platform: String
WorkerNavigator[JT] def sendBeacon(url: String, data: BodyInit?): Boolean (@deprecated in 2.0.0)
WorkerNavigator[JT] def userAgent: String
WorkerOptions[JT] var credentials: js.UndefOr[RequestCredentials]
WorkerOptions[JT] var name: js.UndefOr[String]
WorkerOptions[JT] var `type`: js.UndefOr[WorkerType]
WorkerType[JT]
WorkerType[SO] val classic: WorkerType
WorkerType[SO] val module: WorkerType
WriteableState[JT]
WriteableState[SO] val closed: WriteableState
WriteableState[SO] val closing: WriteableState
Expand Down
7 changes: 7 additions & 0 deletions api-reports/2_13.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24057,6 +24057,7 @@ SharedWorker[JC] var onerror: js.Function1[ErrorEvent, _]
SharedWorker[JC] def port: MessagePort
SharedWorker[JC] def removeEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], options: EventListenerOptions): Unit
SharedWorker[JC] def removeEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], useCapture: Boolean?): Unit
SharedWorker[SO]
SharedWorkerGlobalScope[JO] def self: SharedWorkerGlobalScope
SharedWorkerGlobalScope[JT] def addEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], options: EventListenerOptions): Unit
SharedWorkerGlobalScope[JT] def addEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], useCapture: Boolean?): Unit
Expand Down Expand Up @@ -25351,6 +25352,12 @@ WorkerNavigator[JT] def onLine: Boolean
WorkerNavigator[JT] def platform: String
WorkerNavigator[JT] def sendBeacon(url: String, data: BodyInit?): Boolean (@deprecated in 2.0.0)
WorkerNavigator[JT] def userAgent: String
WorkerOptions[JT] var credentials: js.UndefOr[RequestCredentials]
WorkerOptions[JT] var name: js.UndefOr[String]
WorkerOptions[JT] var `type`: js.UndefOr[WorkerType]
WorkerType[JT]
WorkerType[SO] val classic: WorkerType
WorkerType[SO] val module: WorkerType
WriteableState[JT]
WriteableState[SO] val closed: WriteableState
WriteableState[SO] val closing: WriteableState
Expand Down
11 changes: 11 additions & 0 deletions dom/src/main/scala-2/org/scalajs/dom/WorkerType.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.scalajs.dom

import scala.scalajs.js

@js.native
sealed trait WorkerType extends js.Any

object WorkerType {
val classic: WorkerType = "classic".asInstanceOf[WorkerType]
val module: WorkerType = "module".asInstanceOf[WorkerType]
}
10 changes: 10 additions & 0 deletions dom/src/main/scala-3/org/scalajs/dom/WorkerType.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.scalajs.dom

import scala.scalajs.js

opaque type WorkerType <: String = String

object WorkerType {
val classic: WorkerType = "classic"
val module: WorkerType = "module"
}
16 changes: 11 additions & 5 deletions dom/src/main/scala/org/scalajs/dom/SharedWorker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,24 @@ import scala.scalajs.js.annotation._
* thrown.
* @example
* {{{var myWorker = new SharedWorker("aURL", name);}}}
* @param stringUrl
* @param scriptURL
* A DOMString representing the URL of the script the worker will execute. It must obey the same-origin policy.
* @param name
* An optional argument that specifies an existing SharedWorkerGlobalScope.name — if this is specified then that
* SharedWorkerGlobalScope will be used as the scope for this shared worker.
* @param options
* A DOMString specifying an identifying name for the SharedWorkerGlobalScope representing the scope of the worker,
* which is mainly useful for debugging purposes. Or, an object containing option properties that can set when
* creating the object instance.
*/
@js.native
@JSGlobal
class SharedWorker(stringUrl: String, name: js.UndefOr[String] = js.native) extends AbstractWorker {
class SharedWorker(scriptURL: String) extends AbstractWorker {
def this(scriptURL: String, name: String) = this(scriptURL)

def this(scriptURL: String, options: WorkerOptions) = this(scriptURL)

/** The port property of the SharedWorker interface returns a [[MessagePort]] object used to communicate and control
* the shared worker.
*/
def port: MessagePort = js.native
}

object SharedWorker
9 changes: 8 additions & 1 deletion dom/src/main/scala/org/scalajs/dom/Worker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ import scala.scalajs.js.annotation._
* Of note is the fact that workers may in turn spawn new workers as long as those workers are hosted within the same
* origin as the parent page. In addition, workers may use XMLHttpRequest for network I/O, with the exception that the
* responseXML and channel attributes on XMLHttpRequest always return null.
*
* @param scriptURL
* A USVString representing the URL of the script the worker will execute. It must obey the same-origin policy.
* @param options
* An object containing option properties that can be set when creating the object instance.
*/
@js.native
@JSGlobal
class Worker(stringUrl: String) extends AbstractWorker {
class Worker(scriptURL: String, options: WorkerOptions) extends AbstractWorker {

def this(scriptURL: String) = this(scriptURL, js.native)

/** The Worker.onmessage property represents an EventHandler, that is a function to be called when the message event
* occurs. These events are of type MessageEvent and will be called when the worker calls its own postMessage()
Expand Down
9 changes: 9 additions & 0 deletions dom/src/main/scala/org/scalajs/dom/WorkerOptions.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.scalajs.dom

import scala.scalajs.js

trait WorkerOptions extends js.Object {
var credentials: js.UndefOr[RequestCredentials] = js.undefined
var name: js.UndefOr[String] = js.undefined
var `type`: js.UndefOr[WorkerType] = js.undefined
}