|
|
@ -1,23 +1,5 @@
|
|
|
|
console.log('SW!');
|
|
|
|
|
|
|
|
/* 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 = {
|
|
|
@ -26,16 +8,6 @@ localStorage = {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
self.tabs = {};
|
|
|
|
self.tabs = {};
|
|
|
|
var findTab = function (port) {
|
|
|
|
|
|
|
|
var tab;
|
|
|
|
|
|
|
|
Object.keys(self.tabs).some(function (id) {
|
|
|
|
|
|
|
|
if (self.tabs[id].port === port) {
|
|
|
|
|
|
|
|
tab = port;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
return tab;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var postMsg = function (client, data) {
|
|
|
|
var postMsg = function (client, data) {
|
|
|
|
client.port.postMessage(data);
|
|
|
|
client.port.postMessage(data);
|
|
|
@ -48,114 +20,119 @@ var init = function (client, cb) {
|
|
|
|
debug('SharedW INIT');
|
|
|
|
debug('SharedW INIT');
|
|
|
|
|
|
|
|
|
|
|
|
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([
|
|
|
|
debug('SharedW Required ressources loaded');
|
|
|
|
'/common/common-util.js',
|
|
|
|
var msgEv = Util.mkEvent();
|
|
|
|
'/common/outer/worker-channel.js',
|
|
|
|
|
|
|
|
'/common/outer/store-rpc.js'
|
|
|
|
var postToClient = function (data) {
|
|
|
|
], function (Util, Channel, Rpc) {
|
|
|
|
postMsg(client, data);
|
|
|
|
debug('SharedW Required ressources loaded');
|
|
|
|
};
|
|
|
|
var msgEv = Util.mkEvent();
|
|
|
|
Channel.create(msgEv, postToClient, function (chan) {
|
|
|
|
|
|
|
|
debug('SharedW Channel created');
|
|
|
|
var postToClient = function (data) {
|
|
|
|
|
|
|
|
postMsg(client, data);
|
|
|
|
var clientId = client.id;
|
|
|
|
};
|
|
|
|
client.chan = chan;
|
|
|
|
Channel.create(msgEv, postToClient, function (chan) {
|
|
|
|
Object.keys(Rpc.queries).forEach(function (q) {
|
|
|
|
debug('SharedW Channel created');
|
|
|
|
if (q === 'CONNECT') { return; }
|
|
|
|
|
|
|
|
if (q === 'JOIN_PAD') { return; }
|
|
|
|
var clientId = client.id;
|
|
|
|
if (q === 'SEND_PAD_MSG') { return; }
|
|
|
|
client.chan = chan;
|
|
|
|
chan.on(q, function (data, cb) {
|
|
|
|
Object.keys(Rpc.queries).forEach(function (q) {
|
|
|
|
try {
|
|
|
|
if (q === 'CONNECT') { return; }
|
|
|
|
Rpc.queries[q](clientId, data, cb);
|
|
|
|
if (q === 'JOIN_PAD') { return; }
|
|
|
|
} catch (e) {
|
|
|
|
if (q === 'SEND_PAD_MSG') { return; }
|
|
|
|
console.error('Error in webworker when executing query ' + q);
|
|
|
|
chan.on(q, function (data, cb) {
|
|
|
|
console.error(e);
|
|
|
|
try {
|
|
|
|
console.log(data);
|
|
|
|
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) {
|
|
|
|
chan.on('CONNECT', function (cfg, cb) {
|
|
|
|
debug('SharedW connecting to store...');
|
|
|
|
debug('SharedW connecting to store...');
|
|
|
|
if (self.store) {
|
|
|
|
if (self.store) {
|
|
|
|
debug('Store already exists!');
|
|
|
|
debug('Store already exists!');
|
|
|
|
if (cfg.driveEvents) {
|
|
|
|
if (cfg.driveEvents) {
|
|
|
|
Rpc._subscribeToDrive(clientId);
|
|
|
|
Rpc._subscribeToDrive(clientId);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cfg.messenger) {
|
|
|
|
|
|
|
|
Rpc._subscribeToMessenger(clientId);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return void cb(self.store);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cfg.messenger) {
|
|
|
|
|
|
|
|
Rpc._subscribeToMessenger(clientId);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return void cb(self.store);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
debug('Loading new async store');
|
|
|
|
debug('Loading new async store');
|
|
|
|
// One-time initialization (init async-store)
|
|
|
|
// One-time initialization (init async-store)
|
|
|
|
cfg.query = function (cId, cmd, data, cb) {
|
|
|
|
cfg.query = function (cId, cmd, data, cb) {
|
|
|
|
cb = cb || function () {};
|
|
|
|
cb = cb || function () {};
|
|
|
|
self.tabs[cId].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 () {};
|
|
|
|
|
|
|
|
Object.keys(self.tabs).forEach(function (cId) {
|
|
|
|
|
|
|
|
if (excludes.indexOf(cId) !== -1) { return; }
|
|
|
|
|
|
|
|
self.tabs[cId].chan.query(cmd, data, function (err, data2) {
|
|
|
|
self.tabs[cId].chan.query(cmd, data, function (err, data2) {
|
|
|
|
if (err) { return void cb({error: err}); }
|
|
|
|
if (err) { return void cb({error: err}); }
|
|
|
|
cb(data2);
|
|
|
|
cb(data2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
cfg.broadcast = function (excludes, cmd, data, cb) {
|
|
|
|
|
|
|
|
cb = cb || function () {};
|
|
|
|
|
|
|
|
Object.keys(self.tabs).forEach(function (cId) {
|
|
|
|
|
|
|
|
if (excludes.indexOf(cId) !== -1) { return; }
|
|
|
|
|
|
|
|
self.tabs[cId].chan.query(cmd, data, function (err, data2) {
|
|
|
|
|
|
|
|
if (err) { return void cb({error: err}); }
|
|
|
|
|
|
|
|
cb(data2);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Rpc.queries['CONNECT'](clientId, cfg, function (data) {
|
|
|
|
|
|
|
|
if (cfg.driveEvents) {
|
|
|
|
|
|
|
|
Rpc._subscribeToDrive(clientId);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cfg.messenger) {
|
|
|
|
|
|
|
|
Rpc._subscribeToMessenger(clientId);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data && data.state === "ALREADY_INIT") {
|
|
|
|
|
|
|
|
self.store = data.returned;
|
|
|
|
|
|
|
|
return void cb(data.returned);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
self.store = data;
|
|
|
|
|
|
|
|
cb(data);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
});
|
|
|
|
Rpc.queries['CONNECT'](clientId, cfg, function (data) {
|
|
|
|
chan.on('JOIN_PAD', function (data, cb) {
|
|
|
|
if (cfg.driveEvents) {
|
|
|
|
client.channelId = data.channel;
|
|
|
|
Rpc._subscribeToDrive(clientId);
|
|
|
|
try {
|
|
|
|
}
|
|
|
|
Rpc.queries['JOIN_PAD'](clientId, data, cb);
|
|
|
|
if (cfg.messenger) {
|
|
|
|
} catch (e) {
|
|
|
|
Rpc._subscribeToMessenger(clientId);
|
|
|
|
console.error('Error in webworker when executing query JOIN_PAD');
|
|
|
|
|
|
|
|
console.error(e);
|
|
|
|
|
|
|
|
console.log(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (data && data.state === "ALREADY_INIT") {
|
|
|
|
});
|
|
|
|
self.store = data.returned;
|
|
|
|
chan.on('SEND_PAD_MSG', function (msg, cb) {
|
|
|
|
return void cb(data.returned);
|
|
|
|
var data = {
|
|
|
|
|
|
|
|
msg: msg,
|
|
|
|
|
|
|
|
channel: client.channelId
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.store = data;
|
|
|
|
|
|
|
|
cb(data);
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
cb();
|
|
|
|
chan.on('JOIN_PAD', function (data, cb) {
|
|
|
|
}, true);
|
|
|
|
client.channelId = 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: client.channelId
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
cb();
|
|
|
|
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
client.msgEv = msgEv;
|
|
|
|
client.msgEv = msgEv;
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
onconnect = function(e) {
|
|
|
|
onconnect = function(e) {
|
|
|
|
debug('New ShardWorker client');
|
|
|
|
debug('New SharedWorker client');
|
|
|
|
var port = e.ports[0];
|
|
|
|
var port = e.ports[0];
|
|
|
|
var cId = Number(Math.floor(Math.random() * Number.MAX_SAFE_INTEGER))
|
|
|
|
var cId = Number(Math.floor(Math.random() * Number.MAX_SAFE_INTEGER))
|
|
|
|
var client = self.tabs[cId] = {
|
|
|
|
var client = self.tabs[cId] = {
|
|
|
@ -176,29 +153,3 @@ onconnect = function(e) {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
self.tabs = {};
|
|
|
|
|
|
|
|
self.addEventListener('message', function (e) {
|
|
|
|
|
|
|
|
var cId = e.source.id;
|
|
|
|
|
|
|
|
if (e.data === "INIT") {
|
|
|
|
|
|
|
|
if (tabs[cId]) { return; }
|
|
|
|
|
|
|
|
tabs[cId] = {
|
|
|
|
|
|
|
|
client: e.source
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
init(e.source, function () {
|
|
|
|
|
|
|
|
postMsg(e.source, 'SW_READY');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} else if (self.tabs[cId] && self.tabs[cId].msgEv) {
|
|
|
|
|
|
|
|
self.tabs[cId].msgEv.fire(e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
self.addEventListener('install', function (e) {
|
|
|
|
|
|
|
|
debug('V1 installing…');
|
|
|
|
|
|
|
|
self.skipWaiting();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.addEventListener('activate', function (e) {
|
|
|
|
|
|
|
|
debug('V1 now ready to handle fetches!');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|