-
Notifications
You must be signed in to change notification settings - Fork 0
Asynchronous coding
With CUIF almost all is asyncronous. Please don't confuse with multi-task or parallelism. CUIF is a single threaded program. Async means that one portion or your code (always passed through a callback function) will be executed later, by something that is waiting that an special event occurs.
So, what can you do async? Timing functions, by timeout, or intervals. MySQL query, called async like ajax in the web. And much more. How? Simple. Let Me show:
class MyFirstApplication extends cuif\Application {
public function main() { // CUIF will call the main();
/**
* First I am setting the timeout. Past 10 seconds your challenge is over.
* Note that I am storing the Worker Task ID into a $taskIndex variable, because
* when you confirm your name before timeout, I need remove it.
*/
$taskIndex = salodev\Timer::TimeOut(function() {
$this->alertWindow('Time expired!', 'Try it again!', function() {
$this->end();
});
}, 10000);
/**
* Now I am asking your name... Try typing before 10 seconds. Since restart te app
* and try wait for the timeout.
*
* You may eperience how can type and delete your text into the input box,
* meanwhile something in "background" is waiting for the timeout.
*/
$this->promptWindow('Hello!', 'Write your name in less than 10 seconds', null, function($value) use($to){
salodev\Timer::Delete($taskIndex);
$this->alertWindow('Congratulations!', "{$value}, you are approved", function() {
$this->end();
});
});
}
}
cuif\CUIF::StartApplication('MyFirstApplication', false);
Open a new terminal... and explore your process list. You will not find a child process. Because CUIF does not forks. CUIF start using something I called Worker. It is a kind of EvenetLoop implementation. What is it? A simple class that provides a Start() method that just make a while(true) for keep your program alive, but into loop, it proccess a stack of tasks that prevously or during the loop are stacked with AddTask() method.
Timer class adds tasks to Worker class. It is the way CUIF works, and it is the basic architecture for (almost) all things that CUIF do.
When you call to cuif\CUIF::StartApplication, a main task is added to worker: Reading the standard input. So when a key is pressed the application is notified and it do 'that must do' on key press event.
Because it, I told that CUIF is an event-driven framework.
With CUIF you can allow to user keep using your program while data comes from database server. In other words you can avoid the blocking, or your MUST avoid the blocking. An example:
/**
* An instance of \salodev\MysqlConnection
*/
$sql = "SELECT * FROM projects WHERE name LIKE '%{$search}%'";
$this->_connection->query($sql, function($rs) {
// list is an instance of ListBox in the window.
$this->list->setData($rs);
});
Note that, you receive the entire RecordSet array through a callback function, not by value return. It is because MysqlConnection does not block the process until receive data. Instead add a task to the Worker, to listen for incoming data from the db server, and so use your callback.
Because CUIF is built based on my salodev repository of classes and wrappers for common tasks. Yo can see that Worker class (base of CUIF) is under salodev. CUIF just add control for console, screen, visual objects, and connect all things in a easy way for You.