Liking cljdoc? Tell your friends :D

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:

::workers

Returns service workers by scope.

::worker-registration [scope]

Given scope, will return ServiceWorkerRegistration object.

district.ui.service-workers.events

re-frame events provided by this module:

::register

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

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

::post-message [data & [ports]]

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.

::worker-registered

Event fired when worker has been succesfully registered.

::worker-register-error

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

::service-worker-ready

Event fired when js/navigator.serviceWorker is ready.

::service-worker-ready-error

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

::controller-changed

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

::unregister

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

::watch-service-worker-ready

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

::watch-controller-change

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

::register

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

::unregister

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

::post-message

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.

assoc-worker [db worker]

Associates new worker and returns new re-frame db.

assoc-workers [db]

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

assoc-worker-registration [db worker registration]

Associates ServiceWorkerRegistration object with existing worker.

workers [db]

Returns associated workers.

worker-registration [db scope]

Returns ServiceWorkerRegistration object given scope.

Development

lein deps
lein doo chrome tests once

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close