ruipin/fvtt-lib-wrapper

wrapping property overrides

Closed this issue · 1 comments

I have an override like this

Object.defineProperty(Token.prototype, 'inCombat', {get: inCombat})

function inCombat() {
    const combat = ui.combat.combat;
    if (!combat) return false;
    const combatants = combat.getCombatantByToken(this.id);
    return combatants.some(c => c !== undefined);
}

but I can't find an example of that kind of overrides in the documentation. Is wrapping this kind of overrides possible in the current version?

Yes, libWrapper supports wrapping getters. This is transparent in terms of usage, as getters are essentially methods underneath.

This is stated in the documentation of the libWrapper.register function, but it's not too obvious:

A string containing the path to the function you wish to add the wrapper to, starting at global scope, for example 'SightLayer.prototype.updateToken'.
This works for both normal methods, as well as properties with getters. To wrap a property's setter, append '#set' to the name, for example 'SightLayer.prototype.blurDistance#set'.

For Token.prototype.inCombat, the following works:

libWrapper.register('my-fvtt-package', 'Token.prototype.inCombat', function(wrapped) {
  console.log("Wrapped Token.prototype.inCombat");
  return wrapped();
}, 'WRAPPER');