Skip to content
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

fix(web): postpone creation of the WSClient #1449

Merged
merged 3 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions web/package/agama-web-ui.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
-------------------------------------------------------------------
Tue Jul 9 08:51:34 UTC 2024 - Imobach Gonzalez Sosa <[email protected]>

- Do not try to connect to the WebSocket until the user is
logged in (gh#openSUSE/agama#).
imobachgs marked this conversation as resolved.
Show resolved Hide resolved

-------------------------------------------------------------------
Mon Jul 8 11:12:13 UTC 2024 - José Iván López González <[email protected]>

Expand Down
27 changes: 20 additions & 7 deletions web/src/client/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,24 @@ class HTTPClient {
const httpUrl = new URL(url.toString());
httpUrl.pathname = url.pathname.concat("api");
this.baseUrl = httpUrl.toString();
this.url = url;
}

/**
* Return the websocket client
*
* The WSClient is lazily created in the first call of this method.
*
* @return {WSClient}
*/
ws() {
if (this._ws) return this._ws;

const wsUrl = new URL(url.toString());
const wsUrl = new URL(this.url.toString());
wsUrl.pathname = wsUrl.pathname.concat("api/ws");
wsUrl.protocol = (url.protocol === "http:") ? "ws" : "wss";
this.ws = new WSClient(wsUrl);
wsUrl.protocol = (this.url.protocol === "http:") ? "ws" : "wss";
this._ws = new WSClient(wsUrl);
return this._ws;
}

/**
Expand Down Expand Up @@ -329,7 +342,7 @@ class HTTPClient {
* @return {RemoveFn} - Function to remove the handler.
*/
onClose(func) {
return this.ws.onClose(() => {
return this.ws().onClose(() => {
func();
});
}
Expand All @@ -342,7 +355,7 @@ class HTTPClient {
* @return {RemoveFn} - Function to remove the handler.
*/
onError(func) {
return this.ws.onError((event) => {
return this.ws().onError((event) => {
func(event);
});
}
Expand All @@ -354,7 +367,7 @@ class HTTPClient {
* @return {RemoveFn} - Function to remove the handler.
*/
onOpen(func) {
return this.ws.onOpen((event) => {
return this.ws().onOpen((event) => {
func(event);
});
}
Expand All @@ -367,7 +380,7 @@ class HTTPClient {
* @return {RemoveFn} - Function to remove the handler.
*/
onEvent(type, func) {
return this.ws.onEvent((event) => {
return this.ws().onEvent((event) => {
if (event.type === type) {
func(event);
}
Expand Down
11 changes: 5 additions & 6 deletions web/src/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,8 @@ const createClient = url => {
};
};

const isConnected = () => client.ws?.isConnected() || false;
const isRecoverable = () => !!client.ws?.isRecoverable();
const ws = () => client.ws;
const isConnected = () => client.ws().isConnected() || false;
const isRecoverable = () => !!client.ws().isRecoverable();

return {
l10n,
Expand All @@ -145,9 +144,9 @@ const createClient = url => {
onIssuesChange,
isConnected,
isRecoverable,
onConnect: handler => client.ws.onOpen(handler),
onDisconnect: handler => client.ws.onClose(handler),
ws: () => client.ws
onConnect: handler => client.ws().onOpen(handler),
onDisconnect: handler => client.ws().onClose(handler),
ws: () => client.ws()
};
};

Expand Down
2 changes: 1 addition & 1 deletion web/src/client/software.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ class ProductBaseClient {
* @param {(registration: Registration) => void} handler - Callback function.
*/
onRegistrationChange(handler) {
return this.client.ws.onEvent((event) => {
return this.client.ws().onEvent((event) => {
if (event.type === "RegistrationChanged" || event.type === "RegistrationRequirementChanged") {
this.getRegistration().then(handler);
}
Expand Down
2 changes: 1 addition & 1 deletion web/src/client/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class UsersBaseClient {
* @return {import ("./dbus").RemoveFn} function to disable the callback
*/
onUsersChange(handler) {
return this.client.ws.onEvent((event) => {
return this.client.ws().onEvent((event) => {
if (event.type === "RootChanged") {
const res = {};
if (event.password !== null) {
Expand Down
Loading