Coder Social home page Coder Social logo

klaari / laravel-deferred-event-dispatcher Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jalle19/laravel-deferred-event-dispatcher

0.0 0.0 0.0 8 KB

A deferring event dispatcher for the Laravel and Lumen frameworks

License: MIT License

PHP 100.00%

laravel-deferred-event-dispatcher's Introduction

laravel-deferred-event-dispatcher

Build Status Coverage Status

A deferring event dispatcher for the Laravel and Lumen frameworks.

Motivation

Events are a powerful feature of any framework. It allows you to decouple your code so that the logic that triggers an event doesn't have to know what should happen in reaction to that event.

While the default implementation in Laravel/Lumen is perfectly adequate in many cases, it is fairly naive, which can lead to subpar performance. Consider the following scenario:

  1. You have your own EventServiceProvider where you have registered a bunch of event subscribers.
  2. You register this service provider in your bootstrap file

Under the hood, whenever an EventServiceProvider is registered, the EventDispatcher will also be constructed. The event dispatcher in turn will go through all the event handlers you've defined and construct each of them so that it knows which listeners to trigger in the case of an event.

While this doesn't sound that bad, it means that even for the simplest of requests (e.g. a route that only prints "OK", e.g. a health check route) will cause all your event subscribers to be constructed, which can pull in a lot of code that you don't need to fulfill the request (e.g. Doctrine and other "heavy" services").

This wouldn't be so bad if it weren't for the fact that the majority of your requests probably won't be triggering any events, so constructing and registering all your event handlers is pointless. This event dispatcher implementation aims to solve that problem.

Deferring the resolving of listeners and subscribers

A simple yet fairly effective solution is to defer the resolving of event handlers until an event is actually dispatched. This assures we don't do any unnecessary work in requests that don't trigger any events. But there's a problem...

A lot of services use events under the hood:

  • Some service providers will dispatch events before and after they've booted to allow other service providers to hook into these and perform optional bootstraping logic
  • The cache repository will dispatch Illuminate/Cache/Events/CacheHit and Illuminate/Cache/Events/CacheMissed events every time you attempt to retrieve something from your cache. Depending on your application, this is something that could happen during practically every request.

Even if we deferr the resolving of event handlers until an event is dispatched, chances are there is at least one event dispatched on every request anyway, defeating the whole point.

Deferring specific events

Chances are you don't care about whether a cache operation resulted in a hit or a miss, or whether a service provider has been booted or not. Ignoring these events would defer the resolving of event handlers even further, possibly for the duration of the request, which finally means we've accomplished our goal of not constructing all event handlers needlessly.

Requirements

  • Laravel/Lumen >= 5.4
  • PHP >= 7.1

Usage

  1. Install the library using Composer:
composer require jalle19/laravel-deferred-event-dispatcher
  1. Swap the default event dispatcher for this one:
// The event dispatcher must be a singleton
$app->singleton(\Jalle19\Laravel\Events\DeferredEventDispatcher::class, function () use ($app) {
    return new \Jalle19\Laravel\Events\DeferredEventDispatcher($app, [
        // Cache events
        Illuminate\Cache\Events\CacheHit::class,
        Illuminate\Cache\Events\CacheMissed::class,
    ]);
});

// Swap the default implementation for this one. Some classes type-hint the interface, others simply use "events"
$app->alias(\Jalle19\Laravel\Events\DeferredEventDispatcher::class, 'events');
$app->alias(\Jalle19\Laravel\Events\DeferredEventDispatcher::class, Illuminate\Contracts\Events\Dispatcher::class);

In this example we've decided to defer resolving of event handlers whenever a cache event is dispatched.

  1. Make sure your event service provider is registered somewhere after these lines

That's it, your event handlers will now be resolved only when an event that you haven't explicitly deferred is dispatched!

License

MIT

laravel-deferred-event-dispatcher's People

Contributors

jalle19 avatar klaari avatar

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.