Skip to content

Commit 1d71ee6

Browse files
committed
refactor shared worker into singleton instance
1 parent 105d587 commit 1d71ee6

File tree

4 files changed

+62
-119
lines changed

4 files changed

+62
-119
lines changed

web_src/js/features/notification.js

+1-37
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import $ from 'jquery';
2+
import worker from './shared-worker.js';
23

34
const {appSubUrl, csrfToken, notificationSettings} = window.config;
45
let notificationSequenceNumber = 0;
@@ -51,52 +52,15 @@ export function initNotificationCount() {
5152

5253
if (notificationSettings.EventSourceUpdateTime > 0 && !!window.EventSource && window.SharedWorker) {
5354
// Try to connect to the event source via the shared worker first
54-
const worker = new SharedWorker(`${__webpack_public_path__}js/eventsource.sharedworker.js`, 'notification-worker');
55-
worker.addEventListener('error', (event) => {
56-
console.error(event);
57-
});
58-
worker.port.addEventListener('messageerror', () => {
59-
console.error('Unable to deserialize message');
60-
});
61-
worker.port.postMessage({
62-
type: 'start',
63-
url: `${window.location.origin}${appSubUrl}/user/events`,
64-
});
6555
worker.port.addEventListener('message', (event) => {
6656
if (!event.data || !event.data.type) {
6757
console.error(event);
6858
return;
6959
}
7060
if (event.data.type === 'notification-count') {
7161
const _promise = receiveUpdateCount(event.data);
72-
} else if (event.data.type === 'error') {
73-
console.error(event.data);
74-
} else if (event.data.type === 'logout') {
75-
if (event.data.data !== 'here') {
76-
return;
77-
}
78-
worker.port.postMessage({
79-
type: 'close',
80-
});
81-
worker.port.close();
82-
window.location.href = appSubUrl;
83-
} else if (event.data.type === 'close') {
84-
worker.port.postMessage({
85-
type: 'close',
86-
});
87-
worker.port.close();
8862
}
8963
});
90-
worker.port.addEventListener('error', (e) => {
91-
console.error(e);
92-
});
93-
worker.port.start();
94-
window.addEventListener('beforeunload', () => {
95-
worker.port.postMessage({
96-
type: 'close',
97-
});
98-
worker.port.close();
99-
});
10064

10165
return;
10266
}

web_src/js/features/repo-refresh-pr.js

+2-44
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import $ from 'jquery';
2-
3-
const {appSubUrl} = window.config;
2+
import worker from './shared-worker.js';
43

54
async function receiveBranchUpdated(event) {
65
try {
@@ -41,55 +40,14 @@ export function initRepoRefreshPullRequest() {
4140
});
4241

4342
if (!!window.EventSource && window.SharedWorker) {
44-
const worker = new SharedWorker(
45-
`${__webpack_public_path__}js/eventsource.sharedworker.js`,
46-
'notification-worker'
47-
);
48-
worker.addEventListener('error', (event) => {
49-
console.error(event);
50-
});
51-
worker.port.addEventListener('messageerror', () => {
52-
console.error('Unable to deserialize message');
53-
});
54-
worker.port.postMessage({
55-
type: 'start',
56-
url: `${window.location.origin}${appSubUrl}/user/events`,
57-
});
5843
worker.port.addEventListener('message', (event) => {
5944
if (!event.data || !event.data.type) {
60-
console.error(event);
6145
return;
6246
}
6347
if (event.data.type === 'branch-update') {
64-
const _promise = receiveBranchUpdated(event.data);
65-
} else if (event.data.type === 'error') {
66-
console.error(event.data);
67-
} else if (event.data.type === 'logout') {
68-
if (event.data.data !== 'here') {
69-
return;
70-
}
71-
worker.port.postMessage({
72-
type: 'close',
73-
});
74-
worker.port.close();
75-
window.location.href = appSubUrl;
76-
} else if (event.data.type === 'close') {
77-
worker.port.postMessage({
78-
type: 'close',
79-
});
80-
worker.port.close();
48+
receiveBranchUpdated(event.data);
8149
}
8250
});
83-
worker.port.addEventListener('error', (e) => {
84-
console.error(e);
85-
});
86-
worker.port.start();
87-
window.addEventListener('beforeunload', () => {
88-
worker.port.postMessage({
89-
type: 'close',
90-
});
91-
worker.port.close();
92-
});
9351
} else {
9452
console.error('Service workers not available');
9553
}

web_src/js/features/shared-worker.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const {appSubUrl} = window.config;
2+
3+
const worker = new SharedWorker(
4+
`${__webpack_public_path__}js/eventsource.sharedworker.js`,
5+
'notification-worker'
6+
);
7+
8+
worker.addEventListener('error', (event) => {
9+
console.error(event);
10+
});
11+
12+
worker.port.addEventListener('messageerror', () => {
13+
console.error('Unable to deserialize message');
14+
});
15+
16+
worker.port.postMessage({
17+
type: 'start',
18+
url: `${window.location.origin}${appSubUrl}/user/events`,
19+
});
20+
21+
worker.port.addEventListener('message', (event) => {
22+
if (!event.data || !event.data.type) {
23+
console.error(event);
24+
return;
25+
}
26+
if (event.data.type === 'error') {
27+
console.error(event.data);
28+
} else if (event.data.type === 'logout') {
29+
if (event.data.data !== 'here') {
30+
return;
31+
}
32+
worker.port.postMessage({
33+
type: 'close',
34+
});
35+
worker.port.close();
36+
window.location.href = appSubUrl;
37+
} else if (event.data.type === 'close') {
38+
worker.port.postMessage({
39+
type: 'close',
40+
});
41+
worker.port.close();
42+
}
43+
});
44+
45+
worker.port.addEventListener('error', (e) => {
46+
console.error(e);
47+
});
48+
49+
worker.port.start();
50+
51+
window.addEventListener('beforeunload', () => {
52+
worker.port.postMessage({
53+
type: 'close',
54+
});
55+
worker.port.close();
56+
});
57+
58+
export default worker;

web_src/js/features/stopwatch.js

+1-38
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import $ from 'jquery';
22
import prettyMilliseconds from 'pretty-ms';
3+
import worker from './shared-worker.js';
34

45
const {appSubUrl, csrfToken, notificationSettings, enableTimeTracking} = window.config;
56
let updateTimeInterval = null; // holds setInterval id when active
@@ -28,52 +29,14 @@ export function initStopwatch() {
2829

2930
if (notificationSettings.EventSourceUpdateTime > 0 && !!window.EventSource && window.SharedWorker) {
3031
// Try to connect to the event source via the shared worker first
31-
const worker = new SharedWorker(`${__webpack_public_path__}js/eventsource.sharedworker.js`, 'notification-worker');
32-
worker.addEventListener('error', (event) => {
33-
console.error(event);
34-
});
35-
worker.port.addEventListener('messageerror', () => {
36-
console.error('Unable to deserialize message');
37-
});
38-
worker.port.postMessage({
39-
type: 'start',
40-
url: `${window.location.origin}${appSubUrl}/user/events`,
41-
});
4232
worker.port.addEventListener('message', (event) => {
4333
if (!event.data || !event.data.type) {
44-
console.error(event);
4534
return;
4635
}
4736
if (event.data.type === 'stopwatches') {
4837
updateStopwatchData(JSON.parse(event.data.data));
49-
} else if (event.data.type === 'error') {
50-
console.error(event.data);
51-
} else if (event.data.type === 'logout') {
52-
if (event.data.data !== 'here') {
53-
return;
54-
}
55-
worker.port.postMessage({
56-
type: 'close',
57-
});
58-
worker.port.close();
59-
window.location.href = appSubUrl;
60-
} else if (event.data.type === 'close') {
61-
worker.port.postMessage({
62-
type: 'close',
63-
});
64-
worker.port.close();
6538
}
6639
});
67-
worker.port.addEventListener('error', (e) => {
68-
console.error(e);
69-
});
70-
worker.port.start();
71-
window.addEventListener('beforeunload', () => {
72-
worker.port.postMessage({
73-
type: 'close',
74-
});
75-
worker.port.close();
76-
});
7740

7841
return;
7942
}

0 commit comments

Comments
 (0)