📜 ⬆️ ⬇️

Advanced work with subscriptions in event-driven architecture in javascript

One of the most common methods for building JavaScript applications is an event-oriented approach, which provides greater flexibility and allows you to make the modules of the system as independent as possible from each other. This paradigm is gaining increasing popularity and not rarely becomes part of many frameworks. But, as a rule, as the application grows, the number of events / subscriptions increases, and it becomes more and more difficult for the developer himself to manage and monitor it, not to mention the adaptation of new employees to the project.

To cope with all these problems, Capo is designed to help - a module that manages events in event-driven js architecture and solves one of the biggest drawbacks of the Mediator pattern - the uncertainty of triggers and subscribers.

Faced with a similar problem on a real project, we initially used the standard development tool - search. But over time, the number of events increased, it became increasingly difficult to find the “sources” of events, and it became simply impossible to manage “singles” (events that no one listens to or sends). The development process became more difficult with each passing day, and with the arrival of new people on the project, it became completely burdensome.

As you have noticed, we did not go far from the “guitar” theme (hello mediator.js) and called our brainchild Capo (see capo). Capo is not a method or approach, but rather a ready-made helper, which is embodied as a npm module and plug-in for Sublime Text. What does he do? Let's return to the problem of the Mediator pattern.
')
Imagine a situation - you come to a new project, which uses an architecture built on events. How long will it take you to fully track all the chains of events and subscriptions to them? As practice shows, the exercise it takes a lot of time and does not bring a lot of pleasure.

To simplify this process, we decided to unify the search for all events and provide the result to the developer in the form of an interactive report. The Npm Capo module searches all the files in the specified folder, finds all the connections of the trigger subscriber type and tracks them. After that forms (by default) html report of the following form:


Live demo

On the left, there is a menu for navigating through the events found, on the right side, triggers and subscribers with an indication of the file, line and context. In the section Strange events - events that do not have subscribers or triggers and can cause great trouble. The module is suitable for all projects that use mediator.js, Backbone.js, node.js EventEmmiter, or any other object that provides the publish-subscribe interface. Capo also has a number of additional features that can be very useful. So, for example, there is an opportunity to send a report to print or output directly to the console (capo-cli), Capo itself can be added as a build step to grunt (grunt-capo), as well as to “spy” on events by generating subscriptions-stubs for all events found in the project (spy-generator).

For even more convenience, it was necessary to integrate these capabilities into the development environment. So Sublime Text 3 has been added with another plugin, which is designed to make life easier by directly developing applications using event-driven js architecture.
After installation, just use the key combination to start the search (by default Alt + D) and, using the quick panel, select the desired file. The plugin will open it in a new window and highlight the line with the found event.



Creating Capo, we first of all sought to facilitate the development process by ourselves, as well as to help new guys on the project to quickly understand the architecture of the application. We hope that Capo will really help you keep events under control and solve the perennial problem of uncertainty of triggers and subscriptions in your projects.

The source code and description of the Capo modules can be found at the links:

Source: https://habr.com/ru/post/213937/


All Articles