Skip to content

Commit

Permalink
Account for Document#update returning undefined (foundryvtt#11182)
Browse files Browse the repository at this point in the history
  • Loading branch information
stwlam committed Nov 10, 2023
1 parent 9810e68 commit 165b829
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/module/actor/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1294,7 +1294,7 @@ class ActorPF2e<TParent extends TokenDocumentPF2e | null = TokenDocumentPF2e | n
(this.isOfType("character") && setting === "both" && !!instantDeath);

if (
updated.isDead &&
updated?.isDead &&
deadAtZero &&
((hpDamage >= 0 && !token.combatant?.isDefeated) || (hpDamage < 0 && !!token.combatant?.isDefeated))
) {
Expand Down Expand Up @@ -1898,7 +1898,7 @@ interface ActorPF2e<TParent extends TokenDocumentPF2e | null = TokenDocumentPF2e

get sheet(): ActorSheetPF2e<ActorPF2e>;

update(data: Record<string, unknown>, options?: ActorUpdateContext<TParent>): Promise<this>;
update(data: Record<string, unknown>, options?: ActorUpdateContext<TParent>): Promise<this | undefined>;

getActiveTokens(linked: boolean | undefined, document: true): TokenDocumentPF2e<ScenePF2e>[];
getActiveTokens(linked?: boolean | undefined, document?: false): TokenPF2e<TokenDocumentPF2e<ScenePF2e>>[];
Expand Down
3 changes: 2 additions & 1 deletion src/module/actor/creature/sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,8 @@ export abstract class CreatureSheetPF2e<TActor extends CreaturePF2e> extends Act
}

const dropId = htmlClosest(event.target, ".item-container")?.dataset.containerId;
return dropId ? [await item.update({ "system.location.value": dropId })] : [];
const updated = dropId ? await item.update({ "system.location.value": dropId }) : null;
return updated ? [updated] : [];
}
} else if (item.isOfType("spellcastingEntry") && dropContainerType === "spellcastingEntry") {
// target and source are spellcastingEntries and need to be sorted
Expand Down
2 changes: 1 addition & 1 deletion src/module/item/base/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ class ItemPF2e<TParent extends ActorPF2e | null = ActorPF2e | null> extends Item
}
} else {
// The above method of updating embedded items in an actor update does not work with synthetic actors
const promises: Promise<ItemPF2e<ActorPF2e>>[] = [];
const promises: Promise<ItemPF2e<ActorPF2e> | undefined>[] = [];
for (const item of this.actor.itemTypes.melee) {
const attackEffects = item.system.attackEffects.value;
if (attackEffects.includes(slug)) {
Expand Down
5 changes: 3 additions & 2 deletions src/module/item/kit/sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,16 @@ class KitSheetPF2e extends ItemSheetPF2e<KitPF2e> {
await this.item.update({ [`${pathPrefix}.${id}`]: entry });
}

async removeItem(event: MouseEvent): Promise<KitPF2e> {
async removeItem(event: MouseEvent): Promise<KitPF2e | null> {
const target = htmlClosest(event.currentTarget ?? null, "li");
const index = target?.dataset.index;
if (!index) return this.item;

const containerId = target.closest<HTMLElement>("[data-container-id]")?.dataset.containerId;
const path = containerId ? `${containerId}.items.-=${index}` : `-=${target.dataset.index}`;
const update = await this.item.update({ [`system.items.${path}`]: null });

return this.item.update({ [`system.items.${path}`]: null });
return update ?? null;
}

override activateListeners($html: JQuery): void {
Expand Down
7 changes: 5 additions & 2 deletions src/module/item/spell/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -959,14 +959,17 @@ class SpellPF2e<TParent extends ActorPF2e | null = ActorPF2e | null> extends Ite
return flag;
}

override async update(data: Record<string, unknown>, options: DocumentUpdateContext<TParent> = {}): Promise<this> {
override async update(
data: Record<string, unknown>,
options: DocumentUpdateContext<TParent> = {},
): Promise<this | undefined> {
// Redirect the update of override spell variants to the appropriate update method if the spell sheet is currently rendered
if (this.original && this.appliedOverlays!.has("override") && this.sheet.rendered) {
return this.original.overlays.updateOverride(
this as SpellPF2e<ActorPF2e>,
data,
options as DocumentUpdateContext<ActorPF2e>,
) as Promise<this>;
) as Promise<this | undefined>;
}
return super.update(data, options);
}
Expand Down
6 changes: 3 additions & 3 deletions src/module/item/spellcasting-entry/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class SpellCollection<TActor extends ActorPF2e, TEntry extends BaseSpellcastingE
}

/** Saves the prepared spell slot data to the spellcasting entry */
async prepareSpell(spell: SpellPF2e, slotRank: number, spellSlot: number): Promise<TEntry> {
async prepareSpell(spell: SpellPF2e, slotRank: number, spellSlot: number): Promise<TEntry | undefined> {
this.#assertEntryIsDocument(this.entry);

if (spell.baseRank > slotRank && !(slotRank === 0 && spell.isCantrip)) {
Expand Down Expand Up @@ -147,7 +147,7 @@ class SpellCollection<TActor extends ActorPF2e, TEntry extends BaseSpellcastingE
}

/** Removes the spell slot and updates the spellcasting entry */
unprepareSpell(slotRank: number, spellSlot: number): Promise<TEntry> {
unprepareSpell(slotRank: number, spellSlot: number): Promise<TEntry | undefined> {
this.#assertEntryIsDocument(this.entry);

if (CONFIG.debug.hooks === true) {
Expand All @@ -168,7 +168,7 @@ class SpellCollection<TActor extends ActorPF2e, TEntry extends BaseSpellcastingE
}

/** Sets the expended state of a spell slot and updates the spellcasting entry */
setSlotExpendedState(slotRank: number, spellSlot: number, isExpended: boolean): Promise<TEntry> {
setSlotExpendedState(slotRank: number, spellSlot: number, isExpended: boolean): Promise<TEntry | undefined> {
this.#assertEntryIsDocument(this.entry);

const key = `system.slots.slot${slotRank}.prepared.${spellSlot}.expended`;
Expand Down
6 changes: 3 additions & 3 deletions src/module/item/spellcasting-entry/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,17 +350,17 @@ class SpellcastingEntryPF2e<TParent extends ActorPF2e | null = ActorPF2e | null>
}

/** Saves the prepared spell slot data to the spellcasting entry */
async prepareSpell(spell: SpellPF2e, slotRank: number, spellSlot: number): Promise<this | null> {
async prepareSpell(spell: SpellPF2e, slotRank: number, spellSlot: number): Promise<Maybe<this>> {
return this.spells?.prepareSpell(spell, slotRank, spellSlot) ?? null;
}

/** Removes the spell slot and updates the spellcasting entry */
async unprepareSpell(spellLevel: number, slotRank: number): Promise<this | null> {
async unprepareSpell(spellLevel: number, slotRank: number): Promise<Maybe<this>> {
return this.spells?.unprepareSpell(spellLevel, slotRank) ?? null;
}

/** Sets the expended state of a spell slot and updates the spellcasting entry */
async setSlotExpendedState(slotRank: number, spellSlot: number, isExpended: boolean): Promise<this | null> {
async setSlotExpendedState(slotRank: number, spellSlot: number, isExpended: boolean): Promise<Maybe<this>> {
return this.spells?.setSlotExpendedState(slotRank, spellSlot, isExpended) ?? null;
}

Expand Down
2 changes: 1 addition & 1 deletion types/foundry/common/abstract/document.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ export default abstract class Document<
* const data = [{_id: "12ekjf43kj2312ds", name: "New Name 1"}, {_id: "kj549dk48k34jk34", name: "New Name 2"}]};
* const updated = await Document.update(data); // Returns an Array of Entities, updated in the database
*/
update(data: Record<string, unknown>, options?: DocumentModificationContext<TParent>): Promise<this>;
update(data: Record<string, unknown>, options?: DocumentModificationContext<TParent>): Promise<this | undefined>;

/**
* Delete the current Document.
Expand Down

0 comments on commit 165b829

Please sign in to comment.