mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-20 07:43:12 +09:00
Remove channels to simplify subscription
Make subscriptions either on or off to greatly simplify the logic of subscribing to events.
This commit is contained in:
1 parent
a2eb98ac93
commit
58914042cc
11 files changed
+51
-279
No files matched your search
@@ -36,12 +36,15 @@ export function useHost(id: number): [ HostState, () => void ] {
|
||||
updateHost();
|
||||
|
||||
function updateHandler(newHost: lib.HostDetails) {
|
||||
if (newHost.id !== id) {
|
||||
return;
|
||||
}
|
||||
setHost({ ...newHost, loading: false, missing: false, present: true });
|
||||
}
|
||||
|
||||
control.hostUpdate.subscribeToChannel(id, updateHandler);
|
||||
control.hostUpdate.subscribe(updateHandler);
|
||||
return () => {
|
||||
control.hostUpdate.unsubscribeFromChannel(id, updateHandler);
|
||||
control.hostUpdate.unsubscribe(updateHandler);
|
||||
};
|
||||
}, [id]);
|
||||
|
||||
|
||||
@@ -32,12 +32,15 @@ export function useInstance(id: number): [InstanceState, ()=>void] {
|
||||
updateInstance();
|
||||
|
||||
function updateHandler(newInstance: lib.InstanceDetails) {
|
||||
if (newInstance.id !== id) {
|
||||
return;
|
||||
}
|
||||
setInstance({ ...newInstance, present: true });
|
||||
}
|
||||
|
||||
control.instanceUpdate.subscribeToChannel(id, updateHandler);
|
||||
control.instanceUpdate.subscribe(updateHandler);
|
||||
return () => {
|
||||
control.instanceUpdate.unsubscribeFromChannel(id, updateHandler);
|
||||
control.instanceUpdate.unsubscribe(updateHandler);
|
||||
};
|
||||
}, [id]);
|
||||
|
||||
|
||||
@@ -33,9 +33,15 @@ export function useModPack(id: number) {
|
||||
}
|
||||
updateModPack();
|
||||
|
||||
control.modPackUpdate.subscribeToChannel(id, setModPack);
|
||||
function updateHandler(newModPack: lib.ModPack) {
|
||||
if (newModPack.id !== id) {
|
||||
return;
|
||||
}
|
||||
setModPack(newModPack);
|
||||
}
|
||||
control.modPackUpdate.subscribe(updateHandler);
|
||||
return () => {
|
||||
control.modPackUpdate.unsubscribeFromChannel(id, setModPack);
|
||||
control.modPackUpdate.unsubscribe(updateHandler);
|
||||
};
|
||||
}, [id]);
|
||||
|
||||
|
||||
@@ -26,12 +26,15 @@ export function useSaves(instanceId?: number): lib.SaveDetails[] {
|
||||
updateSaves();
|
||||
|
||||
function updateHandler(data: lib.InstanceSaveListUpdateEvent) {
|
||||
if (data.instanceId !== instanceId) {
|
||||
return;
|
||||
}
|
||||
setSaves(data.saves);
|
||||
}
|
||||
|
||||
control.saveListUpdate.subscribeToChannel(instanceId!, updateHandler);
|
||||
control.saveListUpdate.subscribe(updateHandler);
|
||||
return () => {
|
||||
control.saveListUpdate.unsubscribeFromChannel(instanceId!, updateHandler);
|
||||
control.saveListUpdate.unsubscribe(updateHandler);
|
||||
};
|
||||
}, [instanceId]);
|
||||
|
||||
|
||||
@@ -79,12 +79,15 @@ export function useUser(name: string): [RawUserState, () => void] {
|
||||
updateUser();
|
||||
|
||||
function updateHandler(newUser: lib.User) {
|
||||
if (newUser.name !== name) {
|
||||
return;
|
||||
}
|
||||
setUser({ ...newUser, present: true });
|
||||
}
|
||||
|
||||
control.userUpdate.subscribeToChannel(name, updateHandler);
|
||||
control.userUpdate.subscribe(updateHandler);
|
||||
return () => {
|
||||
control.userUpdate.unsubscribeFromChannel(name, updateHandler);
|
||||
control.userUpdate.unsubscribe(updateHandler);
|
||||
};
|
||||
}, [name]);
|
||||
|
||||
|
||||
Reference in new issue
Block a user