Post by account_disabled on Jan 23, 2024 4:42:42 GMT
In this article, we will look at the basic principles and mechanics of application development using reactive programming techniques. This programming paradigm is relatively new and focuses on data flow and change propagation. In short, it is a way of writing program code based on reactions to certain events that occur within an application. At the same time, we must clearly separate data into dynamic and static, and our model must automatically propagate changes to dynamic data using threads. Reactive programming was supposed to be an easy way to create user interfaces, animations, and any other processes that change over time. Reactive programming can be implemented in several approaches: imperative programming; object-oriented; functional. However, the most natural basis for reactive programming and working with reactive data structures is the functional approach. The most popular library using this approach is RxJS , which was taken as the basis for a framework such as Angular. RxJS is a Javascript library for transforming, composing and retrieving asynchronous data streams. It can be used both in the browser and on the server side.
So, what is reactive programming and what does it mean to “think reactively”? This is a paradigm in which writing program code involves working with an asynchronous data flow and describing the logic of reacting to their changes, in contrast to the imperative approach, where we explicitly handle their changes. In a reactive approach, any enumerated or composite data type, s B2B Email List uch as a list, is treated as a stream and can be represented as follows: image1 Any stream on a timeline has a beginning, a set of sequentially emitted values, and an end, indicated by a vertical line. Thus, the stream generates events for us, ordered in time. These events can be anything: a click on a button, an http request to the server, data coming through a socket connection, animation, etc. And we have the opportunity to listen to such a stream and react accordingly to these events. Using regular functions, we can combine, modify and filter such streams. A thread can emit three types of events: meaning; error; completion.
And our task is to intercept them and describe the reaction to these three types of asynchronous (or synchronous) events. In doing so, we will work with several software entities, the first of which is observability . Observable is a common function with several characteristics specific to it. It takes an observer object as a parameter . This observer describes 3 methods: Next – getting the next value from the stream; Error – error; Complete – thread completion event. Observability provides a mechanism for passing messages between the one who generates the data and the one who uses it. In this case, there are 2 types of behavior strategies between the producer and consumer of data. With the first ( pull strategy ), the place and time of reaction to a change or receipt of data is determined by the consumer , with the second ( push strategy ) - with the manufacturer . An example of a pull strategy is a regular function call from that place in the program where the data that it returns is required, while the function itself knows nothing about who and where it will be “pulled” from. With a push strategy, on the contrary, the producer function is, as it were, “tied” with processors (consumers) who know nothing about where and when this data will be generated.
So, what is reactive programming and what does it mean to “think reactively”? This is a paradigm in which writing program code involves working with an asynchronous data flow and describing the logic of reacting to their changes, in contrast to the imperative approach, where we explicitly handle their changes. In a reactive approach, any enumerated or composite data type, s B2B Email List uch as a list, is treated as a stream and can be represented as follows: image1 Any stream on a timeline has a beginning, a set of sequentially emitted values, and an end, indicated by a vertical line. Thus, the stream generates events for us, ordered in time. These events can be anything: a click on a button, an http request to the server, data coming through a socket connection, animation, etc. And we have the opportunity to listen to such a stream and react accordingly to these events. Using regular functions, we can combine, modify and filter such streams. A thread can emit three types of events: meaning; error; completion.
And our task is to intercept them and describe the reaction to these three types of asynchronous (or synchronous) events. In doing so, we will work with several software entities, the first of which is observability . Observable is a common function with several characteristics specific to it. It takes an observer object as a parameter . This observer describes 3 methods: Next – getting the next value from the stream; Error – error; Complete – thread completion event. Observability provides a mechanism for passing messages between the one who generates the data and the one who uses it. In this case, there are 2 types of behavior strategies between the producer and consumer of data. With the first ( pull strategy ), the place and time of reaction to a change or receipt of data is determined by the consumer , with the second ( push strategy ) - with the manufacturer . An example of a pull strategy is a regular function call from that place in the program where the data that it returns is required, while the function itself knows nothing about who and where it will be “pulled” from. With a push strategy, on the contrary, the producer function is, as it were, “tied” with processors (consumers) who know nothing about where and when this data will be generated.