Skip to content

Commit

Permalink
Re-create the clients when they are updated via HMR (#1221)
Browse files Browse the repository at this point in the history
## Problem

- Updating a client via HMR does not crash anymore but the updated code
is not used, you need to manually reload the page to see the changes.

## Solution

- Add a hook which re-creates the clients when the clients module is
updated via HMR.

## Testing

- Tested manually with changing the `clients/storage.js` file.
  • Loading branch information
lslezak authored May 16, 2024
2 parents 26d852d + a7d9594 commit 6f593c3
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion web/src/context/installer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,28 @@ function InstallerClientProvider({
return;
}

console.warn(`Failed to connect to D-Bus (attempt ${attempt + 1})`);
console.warn(`Failed to connect to Agama API (attempt ${attempt + 1})`);
await new Promise(resolve => setTimeout(resolve, interval));
setAttempt(attempt + 1);
};

// allow hot replacement for the clients code
if (module.hot) {
// if anything coming from `import ... from "~/client"` is updated then this hook is called
module.hot.accept("~/client", async function() {
console.log("[Agama HMR] A client module has been updated");

const updated_client = await createDefaultClient();
if (await updated_client.isConnected()) {
console.log("[Agama HMR] Using new clients");
setValue(updated_client);
} else {
console.warn("[Agama HMR] Updating clients failed, using full page reload");
window.location.reload();
}
});
}

if (!value) connectClient();
}, [setValue, value, setAttempt, attempt, interval]);

Expand Down

0 comments on commit 6f593c3

Please sign in to comment.