mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-19 07:33:12 +09:00
Refactor JSON serialiable class pattern
Change the JSON seriasiable data classes to use an explicit static fromJSON method and an optional toJSON method for turning them to and from JSON. This improves over the previous pattern of passing the JSON to the constructor as it frees up the constructor to be able to take whichever argument types suits the code instead of being beholden to the JSON serialised form.
This commit is contained in:
1 parent
a448d87e3a
commit
9a967a5779
20 files changed
+222
-97
No files matched your search
@@ -57,7 +57,7 @@ function SearchModsTable(props) {
|
||||
// In React < v18 this causes 3 renders and a partial state necessitating the use
|
||||
// of || 0 on modResultSelectedVersion.get() calls. Remove when updating React.
|
||||
setModResults(response.results.map(
|
||||
({ name, versions }) => ({ name, versions: versions.map(mod => new libData.ModInfo(mod)) })
|
||||
({ name, versions }) => ({ name, versions: versions.map(mod => libData.ModInfo.fromJSON(mod)) })
|
||||
));
|
||||
setModResultSelectedVersion(new Map(response.results.map(({ name, versions }) => [name, 0])));
|
||||
setModResultCount(response.result_count);
|
||||
@@ -630,7 +630,7 @@ export default function ModPackViewPage() {
|
||||
return;
|
||||
}
|
||||
|
||||
const modified = new libData.ModPack(modPack.toJSON());
|
||||
const modified = libData.ModPack.fromJSON(JSON.parse(JSON.stringify(modPack)));
|
||||
for (let change of changes) {
|
||||
if (change.type === "name") {
|
||||
modified.name = change.value;
|
||||
|
||||
@@ -95,7 +95,7 @@ function CreateModPackButton() {
|
||||
} catch {
|
||||
return; // Validation failed
|
||||
}
|
||||
const modPack = new libData.ModPack();
|
||||
const modPack = libData.ModPack.fromJSON({});
|
||||
modPack.name = values.name;
|
||||
modPack.factorioVersion = values.factorioVersion;
|
||||
if (values.description) { modPack.description = values.description; }
|
||||
|
||||
@@ -12,7 +12,7 @@ export function useExportManifest() {
|
||||
useEffect(() => {
|
||||
async function load() {
|
||||
let result = await libLink.messages.getDefaultModPack.send(control);
|
||||
let modPack = new libData.ModPack(result.mod_pack);
|
||||
let modPack = libData.ModPack.fromJSON(result.mod_pack);
|
||||
if (modPack.exportManifest) {
|
||||
exportManifestCache = modPack.exportManifest;
|
||||
setExportManifest(exportManifestCache);
|
||||
|
||||
@@ -12,7 +12,7 @@ export function useModPack(id) {
|
||||
|
||||
function updateModPack() {
|
||||
libLink.messages.getModPack.send(control, { id }).then(result => {
|
||||
setModPack(new libData.ModPack(result.mod_pack));
|
||||
setModPack(libData.ModPack.fromJSON(result.mod_pack));
|
||||
}).catch(err => {
|
||||
logger.error(`Failed to get mod pack: ${err}`);
|
||||
setModPack({ missing: true });
|
||||
@@ -41,7 +41,7 @@ export function useModPackList() {
|
||||
|
||||
function updateModPackList() {
|
||||
libLink.messages.listModPacks.send(control).then(result => {
|
||||
setModPackList(result.list.map(pack => new libData.ModPack(pack)));
|
||||
setModPackList(result.list.map(pack => libData.ModPack.fromJSON(pack)));
|
||||
}).catch(err => {
|
||||
logger.error(`Failed to list mod packs:\n${err}`);
|
||||
});
|
||||
|
||||
@@ -13,7 +13,7 @@ export function useModList() {
|
||||
|
||||
function updateModList() {
|
||||
libLink.messages.listMods.send(control).then(result => {
|
||||
setModList(result.list.map(mod => new libData.ModInfo(mod)));
|
||||
setModList(result.list.map(mod => libData.ModInfo.fromJSON(mod)));
|
||||
}).catch(err => {
|
||||
logger.error(`Failed to list mods:\n${err}`);
|
||||
});
|
||||
|
||||
@@ -312,7 +312,7 @@ export class Control extends libLink.Link {
|
||||
}
|
||||
|
||||
async modPackUpdateEventHandler(message) {
|
||||
let modPack = new libData.ModPack(message.data.mod_pack);
|
||||
let modPack = libData.ModPack.fromJSON(message.data.mod_pack);
|
||||
let handlers = [].concat(
|
||||
this.modPackUpdateHandlers.get(null) || [],
|
||||
this.modPackUpdateHandlers.get(modPack.id) || [],
|
||||
@@ -370,7 +370,7 @@ export class Control extends libLink.Link {
|
||||
}
|
||||
|
||||
async modUpdateEventHandler(message) {
|
||||
let mod = new libData.ModInfo(message.data.mod);
|
||||
let mod = libData.ModInfo.fromJSON(message.data.mod);
|
||||
let handlers = [].concat(
|
||||
this.modUpdateHandlers.get(null) || [],
|
||||
this.modUpdateHandlers.get(mod.name) || []
|
||||
|
||||
Reference in new issue
Block a user