Maslosoft Signals Logo Maslosoft Signals Documentation
Wireless Cross-Component Communication

Edit

Emit signal

Prior to emitting signal, signal definition must be generated.

Emitting signal is a process where single point in application emerges action and other, scattered around application receivers take action after receiving such signal.

To emit signal simply call emit method with some signal as param.

Receiving signals

Signals could be received via various injection methods. This depends on receiver implementation. To create receiver use annotation @SlotFor on either class, method or property definition. Depending on where annotation is placed, it will be injected properly by signals.

Note: Classes used here are available in examples folder.

Class Constructor injection

When annotation will be placed in class comment block, Signals will create instance of this class and pass emitted signal as constructor param.

Example:

/**
 * @SlotFor(ConstructorInjected)
 *
 * @see ConstructorInjected
 */
class WithConstructorInjection implements AnnotatedInterface
{
...

When emitting this is equivalent of following code for each class containing annotation @SlotFor(ConstructorInjected):

new WithConstructorInjection(new ConstructorInjected);

Now emit this signal, and see results. This code is really evaluated here:

$results = (new Signal)->emit(new ConstructorInjected);

And the resulting array is one instance of signal with it's property set by WithConstructorInjection class:

Array
(
    [0] => Maslosoft\SignalsExamples\Signals\ConstructorInjected Object
        (
            [emitted] => 1
        )

)

Method injection

Possibly most useful injection type. It will instantiate class containing this method and pass emitted signal as it's parameter to annotated method.

Following class will react on signal, but signal will be injected into reactOn method:

class WithMethodInjection implements AnnotatedInterface
{
    /**
     * @SlotFor(MethodInjected)
     * @param MethodInjected $signal
     */
    public function reactOn(MethodInjected $signal)
    {
        $signal->emitted = true;
    }
}

When emitting this is equivalent of following code for every annotated method (might be many times for one class):

$model = new WithMethodInjection();
$model->reactOn(new MethodInjected());

Emitting this signal will yield array of MethodInjected signals. Live evaluation results are available here:

$results = (new Signal)->emit(new MethodInjected);

Array is one instance of signal with it's property set by WithMethodInjection class:

Array
(
    [0] => Maslosoft\SignalsExamples\Signals\MethodInjected Object
        (
            [emitted] => 1
        )

)

When to use emitting

Use emit when class instantiated by signal must perform some action.