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
…itchParticleEffect` hook

Closes #320
  • Loading branch information
ghost91- committed Jul 24, 2022
1 parent 4f480f2 commit 36c42df
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ module.exports = {
SpriteMesh: "readonly",
},

rules: {
"no-unused-vars": ["error", { vars: "all", args: "after-used", varsIgnorePattern: "^_" }],
},

overrides: [
{
files: ["./*.js", "./tools/**/*"],
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 { format } from "./logger.js";
import { resetFlag } from "./utils.js";
import { omit, resetFlag } from "./utils.js";

export const registerHooks = function () {
Hooks.on(`${packageId}.switchParticleEffect`, onSwitchParticleEffects);
Expand All @@ -16,17 +16,13 @@ async function onSwitchParticleEffects(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 @@ -28,3 +28,14 @@ export function resetFlag(document, key, value) {
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 36c42df

Please sign in to comment.