-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(renterd): config only patch changed
- Loading branch information
1 parent
5833a82
commit c8b167e
Showing
3 changed files
with
136 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'renterd': minor | ||
--- | ||
|
||
The configuration now makes less network requests and only patch updates individual settings that have changed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { | ||
AutopilotConfig, | ||
SettingsGouging, | ||
SettingsPinned, | ||
SettingsUpload, | ||
} from '@siafoundation/renterd-types' | ||
import { isObject, isEqual, union, keys } from '@technically/lodash' | ||
import { ResourcesRequiredLoaded } from './useResources' | ||
|
||
/** | ||
* @param param0 An object containing the resources and payloads. | ||
* @returns An object with each patch payload or undefined if no changes. | ||
*/ | ||
export function getPatchPayloads({ | ||
resources, | ||
payloads, | ||
}: { | ||
resources: ResourcesRequiredLoaded | ||
payloads: { | ||
autopilot?: AutopilotConfig | ||
gouging: SettingsGouging | ||
pinned: SettingsPinned | ||
upload: SettingsUpload | ||
} | ||
}) { | ||
return { | ||
autopilot: getPatch(resources.autopilot.data, payloads.autopilot), | ||
gouging: getPatch(resources.gouging.data, payloads.gouging), | ||
pinned: getPatch(resources.pinned.data, payloads.pinned), | ||
upload: getPatch(resources.upload.data, payloads.upload), | ||
} | ||
} | ||
|
||
/** | ||
* @param existing The existing object. | ||
* @param updated The updated object. | ||
* @returns A partial object of T with the differences between existing and updated. | ||
*/ | ||
function getPatch<T>(existing: T, updated: T): DeepPartial<T> | undefined { | ||
if (isEqual(existing, updated)) { | ||
return undefined | ||
} | ||
|
||
if (!isObject(existing) || !isObject(updated)) { | ||
return updated as DeepPartial<T> | ||
} | ||
|
||
const keysList = union(keys(existing), keys(updated)) as Array<keyof T> | ||
|
||
const result = {} as DeepPartial<T> | ||
let isChanged = false | ||
|
||
keysList.forEach((key) => { | ||
const origValue = existing[key] | ||
const stagedValue = updated[key] | ||
|
||
const valueChange = getPatch(origValue, stagedValue) | ||
|
||
if (valueChange !== undefined) { | ||
// eslint-disable-next-line @typescript-eslint/no-extra-semi, @typescript-eslint/no-explicit-any | ||
;(result as any)[key] = valueChange | ||
isChanged = true | ||
} | ||
}) | ||
|
||
return isChanged ? result : undefined | ||
} | ||
|
||
type DeepPartial<T> = T extends Array<infer U> | ||
? Array<DeepPartial<U>> | ||
: T extends object | ||
? { [K in keyof T]?: DeepPartial<T[K]> } | ||
: T |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters