Skip to content

Commit

Permalink
shell: Include state.jsx in type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
mvollmer committed Nov 14, 2024
1 parent cf7aa37 commit 2a8eef4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 38 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(obj: unknown);

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

interface ControlMessage extends JsonObject {
Expand Down
3 changes: 3 additions & 0 deletions pkg/shell/machines/machines.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ export function get_init_superuser_for_options(options) {
function Machines() {
const self = this;

// HACK - tell TypeScript that addEventListener exists.
self.addEventListener = null;

cockpit.event_target(self);

let flat = null;
Expand Down
79 changes: 41 additions & 38 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 @@ -57,13 +61,13 @@ 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
Expand All @@ -76,7 +80,7 @@ export function ShellState() {
function on_ready() {
if (machines.ready) {
self.ready = true;
window.addEventListener("popstate", ev => {
window.addEventListener("popstate", () => {
update();
ensure_frame_loaded();
ensure_connection();
Expand All @@ -92,7 +96,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 @@ -319,13 +323,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 @@ -483,36 +487,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,
ensure_connection,
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 Down Expand Up @@ -590,7 +564,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,
ensure_connection,
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);

return self;
}

0 comments on commit 2a8eef4

Please sign in to comment.