Handle equality when scraping scene performers (#4094)

This commit is contained in:
WithoutPants
2023-09-11 10:50:24 +10:00
committed by GitHub
parent 50c4ac98af
commit cf3301c8bc
4 changed files with 63 additions and 15 deletions

View File

@@ -1,19 +1,30 @@
import isEqual from "lodash-es/isEqual";
import lodashIsEqual from "lodash-es/isEqual";
import clone from "lodash-es/clone";
import { IHasStoredID } from "src/utils/data";
export class ScrapeResult<T> {
public newValue?: T;
public originalValue?: T;
public scraped: boolean = false;
public useNewValue: boolean = false;
private isEqual: (
v1: T | undefined | null,
v2: T | undefined | null
) => boolean;
public constructor(
originalValue?: T | null,
newValue?: T | null,
useNewValue?: boolean
useNewValue?: boolean,
isEqual: (
v1: T | undefined | null,
v2: T | undefined | null
) => boolean = lodashIsEqual
) {
this.originalValue = originalValue ?? undefined;
this.newValue = newValue ?? undefined;
this.isEqual = isEqual;
// NOTE: this means that zero values are treated as null
// this is incorrect for numbers and booleans, but correct for strings
const hasNewValue = !!this.newValue;
@@ -32,7 +43,7 @@ export class ScrapeResult<T> {
const ret = clone(this);
ret.newValue = value;
ret.useNewValue = !isEqual(ret.newValue, ret.originalValue);
ret.useNewValue = !this.isEqual(ret.newValue, ret.originalValue);
// #2691 - if we're setting the value, assume it should be treated as
// scraped
@@ -53,9 +64,13 @@ export class ZeroableScrapeResult<T> extends ScrapeResult<T> {
public constructor(
originalValue?: T | null,
newValue?: T | null,
useNewValue?: boolean
useNewValue?: boolean,
isEqual: (
v1: T | undefined | null,
v2: T | undefined | null
) => boolean = lodashIsEqual
) {
super(originalValue, newValue, useNewValue);
super(originalValue, newValue, useNewValue, isEqual);
const hasNewValue = this.newValue !== undefined;
@@ -65,6 +80,32 @@ export class ZeroableScrapeResult<T> extends ScrapeResult<T> {
}
}
function storedIDsEqual<T extends IHasStoredID>(
o1: T[] | undefined | null,
o2: T[] | undefined | null
) {
return (
!!o1 &&
!!o2 &&
o1.length === o2.length &&
o1.every((o) => {
return o2.find((oo) => o.stored_id === oo.stored_id);
})
);
}
export class ObjectListScrapeResult<
T extends IHasStoredID
> extends ScrapeResult<T[]> {
public constructor(
originalValue?: T[] | null,
newValue?: T[] | null,
useNewValue?: boolean
) {
super(originalValue, newValue, useNewValue, storedIDsEqual);
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function hasScrapedValues(values: ScrapeResult<any>[]) {
return values.some((r) => r.scraped);