Hacktions
"hacktions speak louder than words", (*1)
Events for Hacklang, (*2)
Usage
Hacktions exposes two traits:, (*3)
EventEmitter
Run of the mill event emitter with hack type safety., (*4)
class MyEmitter
{
use EventEmitter;
}
$emitter = new MyEmitter();
//listeners all use variable argument signatures
$lambda = (...) ==> print(func_get_args()[0]);
$emitter->on('myevent', $lambda);
//trigger fires all registered events with the given name - can take variable arguments
$emitter->trigger('myevent', 'brian');
$emitter->off('myevent', $lambda); //remove a single listener
$emitter->removeListeners(); //remove all listeners
$emitter->removeListeners('myevent') //remove all 'myevent' events
Subject
Hacktions supports explicit subject/observer relationships:, (*5)
class Cook
{
//Cook notifies Waiter objects
use Subject<Waiter>;
protected Vector<Burger> $cookedBurgers = {};
public function cook(Burger $burger): void
{
$this->cookedBurgers->add($burger);
$this->notifyObservers();
}
//implement methods to work on cooked items...
}
class Waiter implements Observer<Cook>
{
//Waiter observes a cook
public function update(Cook $cook, ...): void
{
$burger = $cook->orderUp();
//deliver burger...
}
}
The Subject has the following methods:, (*6)
registerObserver(T $observer): void
Registers a new observer with the subject., (*7)
removeObserver(T $observer): void
Removes the specified object from the list of observers., (*8)
notifyObservers(...): void
Notify all observers of a change and optionally pass additional data., (*9)
Running tests
Tests are written using HackUnit. They can be run with the following:, (*10)
bin/hackunit Tests/
hhi
As always, make sure to copy hhi files to the project directory if type checking is desired:, (*11)
cp -r /usr/share/hhvm/hack/hhi /path/to/hacktions