tapable
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
Tapable is a.package in JavaScript that exposes a number of Hook classes specifically designed for creating plugin hooks. This enables developers to register functions (termed as plugins) at specific points in an application's pipeline, effectively enabling custom logic to be executed at these crucial points. These hooks can be created synchronously or asynchronously and in different modes such as Waterfall, Bail, or Loop.
To use the Tapable package, you first need to install it using npm.
npm install --save tapable
Then, import the required Hooks from the Tapable package.
const {
SyncHook,
SyncBailHook,
SyncWaterfallHook,
SyncLoopHook,
AsyncParallelHook,
AsyncParallelBailHook,
AsyncSeriesHook,
AsyncSeriesBailHook,
AsyncSeriesWaterfallHook
} = require("tapable");
You can then create a new instance of a Hook. Here, we create an instance of SyncHook:
const hook = new SyncHook(["arg1", "arg2", "arg3"]);
In practice, it is common to create all hooks in an object for an application, as follows:
class Car {
constructor() {
this.hooks = {
accelerate: new SyncHook(["newSpeed"]),
brake: new SyncHook(),
calculateRoutes: new AsyncParallelHook(["source", "target", "routesList"])
};
}
/* ... */
}
Once your hooks are set up, you can 'tap' into them at any point in your application to run custom logic:
const myCar = new Car();
// Use the tap method to add a consumer
myCar.hooks.brake.tap("WarningLampPlugin", () => warningLamp.on());
The Tapable documentation is contained within the readme file of its GitHub repository. It offers a comprehensive overview of the Hook classes, how to use them, different types of hooks (sync, async, promise) and how to intercept calls amongst others. The documentation also includes example code snippets to illustrate usage. For implementing complex tasks, other than basic documentation, developers should refer to the source code and tests present in the repository.