How to sketch an event driven system?

I am trying to create a system in Node.js (trying to solve one of my early problems using Node concurrency). but I ran into difficulties figuring out how to draw a plan of how this thing should work.

I get very confused thinking in terms of callbacks instead of return values. The flow is not linear, and it really scares my ability to design it. How to output the workflow for an event-driven system?

I need something that I can look at and say, "Well, yes, how it will work. I will start it here and it will return these results to me."

Pictures will be very useful for this. Thanks.

Change I'm looking for something more granular than UML , in particular, something that will help me move from a blocking and object-oriented programming structure, where it is convenient for me, to a non-blocking and event-driven structure, where I am unfamiliar.

+4
source share
2 answers

Based on http://i.stack.imgur.com/9DDQP.png you need a good thread library that allows pipelined and asynchronous calls to node.

One of these libraries is https://github.com/isaacs/slide-flow-control (see the preso slide there too) and the code diagram for what you need to do is below.

This is self-documenting, and as you can see, it is concise enough, pure nodejs, uml, img, etc. not required.

var chain = require("slide/chain") , asyncMap = require("slide/async-map") ; // start processing main_loop(function() { console.log("its done"); // when finished }); function main_loop(cb) { var res = []; // each entry in chain below fires sequentially ie after // the previous function completes chain ( [ [start_update_q, "user-foo"] , [get_followed_users, chain.last] , [get_favorites, chain.last] , [calc_new_q] , [push_results, chain.last] ] , res , cb ) } function get_favorites(users, cb) { function fn(user, cb_) { get_one_users_favorites(user, cb_); } // this will run thru get_favorites in parallel // and after all user favorites are gotten it will fire // callback cb asyncMap(users, fn, cb); } // code in the various functions in chain here, // remember to either return the callback on completion. // or pass it as an arg to the async call you make within the // function as above ie asyncMap will fire cb on completion 
+3
source

UML may be appropriate. I would look at the behavior diagrams.

0
source

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


All Articles