API Docs for:
Show:

OfflinerClient Class

The exported global off object contains methods for communicating with the offliner worker in charge.

Methods

_activateNextCache

() Promise private

Change the active cache to be the evolved cache if available. Once the active cache has been updated, the former one is lost.

Returns:

Promise:

A Promise resolving in the new version or rejecting if there is no pending activation.

_broadcastMessage

(
  • msg
)
private

Broadcast a message in the clients. The method will add the offliner: prefix to the type of the events but this is stripped out automatically by the client side.

Parameters:

  • msg Any

    the message to be broadcasted.

_checkIfNewVersion

() Promise private

Determine if there is a new version based on the latest version and the current one by using the update middleware.

Returns:

Promise:

latestVersion The new version tag is returned if there is a new version or null otherwise.

_doPrefetch

(
  • cache
)
private

Processes prefetch declared resources using the registered middlewares.

Parameters:

  • cache Cache

    The cache for the middlewares to populate.

_evolveCache

(
  • newCache
)
private

Evolves the current cache to the new cache by using the update middleware.

Parameters:

  • newCache Cache

    The new cache.

_fetch

(
  • request
)
private

Use configured middlewares to perform the fetch process.

Parameters:

  • request Request

    The request to be fetched.

_getCacheNameForVersion

(
  • version
)
Promise private

Return the CACHE name for a version given.

Parameters:

  • version String

    The version to calculate the name for.

Returns:

Promise:

A promise resolving with the name for the version.

_getLatestVersion

() Promise private

Obtains the latest version using the update middleware.

Returns:

Promise:

Tag representing the latest version. The tag will be used as suffix for the new cache.

_handleMessage

(
  • offlinerType
  • msg
)

Discriminates between events which are treated in a special way and the rest of the events that simply trigger the default dispatching algorithm.

Parameters:

  • offlinerType String

    The type of the message without the offliner: prefix.

  • msg Any

    The event.

_has

(
  • type
  • handler
  • willBeThis
)

Parameters:

  • type String

    The type for the listener registration.

  • handler Function

    The listener.

  • willBeThis Object

    The context object this which the function will be called with.

Returns:

true if the listener registration already exists.

_installMessageHandlers

()

Registers the listeners for enabling communication between the worker and the client code.

_openActiveCache

() Promise private

Uses dynamic information to open the active CACHE.

Returns:

Promise:

A promise resolving to the active cache.

_prefetch

() private

Opens current active cache and starts prefetch.

_resolveCrossPromise

(
  • msg
)

Resolves a cross promise based on information received by the implementation in the worker.

Parameters:

  • msg Object

    An object with the proper data to resolve a xpromise.

_runListeners

(
  • type
  • evt
)

Run the listeners for some type of event.

Parameters:

  • type String

    The type of the events selecting the listeners to be run.

  • evt Object

    The event contents.

_send

(
  • msg
)

Sends a message to the worker.

Parameters:

  • msg Any

    The message to be sent.

_sendActivationDone

() private

Broadcast a message to all clients to indicate the activation of the new version ended properly.

_sendActivationFailed

() private

Broadcast a message to all clients to indicate there was a failure while activating the update.

_swapCaches

() private

Makes active cache to be the next-version cache populated during a past update process. After swapping, the previous cache is lost.

_updateCurrentVersion

() private

Updates the current version.

_xpromise

(
  • order
)
Promise

Creates a cross promise registration. A cross promise or xpromise is a special kind of promise that is generated in the client but whose implementation is in a worker.

Parameters:

  • order String

    The string for the implementation part to select the implementation to run.

Returns:

Promise:

A promise delegating its implementation in some code running in a worker.

activate

() Promise

Performs the activation of the pending update. I.e. replaces the current cache with that updated in the update process. Normally, you want to reload the application when the activation ends successfuly.

Returns:

Promise:

A promise resolving into the activated version or rejected with no-activation-pending if there was not an activation.

connect

()

If you are using offliner as a serviceworkerware middleware, instead of calling , call connect() to avoid registering the worker.

install

() Promise

Register the offliner worker. The worker will be installed with root / scope unless you add the data-root attribute to the script tag.

In the same way, the client will look for a script in the specified root called offliner-worker.js. If you want to change this behaviour, use the data-worker attribute.

For instance, suppose your web application is running under: https://lodr.github.com/offliner

And you have your worker at: https://lodr.github.com/offliner/worker.js

Then the script tag should looks like:

<script src="js/offliner-client.js" data-root="offliner" data-worker="worker.js"></script>

Returns:

Promise:

A promise resolving if the installation success.

on

(
  • type
  • handler
  • willBeThis
)

Attaches a listener for a type of event.

Parameters:

  • type String

    The type of the event.

  • handler Callback

    The callback receiving the event.

  • willBeThis Object

    The context object this for the handler.

restore

() OfflinerClient

Call restore() when you want the off name in the global scope for other purposes. The method will restore the previous contents to the global variable and return the OfflinerClient.

Returns:

OfflinerClient:

The current offliner client.

update

() Promise

Request an update to offliner.

Returns:

Promise:

If the update process is successful, the promise will resolve to a new version and an will be triggered. If the update is not needed, the promise will be rejected with no-update-needed reason.

Properties

_eventListeners

Object private

Callbacks for the events.

_xpromises

Object private

Implementation callbacks for cross promises by its unique id.

Events

activationPending

Event emitted on worker activation or under request to point out there is a new version activation pending.