-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Move EventSource to SharedWorker #12095
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
zeripath
merged 22 commits into
go-gitea:master
from
zeripath:notification-shared-worker
Jul 3, 2020
Merged
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
b875eb8
Move EventSource to SharedWorker
zeripath 4c27b3f
Also allow setting EVENT_SOURCE_UPDATE_TIME to disable EventSource up…
zeripath 261e401
as per @silverwind
zeripath eca09df
as per @silverwind
zeripath 4033888
Remove the plain Worker and EventSource fallbacks
zeripath 1681afc
Update custom/conf/app.example.ini
zeripath 7c83c21
as per @silverwind
zeripath 11e1638
as per @silverwind
zeripath e4bab08
as per @silverwind
zeripath da873f4
Update web_src/js/features/eventsource.sharedworker.js
zeripath 372ab50
as per @silverwind
zeripath 49f2c75
Update web_src/js/features/eventsource.sharedworker.js
zeripath 13d4d01
Update web_src/js/features/notification.js
zeripath e1f50b5
multiple stylistic changes as per @silverwind
zeripath bc1b11c
as per @silverwind
zeripath aaccdfc
as per @silverwind
zeripath ad82215
Merge branch 'master' into notification-shared-worker
zeripath 648ecfd
Merge branch 'master' into notification-shared-worker
zeripath 7052eba
more indentation
zeripath beb251c
Merge branch 'master' into notification-shared-worker
zeripath 817066b
Merge branch 'master' into notification-shared-worker
techknowlogick e362de1
As per @silverwind, @lafriks and @techknowlogick
zeripath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
self.name = 'eventsource.sharedworker.js'; | ||
|
||
const sourcesByUrl = {}; | ||
const sourcesByPort = {}; | ||
|
||
class Source { | ||
constructor(url) { | ||
this.url = url; | ||
this.eventSource = new EventSource(url); | ||
this.listening = {}; | ||
this.clients = []; | ||
this.listen('open'); | ||
this.listen('logout'); | ||
this.listen('notification-count'); | ||
this.listen('error'); | ||
} | ||
|
||
register(port) { | ||
if (!this.clients.includes(port)) return; | ||
|
||
this.clients.push(port); | ||
|
||
port.postMessage({ | ||
type: 'status', | ||
message: `registered to ${this.url}`, | ||
}); | ||
} | ||
|
||
deregister(port) { | ||
const portIdx = this.clients.indexOf(port); | ||
if (portIdx < 0) { | ||
return this.clients.length; | ||
} | ||
this.clients.splice(portIdx, 1); | ||
return this.clients.length; | ||
} | ||
|
||
close() { | ||
if (!this.eventSource) return; | ||
|
||
this.eventSource.close(); | ||
this.eventSource = null; | ||
} | ||
|
||
listen(eventType) { | ||
if (this.listening[eventType]) return; | ||
this.listening[eventType] = true; | ||
const self = this; | ||
this.eventSource.addEventListener(eventType, (event) => { | ||
self.notifyClients({ | ||
type: eventType, | ||
data: event.data | ||
}); | ||
}); | ||
} | ||
|
||
notifyClients(event) { | ||
for (const client of this.clients) { | ||
client.postMessage(event); | ||
} | ||
} | ||
|
||
status(port) { | ||
port.postMessage({ | ||
type: 'status', | ||
message: `url: ${this.url} readyState: ${this.eventSource.readyState}`, | ||
}); | ||
} | ||
} | ||
|
||
self.onconnect = (e) => { | ||
for (const port of e.ports) { | ||
port.addEventListener('message', (event) => { | ||
if (event.data.type === 'start') { | ||
const url = event.data.url; | ||
if (sourcesByUrl[url]) { | ||
// we have a Source registered to this url | ||
const source = sourcesByUrl[url]; | ||
source.register(port); | ||
sourcesByPort[port] = source; | ||
return; | ||
} | ||
let source = sourcesByPort[port]; | ||
if (source) { | ||
if (source.eventSource && source.url === url) return; | ||
|
||
// How this has happened I don't understand... | ||
// deregister from that source | ||
const count = source.deregister(port); | ||
// Clean-up | ||
if (count === 0) { | ||
source.close(); | ||
sourcesByUrl[source.url] = null; | ||
} | ||
} | ||
// Create a new Source | ||
source = new Source(url); | ||
source.register(port); | ||
sourcesByUrl[url] = source; | ||
sourcesByPort[port] = source; | ||
return; | ||
} else if (event.data.type === 'listen') { | ||
const source = sourcesByPort[port]; | ||
source.listen(event.data.eventType); | ||
return; | ||
} else if (event.data.type === 'close') { | ||
const source = sourcesByPort[port]; | ||
|
||
if (!source) return; | ||
|
||
const count = source.deregister(port); | ||
if (count === 0) { | ||
source.close(); | ||
sourcesByUrl[source.url] = null; | ||
sourcesByPort[port] = null; | ||
} | ||
return; | ||
} else if (event.data.type === 'status') { | ||
const source = sourcesByPort[port]; | ||
if (!source) { | ||
port.postMessage({ | ||
type: 'status', | ||
message: 'not connected', | ||
}); | ||
return; | ||
} | ||
source.status(port); | ||
return; | ||
} else { | ||
// just send it back | ||
port.postMessage({ | ||
type: 'error', | ||
message: `received but don't know how to handle: ${event.data}`, | ||
}); | ||
return; | ||
} | ||
}); | ||
port.start(); | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2432,7 +2432,6 @@ $(document).ready(async () => { | |
initContextPopups(); | ||
initTableSort(); | ||
initNotificationsTable(); | ||
initNotificationCount(); | ||
|
||
// Repo clone url. | ||
if ($('#repo-clone-url').length > 0) { | ||
|
@@ -2477,6 +2476,7 @@ $(document).ready(async () => { | |
initClipboard(), | ||
initUserHeatmap(), | ||
initServiceWorker(), | ||
initNotificationCount(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is not strictly necessary but as it is technically async this seems reasonable to do so. |
||
]); | ||
}); | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.