Reverse argument order for sendTo

It makes more sense to put the address first then have the large
structure to send as the second argument to sendTo.  The opposite order
was originally chosen to be similar to that of sendto in BSD sockets.
This commit is contained in:
Hornwitser committed 2023-07-27 00:24:24 +02:00
1 parent 183d2921a8
commit 295cc7543a
24 files changed
+87 -74

No files matched your search

+10 -10
View File
@@ -421,7 +421,7 @@ class ControlConnection extends BaseConnection {
// Let host handle request if source and target is on the same host.
if (sourceHostId === targetHostId) {
return await this._controller.sendTo(request, { hostId: sourceHostId });
return await this._controller.sendTo({ hostId: sourceHostId }, request);
}
// Check connectivity
@@ -451,23 +451,23 @@ class ControlConnection extends BaseConnection {
// ensure the file size is known prior to the target host pull.
await Promise.all([
this._controller.sendTo(
{ hostId: sourceHostId },
new libData.InstancePushSaveRequest(request.sourceInstanceId, stream.id, request.sourceName),
{ hostId: sourceHostId }
),
events.once(stream.events, "source"),
]);
// Establish pull from target host to stream and wait for completion.
let storedName = await this._controller.sendTo(
new libData.InstancePullSaveRequest(request.targetInstanceId, stream.id, request.targetName),
{ hostId: targetHostId },
new libData.InstancePullSaveRequest(request.targetInstanceId, stream.id, request.targetName),
);
// Delete source save if this is not a copy
if (!request.copy) {
await this._controller.sendTo(
{ hostId: sourceHostId },
new libData.InstanceDeleteSaveRequest(request.sourceInstanceId, request.sourceName),
{ hostId: sourceHostId }
);
}
@@ -887,7 +887,7 @@ class ControlConnection extends BaseConnection {
user.isAdmin = admin;
this._controller.userUpdated(user);
this._controller.sendTo(new libData.InstanceAdminlistUpdateEvent(name, admin), "allInstances");
this._controller.sendTo("allInstances", new libData.InstanceAdminlistUpdateEvent(name, admin));
}
async handleUserSetBannedRequest(request) {
@@ -905,7 +905,7 @@ class ControlConnection extends BaseConnection {
user.isBanned = banned;
user.banReason = reason;
this._controller.userUpdated(user);
this._controller.sendTo(new libData.InstanceBanlistUpdateEvent(name, banned, reason), "allInstances");
this._controller.sendTo("allInstances", new libData.InstanceBanlistUpdateEvent(name, banned, reason));
}
async handleUserSetWhitelistedRequest(request) {
@@ -922,7 +922,7 @@ class ControlConnection extends BaseConnection {
user.isWhitelisted = whitelisted;
this._controller.userUpdated(user);
this._controller.sendTo(new libData.InstanceWhitelistUpdateEvent(name, whitelisted), "allInstances");
this._controller.sendTo("allInstances", new libData.InstanceWhitelistUpdateEvent(name, whitelisted));
}
async handleUserDeleteRequest(request) {
@@ -937,13 +937,13 @@ class ControlConnection extends BaseConnection {
this._controller.userUpdated(user);
if (user.is_admin) {
this._controller.sendTo(new libData.InstanceAdminlistUpdateEvent(name, false), "allInstances");
this._controller.sendTo("allInstances", new libData.InstanceAdminlistUpdateEvent(name, false));
}
if (user.is_whitelisted) {
this._controller.sendTo(new libData.InstanceWhitelistUpdateEvent(name, false), "allInstances");
this._controller.sendTo("allInstances", new libData.InstanceWhitelistUpdateEvent(name, false));
}
if (user.is_banned) {
this._controller.sendTo(new libData.InstanceBanlistUpdateEvent(name, false, ""), "allInstances");
this._controller.sendTo("allInstances", new libData.InstanceBanlistUpdateEvent(name, false, ""));
}
}
+3 -3
View File
@@ -715,7 +715,7 @@ class Controller {
let instance = this.getRequestInstance(instanceId);
let hostId = instance.config.get("instance.assigned_host");
if (hostId !== null) {
await this.sendTo(new libData.InstanceDeleteInternalRequest(instanceId), { hostId });
await this.sendTo({ hostId }, new libData.InstanceDeleteInternalRequest(instanceId));
}
this.instances.delete(instanceId);
@@ -1028,13 +1028,13 @@ class Controller {
* destination argument supports address shorthands, see {@link
* module:lib/data.Address.fromShorthand}
*
* @param {*} requestOrEvent - The request or event to send.
* @param {*|module:lib/data.Address} address - Where to send it.
* @param {*} requestOrEvent - The request or event to send.
* @returns {Promise<*>|undefined}
* Promise that resolves to the response if a request was sent or
* undefined if it was an event.
*/
sendTo(requestOrEvent, address) {
sendTo(address, requestOrEvent) {
let dst = libData.Address.fromShorthand(address);
if (requestOrEvent.constructor.type === "request") {
return this.sendRequest(requestOrEvent, dst);
+11 -9
View File
@@ -515,7 +515,7 @@ instanceSaveCommands.add(new libCommand.Command({
}],
handler: async function(args, control) {
let instanceId = await libCommand.resolveInstance(control, args.instance);
let saves = await control.sendTo(new libData.InstanceListSavesRequest(), { instanceId });
let saves = await control.sendTo({ instanceId }, new libData.InstanceListSavesRequest());
for (let entry of saves) {
entry.mtime = new Date(entry.mtimeMs).toLocaleString();
delete entry.mtimeMs;
@@ -540,7 +540,8 @@ instanceSaveCommands.add(new libCommand.Command({
let { seed, mapGenSettings, mapSettings } = await loadMapSettings(args);
await control.setLogSubscriptions({ instanceIds: [instanceId] });
await control.sendTo(
new libData.InstanceCreateSaveRequest(args.name, seed, mapGenSettings, mapSettings), { instanceId }
{ instanceId },
new libData.InstanceCreateSaveRequest(args.name, seed, mapGenSettings, mapSettings),
);
},
}));
@@ -711,7 +712,7 @@ instanceCommands.add(new libCommand.Command({
handler: async function(args, control) {
let instanceId = await libCommand.resolveInstance(control, args.instance);
await control.setLogSubscriptions({ instanceIds: [instanceId] });
await control.sendTo(new libData.InstanceExportDataRequest(), { instanceId });
await control.sendTo({ instanceId }, new libData.InstanceExportDataRequest());
},
}));
@@ -722,7 +723,7 @@ instanceCommands.add(new libCommand.Command({
handler: async function(args, control) {
let instanceId = await libCommand.resolveInstance(control, args.instance);
await control.setLogSubscriptions({ instanceIds: [instanceId] });
await control.sendTo(new libData.InstanceExtractPlayersRequest(), { instanceId });
await control.sendTo({ instanceId }, new libData.InstanceExtractPlayersRequest());
},
}));
@@ -737,7 +738,7 @@ instanceCommands.add(new libCommand.Command({
handler: async function(args, control) {
let instanceId = await libCommand.resolveInstance(control, args.instance);
await control.setLogSubscriptions({ instanceIds: [instanceId] });
await control.sendTo(new libData.InstanceStartRequest(args.save), { instanceId });
await control.sendTo({ instanceId }, new libData.InstanceStartRequest(args.save));
control.keepOpen = args.keepOpen;
},
}));
@@ -759,7 +760,8 @@ instanceCommands.add(new libCommand.Command({
await control.setLogSubscriptions({ instanceIds: [instanceId] });
let { seed, mapGenSettings, mapSettings } = await loadMapSettings(args);
await control.sendTo(
new libData.InstanceLoadScenarioRequest(args.scenario, seed, mapGenSettings, mapSettings), { instanceId }
{ instanceId },
new libData.InstanceLoadScenarioRequest(args.scenario, seed, mapGenSettings, mapSettings),
);
control.keepOpen = args.keepOpen;
},
@@ -772,7 +774,7 @@ instanceCommands.add(new libCommand.Command({
handler: async function(args, control) {
let instanceId = await libCommand.resolveInstance(control, args.instance);
await control.setLogSubscriptions({ instanceIds: [instanceId] });
await control.sendTo(new libData.InstanceStopRequest(args.save), { instanceId });
await control.sendTo({ instanceId }, new libData.InstanceStopRequest(args.save));
},
}));
@@ -783,7 +785,7 @@ instanceCommands.add(new libCommand.Command({
handler: async function(args, control) {
let instanceId = await libCommand.resolveInstance(control, args.instance);
await control.setLogSubscriptions({ instanceIds: [instanceId] });
await control.sendTo(new libData.InstanceKillRequest(args.save), { instanceId });
await control.sendTo({ instanceId }, new libData.InstanceKillRequest(args.save));
},
}));
@@ -804,8 +806,8 @@ instanceCommands.add(new libCommand.Command({
}],
handler: async function(args, control) {
let result = await control.sendTo(
new libData.InstanceSendRconRequest(args.command),
{ instanceId: await libCommand.resolveInstance(control, args.instance) },
new libData.InstanceSendRconRequest(args.command),
);
// Factorio includes a newline in its response output.
+13 -10
View File
@@ -303,7 +303,7 @@ class Instance extends libLink.Link {
name,
stats,
};
this.sendTo(new libData.InstancePlayerUpdateEvent("join", name, undefined, stats), "controller");
this.sendTo("controller", new libData.InstancePlayerUpdateEvent("join", name, undefined, stats));
libPlugin.invokeHook(this.plugins, "onPlayerEvent", event);
}
@@ -325,7 +325,7 @@ class Instance extends libLink.Link {
reason,
stats,
};
this.sendTo(new libData.InstancePlayerUpdateEvent("leave", name, reason, stats), "controller");
this.sendTo("controller", new libData.InstancePlayerUpdateEvent("leave", name, reason, stats));
libPlugin.invokeHook(this.plugins, "onPlayerEvent", event);
}
@@ -353,7 +353,7 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
name,
stats: stats.toJSON(),
};
this.sendTo(new libData.InstancePlayerUpdateEvent("import", name, undefined, stats), "controller");
this.sendTo("controller", new libData.InstancePlayerUpdateEvent("import", name, undefined, stats));
libPlugin.invokeHook(this.plugins, "onPlayerEvent", event);
count += 1;
}
@@ -405,10 +405,13 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
}
async sendSaveListUpdate() {
this.sendTo(new libData.InstanceSaveListUpdateEvent(
this.id,
await Instance.listSaves(this.path("saves"), this._loadedSave),
), "controller");
this.sendTo(
"controller",
new libData.InstanceSaveListUpdateEvent(
this.id,
await Instance.listSaves(this.path("saves"), this._loadedSave),
),
);
}
async _autosave(name) {
@@ -436,12 +439,12 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
notifyStatus(status) {
this._status = status;
this.sendTo(
"controller",
new libData.InstanceStatusChangedEvent(
this.id,
status,
this.server && this.server.gamePort || this.config.get("factorio.game_port") || null,
),
"controller"
);
}
@@ -622,9 +625,9 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
const modPackId = this.config.get("factorio.mod_pack");
let modPack;
if (modPackId === null) {
modPack = await this.sendTo(new libData.ModPackGetDefaultRequest(), "controller");
modPack = await this.sendTo("controller", new libData.ModPackGetDefaultRequest());
} else {
modPack = await this.sendTo(new libData.ModPackGetRequest(modPackId), "controller");
modPack = await this.sendTo("controller", new libData.ModPackGetRequest(modPackId));
}
this.activeModPack = modPack;
+4 -4
View File
@@ -151,7 +151,7 @@ async function resolveHost(client, hostName) {
if (/^-?\d+$/.test(hostName)) {
hostId = parseInt(hostName, 10);
} else {
let hosts = await client.sendTo(new libData.HostListRequest(), "controller");
let hosts = await client.sendTo("controller", new libData.HostListRequest());
for (let host of hosts) {
if (host.name === hostName) {
hostId = host.id;
@@ -183,7 +183,7 @@ async function resolveInstance(client, instanceName) {
if (/^-?\d+$/.test(instanceName)) {
instanceId = parseInt(instanceName, 10);
} else {
let instances = await client.sendTo(new libData.InstanceDetailsListRequest(), "controller");
let instances = await client.sendTo("controller", new libData.InstanceDetailsListRequest());
for (let instance of instances) {
if (instance.name === instanceName) {
instanceId = instance.id;
@@ -216,7 +216,7 @@ async function resolveModPack(client, modPackName) {
if (/^-?\d+/.test(modPackName)) {
modPackId = parseInt(modPackName, 10);
} else {
let modPacks = await client.sendTo(new libData.ModPackListRequest(), "controller");
let modPacks = await client.sendTo("controller", new libData.ModPackListRequest());
for (let modPack of modPacks) {
if (modPack.name === modPackName) {
modPackId = modPack.id;
@@ -244,7 +244,7 @@ async function resolveModPack(client, modPackName) {
* @static
*/
async function retrieveRole(client, roleName) {
let roles = await client.sendTo(new libData.RoleListRequest(), "controller");
let roles = await client.sendTo("controller", new libData.RoleListRequest());
let resolvedRole;
if (/^-?\d+$/.test(roleName)) {
+3 -3
View File
@@ -374,19 +374,19 @@ class Link {
* undefined if it was an event.
*/
send(requestOrEvent) {
return this.sendTo(requestOrEvent, this.connector.dst);
return this.sendTo(this.connector.dst, requestOrEvent);
}
/**
* Send a request or event to the given address
*
* @param {*} requestOrEvent - Request or event to send
* @param {*|module:lib/data.Address} destination - Where to send it
* @param {*} requestOrEvent - Request or event to send
* @returns {Promise<*>|undefined}
* Promise that resolves to the response if a request was sent or
* undefined if it was an event.
*/
sendTo(requestOrEvent, destination) {
sendTo(destination, requestOrEvent) {
let dst = libData.Address.fromShorthand(destination);
if (requestOrEvent.constructor.type === "request") {
@@ -52,8 +52,8 @@ export default function CreateSaveModal(props) {
setCreatingSave(true);
control.sendTo(
{ instanceId: props.instance.id },
new libData.InstanceCreateSaveRequest(name, seed, mapGenSettings, mapSettings),
{ instanceId: props.instance.id }
).then(() => {
form.resetFields();
setVisible(false);
@@ -23,8 +23,8 @@ export default function InstanceRcon(props) {
setRunning(true);
try {
let result = await control.sendTo(
{ instanceId: props.id },
new libData.InstanceSendRconRequest(command),
{ instanceId: props.id }
);
setOutput(result);
} finally {
@@ -132,7 +132,8 @@ export default function InstanceViewPage(props) {
if (key === "export") {
setExportingData(true);
control.sendTo(
new libData.InstanceExportDataRequest(), { instanceId }
{ instanceId },
new libData.InstanceExportDataRequest(),
).catch(
notifyErrorHandler("Error exporting data")
).finally(() => {
@@ -141,12 +142,14 @@ export default function InstanceViewPage(props) {
} else if (key === "extract") {
control.sendTo(
new libData.InstanceExtractPlayersRequest(), { instanceId }
{ instanceId },
new libData.InstanceExtractPlayersRequest(),
).catch(notifyErrorHandler("Error extracting player data"));
} else if (key === "kill") {
control.sendTo(
new libData.InstanceKillRequest(), { instanceId }
{ instanceId },
new libData.InstanceKillRequest(),
).catch(notifyErrorHandler("Error killing instance"));
} else if (key === "delete") {
@@ -84,7 +84,8 @@ export default function InstancesPage() {
&& <Button onClick={e => instanceList.forEach(instance => {
if (["starting", "running"].includes(instance.status)) {
control.sendTo(
new libData.InstanceStopRequest(), { instanceId: instance.id }
{ instanceId: instance.id },
new libData.InstanceStopRequest(),
).catch(notifyErrorHandler("Error stopping instance"));
}
})
@@ -52,8 +52,8 @@ export default function LoadScenarioModal(props) {
setLoadingScenario(true);
control.sendTo(
{ instanceId: props.instance.id },
new libData.InstanceLoadScenarioRequest(scenario, seed, mapGenSettings, mapSettings),
{ instanceId: props.instance.id }
).then(() => {
setVisible(false);
}).catch(
+2 -1
View File
@@ -228,7 +228,8 @@ export default function SavesList(props) {
onClick={() => {
setStarting(true);
control.sendTo(
new libData.InstanceStartRequest(save.name), { instanceId: props.instance.id }
{ instanceId: props.instance.id },
new libData.InstanceStartRequest(save.name),
).catch(
notifyErrorHandler("Error loading save")
).finally(
@@ -17,14 +17,16 @@ export default function StartStopInstanceButton(props) {
let action;
if (props.instance["status"] === "stopped") {
action = control.sendTo(
new libData.InstanceStartRequest(undefined), { instanceId: props.instance["id"] }
{ instanceId: props.instance["id"] },
new libData.InstanceStartRequest(undefined),
).catch(
notifyErrorHandler("Error starting instance")
);
} else if (["starting", "running"].includes(props.instance["status"])) {
action = control.sendTo(
new libData.InstanceStopRequest(), { instanceId: props.instance["id"] }
{ instanceId: props.instance["id"] },
new libData.InstanceStopRequest(),
).catch(
notifyErrorHandler("Error stopping instance")
);
+1 -1
View File
@@ -10,7 +10,7 @@ export function useSaves(instanceId) {
let [saves, setSaves] = useState([]);
function updateSaves() {
control.sendTo(new libData.InstanceListSavesRequest(), { instanceId }).then(updatedSaves => {
control.sendTo({ instanceId }, new libData.InstanceListSavesRequest()).then(updatedSaves => {
setSaves(updatedSaves);
}).catch(err => {
logger.error(`Failed to list instance saves: ${err}`);