Skip to content

Commit

Permalink
fix: properly handle switching multiple effects with the `fxmaster.sw…
Browse files Browse the repository at this point in the history
…itchWeather` hook

Closes #320
  • Loading branch information
ghost91- committed Aug 14, 2022
1 parent 5631b5c commit de9b9d5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = {
},

rules: {
// Specify any specific ESLint rules.
"no-unused-vars": ["error", { vars: "all", args: "after-used", varsIgnorePattern: "^_" }],
},

overrides: [
Expand Down
16 changes: 6 additions & 10 deletions src/hooks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { packageId } from "./constants.js";
import { logger } from "./logger.js";
import { formatString, resetFlags } from "./utils.js";
import { formatString, omit, resetFlags } from "./utils.js";

export const registerHooks = function () {
// ------------------------------------------------------------------
Expand All @@ -21,17 +21,13 @@ async function onSwitchWeather(parameters) {
if (!canvas.scene) {
return;
}
const newEffect = { [parameters.name]: { type: parameters.type, options: parameters.options } };

let flags = (await canvas.scene.getFlag(packageId, "effects")) ?? {};
let effects = {};
const currentEffects = canvas.scene.getFlag(packageId, "effects") ?? {};
const shouldSwitchOff = parameters.name in currentEffects;
const effects = shouldSwitchOff
? omit(currentEffects, parameters.name)
: { ...currentEffects, [parameters.name]: { type: parameters.type, options: parameters.options } };

if (foundry.utils.hasProperty(flags, parameters.name)) {
effects = flags;
delete effects[parameters.name];
} else {
effects = foundry.utils.mergeObject(flags, newEffect);
}
if (Object.keys(effects).length == 0) {
await canvas.scene.unsetFlag(packageId, "effects");
} else {
Expand Down
11 changes: 11 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,14 @@ export function isV9OrLater() {
export function roundToDecimals(number, decimals) {
return Number(Math.round(number + "e" + decimals) + "e-" + decimals);
}

/**
* Omit a specific key from an object.
* @param {object} object The object from which to omit
* @param {string|number|symbol} key The key to omit
* @returns {object} The object without the given key.
*/
export function omit(object, key) {
const { [key]: _omitted, ...rest } = object;
return rest;
}

0 comments on commit de9b9d5

Please sign in to comment.