Skip to content

Commit

Permalink
Improve SSE listener reliability
Browse files Browse the repository at this point in the history
  • Loading branch information
allmarkedup committed Jun 9, 2024
1 parent c52ca8a commit 66e7e4a
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 8 deletions.
12 changes: 10 additions & 2 deletions app/components/lookbook/ui/app/router/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default AlpineComponent("router", (sseEndpoint = null) => {
return {
serverEventsListener: null,
routerLogger: null,
lastUpdate: Date.now(),

init() {
this.routerLogger = new Logger("Router");
Expand All @@ -29,6 +30,7 @@ export default AlpineComponent("router", (sseEndpoint = null) => {
await this.updateDOM(location, "router", {
headers: { "X-Lookbook-Frame": "root" },
});
this.lastUpdate = Date.now();
this.routerLogger.info(`Page updated`);
this.$dispatch("page-update:complete");
},
Expand All @@ -41,6 +43,7 @@ export default AlpineComponent("router", (sseEndpoint = null) => {
if (updateHistory) {
history.pushState({}, "", result.url);
}
this.lastUpdate = Date.now();
this.routerLogger.debug(`Page loaded`);
this.$dispatch("page-load:complete");
},
Expand Down Expand Up @@ -69,8 +72,13 @@ export default AlpineComponent("router", (sseEndpoint = null) => {
}
},

handleVisibilityChange() {
if (this.serverEventsListener && !document.hidden) this.updatePage();
async handleVisibilityChange() {
if (this.serverEventsListener && !document.hidden) {
const response = await fetch(`${sseEndpoint}/ping`);
const lastServerUpdate = Date.parse(await response.text());

if (lastServerUpdate > this.lastUpdate) this.updatePage();
}
},

destroy() {
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/lookbook/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,9 @@ def index

head :ok
end

def ping
render html: Engine.updated_at
end
end
end
23 changes: 22 additions & 1 deletion assets/js/server_events_listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ export default class ServerEventsListener {
constructor(endpoint) {
this.endpoint = endpoint;
this.source = null;
this.broadcastChannel = this.initBroadCastChannel();
this.handlers = [];
this.$logger = new Logger("EventsListener");

addEventListener("visibilitychange", () => {
document.hidden ? this.stop() : this.start();
if (!document.hidden) this.start();
});
}

start() {
if (!this.source) {
this.$logger.debug(`Starting`);
this.source = this.initSource();
this.broadcastStart();
}
}

Expand Down Expand Up @@ -56,4 +58,23 @@ export default class ServerEventsListener {

return source;
}

initBroadCastChannel() {
const bc = new BroadcastChannel("lookbook_events");

bc.addEventListener("message", (event) => {
const data = JSON.parse(event.data);
if (data.type === "event-source-start") {
this.stop();
}
});

return bc;
}

broadcastStart() {
this.broadcastChannel.postMessage(
JSON.stringify({ type: "event-source-start" })
);
}
}
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

get "/embed", to: "embeds#show", as: :embed

resources :events, only: [:index]
get "/events", to: "events#index", as: :events
get "/events/ping", to: "events#ping", as: :ping

match "*path", to: "application#not_found", via: :all
end
Expand Down
33 changes: 29 additions & 4 deletions public/lookbook-assets/app.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 66e7e4a

Please sign in to comment.