Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy load description alterations #18010

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/module/item/base/data/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ interface ItemDescriptionData extends ItemDescriptionSource {
contents: AlteredDescriptionContent[];
}[];
override: AlteredDescriptionContent[] | null;
/** True if lazy loaded description altering properties have been applied */
initialized?: boolean;
}

interface AlteredDescriptionContent {
Expand Down
14 changes: 11 additions & 3 deletions src/module/item/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,17 @@ class ItemChatData {

async #prepareDescription(): Promise<Pick<ItemDescriptionData, "value" | "gm">> {
const { data, item } = this;
const rollOptions = new Set(
[item.actor?.getRollOptions(), item.getRollOptions("item")].flat().filter(R.isTruthy),
);
const actor = item.actor;

// Lazy load description alterations now that we need them
if (!data.description.initialized && actor) {
for (const alteration of actor.synthetics.itemAlterations.filter((i) => i.property === "description")) {
alteration.applyAlteration({ singleItem: item as ItemPF2e<ActorPF2e> });
}
data.description.initialized = true;
}

const rollOptions = new Set([actor?.getRollOptions(), item.getRollOptions("item")].flat().filter(R.isTruthy));

const baseText = await (async (): Promise<string> => {
const override = data.description?.override;
Expand Down
13 changes: 10 additions & 3 deletions src/module/rules/rule-element/item-alteration/rule-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ class ItemAlterationRuleElement extends RuleElementPF2e<ItemAlterationRuleSchema
}

/** Alteration properties that should be processed at the end of data preparation */
static #DELAYED_PROPERTIES = ["description", "pd-recovery-dc"];
static #DELAYED_PROPERTIES = ["pd-recovery-dc"];

/** Alteration properties that should only be processed when requested directly */
static #LAZY_PROPERTIES = ["description"];

override async preCreate({ tempItems }: RuleElementPF2e.PreCreateParams): Promise<void> {
if (this.ignored) return;
Expand Down Expand Up @@ -86,13 +89,17 @@ class ItemAlterationRuleElement extends RuleElementPF2e<ItemAlterationRuleSchema

override onApplyActiveEffects(): void {
this.actor.synthetics.itemAlterations.push(this);
if (!this.constructor.#DELAYED_PROPERTIES.includes(this.property)) {
const isLazy = this.constructor.#LAZY_PROPERTIES.includes(this.property);
const isDelayed = this.constructor.#DELAYED_PROPERTIES.includes(this.property);
if (!isLazy && !isDelayed) {
this.applyAlteration();
}
}

override afterPrepareData(): void {
if (this.constructor.#DELAYED_PROPERTIES.includes(this.property)) {
const isLazy = this.constructor.#LAZY_PROPERTIES.includes(this.property);
const isDelayed = this.constructor.#DELAYED_PROPERTIES.includes(this.property);
if (!isLazy && isDelayed) {
this.applyAlteration();
}
}
Expand Down
Loading