Merge branch 'echidna' into serviceworker

pull/1/head
yflory 7 years ago
commit 14db9fad8e

@ -16,13 +16,14 @@ define([
Messaging, Constants, Feedback, LocalStore, /*AStore, */Channel, Messaging, Constants, Feedback, LocalStore, /*AStore, */Channel,
AppConfig, Nthen) { AppConfig, Nthen) {
/* This file exposes functionality which is specific to Cryptpad, but not to /* This file exposes functionality which is specific to Cryptpad, but not to
any particular pad type. This includes functions for committing metadata any particular pad type. This includes functions for committing metadata
about pads to your local storage for future use and improved usability. about pads to your local storage for future use and improved usability.
Additionally, there is some basic functionality for import/export. Additionally, there is some basic functionality for import/export.
*/ */
var urlArgs = Util.find(Config, ['requireConf', 'urlArgs']) || '';
var postMessage = function (/*cmd, data, cb*/) { var postMessage = function (/*cmd, data, cb*/) {
/*setTimeout(function () { /*setTimeout(function () {
AStore.query(cmd, data, cb); AStore.query(cmd, data, cb);
@ -768,7 +769,7 @@ define([
var postMsg, worker; var postMsg, worker;
Nthen(function (waitFor2) { Nthen(function (waitFor2) {
if (SharedWorker) { if (SharedWorker) {
worker = new SharedWorker('/common/outer/sharedworker.js'); worker = new SharedWorker('/common/outer/sharedworker.js' + urlArgs);
console.log(worker); console.log(worker);
worker.port.onmessage = function (ev) { worker.port.onmessage = function (ev) {
if (ev.data === "SW_READY") { if (ev.data === "SW_READY") {
@ -788,7 +789,7 @@ define([
if (worker) { return void worker.postMessage(data); } if (worker) { return void worker.postMessage(data); }
}; };
navigator.serviceWorker.register('/common/outer/serviceworker.js', {scope: '/'}) navigator.serviceWorker.register('/common/outer/serviceworker.js' + urlArgs, {scope: '/'})
.then(function(reg) { .then(function(reg) {
// Add handler for receiving messages from the service worker // Add handler for receiving messages from the service worker
navigator.serviceWorker.addEventListener('message', function (ev) { navigator.serviceWorker.addEventListener('message', function (ev) {
@ -828,7 +829,7 @@ define([
/**/console.log('Registration failed with ' + error); /**/console.log('Registration failed with ' + error);
}); });
} else if (Worker) { } else if (Worker) {
worker = new Worker('/common/outer/webworker.js'); worker = new Worker('/common/outer/webworker.js' + urlArgs);
worker.onmessage = function (ev) { worker.onmessage = function (ev) {
msgEv.fire(ev); msgEv.fire(ev);
}; };

@ -1,112 +1,98 @@
/* jshint ignore:start */ /* jshint ignore:start */
importScripts('/bower_components/requirejs/require.js'); importScripts('/bower_components/requirejs/require.js');
require.config({
// fix up locations so that relative urls work.
baseUrl: '/',
paths: {
// jquery declares itself as literally "jquery" so it cannot be pulled by path :(
"jquery": "/bower_components/jquery/dist/jquery.min",
// json.sortify same
"json.sortify": "/bower_components/json.sortify/dist/JSON.sortify",
cm: '/bower_components/codemirror'
},
map: {
'*': {
'css': '/bower_components/require-css/css.js',
'less': '/common/RequireLess.js',
}
}
});
window = self; window = self;
localStorage = { localStorage = {
setItem: function (k, v) { localStorage[k] = v; }, setItem: function (k, v) { localStorage[k] = v; },
getItem: function (k) { return localStorage[k]; } getItem: function (k) { return localStorage[k]; }
}; };
require([ require([
'/common/common-util.js', '/common/requireconfig.js'
'/common/outer/worker-channel.js', ], function (RequireConfig) {
'/common/outer/store-rpc.js' require.config(RequireConfig());
], function (Util, Channel, Rpc) { require([
var msgEv = Util.mkEvent(); '/common/common-util.js',
'/common/outer/worker-channel.js',
'/common/outer/store-rpc.js'
], function (Util, Channel, Rpc) {
var msgEv = Util.mkEvent();
Channel.create(msgEv, postMessage, function (chan) { Channel.create(msgEv, postMessage, function (chan) {
console.log('ww ready'); var clientId = '1';
var clientId = '1'; Object.keys(Rpc.queries).forEach(function (q) {
Object.keys(Rpc.queries).forEach(function (q) { if (q === 'CONNECT') { return; }
if (q === 'CONNECT') { return; } if (q === 'JOIN_PAD') { return; }
if (q === 'JOIN_PAD') { return; } if (q === 'SEND_PAD_MSG') { return; }
if (q === 'SEND_PAD_MSG') { return; } chan.on(q, function (data, cb) {
chan.on(q, function (data, cb) { try {
Rpc.queries[q](clientId, data, cb);
} catch (e) {
console.error('Error in webworker when executing query ' + q);
console.error(e);
console.log(data);
}
});
});
chan.on('CONNECT', function (cfg, cb) {
// load Store here, with cfg, and pass a "query" (chan.query)
// cId is a clientId used in ServiceWorker or SharedWorker
cfg.query = function (cId, cmd, data, cb) {
cb = cb || function () {};
chan.query(cmd, data, function (err, data2) {
if (err) { return void cb({error: err}); }
cb(data2);
});
};
cfg.broadcast = function (excludes, cmd, data, cb) {
cb = cb || function () {};
if (excludes.indexOf(clientId) !== -1) { return; }
chan.query(cmd, data, function (err, data2) {
if (err) { return void cb({error: err}); }
cb(data2);
});
};
Rpc.queries['CONNECT'](clientId, cfg, function (data) {
if (data && data.state === "ALREADY_INIT") {
return void cb(data);
}
if (cfg.driveEvents) {
Rpc._subscribeToDrive(clientId);
}
if (cfg.messenger) {
Rpc._subscribeToMessenger(clientId);
}
cb(data);
});
});
var chanId;
chan.on('JOIN_PAD', function (data, cb) {
chanId = data.channel;
try { try {
Rpc.queries[q](clientId, data, cb); Rpc.queries['JOIN_PAD'](clientId, data, cb);
} catch (e) { } catch (e) {
console.error('Error in webworker when executing query ' + q); console.error('Error in webworker when executing query JOIN_PAD');
console.error(e); console.error(e);
console.log(data); console.log(data);
} }
}); });
}); chan.on('SEND_PAD_MSG', function (msg, cb) {
chan.on('CONNECT', function (cfg, cb) { var data = {
console.log('onConnect'); msg: msg,
// load Store here, with cfg, and pass a "query" (chan.query) channel: chanId
// cId is a clientId used in ServiceWorker or SharedWorker };
cfg.query = function (cId, cmd, data, cb) { try {
cb = cb || function () {}; Rpc.queries['SEND_PAD_MSG'](clientId, data, cb);
chan.query(cmd, data, function (err, data2) { } catch (e) {
if (err) { return void cb({error: err}); } console.error('Error in webworker when executing query SEND_PAD_MSG');
cb(data2); console.error(e);
}); console.log(data);
};
cfg.broadcast = function (excludes, cmd, data, cb) {
cb = cb || function () {};
if (excludes.indexOf(clientId) !== -1) { return; }
chan.query(cmd, data, function (err, data2) {
if (err) { return void cb({error: err}); }
cb(data2);
});
};
Rpc.queries['CONNECT'](clientId, cfg, function (data) {
console.log(data);
if (data && data.state === "ALREADY_INIT") {
return void cb(data);
}
if (cfg.driveEvents) {
Rpc._subscribeToDrive(clientId);
}
if (cfg.messenger) {
Rpc._subscribeToMessenger(clientId);
} }
cb(data);
}); });
}); }, true);
var chanId;
chan.on('JOIN_PAD', function (data, cb) {
chanId = data.channel;
try {
Rpc.queries['JOIN_PAD'](clientId, data, cb);
} catch (e) {
console.error('Error in webworker when executing query JOIN_PAD');
console.error(e);
console.log(data);
}
});
chan.on('SEND_PAD_MSG', function (msg, cb) {
var data = {
msg: msg,
channel: chanId
};
try {
Rpc.queries['SEND_PAD_MSG'](clientId, data, cb);
} catch (e) {
console.error('Error in webworker when executing query SEND_PAD_MSG');
console.error(e);
console.log(data);
}
});
}, true);
onmessage = function (e) { onmessage = function (e) {
msgEv.fire(e); msgEv.fire(e);
}; };
});
}); });

@ -1,3 +1,4 @@
/* jshint ignore:start */
var id; var id;
//= Math.floor(Math.random()*100000); //= Math.floor(Math.random()*100000);

@ -1 +0,0 @@
worker/
Loading…
Cancel
Save