Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 159526a

Browse files
TATSUNO Yasuhirosjrd
TATSUNO Yasuhiro
authored andcommittedMay 16, 2019
Fix #219: Fix Event constructors. (#367)
* Add valid constructors that accept type name and init, as well as type-safe constructor options `***EventInit` * Removes invalid default (0 args) constructors since `new ***Event()` raises RuntimeError due to lack of required argument * Add `@deprecated` based on MDN
1 parent deffe6c commit 159526a

File tree

9 files changed

+259
-101
lines changed

9 files changed

+259
-101
lines changed
 

‎src/main/scala/org/scalajs/dom/experimental/deviceorientation/DeviceOrientation.scala

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package org.scalajs.dom.experimental.deviceorientation
22

33
import org.scalajs.dom
4+
import org.scalajs.dom.raw.EventInit
45

56
import scala.scalajs.js
67
import scala.scalajs.js.annotation._
78

89
@js.native
910
@JSGlobal
10-
class DeviceOrientationEvent(
11-
`type`: String,
12-
eventInitDict: DeviceOrientationEventInit
13-
) extends dom.Event {
11+
class DeviceOrientationEvent(typeArg: String,
12+
init: js.UndefOr[DeviceOrientationEventInit])
13+
extends dom.Event(typeArg, init) {
1414

1515
/** Z-Axis rotation in degrees. */
1616
val alpha: Double = js.native
@@ -29,26 +29,27 @@ class DeviceOrientationEvent(
2929
val absolute: Boolean = js.native
3030
}
3131

32-
trait DeviceOrientationEventInit extends js.Object {
32+
trait DeviceOrientationEventInit extends EventInit {
3333

3434
/** Z-Axis rotation in degrees. */
35-
val alpha: Double
35+
var alpha: js.UndefOr[Double] = js.undefined
3636

3737
/** X-Axis rotation in degrees. */
38-
val beta: Double
38+
var beta: js.UndefOr[Double] = js.undefined
3939

4040
/** Y-Axis rotation in degrees. */
41-
val gamma: Double
41+
var gamma: js.UndefOr[Double] = js.undefined
4242

4343
/**
4444
* If true, this event data is has been produced using sensor fusion from
4545
* the magnometer and other sensors. When false- only the gyroscope has
4646
* been used.
4747
*/
48-
val absolute: Boolean
48+
var absolute: js.UndefOr[Boolean] = js.undefined
4949
}
5050

5151
object DeviceOrientationEventInit {
52+
@deprecated("Create new DeviceOrientationEventInit instead", "0.9.8")
5253
def apply(alpha: Double, beta: Double, gamma: Double,
5354
absolute: Boolean): DeviceOrientationEventInit = {
5455
js.Dynamic
@@ -83,7 +84,9 @@ trait DeviceRotationRate extends js.Any {
8384

8485
@js.native
8586
@JSGlobal
86-
class DeviceMotionEvent extends dom.Event {
87+
class DeviceMotionEvent(typeArg: String,
88+
init: js.UndefOr[DeviceMotionEventInit] = js.undefined)
89+
extends dom.Event(typeArg, init) {
8790

8891
/** Device acceleration with gravity removed. */
8992
val acceleration: DeviceAcceleration = js.native
@@ -98,17 +101,18 @@ class DeviceMotionEvent extends dom.Event {
98101
val interval: Double = js.native
99102
}
100103

101-
trait DeviceMotionEventInit extends js.Any {
104+
trait DeviceMotionEventInit extends EventInit {
102105

103106
/** Device acceleration with gravity removed. */
104-
val acceleration: DeviceAcceleration
107+
val acceleration: js.UndefOr[DeviceAcceleration] = js.undefined
105108

106109
/** Device acceleration including the force of gravity. */
107-
val accelerationIncludingGravity: DeviceAcceleration
110+
val accelerationIncludingGravity: js.UndefOr[DeviceAcceleration] =
111+
js.undefined
108112

109113
/** The rate of rotation. */
110-
val rotationRate: DeviceRotationRate
114+
val rotationRate: js.UndefOr[DeviceRotationRate] = js.undefined
111115

112116
/** The sampling rate in seconds that data is received from the hardware. */
113-
val interval: Double
117+
val interval: js.UndefOr[Double] = js.undefined
114118
}

‎src/main/scala/org/scalajs/dom/experimental/gamepad/Gamepad.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import scala.scalajs.js
1010
import scala.scalajs.js.annotation._
1111

1212
import org.scalajs.dom
13+
import org.scalajs.dom.raw.EventInit
1314

1415
@js.native
1516
trait GamepadMappingType extends js.Any
@@ -65,18 +66,21 @@ trait Gamepad extends js.Any {
6566
val mapping: GamepadMappingType
6667
}
6768

68-
trait GamepadEventInit extends js.Any {
69-
val gamepad: Gamepad
69+
trait GamepadEventInit extends EventInit {
70+
var gamepad: js.UndefOr[Gamepad]
7071
}
7172

7273
object GamepadEventInit {
74+
@deprecated("Create new ClipboardEventInit instead", "0.9.8")
7375
def apply(gamepad: Gamepad): GamepadEventInit =
7476
js.Dynamic.literal("gamepad" -> gamepad).asInstanceOf[GamepadEventInit]
7577
}
7678

7779
@JSGlobal("GamepadEvent")
7880
@js.native
79-
class GamepadEvent(init: GamepadEventInit) extends dom.Event {
81+
class GamepadEvent(typeArg: String,
82+
init: js.UndefOr[GamepadEventInit] = js.undefined)
83+
extends dom.Event(typeArg, init) {
8084
val gamepad: Gamepad = js.native
8185
}
8286

‎src/main/scala/org/scalajs/dom/experimental/mediastream/MediaStream.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import scala.scalajs.js
77
import scala.scalajs.js.|
88
import scala.scalajs.js.annotation._
99

10-
import org.scalajs.dom.raw.{DOMError, Event, EventTarget}
10+
import org.scalajs.dom.raw.{DOMError, Event, EventInit, EventTarget}
1111

1212
/**
1313
* The MediaStream
@@ -462,12 +462,12 @@ object MediaStreamConstraints {
462462
}
463463
}
464464

465-
@js.native
466-
trait MediaStreamTrackEventInit extends js.Object {
467-
var track: MediaStreamTrack = js.native
465+
trait MediaStreamTrackEventInit extends EventInit {
466+
var track: js.UndefOr[MediaStreamTrack] = js.undefined
468467
}
469468

470469
object MediaStreamTrackEventInit {
470+
@deprecated("Create new MediaStreamTrackEventInit instead", "0.9.8")
471471
@inline
472472
def apply(
473473
track: js.UndefOr[MediaStreamTrack] = js.undefined
@@ -480,9 +480,9 @@ object MediaStreamTrackEventInit {
480480

481481
@js.native
482482
@JSGlobal
483-
class MediaStreamTrackEvent(`type`: String,
484-
eventInitDict: MediaStreamTrackEventInit)
485-
extends Event {
483+
class MediaStreamTrackEvent(typeArg: String,
484+
init: js.UndefOr[MediaStreamTrackEventInit])
485+
extends Event(typeArg, init) {
486486
val track: MediaStreamTrack = js.native
487487
}
488488

‎src/main/scala/org/scalajs/dom/experimental/serviceworkers/ServiceWorkers.scala

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import org.scalajs.dom.experimental.{
99
}
1010
import org.scalajs.dom.raw.{WorkerGlobalScope, ErrorEvent}
1111
import org.scalajs.dom.webgl.RenderingContext
12-
import org.scalajs.dom.{Event, EventTarget, MessagePort}
12+
import org.scalajs.dom.{Event, EventTarget, MessageEvent, MessagePort}
13+
import org.scalajs.dom.raw.EventInit
1314

1415
@js.native
1516
sealed trait FrameType extends js.Any
@@ -80,6 +81,12 @@ trait CanvasProxy extends js.Any {
8081
def setContext(context: RenderingContext): Unit = js.native
8182
}
8283

84+
trait FetchEventInit extends EventInit {
85+
var isReload: js.UndefOr[Boolean] = js.undefined
86+
var request: js.UndefOr[Request] = js.undefined
87+
var clientId: js.UndefOr[String] = js.undefined
88+
}
89+
8390
/**
8491
* See [[https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent FetchEvent]] on MDN
8592
*
@@ -88,7 +95,8 @@ trait CanvasProxy extends js.Any {
8895
*/
8996
@js.native
9097
@JSGlobal
91-
class FetchEvent extends Event {
98+
class FetchEvent(typeArg: String, init: js.UndefOr[FetchEventInit])
99+
extends Event(typeArg, init) {
92100

93101
/**
94102
* Boolean that is true if the event was dispatched with the user's
@@ -103,6 +111,14 @@ class FetchEvent extends Event {
103111
*/
104112
def request: Request = js.native
105113

114+
def preloadResponse: js.Promise[Response] = js.native
115+
116+
def clientId: String = js.native
117+
118+
def replacesClientId: String = js.native
119+
120+
def resultingClientId: String = js.native
121+
106122
/**
107123
* See [[https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent/respondWith respondWith]]
108124
* page on MDN.
@@ -382,9 +398,11 @@ trait ServiceWorkerContainer extends EventTarget {
382398
*
383399
* MDN
384400
*/
385-
var onmessage: js.Function1[ServiceWorkerMessageEvent, _] = js.native
401+
var onmessage: js.Function1[MessageEvent, _] = js.native
386402
}
387403

404+
trait ExtendableEventInit extends EventInit {}
405+
388406
/**
389407
* See [[https://slightlyoff.github.io/ServiceWorker/spec/service_worker_1/#extendable-event-interface ¶4.4 ExtendableEvent]]
390408
* of whatwg ServiceWorker spec.
@@ -394,21 +412,21 @@ trait ServiceWorkerContainer extends EventTarget {
394412
*/
395413
@js.native
396414
@JSGlobal
397-
class ExtendableEvent extends Event {
415+
class ExtendableEvent(typeArg: String, init: js.UndefOr[ExtendableEventInit])
416+
extends Event(typeArg, init) {
398417
def waitUntil(promise: js.Promise[Any]): Unit = js.native
399418
}
400419

401-
@js.native
402-
trait ExtendableMessageEventInit extends js.Object {
403-
var data: js.Any = js.native
420+
trait ExtendableMessageEventInit extends ExtendableEventInit {
421+
var data: js.UndefOr[Any] = js.undefined
404422

405-
var origin: String = js.native
423+
var origin: js.UndefOr[String] = js.undefined
406424

407-
var lastEventId: String = js.native
425+
var lastEventId: js.UndefOr[String] = js.undefined
408426

409-
var source: Client | ServiceWorker | MessagePort = js.native
427+
var source: js.UndefOr[Client | ServiceWorker | MessagePort] = js.undefined
410428

411-
var ports: js.Array[MessagePort] = js.native
429+
var ports: js.UndefOr[js.Array[MessagePort]] = js.undefined
412430
}
413431

414432
/**
@@ -419,9 +437,9 @@ trait ExtendableMessageEventInit extends js.Object {
419437
*/
420438
@js.native
421439
@JSGlobal
422-
class ExtendableMessageEvent(`type`: String,
423-
eventInitDict: ExtendableMessageEventInit)
424-
extends ExtendableEvent {
440+
class ExtendableMessageEvent(typeArg: String,
441+
init: js.UndefOr[ExtendableMessageEventInit])
442+
extends ExtendableEvent(typeArg, init) {
425443

426444
/**
427445
* Returns the event's data. It can be any data type.
@@ -449,17 +467,12 @@ class ExtendableMessageEvent(`type`: String,
449467
def ports: js.Array[MessagePort] = js.native
450468
}
451469

452-
@js.native
453-
trait ServiceWorkerMessageEventInit extends js.Object {
454-
var data: js.Any = js.native
455-
456-
var origin: String = js.native
457-
458-
var lastEventId: String = js.native
459-
460-
var source: ServiceWorker | MessagePort = js.native
461-
462-
var ports: js.Array[MessagePort] = js.native
470+
trait ServiceWorkerMessageEventInit extends EventInit {
471+
var data: js.UndefOr[Any] = js.undefined
472+
var origin: js.UndefOr[String] = js.undefined
473+
var lastEventId: js.UndefOr[String] = js.undefined
474+
var source: js.UndefOr[ServiceWorker | MessagePort] = js.undefined
475+
var ports: js.UndefOr[js.Array[MessagePort]] = js.undefined
463476
}
464477

465478
/**
@@ -474,9 +487,10 @@ trait ServiceWorkerMessageEventInit extends js.Object {
474487
*/
475488
@js.native
476489
@JSGlobal
477-
class ServiceWorkerMessageEvent(`type`: String,
478-
eventInitDict: ServiceWorkerMessageEventInit = js.native)
479-
extends Event {
490+
@deprecated("Instead use MessageEvent", "0.9.8")
491+
class ServiceWorkerMessageEvent(typeArg: String,
492+
init: js.UndefOr[ServiceWorkerMessageEventInit] = js.undefined)
493+
extends Event(typeArg, init) {
480494

481495
/**
482496
* Returns the event's data. It can be any data type.
@@ -780,7 +794,7 @@ trait ServiceWorkerGlobalScope extends WorkerGlobalScope {
780794
*
781795
* MDN
782796
*/
783-
var onmessage: js.Function1[ServiceWorkerMessageEvent, _] = js.native
797+
var onmessage: js.Function1[MessageEvent, _] = js.native
784798

785799
/**
786800
* Forces the waiting service worker to become the active service worker.

‎src/main/scala/org/scalajs/dom/experimental/webrtc/WebRTC.scala

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import scala.scalajs.js.typedarray.{ArrayBufferView, ArrayBuffer}
99
import scala.scalajs.js.|
1010

1111
import org.scalajs.dom.Blob
12-
import org.scalajs.dom.raw.{DOMError, Event, EventTarget, MessageEvent}
12+
import org.scalajs.dom.raw.{
13+
DOMError, Event, EventInit, EventTarget, MessageEvent
14+
}
1315
import org.scalajs.dom.experimental.mediastream._
1416

1517
@js.native
@@ -498,8 +500,10 @@ trait RTCDataChannelInit extends js.Object {
498500
*/
499501
@js.native
500502
@JSGlobal
501-
class RTCDataChannelEvent protected () extends Event {
502-
def this(eventInitDict: RTCDataChannelEventInit) = this()
503+
class RTCDataChannelEvent(typeArg: String,
504+
init: js.UndefOr[RTCDataChannelEventInit])
505+
extends Event(typeArg, init) {
506+
def this(init: RTCDataChannelEventInit) = this("datachannel", init)
503507

504508
/**
505509
* Contains the RTCDataChannel containing the data channel associated with
@@ -510,9 +514,8 @@ class RTCDataChannelEvent protected () extends Event {
510514
val channel: RTCDataChannel = js.native
511515
}
512516

513-
@js.native
514-
trait RTCDataChannelEventInit extends js.Object {
515-
val channel: RTCDataChannel = js.native
517+
trait RTCDataChannelEventInit extends EventInit {
518+
var channel: js.UndefOr[RTCDataChannel] = js.undefined
516519
}
517520

518521
object RTCDataChannelInit {
@@ -634,12 +637,12 @@ trait RTCStatsReport extends js.Object {
634637
def apply(id: String): RTCStats = js.native
635638
}
636639

637-
@js.native
638-
trait RTCPeerConnectionIceEventInit extends js.Object {
639-
var candidate: RTCIceCandidate = js.native
640+
trait RTCPeerConnectionIceEventInit extends EventInit {
641+
var candidate: js.UndefOr[RTCIceCandidate] = js.undefined
640642
}
641643

642644
object RTCPeerConnectionIceEventInit {
645+
@deprecated("Create new RTCPeerConnectionIceEventInit instead", "0.9.8")
643646
@inline
644647
def apply(
645648
candidate: js.UndefOr[RTCIceCandidate] = js.undefined
@@ -659,9 +662,9 @@ object RTCPeerConnectionIceEventInit {
659662
*/
660663
@js.native
661664
@JSGlobal
662-
class RTCPeerConnectionIceEvent(`type`: String,
663-
eventInitDict: RTCPeerConnectionIceEventInit)
664-
extends Event {
665+
class RTCPeerConnectionIceEvent(typeArg: String,
666+
init: js.UndefOr[RTCPeerConnectionIceEventInit])
667+
extends Event(typeArg, init) {
665668

666669
/**
667670
* Contains the RTCIceCandidate containing the candidate associated with
@@ -775,6 +778,10 @@ object RTCIceGatheringState {
775778
val complete = "complete".asInstanceOf[RTCIceGatheringState]
776779
}
777780

781+
trait MediaStreamEventInit extends EventInit {
782+
var stream: js.UndefOr[MediaStream] = js.undefined
783+
}
784+
778785
/**
779786
* The MediaStreamEvent interface represents events that occurs in relation to a
780787
* MediaStream. Two events of this type can be thrown:
@@ -784,8 +791,9 @@ object RTCIceGatheringState {
784791
*/
785792
@js.native
786793
@JSGlobal
787-
class MediaStreamEvent(`type`: String, ms: js.Dictionary[js.Any])
788-
extends Event {
794+
@deprecated("Obsolte.", "0.9.8")
795+
class MediaStreamEvent(typeArg: String, init: js.UndefOr[MediaStreamEventInit])
796+
extends Event(typeArg, init) {
789797
val stream: MediaStream = js.native
790798
}
791799

@@ -926,7 +934,14 @@ class RTCPeerConnection(
926934
*
927935
* MDN
928936
*/
929-
var onaddstream: js.Function1[MediaStreamEvent, Any] = js.native
937+
@deprecated("Deprecated in favor of ontrack", "0.9.8")
938+
def onaddstream: js.Function1[MediaStreamEvent, Any] = js.native
939+
940+
@deprecated("Deprecated in favor of ontrack", "0.9.8")
941+
def onaddstream_=(
942+
handler: js.Function1[MediaStreamEvent, Any]): Unit = js.native
943+
944+
var ontrack: js.Function1[MediaStreamTrackEvent, Any] = js.native
930945

931946
/**
932947
* Is the event handler called when the datachannel event is received. Such
@@ -1003,7 +1018,14 @@ class RTCPeerConnection(
10031018
*
10041019
* MDN
10051020
*/
1006-
var onremovestream: js.Function1[MediaStreamEvent, Any] = js.native
1021+
@deprecated("Deprecated in favor of onremovetrack", "0.9.8")
1022+
def onremovestream: js.Function1[MediaStreamEvent, Any] = js.native
1023+
1024+
@deprecated("Deprecated in favor of onremovetrack", "0.9.8")
1025+
def onremovestream_=(
1026+
handler: js.Function1[MediaStreamEvent, Any]): Unit = js.native
1027+
1028+
var onremovetrack: js.Function1[MediaStreamTrackEvent, Any] = js.native
10071029

10081030
/**
10091031
* Is the event handler called when the signalingstatechange event, sent when

‎src/main/scala/org/scalajs/dom/raw/Idb.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ class IDBObjectStore extends js.Object {
122122
def delete(key: js.Any): IDBRequest = js.native
123123
}
124124

125+
trait IDBVersionChangeEventInit extends EventInit {
126+
var newVersion: js.UndefOr[Int] = js.undefined
127+
var oldVersion: js.UndefOr[Int] = js.undefined
128+
}
129+
125130
/**
126131
* The specification has changed and some not up-to-date browsers only support the
127132
* deprecated unique attribute, version, from an early draft version.
@@ -130,7 +135,9 @@ class IDBObjectStore extends js.Object {
130135
*/
131136
@js.native
132137
@JSGlobal
133-
class IDBVersionChangeEvent extends Event {
138+
class IDBVersionChangeEvent(typeArg: String,
139+
init: js.UndefOr[IDBVersionChangeEventInit])
140+
extends Event(typeArg, init) {
134141

135142
/**
136143
* Returns the new version of the database.

‎src/main/scala/org/scalajs/dom/raw/Svg.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,19 @@ class SVGPathSegCurvetoCubicSmoothAbs extends SVGPathSeg {
8787
var y2: Double = js.native
8888
}
8989

90+
trait SVGZoomEventInit extends UIEventInit {
91+
var zoomRectScreen: js.UndefOr[SVGRect] = js.undefined
92+
var previousScale: js.UndefOr[Double] = js.undefined
93+
var newScale: js.UndefOr[Double] = js.undefined
94+
var previousTranslate: js.UndefOr[SVGPoint] = js.undefined
95+
var newTranslate: js.UndefOr[SVGPoint] = js.undefined
96+
}
97+
9098
@js.native
9199
@JSGlobal
92-
class SVGZoomEvent extends UIEvent {
100+
@deprecated("Removed from SVG 2.0", "0.9.8")
101+
class SVGZoomEvent(typeArg: String, init: js.UndefOr[SVGZoomEventInit])
102+
extends UIEvent(typeArg, init) {
93103
def zoomRectScreen: SVGRect = js.native
94104

95105
def previousScale: Double = js.native

‎src/main/scala/org/scalajs/dom/raw/lib.scala

Lines changed: 125 additions & 29 deletions
Large diffs are not rendered by default.

‎src/main/scala/org/scalajs/dom/svg.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,5 +169,6 @@ object svg {
169169

170170
type ZoomAndPan = raw.SVGZoomAndPan
171171
@inline def ZoomAndPan = raw.SVGZoomAndPan
172+
@deprecated("Removed from SVG 2.0", "0.9.8")
172173
type ZoomEvent = raw.SVGZoomEvent
173174
}

0 commit comments

Comments
 (0)
Please sign in to comment.