cryptpad/www/common/outer/webworker.js

53 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-06-01 17:23:30 +00:00
/* jshint ignore:start */
importScripts('/bower_components/requirejs/require.js');
window = self;
localStorage = {
setItem: function (k, v) { localStorage[k] = v; },
getItem: function (k) { return localStorage[k]; }
};
2018-06-06 15:45:43 +00:00
2018-06-01 17:23:30 +00:00
require([
2018-06-06 15:45:43 +00:00
'/common/requireconfig.js'
], function (RequireConfig) {
require.config(RequireConfig());
require([
'/common/common-util.js',
'/common/outer/worker-channel.js',
'/common/outer/store-rpc.js'
], function (Util, Channel, Rpc) {
var msgEv = Util.mkEvent();
2018-06-01 17:23:30 +00:00
2018-06-06 15:45:43 +00:00
Channel.create(msgEv, postMessage, function (chan) {
console.log('ww ready');
Object.keys(Rpc.queries).forEach(function (q) {
if (q === 'CONNECT') { return; }
chan.on(q, function (data, cb) {
try {
Rpc.queries[q](data, cb);
} catch (e) {
console.error('Error in webworker when executing query ' + q);
console.error(e);
console.log(data);
}
2018-06-01 17:23:30 +00:00
});
2018-06-06 15:45:43 +00:00
});
chan.on('CONNECT', function (cfg, cb) {
console.log('onConnect');
// load Store here, with cfg, and pass a "query" (chan.query)
cfg.query = function (cmd, data, cb) {
chan.query(cmd, data, function (err, data) {
if (err) { return void cb({error: err}); }
cb(data);
});
};
Rpc.queries['CONNECT'](cfg, cb);
});
}, true);
2018-06-01 17:23:30 +00:00
2018-06-06 15:45:43 +00:00
onmessage = function (e) {
msgEv.fire(e);
};
});
2018-06-01 17:23:30 +00:00
});