Skip to content

Commit 5bcdadd

Browse files
authored
Merge pull request #57 from rescript-lang/notifications
Add bindings for push notifications using service worker
2 parents a8a8f3b + df5ae0d commit 5bcdadd

40 files changed

+485
-49
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
},
3636
"license": "MIT",
3737
"dependencies": {
38-
"rescript": "^12.0.0-alpha.6"
38+
"rescript": "^12.0.0-alpha.8"
3939
},
4040
"devDependencies": {
4141
"@astrojs/starlight": "0.32.0",

src/DOMAPI.res

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,14 @@ open MediaCaptureAndStreamsAPI
1010
open MediaSessionAPI
1111
open PermissionsAPI
1212
open ScreenWakeLockAPI
13+
open WebWorkersAPI
1314
open ServiceWorkerAPI
1415
open EncryptedMediaExtensionsAPI
15-
open GamepadAPI
1616
open FileAPI
17-
open WebMIDIAPI
1817
open HistoryAPI
1918
open VisualViewportAPI
2019
open WebSpeechAPI
21-
open ViewTransitionsAPI
2220
open FileAndDirectoryEntriesAPI
23-
open WebVTTAPI
2421
open RemotePlaybackAPI
2522
open CanvasAPI
2623
open StorageAPI

src/EventAPI.res

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type eventType =
5151
| @as("mouseout") Mouseout
5252
| @as("mouseover") Mouseover
5353
| @as("mouseup") Mouseup
54+
| @as("notificationclick") NotificationClick
5455
| @as("paste") Paste
5556
| @as("pause") Pause
5657
| @as("play") Play
@@ -94,6 +95,7 @@ type eventType =
9495
| @as("pointercancel") Pointercancel
9596
| @as("pointerout") Pointerout
9697
| @as("pointerleave") Pointerleave
98+
| @as("push") Push
9799
| @as("gotpointercapture") Gotpointercapture
98100
| @as("lostpointercapture") Lostpointercapture
99101
| @as("selectstart") Selectstart
@@ -217,3 +219,12 @@ type eventInit = {
217219
mutable cancelable?: bool,
218220
mutable composed?: bool,
219221
}
222+
223+
/**
224+
The ExtendableEvent interface extends the lifetime of the install and activate events dispatched on the global scope as part of the service worker lifecycle.
225+
[See ExtendableEvent on MDN](https://developer.mozilla.org/docs/Web/API/ExtendableEvent)
226+
*/
227+
@editor.completeFrom(ExtendableEvent)
228+
type extendableEvent = {
229+
...event
230+
}

src/EventAPI/ExtendableEvent.js

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/EventAPI/ExtendableEvent.res

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
open EventAPI
2+
3+
module Impl = (
4+
T: {
5+
type t
6+
},
7+
) => {
8+
external asExtendableEvent: T.t => extendableEvent = "%identity"
9+
10+
include Event.Impl({
11+
type t = T.t
12+
})
13+
14+
@send
15+
external waitUntil: (T.t, promise<'a>) => unit = "waitUntil"
16+
}
17+
18+
include Impl({
19+
type t = extendableEvent
20+
})

src/Global.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ open WebSpeechAPI
55
open IndexedDBAPI
66
open WebCryptoAPI
77
open PerformanceAPI
8-
open ServiceWorkerAPI
8+
open WebWorkersAPI
99
open WebStorageAPI
1010
open CanvasAPI
1111
open FileAPI

src/NotificationAPI.res

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,17 @@ type notification = {
6363
/**
6464
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Notification/data)
6565
*/
66-
data: JSON.t,
66+
data?: JSON.t,
67+
}
68+
69+
/**
70+
An array of actions to display in the notification, for which the default is an empty array.
71+
[Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification#actions)
72+
*/
73+
type notificationAction = {
74+
action: string,
75+
title: string,
76+
icon?: string
6777
}
6878

6979
type notificationOptions = {
@@ -76,8 +86,21 @@ type notificationOptions = {
7686
mutable silent?: Null.t<bool>,
7787
mutable requireInteraction?: bool,
7888
mutable data?: JSON.t,
89+
mutable actions?: array<notificationAction>
7990
}
8091

8192
type getNotificationOptions = {mutable tag?: string}
8293

8394
type notificationPermissionCallback = notificationPermission => unit
95+
96+
type notificationEvent = {
97+
...extendableEvent,
98+
/**
99+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/NotificationEvent/action)
100+
*/
101+
action: string,
102+
/**
103+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/NotificationEvent/notification)
104+
*/
105+
notification: notification,
106+
}
File renamed without changes.

src/PushManagerAPI.res renamed to src/PushAPI.res

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@@warning("-30")
22

33
open Prelude
4+
open EventAPI
45

56
type permissionState =
67
| @as("denied") Denied
@@ -23,6 +24,8 @@ type pushManager = {
2324
supportedContentEncodings: array<string>,
2425
}
2526

27+
type applicationServerKey
28+
2629
/**
2730
[See PushSubscriptionOptions on MDN](https://developer.mozilla.org/docs/Web/API/PushSubscriptionOptions)
2831
*/
@@ -34,7 +37,7 @@ type pushSubscriptionOptions = {
3437
/**
3538
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/PushSubscriptionOptions/applicationServerKey)
3639
*/
37-
applicationServerKey: Null.t<ArrayBuffer.t>,
40+
applicationServerKey: applicationServerKey,
3841
}
3942

4043
/**
@@ -59,11 +62,23 @@ type pushSubscription = {
5962

6063
type pushSubscriptionOptionsInit = {
6164
mutable userVisibleOnly?: bool,
62-
mutable applicationServerKey?: Null.t<unknown>,
65+
mutable applicationServerKey?: applicationServerKey,
6366
}
6467

6568
type pushSubscriptionJSON = {
6669
mutable endpoint?: string,
6770
mutable expirationTime?: Null.t<int>,
6871
mutable keys?: any,
6972
}
73+
74+
@editor.completeFrom(PushMessageData)
75+
type pushMessageData
76+
77+
@editor.completeFrom(PushEvent)
78+
type pushEvent = {
79+
...extendableEvent,
80+
/**
81+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/PushEvent/data)
82+
*/
83+
data?: pushMessageData,
84+
}

0 commit comments

Comments
 (0)