⬆️ ⬇️

Asynchronous queues in the code

Good day.



image

Picture to attract attention.



In the previous ( and my first post ) we talked about the digestibility of reading the written code. That was why I wrote that Instruction class. In this post we will talk about the very convenience of using the instruction class. And also, about the beginning of writing the library utilitok.





In short - why all this?
The utilities given in the article allowed me to personally get rid of the clutter of structures in which one function caused another and so on, where the chains could be very, very long. For example:

private function foo_1():void { // code here foo_2(param); } private function foo_2(param:Object):void { // code here foo_3(param, param2); } // etc 


Methods of use:

1) Sequential animations. In my case, according to the instructions, UI elements in windows, the very appearance of windows, and other minor effects were animated programmatically.

2) Also, the instructions were very practical to use when communicating with the API services. Knowing what data the service returns, you can easily construct the initialization of the data at the start as an instruction.

')

A win in use is code readability. Having looked at the announcement of the instruction, it is immediately clear what is being called for and with what parameters. Adding new chains and embedding them in existing ones - just with the instruction - is painless.





Almost two months after the first article, I managed to face various tasks related to the sequential execution of scripts. In the process, the instruction class has been enhanced with additional functions. And also, one more utility «Order» was born. Order, or task list - can be interpreted as you like. The principle of operation is similar to the original instruction. But first things first.



The usual instruction:


Example of normal instructions
 public function init():void { Async.instruction .add(commandOne, 'Param for commandOne') .add(commandTwo, 100500) .add(commandThree, [1, 2, 3, 4, 5]) .execute(finalCommand, 'instruction executed successfully'); } private function commandOne(completeCb:Function, string:String):void { trace(string); // 'Param for commandOne' completeCb(); } private function commandTwo(completeCb:Function, number:int):void { trace(number); // 100500 completeCb(); } private function commandThree(completeCb:Function, array:Array):void { trace(array); // [1, 2, 3, 4, 5] completeCb(); } private function finalCommand(input:String):void { trace(input); // 'instruction executed successfully' } 


All functions are performed sequentially. Also, it is worth considering that the class will now throw an error in case there is not a single argument in one of the commands. Logically - one argument in the called command must be guaranteed - completeCallback, to pass the queue to the next function. Another point is that the parameters and the final function can not be passed to the parameters at all at the time of the announcement of the instruction.



Instructions with a bunch:


Example instructions with a bunch
 public function init():void { Async.instruction.add(commandOne) .add(commandTwo, 100500) .add(commandThree, { type:'init data' } ) .executeCollectingHeap(finalCommand); } private function commandOne(completeCb:Function):void { completeCb('commandOne', '1'); } private function commandTwo(completeCb:Function, number:int):void { trace(number); // 100500 completeCb('commandTwo', 2); } private function commandThree(completeCb:Function, object:Object):void { trace(JSON.stringify(object)); // { type:'init data' } completeCb('commandThree', {type:3}); } private function finalCommand(heapResult:Array):void { trace(heapResult[0]); // ['commandOne', '1'] trace(heapResult[1]); // ['commandTwo', 2] trace(heapResult[2]); // ['commandThree', {type:3}] trace('Instruction with heap executed successfully'); } 


The peculiarity of this type of instruction is that the called callbacks at the completion of each of the commands can transmit any number of parameters. Inside the instructions, they are carefully collected into an array and at the end of the instructions are transmitted to the final command, which can be seen from the commented out prompts in the code. Also, the peculiarity is that we don’t transfer anything to the final team at the announcement stage.



Order, or action list


Sample action list
 public function init() { Async.order .add(commandOne) .add(commandTwo) .add(commandThree) .execute(finalCommand); } private function commandOne(completeCb:Function):void { completeCb('string from commandOne'); } private function commandTwo(completeCb:Function, string:String):void { trace(string); // string from commandOne completeCb(2, 'string from commandTwo'); } private function commandThree(completeCb:Function, number:int, string:String):void { trace(number); // 2 trace(string); // string from commandTwo completeCb(); } private function finalCommand():void { trace('Order completed'); } 


I singled out this view separately, since the instruction manual was superfluous, and there was no wish to fence a bunch of conditions by soldering in new logic. The point here is that each team in the chain (in order) transmits to the next some data. It is very convenient to use when communicating with api services.



All code is laid out on githab . A start has been laid for a small (and, perhaps, a large one later, who knows?) A library.

I would be very happy with any criticism and comments to the libraries. And extremely grateful for corrections or improvements. Special puppy delight will bring the fact, if what I do - will be useful to other users. Thank you for attention.

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



All Articles