Увод у програмирање понашања са Реацт-ом: захтевајте, сачекајте и блокирајте

Програмирање понашања (БП) је парадигма настала у чланку Давид Харела, Ассафа Маррона и Гере Веисс из 2012. године.

Директно из сажетка:

Програмирање понашања поједностављује задатак решавања недовољно специфицираних и сукобљених захтева омогућавањем додавања софтверских модула који могу не само да додају већ и да модификују постојећа понашања .

Концепти на високом нивоу

Прво ћу објаснити концепте на високом нивоу користећи пример две компоненте Реацт MoviesListи MoviesCount. Један приказује листу филмова, други број колико филмова постоји. Тада ћу заронити у то како тачно функционише програмирање понашања.

Обе компоненте преузимају податке са исте ХТТП УРЛ адресе. Развили су их два различита тима у великој организацији. Када прикажемо обе компоненте на страници, имамо проблем јер оне извршавају исти захтев:

Нисмо знали да су то компоненте понашања . То значи да можемо учинити нешто врло паметно како бисмо избегли покретање оба захтева:

const MoviesCountFromList = withBehavior([ function* () { // block FETCH_COUNT from happening yield { block: ['FETCH_COUNT'] } }, function* () { // wait for FETCH_LIST, requested by the other // MoviesList component, and derive the count const response = yield { wait: ['FETCH_LIST'] } this.setState({ count: response.length }) }])(MoviesCount)

У горњем примеру закорачили смо у MoviesCountкомпоненту. Чекали смо и тражили да се нешто догоди. И, јединственије за програмирање понашања, такође смо блокирали да се нешто не догоди.

Будући да смо покушавали да избегнемо FETCH_COUNTпокретање оба захтева, блокирали смо покретање догађаја (јер је FETCH_LISTдогађај већ прибавио исте податке ).

Додавање функционалности постојећим компонентама без модификовања њиховог кода новост је парадигме понашања у програмирању.

Интуитивно, ово може омогућити стварање компонената за вишекратну употребу.

У остатку чланка детаљније ћу проучити како програмирање понашања (БП), посебно у контексту Реацт-а .

Преиспитивање тока програмирања

Да бисмо постигли горњу функционалност, морамо да размишљамо о понашању програмирања мало другачије. Конкретно, догађаји играју пресудну улогу у организовању синхронизације између различитих понашања која дефинишемо за наше компоненте.

const addHotThreeTimes = behavior( function* () { yield { request: ['ADD_HOT'] } yield { request: ['ADD_HOT'] } yield { request: ['ADD_HOT'] } })
const addColdThreeTimes = behavior( function* () { yield { request: ['ADD_COLD'] } yield { request: ['ADD_COLD'] } yield { request: ['ADD_COLD'] } })
run( addHotThreeTimes, addColdThreeTimes)

Када покренемо горњи код, вратимо листу захтеваних догађаја:

ADD_HOTADD_HOTADD_HOTADD_COLDADD_COLDADD_COLD

Као што се и очекивало, прво понашање се извршава. Једном кад се заврши, друго понашање се наставља. Међутим, нове спецификације за нашу компоненту захтевају да променимо редослед покретања оба догађаја. Уместо да покрећу ADD_HOTтри пута, а затим ADD_COLDтри пута, желимо да се међусобно препусте и покрену ADD_COLDодмах након а ADD_HOT. Ово ће одржавати температуру донекле стабилном.

...
const interleave = behavior( function* () { while (true) { // wait for ADD_HOT while blocking ADD_COLD yield { wait: ['ADD_HOT'], block: ['ADD_COLD'] }
 // wait for ADD_COLD while blocking ADD_HOT yield { wait: ['ADD_COLD'], block: ['ADD_HOT'] } } })
run( addHotThreeTimes, addColdThreeTimes, interleave)

У горњем примеру уводимо ново понашање у преплитању које ради тачно оно што нам треба.

ADD_HOTADD_COLDADD_HOTADD_COLDADD_HOTADD_COLD

Променили смо редослед извршења ствари, а да нисмо морали да модификујемо код већ написаног понашања.

Процес је сажет на доњој слици.

Кључни концепти овог начина програмирања су оператори захтева , чекања и блокирања . Семантика ових оператора је:

  • Захтев за догађајем: предлагање да се догађај размотри због покретања и тражење обавештења када се покрене
  • Чекање догађаја: без предлагања покретања, тражење обавештења о покретању догађаја
  • Блокирање догађаја: забрана покретања догађаја, стављање вета на захтеве других б-нити.

Свака б-нит (нит понашања) живи самостално и није свесна других нити. Али сви су испреплетени током извођења, што им омогућава да међусобно комуницирају на врло нов начин.

Синтакса генератора је од суштинског значаја за функционисање програма понашања. Морамо да контролишемо када ћемо прећи на следећу изјаву о приносу.

Повратак на Реацт

Како се ови концепти БП могу користити у контексту Реацт-а?

Испоставило се да помоћу компонената високог реда (ХОЦ) можете на врло интуитиван начин додати овај идиом понашања постојећим компонентама:

class CommentsCount extends React.Component { render() { return {this.state.commentsCount} }}
const FetchCommentsCount = withBehavior([ function* () { yield { request: ['FETCH_COMMENTS_COUNT']} const comments = yield fetchComments() yield { request: ['FETCH_COMMENTS_COUNT_SUCCESS']} this.setState({ commentsCount: comments.length }) },])(CommentsCount)

Овде користимо withBehavior, из библиотеке б-нити, да направимо CommentsCountкомпоненту понашања. Конкретно, правимо да дохвати коментаре и прикаже податке када подаци буду спремни.

За једноставне компоненте ово можда неће бити таква промена игре. Али замислимо сложеније компоненте, са пуно логике и другим компонентама у себи.

Могли бисмо да замислимо целу веб страницу Нетфлик-а као /> component:

When we use this component in our app, we’d like to interact with it. Specifically, when a movie is clicked, we don’t want to start the movie immediately, but instead we want to make an HTTP request, show other data about the movie, and then start the movie.

Without changing code inside the /> component, I’d argue that this would be impossible to achieve without it being a behavioral component.

Instead let’s imagine that /> was developed using behavioral programming:

const NetflixWithMovieInfo = withBehavior([ function* () { // First, block the MOVIE_START from happening // within until a new // FETCH_MOVIE_INFO_SUCCESS event has been requested. // The yield statement below can be read as: // wait for FETCH_MOVIE_INFO_SUCCESS while blocking MOVIE_START yield { wait: ['FETCH_MOVIE_INFO_SUCCESS'], block: ['MOVIE_START'] } }, function* () { // Here we wait for MOVIE_CLICKED, which is // triggered within , and we fetch our // movie info. Once that's done we request a new event // which the earlier behavior is waiting upon const movie = yield { wait: ['MOVIE_CLICKED'] } const movieInfo = yield fetchMovieInfo(movie) yield { request: ['FETCH_MOVIE_INFO_SUCCESS'], payload: movieInfo } }])(Netflix)

Above we’ve created a new NetflixWithMovieInfo component which modifies the behavior of the /> component (again, without changing its source code). The addition of the above behaviors makes it so that MOVIE_CLICKED will not tr igger MOVIE_START immediately.

Instead, it uses a combination of “waiting while blocking”: a wait and a block can be defined within a single yield statement.

The picture above describes, more in detail, what is happening within our behavioral components. Each little box within the components is a yield statement. Each vertical dashed arrow represents a behavior (aka b-thread).

Internally, the behavioral implementation will start by looking at all the yield statements of all b-threads at the current synchronization point, depicted using an horizontal yellow line. It will only continue to the next yield statement within a b-thread if no events in other b-threads are blocking it.

Since nothing is blocking MOVIE_CLICKED , it will be requested. We can then continue to the next yield statement for the Netflix behavior. At the next synch point, the b-thread on the far right, which is waiting for MOVIE_CLICKED, will proceed to its next yield statement.

The middle behavior that is waiting-and-blocking does not proceed. FETCH_MOVIE_INFO_SUCCESS was not requested by other b-threads, so it still waits-and-blocks. The next synchronization point will look something like this:

As before, we will look at all the yield statement at this synchronization point. This time, however, we cannot request MOVIE_START because there’s another b-thread that is blocking it (the black yield statement). The Netflix component will therefore not start the movie.

FETCH_MOVIE_INFO_SUCCESS on the far right, however, is free to be requested. This will unblock MOVIE_START at the next synch point.

All this in practice allowed us to change the order of things happening within other components, without directly modifying their code. We were able to block certain events from firing until other conditions were met in other components.

This changes the way we might think of programming: not necessarily a set of statements executed in order, but rather an interleaving of yield statements all synchronized through specific event semantics.

Here’s a simple animation depicting the way b-threads are executed and interwoven at runtime.

Programming without changing old code

There is another way we can understand this programming idiom. We can compare the way we currently program as specifications change, versus how it would be done with behavioral programming.

In the above caption, we imagine how behavior may be added to a non-behavioral program. We start with a program described only using three black rectangles (on the left).

As specifications change, we realize we need to modify the program and add new behavior in various sections of the program, depicted as newly added colored rectangles. We continue doing this as requirements for our software change.

Every addition of behavior requires us to change code that was written, which possibly litters the old behavior with bugs. Furthermore, if the program we are changing is part of various other modules used by different people, we might be introducing unwanted behavior to their software. Finally, it may not be possible to change specific programs as they might be distributed as libraries with licensed source code.

In the above figure, we see how the same program-modifications can be achieved using behavioral programming idioms. We still start with our three rectangles on the left as we did before. But as new specifications arise, we don’t modify them. Instead we add new b-threads, represented as columns.

The resulting program is the same, although constructed in a very different way. One of the advantages of the behavioral approach is that we don’t have to modify old code as requirements change.

You can also imagine developing each b-thread in parallel, possibly by different people in a large organization, since they do not directly depend on each other.

The benefit of this approach also seems to be with packaging: we can change the behavior of a library without needing to access or modify its source-code.

APIs not only as props, but as events

Currently, the only way for a React component to communicate with the outside world is via props (apart from the Context API).

By making a component behavioral, instead of using props, we tell the outside world about when things happen within the component by yielding events.

To allow other developers to interact with the behavior of a component, we must therefore document the events that it requests, the events it waits for, and finally the events it blocks.

Events become the new API.

For instance, in a non-behavioral Counter component, we tell the outside world when the counter is incremented and what the current count is, using an onIncrement prop:

class Counter extends React.Component { state = { currentCount: 0 } handleClick = () => { this.setState(prevState => ({ currentCount: prevState.currentCount + 1 }), () => { this.props.onIncrement(this.state.currentCount) }) } render() { {this.state.currentCount} + }}
 console.log(currentCount) }/>

What if we want to do something else before the counter’s state gets incremented? Indeed we could add a new prop such as onBeforeIncrement, but the point is that we don’t want to add props and refactor code every time a new specific arises.

If we transform it into a behavioral component we can avoid refactoring when new specifications emerge:

class Counter extends React.Component { state = { currentCount: 0 } handleClick = () => { bp.event('CLICKED_INCREMENT') } render() { {this.state.currentCount} + }}
const BehavioralCounter = withBehavior([ function* () { yield { wait: ['CLICKED_INCREMENT'] } yield { request: ['UPDATE_CURRENT_COUNT'] }
 this.setState(prevState => ({ currentCount: prevState.currentCount + 1 }), () => { this.props.onIncrement(this.state.currentCount) }) }])(Counter)

Notice how we moved the logic for when the state is updated inside a b-thread. Furthermore, before the update actually takes place, a new event UPDATE_CURRENT_COUNT is requested.

This effectively allows other b-threads to block the update from happening.

Components can also be encapsulated and shared as different packages, and users can add behavior as they see fit.

// package-name: movies-listexport const function MoviesList() { ...}
// package-name: movies-list-with-paginationexport const MoviesListWithPagination = pipe( withBehavior(addPagination))(MoviesList)
// package-name: movies-list-with-pagination-logicexport const MoviesListWithDifferentPaginationLogic = pipe( withBehavior(changePaginationLogic))(MoviesListWithPagination)

Again this is different from simply enhancing a component, as a regular HOC would do. We can block certain things from happening in the components we extend from, effectively modifying their behavior.

Conclusion

This new programming idiom might feel uncomfortable at first, but it seems to alleviate a prominent issue we have when using UI components: it is hard to reuse components, because they don’t blend with the environment they were put into.

In the future, perhaps using these behavioral concepts, we will be able to add new behavior to apps by simply mounting new components. Stuff like this will be possible:

Additionally, events don’t need to pollute the whole app and can be broadcast only within a specific environment.

Thanks for reading! If you’re interested in an actual implementation of behavioral programming, please see my current work in progress library that works with React: //github.com/lmatteis/b-thread. The Behavioral Programming homepage also contains various implementations.

For more information on this exciting new concept, I suggest you read the scientific papers on Behavioral Programming or check some of my other articles on the subject.