mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 20:34:37 +03:00
Handle equality when scraping scene performers (#4094)
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user