Skip to content

Commit 9e6fc44

Browse files
committed
v1.3.6.0: Allow OVERRIDE to chain
Fixes #28
1 parent 80fea11 commit 9e6fc44

File tree

6 files changed

+33
-10
lines changed

6 files changed

+33
-10
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.3.6.0 (2021-04-09)
2+
3+
* Allow OVERRIDE wrappers to continue chain if they pass `{chain: true}` as a fourth parameter to `libWrapper.register`.
4+
15
# 1.3.5.0 (2021-01-11)
26

37
* Bugfix: Refactor usage of the handler objects (used to bootstrap a libWrapper call) so that dispatch is dynamic. Prevents references to wrapped methods from skipping the wrappers.

module.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "lib-wrapper",
33
"title": "libWrapper",
44
"description": "Library for wrapping core Foundry VTT methods, meant to improve compatibility between modules that wrap the same methods.",
5-
"version": "1.3.5.0",
5+
"version": "1.3.6.0",
66
"author": "Rui Pinheiro",
77
"esmodules": ["src/index.js"],
88
"styles": ["dist/lib-wrapper.css"],

src/lib/lib-wrapper.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ export class libWrapper {
252252
*
253253
*
254254
*/
255-
static register(module, target, fn, type='MIXED') {
255+
static register(module, target, fn, type='MIXED', {chain=undefined}={}) {
256256
// Validate module
257257
this._validate_module(module);
258258

@@ -271,6 +271,10 @@ export class libWrapper {
271271
if(typeof type === 'undefined' || !(type in TYPES_REVERSE))
272272
throw new LibWrapperModuleError(`Parameter 'type' must be one of [${TYPES_LIST.join(', ')}].`, module);
273273

274+
chain = chain ?? (type < TYPES.OVERRIDE);
275+
if(typeof chain !== 'boolean')
276+
throw new LibWrapperModuleError(`Parameter 'chain' must be a boolean.`, module);
277+
274278
// Split '#set' from the target
275279
const target_and_setter = this._split_target_and_setter(target);
276280
const target_without_set = target_and_setter[0];
@@ -291,7 +295,7 @@ export class libWrapper {
291295
const priority = this._get_default_priority(module, target);
292296

293297
// Only allow one 'OVERRIDE' type
294-
if(type == TYPES.OVERRIDE) {
298+
if(type >= TYPES.OVERRIDE) {
295299
const existing = wrapper.get_fn_data(is_setter).find((x) => { return x.type == TYPES.OVERRIDE });
296300

297301
if(existing) {
@@ -319,7 +323,8 @@ export class libWrapper {
319323
fn : fn,
320324
type : type,
321325
wrapper : wrapper,
322-
priority: priority
326+
priority: priority,
327+
chain : chain
323328
};
324329

325330
wrapper.add(data);

src/lib/wrapper.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,8 @@ export class Wrapper {
392392
// Grab wrapper function from function data object
393393
const fn = data.fn;
394394

395-
// OVERRIDE type does not continue the chain
396-
if(data.type >= TYPES.OVERRIDE) {
395+
// OVERRIDE type will usually not continue the chain
396+
if(!data.chain) {//data.type >= TYPES.OVERRIDE) {
397397
// Call next method in the chain
398398
return fn.apply(obj, args);
399399
}

tests/test_lib.js

+9
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ test_sync_async('Library: Main', async function (t) {
104104
libWrapper.register('m1', 'A.prototype.x', chkr.gen_wr('m1:Mix:7'));
105105
await chkr.call(a, 'x', ['m1:Mix:7','Orig',-2]);
106106

107+
// Register an OVERRIDE with chain=true
108+
game.add_module('m3');
109+
libWrapper.register('m3', 'A.prototype.x', chkr.gen_wr('m3:Ovr:8'), 'OVERRIDE', {chain: true});
110+
await chkr.call(a, 'x', ['m1:Mix:7','m3:Ovr:8','Orig',-3]);
111+
112+
// Unregister OVERRIDE
113+
libWrapper.unregister('m3', 'A.prototype.x');
114+
await chkr.call(a, 'x', ['m1:Mix:7','Orig',-2]);
115+
107116
// Test manual wrapping
108117
A.prototype.x = (function() {
109118
const wrapped = A.prototype.x;

tests/utilities.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ class Game {
7575
this.add_module('lib-wrapper');
7676
}
7777
}
78-
79-
8078
globalThis.game = new Game();
8179
globalThis.game.clear_modules();
8280

@@ -100,15 +98,22 @@ class UiNotifications {
10098
globalThis.ui = { notifications: new UiNotifications() };
10199

102100

101+
// Dialog
102+
class Dialog {
103+
render() {}
104+
}
105+
globalThis.Dialog = Dialog;
106+
103107

104108

105109
// Wrap helpers to bypass libWrapper public API
106-
export const wrap_front = function(obj, fn_name, fn, is_setter=false) {
110+
export const wrap_front = function(obj, fn_name, fn, is_setter=false, chain=true) {
107111
const wrapper = libWrapper._create_wrapper_from_object(obj, fn_name);
108112
wrapper.get_fn_data(is_setter).splice(0, 0, {
109113
fn: fn,
110114
priority: undefined,
111-
active: true
115+
active: true,
116+
chain: chain
112117
});
113118
};
114119

0 commit comments

Comments
 (0)