Events Simple Event Manager: Events

Source: Internet
Author: User
EventsSimple event Manager. The Leaps/events event provides a great deal of flexibility in building loosely coupled applications, allowing expansion packs to participate in the core execution process of the application without modifying the program code.

Installation

"Require": {    "php": ">=5.4.0",    "leaps/events": "1.0.*"}

Basic use

This component is extremely easy to use:

 
  Listen (' foo ', function () {    return ' hello ';}); $response = $d->trigger (' foo '); Echo $response;

Triggering events

Just tell the Dispatcher class The event name you want to trigger to trigger an event:

To trigger an event:

 
  Trigger (' loaded ');

Note that we assign a variable to the execution result of the trigger method. This method returns an array containing all the returned data after the listener that listens to the event executes.

If you just want to get the first return data, you can:

Triggers an event and gets the first return value:

 
  Trigger (' loaded ');

Note: The first method triggers all listeners listening for an event, but finally returns only the last data.

Triggers an event until an event listener returns non-null data:

 
  First (' Loaded ');

Note: The until method performs all listeners that listen to an event sequentially before obtaining the first non-null return data.

Monitoring Events

The light has the event, no one listens is also useless. The following demonstrates registering an event handler for an event:

Register an event handler:

 
  Listen (' Loaded ' function ()    {        //I ' m executed on the "Loaded" event!    });

The closure function that passes this method is executed every time the "loaded" event is triggered.

Event queue

Sometimes you want to put events that need to be triggered in a "queue" to be stored instead of triggering them immediately. This requires the use of the queue and flush methods. First, register an event with a unique identifier to the event queue:

To register an event with an event queue:

 
  Queue (' Loaded ', $user->id, Array ($user));

This method accepts 3 parameters. The first parameter is the name of the event queue, the second argument is the unique identifier of the event, and the third parameter is the array to pass to the event cleaner, which carries useful data.

Next, we register an event handler for the Foo event:

Register an event handler:

 
  Flusher (' Loaded ', function ($key, $user)    {        //    });

Note that the Flusher method accepts two parameters. The first thing is the unique identifier of an event that is stored in the event queue, the user ID in the example above, and the second parameter (which can have more arguments thereafter) is the data that this event carries.

Finally, we call the flush method to dispose of all events in the queue to the event handler:

 
  Flush (' foo ', function ($key, $user)    {        //    });

Project home:http://www.open-open.com/lib/view/home/1430106120693

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.