Code Monkey home page Code Monkey logo

Comments (4)

electricjones avatar electricjones commented on September 27, 2024

What sort of action would you want to be fired on every db event? I can see the usefulness of this, but I was wondering what real-world problem you are having now and if a workaround may be more appropriate.

from event.

mechastorm avatar mechastorm commented on September 27, 2024

Hey,

The database example is just one possible use case.

The real world use case that I have is for application monitoring metrics. ie. In an API library, we want to track how many times an event was triggered or how long the event took.

For instance in an application, we prefix all calls relating to API library as "client.api.". WIth that we want to record when this event was triggered with a record call to StatsD/Graphite as count. We have a separate prefix for timing ie. "client.timing.api.".

The current workaround I have done is to so far use the all wildcard "*" to catch ALL events being handled by the Emitter instance. I then manually decide if that event is part of that prefix and record. This method works so far but I have reservations on the performance of this, hence why I would prefer a support for partial wildcards.

from event.

electricjones avatar electricjones commented on September 27, 2024

Well, I threw together a very small patch that adds this functionality. Its on my fork at https://github.com/chrismichaels84/event/tree/chrismichaels84-wildcards. Be sure to use the wildcards branch.

I only added a few lines to src/Emmitter.php inside the emit() method and then added a helper (getWildcard()) inside AbstractEvent.

So now, the following works as expected:

$emitter = new League\Event\Emitter();

// Standard listener
$emitter->addListener('app.action.db.standard', function($event, $param) {
    echo "this is the listener for app.action.db.standard";
});

$emitter->emit('app.action.db.standard', 1);

// Wildcards
$emitter->addListener('app.action.db.*', function($event, $param) {
    echo "This will fire everytime any app.action.db event is fired."
    echo "This event wildcard was: " . $event->getWildcard();
});

$emitter->emit('app.action.db.here', 2);
$emitter->emit('app.action.db.there', 3);
$emitter->emit('app.action.db.everywhere', 4);

I haven't wrote any tests yet, but this doesn't break any existing tests. Lemme know if you have any feedback.

from event.

frankdejonge avatar frankdejonge commented on September 27, 2024

This is something I don't have planned for this package, nor see fit in the future. It's very inexpensive to register a listener to multiple events. Since there are already * listeners it could be handled within the listener anyway.

from event.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.