@serwist/window 8.4.3 → 9.0.0-preview.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Serwist.d.ts +1 -0
- package/dist/Serwist.d.ts.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/messageSW.d.ts +1 -0
- package/dist/messageSW.d.ts.map +1 -0
- package/dist/utils/SerwistEvent.d.ts +1 -0
- package/dist/utils/SerwistEvent.d.ts.map +1 -0
- package/dist/utils/SerwistEventTarget.d.ts +1 -0
- package/dist/utils/SerwistEventTarget.d.ts.map +1 -0
- package/dist/utils/urlsMatch.d.ts +1 -0
- package/dist/utils/urlsMatch.d.ts.map +1 -0
- package/package.json +18 -16
- package/src/Serwist.ts +709 -0
- package/src/index.ts +15 -0
- package/src/messageSW.ts +33 -0
- package/src/utils/SerwistEvent.ts +57 -0
- package/src/utils/SerwistEventTarget.ts +68 -0
- package/src/utils/urlsMatch.ts +21 -0
- package/dist/index.cjs +0 -710
- package/dist/index.d.cts +0 -4
package/src/index.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2019 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { Serwist } from "./Serwist.js";
|
|
10
|
+
import { messageSW } from "./messageSW.js";
|
|
11
|
+
|
|
12
|
+
export { messageSW, Serwist };
|
|
13
|
+
|
|
14
|
+
// See https://github.com/GoogleChrome/workbox/issues/2770
|
|
15
|
+
export * from "./utils/SerwistEvent.js";
|
package/src/messageSW.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2019 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Sends a data object to a service worker via `postMessage` and resolves with
|
|
11
|
+
* a response (if any).
|
|
12
|
+
*
|
|
13
|
+
* A response can be set in a message handler in the service worker by
|
|
14
|
+
* calling `event.ports[0].postMessage(...)`, which will resolve the promise
|
|
15
|
+
* returned by `messageSW()`. If no response is set, the promise will not
|
|
16
|
+
* resolve.
|
|
17
|
+
*
|
|
18
|
+
* @param sw The service worker to send the message to.
|
|
19
|
+
* @param data An object to send to the service worker.
|
|
20
|
+
* @returns
|
|
21
|
+
*/
|
|
22
|
+
// biome-ignore lint/complexity/noBannedTypes: Better not change type of data.
|
|
23
|
+
function messageSW(sw: ServiceWorker, data: {}): Promise<any> {
|
|
24
|
+
return new Promise((resolve) => {
|
|
25
|
+
const messageChannel = new MessageChannel();
|
|
26
|
+
messageChannel.port1.onmessage = (event: MessageEvent) => {
|
|
27
|
+
resolve(event.data);
|
|
28
|
+
};
|
|
29
|
+
sw.postMessage(data, [messageChannel.port2]);
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export { messageSW };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2019 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { SerwistEventTarget } from "./SerwistEventTarget.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A minimal `Event` subclass shim.
|
|
13
|
+
* This doesn't *actually* subclass `Event` because not all browsers support
|
|
14
|
+
* constructable `EventTarget`, and using a real `Event` will error.
|
|
15
|
+
* @private
|
|
16
|
+
*/
|
|
17
|
+
export class SerwistEvent<K extends keyof SerwistEventMap> {
|
|
18
|
+
target?: SerwistEventTarget;
|
|
19
|
+
sw?: ServiceWorker;
|
|
20
|
+
originalEvent?: Event;
|
|
21
|
+
isExternal?: boolean;
|
|
22
|
+
|
|
23
|
+
constructor(
|
|
24
|
+
public type: K,
|
|
25
|
+
props: Omit<SerwistEventMap[K], "target" | "type">,
|
|
26
|
+
) {
|
|
27
|
+
Object.assign(this, props);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface SerwistMessageEvent extends SerwistEvent<"message"> {
|
|
32
|
+
data: any;
|
|
33
|
+
originalEvent: Event;
|
|
34
|
+
ports: readonly MessagePort[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface SerwistLifecycleEvent extends SerwistEvent<keyof SerwistLifecycleEventMap> {
|
|
38
|
+
isUpdate?: boolean;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface SerwistLifecycleWaitingEvent extends SerwistLifecycleEvent {
|
|
42
|
+
wasWaitingBeforeRegister?: boolean;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface SerwistLifecycleEventMap {
|
|
46
|
+
installing: SerwistLifecycleEvent;
|
|
47
|
+
installed: SerwistLifecycleEvent;
|
|
48
|
+
waiting: SerwistLifecycleWaitingEvent;
|
|
49
|
+
activating: SerwistLifecycleEvent;
|
|
50
|
+
activated: SerwistLifecycleEvent;
|
|
51
|
+
controlling: SerwistLifecycleEvent;
|
|
52
|
+
redundant: SerwistLifecycleEvent;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface SerwistEventMap extends SerwistLifecycleEventMap {
|
|
56
|
+
message: SerwistMessageEvent;
|
|
57
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2019 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { SerwistEvent, SerwistEventMap } from "./SerwistEvent.js";
|
|
10
|
+
|
|
11
|
+
export type ListenerCallback = (event: SerwistEvent<any>) => any;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A minimal `EventTarget` shim.
|
|
15
|
+
* This is necessary because not all browsers support constructable
|
|
16
|
+
* `EventTarget`, so using a real `EventTarget` will error.
|
|
17
|
+
* @private
|
|
18
|
+
*/
|
|
19
|
+
export class SerwistEventTarget {
|
|
20
|
+
private readonly _eventListenerRegistry: Map<keyof SerwistEventMap, Set<ListenerCallback>> = new Map();
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param type
|
|
24
|
+
* @param listener
|
|
25
|
+
* @private
|
|
26
|
+
*/
|
|
27
|
+
addEventListener<K extends keyof SerwistEventMap>(type: K, listener: (event: SerwistEventMap[K]) => any): void {
|
|
28
|
+
const foo = this._getEventListenersByType(type);
|
|
29
|
+
foo.add(listener as ListenerCallback);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @param type
|
|
34
|
+
* @param listener
|
|
35
|
+
* @private
|
|
36
|
+
*/
|
|
37
|
+
removeEventListener<K extends keyof SerwistEventMap>(type: K, listener: (event: SerwistEventMap[K]) => any): void {
|
|
38
|
+
this._getEventListenersByType(type).delete(listener as ListenerCallback);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @param event
|
|
43
|
+
* @private
|
|
44
|
+
*/
|
|
45
|
+
dispatchEvent(event: SerwistEvent<any>): void {
|
|
46
|
+
event.target = this;
|
|
47
|
+
|
|
48
|
+
const listeners = this._getEventListenersByType(event.type);
|
|
49
|
+
for (const listener of listeners) {
|
|
50
|
+
listener(event);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Returns a Set of listeners associated with the passed event type.
|
|
56
|
+
* If no handlers have been registered, an empty Set is returned.
|
|
57
|
+
*
|
|
58
|
+
* @param type The event type.
|
|
59
|
+
* @returns An array of handler functions.
|
|
60
|
+
* @private
|
|
61
|
+
*/
|
|
62
|
+
private _getEventListenersByType(type: keyof SerwistEventMap) {
|
|
63
|
+
if (!this._eventListenerRegistry.has(type)) {
|
|
64
|
+
this._eventListenerRegistry.set(type, new Set());
|
|
65
|
+
}
|
|
66
|
+
return this._eventListenerRegistry.get(type)!;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2019 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Returns true if two URLs have the same `.href` property. The URLS can be
|
|
11
|
+
* relative, and if they are the current location href is used to resolve URLs.
|
|
12
|
+
*
|
|
13
|
+
* @private
|
|
14
|
+
* @param url1
|
|
15
|
+
* @param url2
|
|
16
|
+
* @returns
|
|
17
|
+
*/
|
|
18
|
+
export function urlsMatch(url1: string, url2: string): boolean {
|
|
19
|
+
const { href } = location;
|
|
20
|
+
return new URL(url1, href).href === new URL(url2, href).href;
|
|
21
|
+
}
|