Add a generic ActiveEffect class + proxy
This commit is contained in:
parent
8aff8b0fda
commit
cfaed0d230
3 changed files with 51 additions and 0 deletions
7
module/documents/ActiveEffect/GenericActiveEffect.mjs
Normal file
7
module/documents/ActiveEffect/GenericActiveEffect.mjs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
export class DotDungeonActiveEffect extends ActiveEffect {
|
||||
|
||||
// Invert the logic of the disabled property so it's easier to modify via
|
||||
// embedded controls
|
||||
get enabled() { return !this.disabled };
|
||||
set enabled(newValue) { this.disabled = !newValue };
|
||||
};
|
||||
42
module/documents/ActiveEffect/_proxy.mjs
Normal file
42
module/documents/ActiveEffect/_proxy.mjs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import { DotDungeonActiveEffect } from "./GenericActiveEffect.mjs";
|
||||
|
||||
const classes = {};
|
||||
|
||||
const defaultClass = DotDungeonActiveEffect;
|
||||
|
||||
export const ActiveEffectProxy = new Proxy(function () {}, {
|
||||
construct(target, args) {
|
||||
const [data] = args;
|
||||
|
||||
if (!classes.hasOwnProperty(data.type)) {
|
||||
return new defaultClass(...args);
|
||||
}
|
||||
|
||||
return new classes[data.type](...args);
|
||||
},
|
||||
get(target, prop, receiver) {
|
||||
|
||||
if (["create", "createDocuments"].includes(prop)) {
|
||||
return function (data, options) {
|
||||
if (data.constructor === Array) {
|
||||
return data.map(i => ActiveEffectProxy.create(i, options))
|
||||
}
|
||||
|
||||
if (!classes.hasOwnProperty(data.type)) {
|
||||
return defaultClass.create(data, options);
|
||||
}
|
||||
|
||||
return classes[data.type].create(data, options);
|
||||
};
|
||||
};
|
||||
|
||||
if (prop == Symbol.hasInstance) {
|
||||
return function (instance) {
|
||||
if (instance instanceof defaultClass) return true;
|
||||
return Object.values(classes).some(i => instance instanceof i);
|
||||
};
|
||||
};
|
||||
|
||||
return defaultClass[prop];
|
||||
},
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue