Commit b16c395f authored by Boris Kocherov's avatar Boris Kocherov

erp5_officejs: add serviceworker/jio offline mode

:
parent 4218ac97
/*jslint indent: 2*/
/*global self, caches, importScripts, fetch, Promise, Request, Response, jIO, console*/
var global = self,
window = self;
(function (self, caches, fetch) {
"use strict";
self.DOMParser = {};
self.sessionStorage = {};
self.localStorage = {};
self.openDatabase = {};
importScripts('rsvp.js', 'jiodev.js');
Response.prototype.metadata = function () {
return {
//'ok': this.ok
url: this.url,
headers: Array.from(this.headers.entries())
};
};
Response.prototype.metadata_w_blob = function () {
var metadata = this.metadata();
return this.blob().then(function (blob) {
return Promise.resolve({
'blob': blob,
'metadata': metadata
});
});
};
self.jio_cache_storage = jIO.createJIO({
type: "uuid",
sub_storage: {
"type": "indexeddb",
"database": self.CACHE_NAME
}
});
self.jio_cache_install = function (event) {
// debugger;
// Perform install step: loading each required file into cache
// create a new jio
var requests = self.CACHE_REQUIRED_FILES.map(function (file_name) {
return new Request(file_name);
});
event.waitUntil(
Promise.all(requests.map(function (request) {
return fetch(request).then(function (response) {
return response.metadata_w_blob();
});
}))
.then(function (responses) {
return Promise.all(
responses.map(function (response, i) {
return new Promise(function (resolve, reject) {
var jio_key = requests[i].url;
self.jio_cache_storage.put(jio_key, response.metadata)
.push(function () {
return self.jio_cache_storage.putAttachment(
jio_key,
"body",
response.blob
)
.push(function () {
console.log('jio_save: ' + jio_key);
resolve();
});
})
.push(undefined, function (error) {
console.log(error);
reject();
});
})
.then(undefined, console.log);
})
)
.then(function () {
console.log('cache loaded');
return self.skipWaiting();
})
.then(undefined, console.log);
})
);
};
self.jio_cache_fetch = function (event) {
var jio_key = event.request.url;
event.respondWith(new Promise(function (resolve, reject) {
self.jio_cache_storage.get(jio_key)
.push(function (metadata) {
return self.jio_cache_storage.getAttachment(jio_key, 'body')
.push(function (body) {
//console.log(metadata.url + ' return from jio');
resolve(new Response(body, {'headers': metadata.headers}));
});
})
.push(undefined, function (error) {
if (error.status_code == 404)
console.log(jio_key + ' not found in cache');
else {
console.log(jio_key);
console.log(error);
}
resolve(fetch(event.request));
});
})
.then(undefined, function (error) {
console.log(error);
})
);
};
self.jio_cache_activate = function (event) {
/* Just like with the install event, event.waitUntil blocks activate on a promise.
Activation will fail unless the promise is fulfilled.
*/
event.waitUntil(self.clients.claim());
};
self.addEventListener('install', self.jio_cache_install);
self.addEventListener('fetch', self.jio_cache_fetch);
self.addEventListener("activate", self.jio_cache_activate);
}(self, caches, fetch));
...@@ -3,6 +3,18 @@ ...@@ -3,6 +3,18 @@
(function (window, rJS) { (function (window, rJS) {
"use strict"; "use strict";
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('gadget_officejs_spreadsheet_serviceworker.js')
.then(function (reg) {
// registration worked
console.log('Registration succeeded. Scope is ' + reg.scope);
})
.then(undefined, function (error) {
// registration failed
console.log('Registration failed with ' + error);
});
}
var gadget_klass = rJS(window), var gadget_klass = rJS(window),
MAIN_PAGE_PREFIX = "gadget_officejs_", MAIN_PAGE_PREFIX = "gadget_officejs_",
DEFAULT_PAGE = "spreadsheet_list", DEFAULT_PAGE = "spreadsheet_list",
......
...@@ -216,7 +216,7 @@ ...@@ -216,7 +216,7 @@
</item> </item>
<item> <item>
<key> <string>actor</string> </key> <key> <string>actor</string> </key>
<value> <string>cedric.le.ninivin</string> </value> <value> <string>zope</string> </value>
</item> </item>
<item> <item>
<key> <string>comment</string> </key> <key> <string>comment</string> </key>
...@@ -230,7 +230,7 @@ ...@@ -230,7 +230,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>947.15310.53603.16298</string> </value> <value> <string>950.59503.49307.64665</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -248,7 +248,7 @@ ...@@ -248,7 +248,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1448034197.3</float> <float>1462142989.27</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -3,6 +3,18 @@ ...@@ -3,6 +3,18 @@
(function (window, rJS) { (function (window, rJS) {
"use strict"; "use strict";
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('gadget_officejs_text_serviceworker.js')
.then(function (reg) {
// registration worked
console.log('Registration succeeded. Scope is ' + reg.scope);
})
.then(undefined, function (error) {
// registration failed
console.log('Registration failed with ' + error);
});
}
var gadget_klass = rJS(window), var gadget_klass = rJS(window),
MAIN_PAGE_PREFIX = "gadget_officejs_", MAIN_PAGE_PREFIX = "gadget_officejs_",
DEFAULT_PAGE = "text_list", DEFAULT_PAGE = "text_list",
......
...@@ -230,7 +230,7 @@ ...@@ -230,7 +230,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>947.62779.43939.6877</string> </value> <value> <string>949.50294.65237.43212</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -248,7 +248,7 @@ ...@@ -248,7 +248,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1450796023.24</float> <float>1463147142.93</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment