Skip to content

Commit fe6bb18

Browse files
committed
Add ServiceWorker
Add ServiceWorker
1 parent e51288d commit fe6bb18

File tree

1 file changed

+202
-1
lines changed

1 file changed

+202
-1
lines changed

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

Lines changed: 202 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
package org.scalajs.dom.raw
1212

1313
import scala.scalajs.js
14+
import scala.scalajs.js.annotation.JSName
1415

1516
object XPathResult extends js.Object {
1617

@@ -460,6 +461,202 @@ trait WindowTimers extends WindowTimersExtension {
460461
def setInterval(handler: js.Function0[Any], timeout: Double): Int = js.native
461462
}
462463

464+
/**
465+
* The Promise object is used for deferred and asynchronous computations. A Promise is in one of these states:
466+
* pending: initial state, not fulfilled or rejected.
467+
* fulfilled: successful operation
468+
* rejected: failed operation.
469+
* settled: the Promise is either fulfilled or rejected, but not pending.
470+
*
471+
* MDN
472+
*/
473+
trait Promise[+A] extends js.Object {
474+
475+
/**
476+
* The catch() method returns a Promise and deals with rejected cases only.
477+
* It behaves the same as calling Promise.prototype.then(undefined, onRejected).
478+
*
479+
* MDN
480+
*/
481+
@JSName("catch")
482+
def recover[B >: A](onRejected: js.Function1[Any, B]): Promise[Any] = js.native
483+
484+
/**
485+
* The then() method returns a Promise.
486+
* It takes two arguments, both are callback functions for the success and failure cases of the Promise.
487+
*
488+
* MDN
489+
*/
490+
@JSName("then")
491+
def andThen[B](onFulfilled: js.Function1[A,B]): Promise[Any] = js.native
492+
493+
@JSName("then")
494+
def andThen[B](onFulfilled: js.Function1[A,B], onRejected: js.Function1[Any,B]): Promise[Any] = js.native
495+
496+
}
497+
498+
object Promise extends js.Object {
499+
500+
/**
501+
* The Promise.all(iterable) method returns a promise that resolves when all of the promises in the iterable argument have resolved.
502+
*
503+
* MDN
504+
*/
505+
def all[B](iterable: js.Array[Promise[B]]): Promise[Any] = js.native
506+
507+
/**
508+
* The Promise.race(iterable) method returns a promise that resolves or rejects as soon as one of the promises in the iterable resolves or rejects, with the value or reason from that promise.
509+
*
510+
* MDN
511+
*/
512+
def race[B](iterable: js.Array[Promise[B]]): Promise[Any] = js.native
513+
514+
/**
515+
* The Promise.reject(reason) method returns a Promise object that is rejected with the given reason.
516+
*
517+
* MDN
518+
*/
519+
def reject(reason: String): Promise[Nothing] = js.native
520+
521+
}
522+
523+
/**
524+
* The ServiceWorker interface of the ServiceWorker API provides a reference to a service worker.
525+
* Multiple browsing contexts (e.g. pages, workers, etc.) can be associated with the same service worker,
526+
* each through a unique ServiceWorker object.
527+
*
528+
* MDN
529+
*/
530+
trait ServiceWorker extends EventTarget {
531+
/**
532+
* Returns the ServiceWorker serialized script URL defined as part of ServiceWorkerRegistration.
533+
* Must be on the same origin as the document that registers the ServiceWorker.
534+
*
535+
* MDN
536+
*/
537+
def scriptURL: String = js.native
538+
539+
/**
540+
* The state read-only property of the ServiceWorker interface returns a string representing the current state of the service worker.
541+
* It can be one of the following values: installing, installed, activating, activated, or redundant.
542+
*
543+
* MDN
544+
*/
545+
def state: String = js.native
546+
547+
/**
548+
* An EventListener property called whenever an event of type statechange is fired; it is basically fired anytime the ServiceWorker.state changes.
549+
*
550+
* MDN
551+
*/
552+
var onstatechange: js.Function1[Event, Any] = js.native
553+
554+
}
555+
556+
/**
557+
* The ServiceWorkerRegistion interface of the ServiceWorker API represents the service worker registration.
558+
* You register a service worker to control one or more pages that share the same origin.
559+
*
560+
* MDN
561+
*/
562+
trait ServiceWorkerRegistration extends EventTarget {
563+
/**
564+
* The installing property of the ServiceWorkerRegistration interface returns a service worker whose ServiceWorker.state is installing.
565+
* This property is initially set to null.
566+
*
567+
* MDN
568+
*/
569+
var installing: ServiceWorker = js.native
570+
571+
/**
572+
* The waiting property of the ServiceWorkerRegistration interface returns a service worker whose ServiceWorker.state is installed.
573+
* This property is initially set to null.
574+
*
575+
* MDN
576+
*/
577+
var waiting: ServiceWorker = js.native
578+
579+
/**
580+
* The active property of the ServiceWorkerRegistration interface returns a service worker whose ServiceWorker.state is activating or activated.
581+
* This property is initially set to null.
582+
*
583+
* MDN
584+
*/
585+
var active: ServiceWorker = js.native
586+
587+
/**
588+
* The scope read-only property of the ServiceWorkerRegistration interface returns a unique identifier for a service worker registration.
589+
* The service worker must be on the same origin as the document that registers the ServiceWorker.
590+
*
591+
* MDN
592+
*/
593+
var scope: String = js.native
594+
595+
/**
596+
* The update method of the ServiceWorkerRegistration interface allows you to ping the server for an updated service worker script.
597+
* If you don't explicitly call this,
598+
* the UA will do this automatically once every 24 hours.
599+
*
600+
* MDN
601+
*/
602+
def update(): Unit = js.native
603+
604+
/**
605+
* The unregister method of the ServiceWorkerRegistration interface unregisters the service worker registration and returns a Promise.
606+
* The promise will resolve to false if no registration was found, otherwise it resolves to true irrespective of whether unregistration happened or not (it may not unregister if someone else just called ServiceWorkerContainer.register with the same scope.)
607+
* The service worker will finish any ongoing operations before it is unregistered.
608+
*
609+
* MDN
610+
*/
611+
def unregister(): Promise[Boolean] = js.native
612+
613+
/**
614+
* The onupdatefound property of the ServiceWorkerRegistration interface is an EventListener property called whenever an event of type statechange is fired;
615+
* it is fired any time the ServiceWorkerRegistration.
616+
* installing property acquires a new service worker.
617+
*
618+
* MDN
619+
*/
620+
var onupdatefound: js.Function1[Event, Any] = js.native
621+
}
622+
623+
/**
624+
* The ServiceWorkerContainer interface of the ServiceWorker API exposes the ServiceWorkerContainer.
625+
* register(scriptURL, scope[, base]) method used to register service workers, and the ServiceWorkerContainer.
626+
* controller property used to determine whether or not the current page is actively controlled.
627+
*
628+
* MDN
629+
*/
630+
trait ServiceWorkerContainer extends EventTarget {
631+
632+
/**
633+
* Creates or updates a ServiceWorkerRegistration for the given scriptURL.
634+
* If successful, a service worker registration ties the provided script URL to a scope, which is subsequently used for navigation matching.
635+
* If the method can't return a ServiceWorkerRegistration, it returns a Promise.
636+
* You can call this method unconditionally from the controlled page, i.e.,
637+
* you don't need to first check whether there's an active registration.
638+
*
639+
* MDN
640+
*/
641+
def register(scriptURL: String, options: js.Object = new js.Object()): Promise[ServiceWorkerRegistration] = js.native
642+
643+
/**
644+
* The ServiceWorkerContainer.controller read-only property of the ServiceWorkerContainer interface returns the ServiceWorker whose state is activated (the same object returned by ServiceWorkerRegistration.active).
645+
* This property returns null if the request is a force refresh (Shift + refresh) or if there is no active worker.
646+
*
647+
* MDN
648+
*/
649+
def controller: ServiceWorker = js.native
650+
651+
/**
652+
* Gets a ServiceWorkerRegistration object whose scope URL matches the document URL.
653+
* If the method can't return a ServiceWorkerRegistration, it returns a Promise.
654+
*
655+
* MDN
656+
*/
657+
def getRegistration(scope: String = ""): Promise[ServiceWorkerRegistration] = js.native
658+
}
659+
463660
/**
464661
* The Navigator interface represents the state and the identity of the user
465662
* agent. It allows scripts to query it and to register themselves to carry
@@ -470,7 +667,11 @@ trait WindowTimers extends WindowTimersExtension {
470667
*
471668
* MDN
472669
*/
473-
class Navigator extends NavigatorID with NavigatorOnLine with NavigatorContentUtils with NavigatorGeolocation with NavigatorStorageUtils
670+
class Navigator extends NavigatorID with NavigatorOnLine with NavigatorContentUtils with NavigatorGeolocation with NavigatorStorageUtils {
671+
672+
val serviceWorker: ServiceWorkerContainer = js.native
673+
674+
}
474675

475676
trait NodeSelector extends js.Object {
476677
/**

0 commit comments

Comments
 (0)