Skip to content

Commit

Permalink
fix shared worker issue on restart and removed build files
Browse files Browse the repository at this point in the history
maxime-aknin committed Feb 24, 2022
1 parent cebf5a5 commit e263ea0
Showing 4 changed files with 44 additions and 2,318 deletions.
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -2,10 +2,10 @@
.DS_Store
devsync.json
/devsync
public/dist
public/css/*
!public/css/reset.css
!public/css/style.css
web/dist
web/css/*
!web/css/reset.css
!web/css/style.css
node_modules
yarn.lock
/vendor
2,281 changes: 0 additions & 2,281 deletions web/dist/sync-bundle.js

This file was deleted.

1 change: 0 additions & 1 deletion web/dist/sync-bundle.min.js

This file was deleted.

72 changes: 40 additions & 32 deletions web/shared_worker.js
Original file line number Diff line number Diff line change
@@ -2,46 +2,54 @@
let ports = [];
let debug = false
let EventSourceSingleton = (function () {
var instance = null;
return {
getInstance: function (sse_url) {
if (!instance) {
instance = new EventSource(sse_url);
}
return instance;
}
};
var instance = null;
return {
getInstance: function (sse_url) {
if (!instance) {
instance = new EventSource(sse_url);
}
return instance;
},
reset: function () {
instance = null;
}
};
})();


// https://developer.mozilla.org/en-US/docs/Web/API/SharedWorkerGlobalScope/onconnect
onconnect = function(e) {
console.log('worker connected');
var port = e.ports[0];
ports.push(port);
var notifyAll = function(message){
ports.forEach(port => port.postMessage(message));
}
var notifyAll = function(message){
ports.forEach(port => port.postMessage(message));
}
var makeConnection = function (sse_url) {
var source = EventSourceSingleton.getInstance(sse_url);
source.onopen = function (e){
console.log(e)
var message = "Connection open"
port.postMessage({type: 'open' , msg: 'Connection opened !'});
}
source.onerror = function(e){
console.log(e)
port.postMessage({type: 'error' , msg: 'error connecting to server'});
source.close()
}
source.onmessage = function(e){
console.log(e)
notifyAll({type: 'message' , msg: e.data});
}
}
var source = EventSourceSingleton.getInstance(sse_url);
source.onopen = function (e){
console.log(e);
port.postMessage({type: 'open' , msg: 'Connection opened !'});
}
source.onerror = function(e){
console.log(e);
port.postMessage({type: 'error' , msg: 'error connecting to server'});
// on server disconnect, we close the connection and reset singleton
// or as long as there are old pages with connection active, any new connection after restart won't work
source.close();
EventSourceSingleton.reset();
ports.forEach(p => p.close());
}
source.onmessage = function(e){
console.log(e);
notifyAll({type: 'message' , msg: e.data});
}
}

port.onmessage = function(e) {
port.onmessage = function(e) {
console.log('received init message, making connection...')
makeConnection(e.data);
}
port.start(); // Required when using addEventListener. Otherwise called implicitly by onmessage setter.
makeConnection(e.data);
}
port.start(); // Required when using addEventListener. Otherwise called implicitly by onmessage setter.

}

0 comments on commit e263ea0

Please sign in to comment.