fvtt-lib-wrapper icon indicating copy to clipboard operation
fvtt-lib-wrapper copied to clipboard

Support wrapping constructors

Open ruipin opened this issue 3 years ago • 2 comments

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 avatar Jan 15 '21 00:01 ruipin

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)

ruipin avatar Feb 09 '21 23:02 ruipin

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

  • patch the existing method, which basically means copy/pasting the whole thing then modifying the part just before the return object's constructor is called.
  • patch the constructor for the object being constructed, look at the arguments going into the constructor, modify them, then call the real constructor with the modified arguments
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

dmrickey avatar Mar 21 '24 22:03 dmrickey