-
Notifications
You must be signed in to change notification settings - Fork 10
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
e21682a
commit 571da0c
Showing
4 changed files
with
142 additions
and
57 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,76 @@ | ||
import { | ||
AutopilotConfig, | ||
SettingsGouging, | ||
SettingsPinned, | ||
SettingsUpload, | ||
} from '@siafoundation/renterd-types' | ||
import { isObject, isEqual, union, keys } from '@technically/lodash' | ||
import { ResourcesRequiredLoaded } from './useResources' | ||
|
||
/** | ||
* @param param0 The resources and staged objects. | ||
* @returns An object with each patch payload or undefined if no changes. | ||
*/ | ||
export function getPatches({ | ||
resources, | ||
staged, | ||
}: { | ||
resources: ResourcesRequiredLoaded | ||
staged: { | ||
autopilot: AutopilotConfig | ||
gouging: SettingsGouging | ||
pinned: SettingsPinned | ||
upload: SettingsUpload | ||
} | ||
}) { | ||
return { | ||
autopilot: getPatch(resources.autopilot.data, staged.autopilot), | ||
gouging: getPatch(resources.gouging.data, staged.gouging), | ||
pinned: getPatch(resources.pinned.data, staged.pinned), | ||
upload: getPatch(resources.upload.data, staged.upload), | ||
} | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
type DeepPartial<T> = T extends Function | ||
? T | ||
: T extends Array<infer U> | ||
? Array<DeepPartial<U>> | ||
: T extends object | ||
? { [K in keyof T]?: DeepPartial<T[K]> } | ||
: T | ||
|
||
/** | ||
* @param existing The existing object. | ||
* @param staged The staged object. | ||
* @returns A partial object of T with the differences between existing and staged. | ||
*/ | ||
function getPatch<T>(existing: T, staged: T): DeepPartial<T> | undefined { | ||
if (isEqual(existing, staged)) { | ||
return undefined | ||
} | ||
|
||
if (!isObject(existing) || !isObject(staged)) { | ||
return staged as DeepPartial<T> | ||
} | ||
|
||
const keysList = union(keys(existing), keys(staged)) as Array<keyof T> | ||
|
||
const result = {} as DeepPartial<T> | ||
let isChanged = false | ||
|
||
keysList.forEach((key) => { | ||
const origValue = existing[key] | ||
const stagedValue = staged[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 | ||
} |
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
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
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