Clojurescript re-mount module for handling Service Workers.
Latest released version of this library:
Include [district.ui.service-workers] in your CLJS file, where you use mount/start
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 "/".
re-frame subscriptions provided by this module:
::workersReturns service workers by scope.
::worker-registration [scope]Given scope, will return ServiceWorkerRegistration object.
re-frame events provided by this module:
::registerWill 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-registeredEvent fired when worker has been succesfully registered.
::worker-register-errorEvent fired when worker there's been error registering a service worker.
::service-worker-readyEvent fired when js/navigator.serviceWorker is ready.
::service-worker-ready-errorEvent fired when js/navigator.serviceWorker failed to be ready.
::controller-changedEvent fired when js/navigator.serviceWorker.controller has changed.
::unregisterEvent to unregister a service worker by its scope.
(dispatch [::service-workers-events/unregister {:scope "/"}])
re-frame effects provided by this module
::watch-service-worker-readyEffect to start watching when js/navigator.serviceWorker is ready.
::watch-controller-changeEffect to start watching when js/navigator.serviceWorker.controller changes.
::registerEffect to register new service worker. You should be using event for this unless you have special use case.
::unregisterEffect to unregister a service worker. You should be using event for this unless you have special use case.
::post-messageEffect to post message to a service worker. You should be using event for this unless you have special use case.
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.
lein deps
lein doo chrome tests once
Can you improve this documentation?Edit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |