Skip to content

district0x/district-ui-service-workers

Repository files navigation

district-ui-service-workers

Build Status

Clojurescript re-mount module for handling Service Workers.

Installation

Latest released version of this library:
Clojars Project

Include [district.ui.service-workers] in your CLJS file, where you use mount/start

API Overview

district.ui.service-workers

This namespace contains service-workers mount module.

You can pass following args to initiate this module:

  • :workers Collection of workers to register at mount start
  (ns my-district.core
    (:require [mount.core :as mount]
              [district.ui.service-workers]))
              
  (-> (mount/with-args
        {:service-workers {:workers [{:script-url "/my-service-worker.js" :scope "/"}]}})
    (mount/start))

Important note: Don't forget to have your Service Worker file at the same path as you define scope. Therefore, if you define root scope "/", you need to have worker file accessible at https://my-server.com/my-service-worker.js. Also note, post-message function can be used only for a worker at root scope "/".

district.ui.service-workers.subs

re-frame subscriptions provided by this module:

Returns service workers by scope.

Given scope, will return ServiceWorkerRegistration object.

district.ui.service-workers.events

re-frame events provided by this module:

Will register new service worker. Pass configuration of single worker.

(dispatch [::service-workers-events/register {:script-url "/my-service-worker.js" :scope "/"}])

Will post message to service worker under root scope ("/").

(dispatch [::service-workers-events/post-message {:a 1 :b 2}])

In case you want to receive response as well, second, optional parameter is for MessageChannel ports, that will receive response. Follow tests for an example.

Event fired when worker has been succesfully registered.

Event fired when worker there's been error registering a service worker.

Event fired when js/navigator.serviceWorker is ready.

Event fired when js/navigator.serviceWorker failed to be ready.

Event fired when js/navigator.serviceWorker.controller has changed.

Event to unregister a service worker by its scope.

(dispatch [::service-workers-events/unregister {:scope "/"}])

district.ui.service-workers.effects

re-frame effects provided by this module

Effect to start watching when js/navigator.serviceWorker is ready.

Effect to start watching when js/navigator.serviceWorker.controller changes.

Effect to register new service worker. You should be using event for this unless you have special use case.

Effect to unregister a service worker. You should be using event for this unless you have special use case.

Effect to post message to a service worker. You should be using event for this unless you have special use case.

district.ui.service-workers.queries

DB queries provided by this module:
You should use them in your events, instead of trying to get this module's data directly with get-in into re-frame db.

Associates new worker and returns new re-frame db.

Associates multiple new workers and returns new re-frame db.

Associates ServiceWorkerRegistration object with existing worker.

Returns associated workers.

Returns ServiceWorkerRegistration object given scope.

Development

lein deps
lein doo chrome tests once

About

district UI module for handling Service Workers

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published