Skip to content

Migrate beacon to dom #564

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 2 commits into from
Sep 6, 2021
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
13 changes: 8 additions & 5 deletions api-reports/2_12.txt
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ AudioTrackList[JT] var onaddtrack: js.Function1[TrackEvent, _]
AudioTrackList[JT] var onchange: js.Function1[js.Any, _]
AudioTrackList[JT] def removeEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], options: EventListenerOptions): Unit
AudioTrackList[JT] def removeEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], useCapture: Boolean?): Unit
BeaconNavigator[JT] def sendBeacon(url: String, data: dom.BodyInit = null): Boolean
BeaconWorkerNavigator[JT] def sendBeacon(url: String, data: dom.BodyInit = null): Boolean
BeforeUnloadEvent[JC] def bubbles: Boolean
BeforeUnloadEvent[JC] def cancelBubble: Boolean
BeforeUnloadEvent[JC] def cancelable: Boolean
Expand Down Expand Up @@ -14356,6 +14358,7 @@ Navigator[JC] def platform: String
Navigator[JC] def userAgent: String
Navigator[JC] def vibrate(duration: Double): Boolean
Navigator[JC] def vibrate(pattern: js.Array[Double]): Boolean
Navigator[SO] implicit def toBeaconNavigator(n: Navigator): BeaconNavigator
NavigatorContentUtils[JT]
NavigatorGeolocation[JT] def geolocation: Geolocation
NavigatorID[JT] def appName: String
Expand Down Expand Up @@ -24013,6 +24016,7 @@ WorkerNavigator[JT] def languages: js.Array[String]
WorkerNavigator[JT] def onLine: Boolean
WorkerNavigator[JT] def platform: String
WorkerNavigator[JT] def userAgent: String
WorkerNavigator[SO] implicit def toBeaconWorkerNavigator(n: WorkerNavigator): BeaconWorkerNavigator
WriteableState[JT]
WriteableState[SO] val closed: WriteableState
WriteableState[SO] val closing: WriteableState
Expand Down Expand Up @@ -24309,11 +24313,10 @@ experimental/PointerLock.PointerLockElement[JT] def requestPointerLock(): Unit
experimental/PointerLock.PointerLockMouseEvent[JT] def movementX: Double
experimental/PointerLock.PointerLockMouseEvent[JT] def movementY: Double
experimental/Vibration[SO] (@deprecated in 2.0.0)
experimental/beacon/package[SO] def sendBeacon(url: String, data: dom.BodyInit = null): Boolean
experimental/beacon/package[SO] implicit def toBeaconNavigator(n: Navigator): BeaconNavigator
experimental/beacon/package[SO] implicit def toBeaconWorkerNavigator(n: WorkerNavigator): BeaconWorkerNavigator
experimental/beacon/package.BeaconNavigator[JT] def sendBeacon(url: String, data: dom.BodyInit = null): Boolean
experimental/beacon/package.BeaconWorkerNavigator[JT] def sendBeacon(url: String, data: dom.BodyInit = null): Boolean
experimental/beacon/package[SO] type BeaconNavigator = dom.BeaconNavigator (@deprecated in 2.0.0)
experimental/beacon/package[SO] type BeaconWorkerNavigator = dom.BeaconWorkerNavigator (@deprecated in 2.0.0)
experimental/beacon/package[SO] implicit def toBeaconNavigator(n: Navigator): BeaconNavigator (@deprecated in 2.0.0)
experimental/beacon/package[SO] implicit def toBeaconWorkerNavigator(n: WorkerNavigator): BeaconWorkerNavigator (@deprecated in 2.0.0)
experimental/cachestorage/Cache[JC] def add(request: RequestInfo): js.Promise[Unit]
experimental/cachestorage/Cache[JC] def addAll(requests: js.Array[RequestInfo]): js.Promise[Unit]
experimental/cachestorage/Cache[JC] def delete(request: RequestInfo, options: js.UndefOr[CacheQueryOptions]?): js.Promise[Boolean]
Expand Down
13 changes: 8 additions & 5 deletions api-reports/2_13.txt
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ AudioTrackList[JT] var onaddtrack: js.Function1[TrackEvent, _]
AudioTrackList[JT] var onchange: js.Function1[js.Any, _]
AudioTrackList[JT] def removeEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], options: EventListenerOptions): Unit
AudioTrackList[JT] def removeEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], useCapture: Boolean?): Unit
BeaconNavigator[JT] def sendBeacon(url: String, data: dom.BodyInit = null): Boolean
BeaconWorkerNavigator[JT] def sendBeacon(url: String, data: dom.BodyInit = null): Boolean
BeforeUnloadEvent[JC] def bubbles: Boolean
BeforeUnloadEvent[JC] def cancelBubble: Boolean
BeforeUnloadEvent[JC] def cancelable: Boolean
Expand Down Expand Up @@ -14356,6 +14358,7 @@ Navigator[JC] def platform: String
Navigator[JC] def userAgent: String
Navigator[JC] def vibrate(duration: Double): Boolean
Navigator[JC] def vibrate(pattern: js.Array[Double]): Boolean
Navigator[SO] implicit def toBeaconNavigator(n: Navigator): BeaconNavigator
NavigatorContentUtils[JT]
NavigatorGeolocation[JT] def geolocation: Geolocation
NavigatorID[JT] def appName: String
Expand Down Expand Up @@ -24013,6 +24016,7 @@ WorkerNavigator[JT] def languages: js.Array[String]
WorkerNavigator[JT] def onLine: Boolean
WorkerNavigator[JT] def platform: String
WorkerNavigator[JT] def userAgent: String
WorkerNavigator[SO] implicit def toBeaconWorkerNavigator(n: WorkerNavigator): BeaconWorkerNavigator
WriteableState[JT]
WriteableState[SO] val closed: WriteableState
WriteableState[SO] val closing: WriteableState
Expand Down Expand Up @@ -24309,11 +24313,10 @@ experimental/PointerLock.PointerLockElement[JT] def requestPointerLock(): Unit
experimental/PointerLock.PointerLockMouseEvent[JT] def movementX: Double
experimental/PointerLock.PointerLockMouseEvent[JT] def movementY: Double
experimental/Vibration[SO] (@deprecated in 2.0.0)
experimental/beacon/package[SO] def sendBeacon(url: String, data: dom.BodyInit = null): Boolean
experimental/beacon/package[SO] implicit def toBeaconNavigator(n: Navigator): BeaconNavigator
experimental/beacon/package[SO] implicit def toBeaconWorkerNavigator(n: WorkerNavigator): BeaconWorkerNavigator
experimental/beacon/package.BeaconNavigator[JT] def sendBeacon(url: String, data: dom.BodyInit = null): Boolean
experimental/beacon/package.BeaconWorkerNavigator[JT] def sendBeacon(url: String, data: dom.BodyInit = null): Boolean
experimental/beacon/package[SO] type BeaconNavigator = dom.BeaconNavigator (@deprecated in 2.0.0)
experimental/beacon/package[SO] type BeaconWorkerNavigator = dom.BeaconWorkerNavigator (@deprecated in 2.0.0)
experimental/beacon/package[SO] implicit def toBeaconNavigator(n: Navigator): BeaconNavigator (@deprecated in 2.0.0)
experimental/beacon/package[SO] implicit def toBeaconWorkerNavigator(n: WorkerNavigator): BeaconWorkerNavigator (@deprecated in 2.0.0)
experimental/cachestorage/Cache[JC] def add(request: RequestInfo): js.Promise[Unit]
experimental/cachestorage/Cache[JC] def addAll(requests: js.Array[RequestInfo]): js.Promise[Unit]
experimental/cachestorage/Cache[JC] def delete(request: RequestInfo, options: js.UndefOr[CacheQueryOptions]?): js.Promise[Boolean]
Expand Down
7 changes: 7 additions & 0 deletions src/main/scala/org/scalajs/dom/WebWorkerTypes.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.scalajs.dom

import scala.language.implicitConversions
import scala.scalajs.js
import scala.scalajs.js.annotation._

Expand Down Expand Up @@ -169,6 +170,12 @@ object DedicatedWorkerGlobalScope extends js.Object {
@js.native
trait WorkerNavigator extends NavigatorID with NavigatorOnLine with NavigatorLanguage

object WorkerNavigator {

implicit def toBeaconWorkerNavigator(n: WorkerNavigator): BeaconWorkerNavigator =
n.asInstanceOf[BeaconWorkerNavigator]
}

/** The WorkerLocation interface defines the absolute location of the script executed by the Worker. Such an object is
* initialized for each worker and is available via the WorkerGlobalScope.location property obtained by calling
* window.self.location.
Expand Down
52 changes: 52 additions & 0 deletions src/main/scala/org/scalajs/dom/beacon.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.scalajs.dom

import org.scalajs.dom
import scala.scalajs.js

/** The Beacon interface is used to schedule an asynchronous and non-blocking request to a web server. Beacon requests
* use the HTTP PUT method and requests typically do not require a response. Requests are guaranteed to be initiated
* before a page is unloaded and they are run to completion without requiring a blocking request (for example
* XMLHttpRequest).
*
* @see
* [[https://www.w3.org/TR/2016/WD-beacon-20160204/ Beacon W3C Working Draft]]
* @see
* [[https://developer.mozilla.org/en-US/docs/Web/API/Beacon_API MDN Beacon API]]
*/
@js.native
trait BeaconNavigator extends js.Object {

/** The navigator.sendBeacon() method can be used to asynchronously transfer small HTTP data from the User Agent to a
* web server.
*
* @param url
* The url parameter indicates the resolved URL where the data is to be transmitted.
* @param data
* The data parameter is the ArrayBufferView, Blob, DOMString, or FormData data that is to be transmitted.
*/
def sendBeacon(url: String, data: dom.BodyInit = null): Boolean = 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.

It would be much simpler and cleaner to directly integrate this method in Navigator instead of using those implicit conversions. See for example d7933f9 where I did this for the Vibration API.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point! I guess the reason they were separated before was to quarantine the experimental methods?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, that was it. But now it doesn't make sense anymore, and it just obscures the API.

}

/** The Beacon interface is used to schedule an asynchronous and non-blocking request to a web server. Beacon requests
* use the HTTP PUT method and requests typically do not require a response. Requests are guaranteed to be initiated
* before a page is unloaded and they are run to completion without requiring a blocking request (for example
* XMLHttpRequest).
*
* @see
* [[https://www.w3.org/TR/2016/WD-beacon-20160204/ Beacon W3C Working Draft]]
* @see
* [[https://developer.mozilla.org/en-US/docs/Web/API/Beacon_API MDN Beacon API]]
*/
@js.native
trait BeaconWorkerNavigator extends js.Object {

/** The navigator.sendBeacon() method can be used to asynchronously transfer small HTTP data from the User Agent to a
* web server.
*
* @param url
* The url parameter indicates the resolved URL where the data is to be transmitted.
* @param data
* The data parameter is the ArrayBufferView, Blob, DOMString, or FormData data that is to be transmitted.
*/
def sendBeacon(url: String, data: dom.BodyInit = null): Boolean = js.native
}
57 changes: 13 additions & 44 deletions src/main/scala/org/scalajs/dom/experimental/beacon/package.scala
Original file line number Diff line number Diff line change
@@ -1,54 +1,23 @@
package org.scalajs.dom.experimental
package org.scalajs
package dom
package experimental

import org.scalajs.dom
import org.scalajs.dom.{Navigator, WorkerNavigator}
import scala.language.implicitConversions
import scala.scalajs.js

/** The Beacon interface is used to schedule an asynchronous and non-blocking request to a web server. Beacon requests
* use the HTTP PUT method and requests typically do not require a response. Requests are guaranteed to be initiated
* before a page is unloaded and they are run to completion without requiring a blocking request (for example
* XMLHttpRequest).
*
* @see
* [[https://www.w3.org/TR/2016/WD-beacon-20160204/ Beacon W3C Working Draft]]
* @see
* [[https://developer.mozilla.org/en-US/docs/Web/API/Beacon_API MDN Beacon API]]
*/
package object beacon {

implicit def toBeaconNavigator(n: Navigator): BeaconNavigator =
n.asInstanceOf[BeaconNavigator]

implicit def toBeaconWorkerNavigator(n: WorkerNavigator): BeaconWorkerNavigator = {
n.asInstanceOf[BeaconWorkerNavigator]
}
@deprecated("use dom.BeaconNavigator instead", "2.0.0")
type BeaconNavigator = dom.BeaconNavigator

@js.native
trait BeaconNavigator extends js.Object {
@deprecated("use dom.BeaconWorkerNavigator instead", "2.0.0")
type BeaconWorkerNavigator = dom.BeaconWorkerNavigator

/** The navigator.sendBeacon() method can be used to asynchronously transfer small HTTP data from the User Agent to
* a web server.
*
* @param url
* The url parameter indicates the resolved URL where the data is to be transmitted.
* @param data
* The data parameter is the ArrayBufferView, Blob, DOMString, or FormData data that is to be transmitted.
*/
def sendBeacon(url: String, data: dom.BodyInit = null): Boolean = js.native
}
@deprecated("use dom.Navigator.toBeaconNavigator instead", "2.0.0")
implicit def toBeaconNavigator(n: Navigator): BeaconNavigator =
Navigator.toBeaconNavigator(n)

@js.native
trait BeaconWorkerNavigator extends js.Object {
@deprecated("use dom.WorkerNavigator.toBeaconWorkerNavigator instead", "2.0.0")
implicit def toBeaconWorkerNavigator(n: WorkerNavigator): BeaconWorkerNavigator =
WorkerNavigator.toBeaconWorkerNavigator(n)

/** The navigator.sendBeacon() method can be used to asynchronously transfer small HTTP data from the User Agent to
* a web server.
*
* @param url
* The url parameter indicates the resolved URL where the data is to be transmitted.
* @param data
* The data parameter is the ArrayBufferView, Blob, DOMString, or FormData data that is to be transmitted.
*/
def sendBeacon(url: String, data: dom.BodyInit = null): Boolean = js.native
}
}
7 changes: 7 additions & 0 deletions src/main/scala/org/scalajs/dom/lib.scala
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,13 @@ class Navigator
def clipboard: Clipboard = js.native
}

object Navigator {

implicit def toBeaconNavigator(n: Navigator): BeaconNavigator =
n.asInstanceOf[BeaconNavigator]

}

@js.native
trait NodeSelector extends js.Object {

Expand Down