Skip to content

Commit

Permalink
shell: Check types in "state"
Browse files Browse the repository at this point in the history
The "state" file now participates in type checking, but we still allow
the "any" type.  TypeScript will automatically infer "any" in many
places and shut up.  Later we can add more types, which will mean a
rewrite of ShellState as a proper class.
  • Loading branch information
mvollmer committed Nov 27, 2024
1 parent 896fda0 commit 61d3f8b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 40 deletions.
3 changes: 3 additions & 0 deletions pkg/lib/cockpit.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ declare module 'cockpit' {
export const manifests: { [package in string]?: JsonObject };

export let language: string;
export let language_direction: string;

interface Transport {
csrf_token: string;
Expand Down Expand Up @@ -98,6 +99,8 @@ declare module 'cockpit' {
changed(): void;
}

function event_target<T, EM extends EventMap>(obj: T): T & EventSource<EM>;

/* === Channel =============================== */

interface ControlMessage extends JsonObject {
Expand Down
82 changes: 42 additions & 40 deletions pkg/shell/state.jsx → pkg/shell/state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* along with Cockpit; If not, see <https://www.gnu.org/licenses/>.
*/

// @cockpit-ts-relaxed

import cockpit from "cockpit";

import { Router } from "./router.jsx";
Expand All @@ -27,6 +29,8 @@ import {
} from "./util.jsx";

export function ShellState() {
let self = null;

/* CONFIG
*/

Expand Down Expand Up @@ -75,26 +79,23 @@ export function ShellState() {

machines.addEventListener("ready", on_ready);

machines.addEventListener("removed", (ev, machine) => {
machines.addEventListener("removed", (_, machine) => {
remove_machine_frames(machine);
});
machines.addEventListener("added", (ev, machine) => {
machines.addEventListener("added", (_, machine) => {
preload_machine_frames(machine);
});
machines.addEventListener("updated", (ev, machine) => {
machines.addEventListener("updated", (_, machine) => {
if (!machine.visible || machine.problem)
remove_machine_frames(machine);
else
preload_machine_frames(machine);
});

if (machines.ready)
on_ready();

function on_ready() {
if (machines.ready && config_ready) {
self.ready = true;
window.addEventListener("popstate", ev => {
window.addEventListener("popstate", () => {
update();
ensure_frame_loaded();
ensure_connection();
Expand All @@ -110,7 +111,7 @@ export function ShellState() {
*/

const watchdog = cockpit.channel({ payload: "null" });
watchdog.addEventListener("close", (event, options) => {
watchdog.addEventListener("close", (_, options) => {
const watchdog_problem = options.problem || "disconnected";
console.warn("transport closed: " + watchdog_problem);
self.problem = watchdog_problem;
Expand Down Expand Up @@ -337,13 +338,13 @@ export function ShellState() {
},
};

const router = new Router(router_callbacks);

function send_frame_hidden_hint (frame_name) {
const hidden = !self.current_frame || self.current_frame.name != frame_name;
router.hint(frame_name, { hidden });
}

const router = new Router(router_callbacks);

/* NAVIGATION
*
* The main navigation function, jump(), will change
Expand Down Expand Up @@ -501,35 +502,6 @@ export function ShellState() {
* for the "Overview" menu entry from the "system" package.
*/

const self = {
ready: false,
problem: null,
has_oops: false,

config,
page_status,
frames,

current_location: null,
current_machine: null,
current_manifest_item: null,
current_machine_manifest_items: null,
current_manifest: null,

// Methods
jump,
remove_frame,
most_recent_path_for_host,

// Access to the inner parts of the machinery, use with
// caution.
machines,
loader,
router,
};

cockpit.event_target(self);

function update() {
if (!self.ready || self.problem) {
self.dispatchEvent("update");
Expand All @@ -553,6 +525,7 @@ export function ShellState() {
if (!machine || !machine.visible) {
machine = {
key: location.host,
connection_string: location.host,
address: location.host,
label: location.host,
state: "failed",
Expand Down Expand Up @@ -608,7 +581,36 @@ export function ShellState() {
self.dispatchEvent("update");
}

self.update = update;
self = {
ready: false,
problem: null,
has_oops: false,

config,
page_status,
frames,

current_location: null,
current_machine: null,
current_manifest_item: null,
current_machine_manifest_items: null,
current_manifest: null,

// Methods
jump,
remove_frame,
most_recent_path_for_host,
update,

// Access to the inner parts of the machinery, use with
// caution.
machines,
loader,
router,
};

cockpit.event_target(self);
on_ready();

return self;
}

0 comments on commit 61d3f8b

Please sign in to comment.