The shim.js file can be used to avoid a hard dependency on libWrapper.
Unlike the rest of the libWrapper repository, this compatibility shim is licensed under MIT.
This section contains a list of the most significant differences between the shim and the full library.
⚠ Due to these differences in behaviour, it is extremely important to test your code both with the shim and with the full library.
-
This shim does not support any user-facing functionality. Think prioritization of modules, conflict detection, list of wrappers, etc. Basically, anything you can access through the libWrapper settings menu.
-
This shim does not support
libWrapper.unregister
/libWrapper.unregister_all
calls. -
This shim does not support unique numeric
target
identifiers. In particular,libWrapper.register
when using the shim does not return any useful value. -
This shim does not support dynamic dispatch. The next method in the wrapper chain is calculated at the time of each
register
call, and never changed/reordered later. This has many implications:-
The wrapper type metadata (
WRAPPER
,MIXED
,OVERRIDE
,LISTENER
) is completely ignored. Unlike the full library, nothing guaranteesMIXED
wrappers come after allWRAPPER
wrappers, nor thatOVERRIDE
wrappers come after allMIXED
wrappers. The wrapper call order will match the order in which they are registered. For instance, if a module registers anOVERRIDE
, previously registered wrappers (OVERRIDE
or not) will never be called. -
Inheritance chains are static and calculated at
register
time. For instance, if there isclass B extends A
and a module overridesB.prototype.foo
before another overridesA.prototype.foo
, callingB.prototype.foo
will skip theA.prototype.foo
wrapper. -
There is no distinction between a libWrapper (Shim) wrapper and a non-libWrapper wrapper. While normally non-libWrapper wrappers will always come after all libWrapper wrappers, when using the shim this is not the case.
-
-
None of the libWrapper safeties are guaranteed when using the shim:
-
Nothing checks that modules alway call
wrapped(...args)
when usingWRAPPER
. -
Nothing checks that there can only be one
OVERRIDE
wrapper on a given method.
-
-
The various libWrapper Hooks are not triggered when using the shim.
Using the shim does not give you any advantage (or disadvantage) in terms of compatibility with other modules when compared to not using libWrapper at all. The intent of the shim is to remove the main disadvantage from using libWrapper, i.e. of tying yourself to a dependency. With this shim, you can take advantage of the benefits of libWrapper when it is installed, while still behaving correctly when it isn't.
The shim exports a libWrapper
symbol which will at the init
hook become a reference to the real libWrapper library if present, or to a fallback/polyfill implementation otherwise.
⚠ Note that this symbol will be undefined
until the init
hook fires.
A fallback implementation is included for the register
function only (see full library documentation). This fallback implementation does not have any of the "fancy" features of the libWrapper library. See above for more detail.
To programmatically detect whether the fallback implementation is active, you can check libWrapper.is_fallback == true
.
To be able to use this shim, your module needs to use esmodules
in its manifest file. Then, you can import the shim by adding e.g. import {libWrapper} from './relative/path/to/shim.js';
to your JS code. While the shim is mostly plug-and-play, please feel free to modify it to your liking - in particular, some places you might wish to customize are explicitly marked with //************** USER CUSTOMIZABLE:
.
Since v1.7.1
, the shim.js file exports the VERSIONS
symbol with format [MAJOR, MINOR, PATCH]
following Semantic Versioning.
This symbol will be modified any time the shim is modified, and can be used to decide whether you need to update the shim or not.
While the PATCH
version has no direct relation to the main libWrapper library's version and is unique to the Shim, the MAJOR
and MINOR
versions will always match between the two. Them being equal signifies that the shim implements the same API.
By default, the shim displays a warning dialog similar to the image below when libWrapper is not installed and therefore the fallback code path is being used.
This is meant to be a "sane default", but you should feel free to customize this dialog by modifying the shim code or even just strip it out completely if you do not wish to have a warning dialog at all.
Note: Images may be out-of-date.