ruipin / fvtt-lib-wrapper

Library for Foundry VTT which provides module developers with a simple way to modify core Foundry VTT code, while reducing the likelihood of conflict with other modules.
GNU Lesser General Public License v3.0
35 stars 15 forks source link

Support wrapping constructors #14

Open ruipin opened 3 years ago

ruipin commented 3 years ago

Right now, libWrapper does not support wrapping constructors.

Some investigation should be done as to whether this is actually feasible, and if so we should implement this.

Something like patchwork.js might serve as inspiration.

ruipin commented 3 years ago

While asking around for an example use-case, I got the following reply from Discord user BadIdeasBureau:

I think there are some instances where you might have to do this to change what's in a right click menu (Like, IIRC Compendium Scene Viewer doesn't patch the constructor directly, but patches a function that's only called from the constructor, since that's the only place the context menu object is even slightly exposed)

dmrickey commented 7 months ago

Another example. There's a system method that's a few hundred lines long. At the end it constructs an object. I'd like to modify the arguments going into the constructor. I've got two options

function theWrapper(wrapper, ...args) {
  // add extra data, in a real world scenario this would be based on some other criteria in the args, but this example is short
  args[0].newData = { extra: 'data' };
  args[1] += 3;
  // etc.
  return wrapper(...args);
};

libwrapper.wrap('foo.bar.prototype.constructor', theWrapper);

But since libwrapper doesn't support constructors my only option right now is the first option