Skip to content

Latest commit

 

History

History
65 lines (47 loc) · 3 KB

230815.cancel.md

File metadata and controls

65 lines (47 loc) · 3 KB

https://discordapp.com/channels/918498540232253480/918498540232253483/1140949143720820806

raveesh-me — 08/15/2023 3:04 AM Hi Team! I am going through the services tutorial. I can see two keywords that I can not find an explanation / documentation for. One of them is run and the next is critical_do. Is there any languge documentation that can introduce these to me? VSCode highlight does highlight run like is highlighted in the documentation.

Another thing I noticed is if I declate a_variable, vscode autocompletion suggests a-variable everytime I want to access it, and it always works! (edited)

kasperl — 08/15/2023 3:05 AM run is just a method name, like sleep or receive.

@kasperl run is just a method name, like sleep or receive.

raveesh-me — 08/15/2023 3:06 AM Is there a place where I can get the list of methods?

kasperl — 08/15/2023 3:07 AM critical_do is also just a method, but that probably deserves a good explanation. It tells the system to call the code block in a special mode where things that may block continue to work even if called while the task is being canceled. [3:08 AM] In this case, you're defining the run method in the example code. [3:08 AM] The other methods are pulled in through libraries and packages. You can see the methods in the core library (implicitly imported) here: https://libs.toit.io/core/library-summary. [3:09 AM] https://libs.toit.io/core/library-summary#sleep(1%2C0%2C0%2C) [3:09 AM] https://libs.toit.io/core/library-summary#critical_do(2%2C1%2C0%2Crespect_deadline)

raveesh-me — 08/15/2023 3:12 AM just one more thing, what are critical blocks?

kasperl — 08/15/2023 3:12 AM Blocks are snippets of code that you pass to methods. [3:13 AM] You define them using : and an indented sequence of statements.

raveesh-me — 08/15/2023 3:13 AM I uderstand blocks, I want to know the significance of critical

kasperl — 08/15/2023 3:13 AM So a block is critical if it needs to run even if the task that executes it is being canceled. [3:14 AM] Cancelation is very useful, but also somewhat tricky, because it requires a task to stop running code and return out at well-defined points.

raveesh-me — 08/15/2023 3:14 AM that makes sense. like what then/finally is supposed to do in dart? (edited)

kasperl — 08/15/2023 3:14 AM Often you may need some clean up to happen in a finally block, and that is typically where you'll see critical_do. [3:15 AM] Yeah, kinda, but cancelation works by letting a task continue to run until it hits a blocking point and then stopping (unwinding) from there.

raveesh-me — 08/15/2023 3:16 AM Is there a resource that would allow me to read up more on cancellation?

kasperl — 08/15/2023 3:17 AM Very good question. The documentation at https://libs.toit.io/core/task/class-Task#cancel(0%2C0%2C0%2C) is pretty sparse. [3:18 AM] The example you sent just now illustrates the cancelation. The sending task will continue to run until canceled from the outside.

raveesh-me — 08/15/2023 3:20 AM This explains a lot! Now I can finally understand the whole example.