Skip to content

Add bindings for push notifications using service worker #57

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 4 commits into from
Feb 20, 2025
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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
"license": "MIT",
"dependencies": {
"rescript": "^12.0.0-alpha.6"
"rescript": "^12.0.0-alpha.8"
},
"devDependencies": {
"@astrojs/starlight": "0.32.0",
Expand Down
5 changes: 1 addition & 4 deletions src/DOMAPI.res
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@ open MediaCaptureAndStreamsAPI
open MediaSessionAPI
open PermissionsAPI
open ScreenWakeLockAPI
open WebWorkersAPI
open ServiceWorkerAPI
open EncryptedMediaExtensionsAPI
open GamepadAPI
open FileAPI
open WebMIDIAPI
open HistoryAPI
open VisualViewportAPI
open WebSpeechAPI
open ViewTransitionsAPI
open FileAndDirectoryEntriesAPI
open WebVTTAPI
open RemotePlaybackAPI
open CanvasAPI
open StorageAPI
Expand Down
11 changes: 11 additions & 0 deletions src/EventAPI.res
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type eventType =
| @as("mouseout") Mouseout
| @as("mouseover") Mouseover
| @as("mouseup") Mouseup
| @as("notificationclick") NotificationClick
| @as("paste") Paste
| @as("pause") Pause
| @as("play") Play
Expand Down Expand Up @@ -94,6 +95,7 @@ type eventType =
| @as("pointercancel") Pointercancel
| @as("pointerout") Pointerout
| @as("pointerleave") Pointerleave
| @as("push") Push
| @as("gotpointercapture") Gotpointercapture
| @as("lostpointercapture") Lostpointercapture
| @as("selectstart") Selectstart
Expand Down Expand Up @@ -217,3 +219,12 @@ type eventInit = {
mutable cancelable?: bool,
mutable composed?: bool,
}

/**
The ExtendableEvent interface extends the lifetime of the install and activate events dispatched on the global scope as part of the service worker lifecycle.
[See ExtendableEvent on MDN](https://developer.mozilla.org/docs/Web/API/ExtendableEvent)
*/
@editor.completeFrom(ExtendableEvent)
type extendableEvent = {
...event
}
15 changes: 15 additions & 0 deletions src/EventAPI/ExtendableEvent.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/EventAPI/ExtendableEvent.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
open EventAPI

module Impl = (
T: {
type t
},
) => {
external asExtendableEvent: T.t => extendableEvent = "%identity"

include Event.Impl({
type t = T.t
})

@send
external waitUntil: (T.t, promise<'a>) => unit = "waitUntil"
}

include Impl({
type t = extendableEvent
})
2 changes: 1 addition & 1 deletion src/Global.res
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ open WebSpeechAPI
open IndexedDBAPI
open WebCryptoAPI
open PerformanceAPI
open ServiceWorkerAPI
open WebWorkersAPI
open WebStorageAPI
open CanvasAPI
open FileAPI
Expand Down
25 changes: 24 additions & 1 deletion src/NotificationAPI.res
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,17 @@ type notification = {
/**
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Notification/data)
*/
data: JSON.t,
data?: JSON.t,
}

/**
An array of actions to display in the notification, for which the default is an empty array.
[Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification#actions)
*/
type notificationAction = {
action: string,
title: string,
icon?: string
}

type notificationOptions = {
Expand All @@ -76,8 +86,21 @@ type notificationOptions = {
mutable silent?: Null.t<bool>,
mutable requireInteraction?: bool,
mutable data?: JSON.t,
mutable actions?: array<notificationAction>
}

type getNotificationOptions = {mutable tag?: string}

type notificationPermissionCallback = notificationPermission => unit

type notificationEvent = {
...extendableEvent,
/**
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/NotificationEvent/action)
*/
action: string,
/**
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/NotificationEvent/notification)
*/
notification: notification,
}
File renamed without changes.
19 changes: 17 additions & 2 deletions src/PushManagerAPI.res → src/PushAPI.res
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@@warning("-30")

open Prelude
open EventAPI

type permissionState =
| @as("denied") Denied
Expand All @@ -23,6 +24,8 @@ type pushManager = {
supportedContentEncodings: array<string>,
}

type applicationServerKey

/**
[See PushSubscriptionOptions on MDN](https://developer.mozilla.org/docs/Web/API/PushSubscriptionOptions)
*/
Expand All @@ -34,7 +37,7 @@ type pushSubscriptionOptions = {
/**
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/PushSubscriptionOptions/applicationServerKey)
*/
applicationServerKey: Null.t<ArrayBuffer.t>,
applicationServerKey: applicationServerKey,
}

/**
Expand All @@ -59,11 +62,23 @@ type pushSubscription = {

type pushSubscriptionOptionsInit = {
mutable userVisibleOnly?: bool,
mutable applicationServerKey?: Null.t<unknown>,
mutable applicationServerKey?: applicationServerKey,
}

type pushSubscriptionJSON = {
mutable endpoint?: string,
mutable expirationTime?: Null.t<int>,
mutable keys?: any,
}

@editor.completeFrom(PushMessageData)
type pushMessageData

@editor.completeFrom(PushEvent)
type pushEvent = {
...extendableEvent,
/**
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/PushEvent/data)
*/
data?: pushMessageData,
}
File renamed without changes.
4 changes: 4 additions & 0 deletions src/PushAPI/ApplicationServerKey.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
open PushAPI

external fromString: string => applicationServerKey = "%identity"
external fromUint8Array: Uint8Array.t => applicationServerKey = "%identity"
7 changes: 7 additions & 0 deletions src/PushAPI/PushEvent.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/PushAPI/PushEvent.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
open PushAPI

include ExtendableEvent.Impl({
type t = pushEvent;
});
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
open PushManagerAPI
open PushAPI

/**
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/PushManager/subscribe)
Expand Down
File renamed without changes.
15 changes: 15 additions & 0 deletions src/PushAPI/PushMessageData.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
open PushAPI

/**
The json() method of the PushMessageData interface extracts push message data by parsing it as a JSON string and returning the result.
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/PushMessageData/json)
*/
@send
external json: pushMessageData => JSON.t = "json"

/**
The text() method of the PushMessageData interface extracts push message data as a plain text string.
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/PushMessageData/text)
*/
@send
external text: pushMessageData => string = "text"
2 changes: 2 additions & 0 deletions src/PushAPI/PushSubscription.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
open PushManagerAPI
open PushAPI

/**
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/PushSubscription/getKey)
Expand Down
70 changes: 44 additions & 26 deletions src/ServiceWorkerAPI.res
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

open Prelude
open EventAPI
open PushManagerAPI
open NotificationAPI
open FetchAPI
open ChannelMessagingAPI
open PushAPI
open WebWorkersAPI

type serviceWorkerState =
| @as("activated") Activated
Expand Down Expand Up @@ -101,20 +99,6 @@ type serviceWorkerContainer = {
ready: promise<serviceWorkerRegistration>,
}

/**
The storage for Cache objects.
[See CacheStorage on MDN](https://developer.mozilla.org/docs/Web/API/CacheStorage)
*/
@editor.completeFrom(CacheStorage)
type cacheStorage = {}

/**
Provides a storage mechanism for Request / Response object pairs that are cached, for example as part of the ServiceWorker life cycle. Note that the Cache interface is exposed to windowed scopes as well as workers. You don't have to use it in conjunction with service workers, even though it is defined in the service worker spec.
[See Cache on MDN](https://developer.mozilla.org/docs/Web/API/Cache)
*/
@editor.completeFrom(Cache)
type cache = {}

type navigationPreloadState = {
mutable enabled?: bool,
mutable headerValue?: string,
Expand All @@ -126,15 +110,49 @@ type registrationOptions = {
mutable updateViaCache?: serviceWorkerUpdateViaCache,
}

type cacheQueryOptions = {
mutable ignoreSearch?: bool,
mutable ignoreMethod?: bool,
mutable ignoreVary?: bool,
type requestInfo = any

/**
The Clients interface provides access to Client objects. Access it via self.clients within a service worker.
[See Clients on MDN](https://developer.mozilla.org/en-US/docs/Web/API/Clients)
*/
@editor.completeFrom(Clients)
type clients

/**
The ServiceWorkerGlobalScope interface of the Service Worker API represents the global execution context of a service worker.
[See ServiceWorkerGlobalScope on MDN](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope)
*/
@editor.completeFrom(ServiceWorkerGlobalScope)
type serviceWorkerGlobalScope = {
...workerGlobalScope,
/**
[Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/clients)
*/
clients: clients,
/**
[Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/registration)
*/
registration: serviceWorkerRegistration,
}

type multiCacheQueryOptions = {
...cacheQueryOptions,
mutable cacheName?: string,
/**
The Client interface represents an executable context such as a Worker, or a SharedWorker. Window clients are represented by the more-specific WindowClient.
[See Client on MDN](https://developer.mozilla.org/en-US/docs/Web/API/Client)
*/
type client = {
/**
[Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/Client/id)
*/
id: string,
/** [Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/Client/url) */
url: string,
}

type requestInfo = any
/**
The WindowClient interface of the ServiceWorker API represents the scope of a service worker client that is a document in a browsing context, controlled by an active worker.
[See WindowClient on MDN](https://developer.mozilla.org/en-US/docs/Web/API/WindowClient)
*/
type windowClient = {
...client,
}
2 changes: 1 addition & 1 deletion src/ServiceWorkerAPI/Cache.res
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
open FetchAPI
open ServiceWorkerAPI
open WebWorkersAPI

/**
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Cache/match)
Expand Down
2 changes: 2 additions & 0 deletions src/ServiceWorkerAPI/Clients.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/ServiceWorkerAPI/Clients.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
open ServiceWorkerAPI

@send
external openWindow: (clients, string) => promise<windowClient> = "open"
3 changes: 1 addition & 2 deletions src/ServiceWorkerAPI/ServiceWorkerContainer.res
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
open EventAPI
open ServiceWorkerAPI

include EventTarget.Impl({
Expand All @@ -11,7 +10,7 @@ include EventTarget.Impl({
@send
external register: (
serviceWorkerContainer,
~scriptURL: string,
string,
~options: registrationOptions=?,
) => promise<serviceWorkerRegistration> = "register"

Expand Down
7 changes: 7 additions & 0 deletions src/ServiceWorkerAPI/ServiceWorkerGlobalScope.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/ServiceWorkerAPI/ServiceWorkerGlobalScope.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
open ServiceWorkerAPI

include WorkerGlobalScope.Impl({
type t = serviceWorkerGlobalScope
})
2 changes: 2 additions & 0 deletions src/WebWorkersAPI.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading