Skip to content

Commit

Permalink
Merge pull request #233 from KKoukiou/more-cleanups
Browse files Browse the repository at this point in the history
More code refactoring and cleanups
  • Loading branch information
KKoukiou authored Mar 26, 2024
2 parents a4354b9 + 5f704e5 commit 31eb5b8
Show file tree
Hide file tree
Showing 18 changed files with 403 additions and 344 deletions.
17 changes: 17 additions & 0 deletions src/apis/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { BossClient } from "./boss.js";
import { LocalizationClient } from "./localization.js";
import { NetworkClient } from "./network.js";
import { PayloadsClient } from "./payloads";
import { RuntimeClient } from "./runtime";
import { StorageClient } from "./storage.js";
import { UsersClient } from "./users";

export const clients = [
BossClient,
LocalizationClient,
NetworkClient,
PayloadsClient,
RuntimeClient,
StorageClient,
UsersClient
];
57 changes: 30 additions & 27 deletions src/apis/localization.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const getProperty = (...args) => {
};

export class LocalizationClient {
constructor (address) {
constructor (address, dispatch) {
if (LocalizationClient.instance && (!address || LocalizationClient.instance.address === address)) {
return LocalizationClient.instance;
}
Expand All @@ -50,10 +50,38 @@ export class LocalizationClient {
{ address, bus: "none", superuser: "try" }
);
this.address = address;
this.dispatch = dispatch;
}

init () {
async init () {
this.client.addEventListener("close", () => console.error("Localization client closed"));

this.startEventMonitor();

await this.initData();
}

async initData () {
await this.dispatch(getLanguageAction());
await this.dispatch(getLanguagesAction());
}

startEventMonitor () {
this.client.subscribe(
{ },
(path, iface, signal, args) => {
switch (signal) {
case "PropertiesChanged":
if (args[0] === INTERFACE_NAME && Object.hasOwn(args[1], "Language")) {
this.dispatch(getLanguageAction());
} else {
debug(`Unhandled signal on ${path}: ${iface}.${signal}`, JSON.stringify(args));
}
break;
default:
debug(`Unhandled signal on ${path}: ${iface}.${signal}`, JSON.stringify(args));
}
});
}
}

Expand Down Expand Up @@ -111,28 +139,3 @@ export const getLocaleData = ({ locale }) => {
export const setLanguage = ({ lang }) => {
return setProperty("Language", cockpit.variant("s", lang));
};

export const startEventMonitorLocalization = ({ dispatch }) => {
return new LocalizationClient().client.subscribe(
{ },
(path, iface, signal, args) => {
switch (signal) {
case "PropertiesChanged":
if (args[0] === INTERFACE_NAME && Object.hasOwn(args[1], "Language")) {
dispatch(getLanguageAction());
} else {
debug(`Unhandled signal on ${path}: ${iface}.${signal}`, JSON.stringify(args));
}
break;
default:
debug(`Unhandled signal on ${path}: ${iface}.${signal}`, JSON.stringify(args));
}
});
};

export const initDataLocalization = ({ dispatch }) => {
return Promise.all([
dispatch(getLanguageAction()),
dispatch(getLanguagesAction())
]);
};
57 changes: 30 additions & 27 deletions src/apis/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const getProperty = (...args) => {
};

export class NetworkClient {
constructor (address) {
constructor (address, dispatch) {
if (NetworkClient.instance && (!address || NetworkClient.instance.address === address)) {
return NetworkClient.instance;
}
Expand All @@ -44,10 +44,38 @@ export class NetworkClient {
{ address, bus: "none", superuser: "try" }
);
this.address = address;
this.dispatch = dispatch;
}

init () {
async init () {
this.client.addEventListener("close", () => console.error("Network client closed"));

this.startEventMonitor();

await this.initData();
}

async initData () {
await this.dispatch(getConnectedAction());
}

startEventMonitor () {
this.client.subscribe(
{ },
(path, iface, signal, args) => {
switch (signal) {
case "PropertiesChanged":
if (args[0] === INTERFACE_NAME && Object.hasOwn(args[1], "Connected")) {
this.dispatch(getConnectedAction());
} else {
debug(`Unhandled signal on ${path}: ${iface}.${signal}`, JSON.stringify(args));
}
break;
default:
debug(`Unhandled signal on ${path}: ${iface}.${signal}`, JSON.stringify(args));
}
}
);
}
}

Expand All @@ -57,28 +85,3 @@ export class NetworkClient {
export const getConnected = () => {
return getProperty("Connected");
};

export const startEventMonitorNetwork = ({ dispatch }) => {
return new NetworkClient().client.subscribe(
{ },
(path, iface, signal, args) => {
switch (signal) {
case "PropertiesChanged":
if (args[0] === INTERFACE_NAME && Object.hasOwn(args[1], "Connected")) {
dispatch(getConnectedAction());
} else {
debug(`Unhandled signal on ${path}: ${iface}.${signal}`, JSON.stringify(args));
}
break;
default:
debug(`Unhandled signal on ${path}: ${iface}.${signal}`, JSON.stringify(args));
}
}
);
};

export const initDataNetwork = ({ dispatch }) => {
return Promise.all([
dispatch(getConnectedAction())
]);
};
3 changes: 2 additions & 1 deletion src/apis/payloads.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const callClient = (...args) => {
};

export class PayloadsClient {
constructor (address) {
constructor (address, dispatch) {
if (PayloadsClient.instance && (!address || PayloadsClient.instance.address === address)) {
return PayloadsClient.instance;
}
Expand All @@ -40,6 +40,7 @@ export class PayloadsClient {
{ address, bus: "none", superuser: "try" }
);
this.address = address;
this.dispatch = dispatch;
}

init () {
Expand Down
57 changes: 30 additions & 27 deletions src/apis/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const getProperty = (...args) => {
};

export class RuntimeClient {
constructor (address) {
constructor (address, dispatch) {
if (RuntimeClient.instance && (!address || RuntimeClient.instance.address === address)) {
return RuntimeClient.instance;
}
Expand All @@ -44,12 +44,40 @@ export class RuntimeClient {
{ address, bus: "none", superuser: "try" }
);
this.address = address;
this.dispatch = dispatch;
}

init () {
async init () {
this.client.addEventListener(
"close", () => console.error("Runtime client closed")
);

this.startEventMonitor();

await this.initData();
}

startEventMonitor () {
this.client.subscribe(
{ },
(path, iface, signal, args) => {
switch (signal) {
case "PropertiesChanged":
if (args[0] === INTERFACE_NAME && Object.hasOwn(args[1], "PasswordPolicies")) {
this.dispatch(getPasswordPoliciesAction());
} else {
debug(`Unhandled signal on ${path}: ${iface}.${signal}`, JSON.stringify(args));
}
break;
default:
debug(`Unhandled signal on ${path}: ${iface}.${signal}`, JSON.stringify(args));
}
}
);
}

async initData () {
await this.dispatch(getPasswordPoliciesAction());
}
}

Expand All @@ -68,28 +96,3 @@ export const getIsFinal = () => {
export const getPasswordPolicies = () => {
return getProperty("PasswordPolicies");
};

export const startEventMonitorRuntime = ({ dispatch }) => {
return new RuntimeClient().client.subscribe(
{ },
(path, iface, signal, args) => {
switch (signal) {
case "PropertiesChanged":
if (args[0] === INTERFACE_NAME && Object.hasOwn(args[1], "PasswordPolicies")) {
dispatch(getPasswordPoliciesAction());
} else {
debug(`Unhandled signal on ${path}: ${iface}.${signal}`, JSON.stringify(args));
}
break;
default:
debug(`Unhandled signal on ${path}: ${iface}.${signal}`, JSON.stringify(args));
}
}
);
};

export const initDataRuntime = ({ dispatch }) => {
return Promise.all([
dispatch(getPasswordPoliciesAction())
]);
};
85 changes: 45 additions & 40 deletions src/apis/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const getProperty = (...args) => {
};

export class StorageClient {
constructor (address) {
constructor (address, dispatch) {
if (StorageClient.instance && (!address || StorageClient.instance.address === address)) {
return StorageClient.instance;
}
Expand All @@ -53,10 +53,53 @@ export class StorageClient {
{ address, bus: "none", superuser: "try" }
);
this.address = address;
this.dispatch = dispatch;
}

init () {
async init () {
this.client.addEventListener("close", () => console.error("Storage client closed"));

this.startEventMonitor();

await this.initData();
}

startEventMonitor () {
this.client.subscribe(
{ },
(path, iface, signal, args) => {
switch (signal) {
case "PropertiesChanged":
if (args[0] === "org.fedoraproject.Anaconda.Modules.Storage.DiskSelection") {
this.dispatch(getDiskSelectionAction());
} else if (args[0] === "org.fedoraproject.Anaconda.Modules.Storage.Partitioning.Manual" && Object.hasOwn(args[1], "Requests")) {
this.dispatch(getPartitioningDataAction({ partitioning: path, requests: args[1].Requests.v }));
} else if (args[0] === "org.fedoraproject.Anaconda.Modules.Storage.Partitioning.Automatic" && Object.hasOwn(args[1], "Request")) {
this.dispatch(getPartitioningDataAction({ partitioning: path, requests: [args[1].Request.v] }));
} else if (args[0] === INTERFACE_NAME && Object.hasOwn(args[1], "CreatedPartitioning")) {
const last = args[1].CreatedPartitioning.v.length - 1;
this.dispatch(getPartitioningDataAction({ partitioning: args[1].CreatedPartitioning.v[last] }));
} else {
debug(`Unhandled signal on ${path}: ${iface}.${signal} ${JSON.stringify(args)}`);
}
break;
default:
debug(`Unhandled signal on ${path}: ${iface}.${signal} ${JSON.stringify(args)}`);
}
});
}

async initData () {
this.dispatch(setStorageScenarioAction(window.localStorage.getItem("storage-scenario-id") || getDefaultScenario().id));

const partitioning = await getProperty("CreatedPartitioning");
if (partitioning.length !== 0) {
for (const path of partitioning) {
await this.dispatch(getPartitioningDataAction({ partitioning: path }));
}
}
await this.dispatch(getDevicesAction());
await this.dispatch(getDiskSelectionAction());
}
}

Expand Down Expand Up @@ -96,41 +139,3 @@ export const runStorageTask = ({ onFail, onSuccess, task }) => {
export const scanDevicesWithTask = () => {
return callClient("ScanDevicesWithTask", []);
};

export const startEventMonitorStorage = ({ dispatch }) => {
return new StorageClient().client.subscribe(
{ },
(path, iface, signal, args) => {
switch (signal) {
case "PropertiesChanged":
if (args[0] === "org.fedoraproject.Anaconda.Modules.Storage.DiskSelection") {
dispatch(getDiskSelectionAction());
} else if (args[0] === "org.fedoraproject.Anaconda.Modules.Storage.Partitioning.Manual" && Object.hasOwn(args[1], "Requests")) {
dispatch(getPartitioningDataAction({ partitioning: path, requests: args[1].Requests.v }));
} else if (args[0] === "org.fedoraproject.Anaconda.Modules.Storage.Partitioning.Automatic" && Object.hasOwn(args[1], "Request")) {
dispatch(getPartitioningDataAction({ partitioning: path, requests: [args[1].Request.v] }));
} else if (args[0] === INTERFACE_NAME && Object.hasOwn(args[1], "CreatedPartitioning")) {
const last = args[1].CreatedPartitioning.v.length - 1;
dispatch(getPartitioningDataAction({ partitioning: args[1].CreatedPartitioning.v[last] }));
} else {
debug(`Unhandled signal on ${path}: ${iface}.${signal} ${JSON.stringify(args)}`);
}
break;
default:
debug(`Unhandled signal on ${path}: ${iface}.${signal} ${JSON.stringify(args)}`);
}
});
};

export const initDataStorage = ({ dispatch }) => {
dispatch(setStorageScenarioAction(window.localStorage.getItem("storage-scenario-id") || getDefaultScenario().id));

return getProperty("CreatedPartitioning")
.then(res => {
if (res.length !== 0) {
return Promise.all(res.map(path => dispatch(getPartitioningDataAction({ partitioning: path }))));
}
})
.then(() => dispatch(getDevicesAction()))
.then(() => dispatch(getDiskSelectionAction()));
};
3 changes: 2 additions & 1 deletion src/apis/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const callClient = (...args) => {
};

export class UsersClient {
constructor (address) {
constructor (address, dispatch) {
if (UsersClient.instance && (!address || UsersClient.instance.address === address)) {
return UsersClient.instance;
}
Expand All @@ -44,6 +44,7 @@ export class UsersClient {
{ address, bus: "none", superuser: "try" }
);
this.address = address;
this.dispatch = dispatch;
}

init () {
Expand Down
Loading

0 comments on commit 31eb5b8

Please sign in to comment.