OfflinerClient Class
The exported global off object contains methods for communicating with
the offliner worker in charge.
Item Index
Methods
- _activateNextCache
- _broadcastMessage
- _checkIfNewVersion
- _doPrefetch
- _evolveCache
- _fetch
- _getCacheNameForVersion
- _getLatestVersion
- _handleMessage
- _has
- _installMessageHandlers
- _openActiveCache
- _prefetch
- _resolveCrossPromise
- _runListeners
- _send
- _sendActivationDone
- _sendActivationFailed
- _swapCaches
- _updateCurrentVersion
- _xpromise
- activate
- connect
- install
- on
- restore
- update
Properties
Events
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:
A Promise resolving in the new version or rejecting if there is no pending activation.
_broadcastMessage
-
msg
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:
-
msgAnythe message to be broadcasted.
_checkIfNewVersion
()
PromiseDetermine if there is a new version based on the latest version and the current one by using the update middleware.
Returns:
latestVersion The new version tag is returned
if there is a new version or null otherwise.
_doPrefetch
-
cache
Processes prefetch declared resources using the registered middlewares.
Parameters:
-
cacheCacheThe cache for the middlewares to populate.
_evolveCache
-
newCache
Evolves the current cache to the new cache by using the update middleware.
Parameters:
-
newCacheCacheThe new cache.
_fetch
-
request
Use configured middlewares to perform the fetch process.
Parameters:
-
requestRequestThe request to be fetched.
_getCacheNameForVersion
-
version
Return the CACHE name for a version given.
Parameters:
-
versionStringThe version to calculate the name for.
Returns:
A promise resolving with the name for the version.
_getLatestVersion
()
PromiseObtains the latest version using the update middleware.
Returns:
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:
-
offlinerTypeStringThe type of the message without the
offliner:prefix. -
msgAnyThe event.
_has
-
type -
handler -
willBeThis
Parameters:
-
typeStringThe type for the listener registration.
-
handlerFunctionThe listener.
-
willBeThisObjectThe context object
thiswhich 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
()
PromiseUses dynamic information to open the active CACHE.
Returns:
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:
-
msgObjectAn object with the proper data to resolve a xpromise.
_runListeners
-
type -
evt
Run the listeners for some type of event.
Parameters:
-
typeStringThe type of the events selecting the listeners to be run.
-
evtObjectThe event contents.
_send
-
msg
Sends a message to the worker.
Parameters:
-
msgAnyThe 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
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:
-
orderStringThe string for the implementation part to select the implementation to run.
Returns:
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:
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:
A promise resolving if the installation success.
on
-
type -
handler -
willBeThis
Attaches a listener for a type of event.
Parameters:
-
typeStringThe type of the event.
-
handlerCallbackThe callback receiving the event.
-
willBeThisObjectThe context object
thisfor thehandler.
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:
The current offliner client.
update
()
Promise
Request an update to offliner.
Returns:
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.
