Make the system use Proxy and proper subclassing instead of the weird middle-ground polymorphism (closes #86)
This commit is contained in:
parent
745824f6cc
commit
cd98e66484
17 changed files with 322 additions and 351 deletions
|
|
@ -1,10 +1,9 @@
|
|||
/** @this {ItemHandler} */
|
||||
async function _preCreate(_data, _options, _user) {
|
||||
if (this.isEmbedded) {
|
||||
return await this.actor?.preItemEmbed(this);
|
||||
};
|
||||
};
|
||||
import { DotDungeonItem } from "./GenericItem.mjs";
|
||||
|
||||
export default {
|
||||
_preCreate,
|
||||
export class Aspect extends DotDungeonItem {
|
||||
async _preCreate() {
|
||||
if (this.isEmbedded) {
|
||||
return await this.actor?.preItemEmbed(this);
|
||||
};
|
||||
}
|
||||
};
|
||||
|
|
|
|||
1
module/documents/Item/GenericItem.mjs
Normal file
1
module/documents/Item/GenericItem.mjs
Normal file
|
|
@ -0,0 +1 @@
|
|||
export class DotDungeonItem extends Item {};
|
||||
|
|
@ -1,30 +0,0 @@
|
|||
import AspectItem from "./Aspect.mjs";
|
||||
import SpellItem from "./Spell.mjs";
|
||||
|
||||
/** @extends {Item} */
|
||||
export class ItemHandler extends Item {
|
||||
proxyTargets = {
|
||||
aspect: AspectItem,
|
||||
spell: SpellItem,
|
||||
};
|
||||
|
||||
constructor(data, ctx) {
|
||||
super(data, ctx);
|
||||
};
|
||||
|
||||
/** @type {class|undefined} */
|
||||
get fn() {
|
||||
return this.proxyTargets[this.type];
|
||||
};
|
||||
|
||||
async proxyFunction(funcName, ...args) {
|
||||
if (!this.fn?.[funcName]) return;
|
||||
return await this.fn?.[funcName].bind(this)(...args);
|
||||
};
|
||||
|
||||
async _preCreate(...args) {
|
||||
if (this.fn?._preCreate) return this.fn?._preCreate.bind(this)(...args);
|
||||
if (this.isEmbedded) return await this.actor?.preItemEmbed(this);
|
||||
return;
|
||||
};
|
||||
};
|
||||
|
|
@ -1 +0,0 @@
|
|||
export default {};
|
||||
42
module/documents/Item/_proxy.mjs
Normal file
42
module/documents/Item/_proxy.mjs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import { Aspect } from "./Aspect.mjs";
|
||||
import { DotDungeonItem } from "./GenericItem.mjs";
|
||||
|
||||
const classes = {
|
||||
aspect: Aspect,
|
||||
};
|
||||
|
||||
export const ItemProxy = new Proxy(function () {}, {
|
||||
construct(target, args) {
|
||||
const [data] = args;
|
||||
|
||||
if (!classes.hasOwnProperty(data.type)) {
|
||||
return new DotDungeonItem(...args);
|
||||
}
|
||||
|
||||
return new classes[data.type](...args);
|
||||
},
|
||||
get(target, prop, receiver) {
|
||||
console.log(prop)
|
||||
if (["create", "createDocuments"].includes(prop)) {
|
||||
return function (data, options) {
|
||||
if (data.constructor === Array) {
|
||||
return data.map(i => ItemProxy.create(i, options))
|
||||
}
|
||||
|
||||
if (!classes.hasOwnProperty(data.type)) {
|
||||
return DotDungeonItem.create(data, options);
|
||||
}
|
||||
|
||||
return classes[data.type].create(data, options)
|
||||
};
|
||||
};
|
||||
|
||||
if (prop == Symbol.hasInstance) {
|
||||
return function (instance) {
|
||||
return Object.values(classes).some(i => instance instanceof i)
|
||||
};
|
||||
};
|
||||
|
||||
return DotDungeonItem[prop];
|
||||
},
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue