Skip to content

Replace hooks and emitter events and listener to use the Symfony ones #29672

Closed

Description

@PVince81

For each hook and emitter:

  1. delete said hook/emitter
  2. search for usages across all apps
  3. replace everywhere to use symfony
  4. write them down and list aspects of OC that will need to be regression tested
  5. check that we have tests for said events, if not, write them

@DeepDiver1975 @sharidas

Activity

  1. added this to the backlog milestone on Nov 24, 2017
  2. tomneedham commented on Jun 27, 2018

    @tomneedham
    Contributor

    The next way of adding EventEmitterTrait::emittingCall is not so optimal:

    • Often involves wrapping whole code blocks to emit the event after execution, making PRs hard to read & spot implementation mistakes (eg: 76ae48e#diff-2ad387ea4134715ffda3bb0f12140f91R298)
    • Exceptions occurring during the callback are hard to debug because the stacktrace is masked by the execution of the callback callable.
    • Variables passed by reference are not strict enough and allow too much magic to be implemented, as well as being tricky to debug

    Is there a nicer way to achieve the addition of events?

    • Maintainable
    • Testable
    • Debuggable
    • Nice code footprint
  3. butonic commented on Jun 27, 2018

    @butonic
    Contributor

    While I totally agree that we should move forward with moving hooks to events I am very unhappy with the EventEmitterTrait::emittingCall() added with https://github.com/owncloud/core/pull/29651/files#diff-88dd9ddb262ef3b3df2aa72d7793d462R46 / stable10 https://github.com/owncloud/core/pull/29939/files#diff-88dd9ddb262ef3b3df2aa72d7793d462R46:

    1. it makes the code a lot harder to read because now the code no longer reflects the order of events
    2. it forces dereferencing variables used in the after event if they are changed in the closure
    3. the closure makes the code really hard to debug
    4. stack traces become useless.

    IMNSHO: stop using that method and get rid of the emitter trait asap.

  4. jvillafanez commented on Jun 27, 2018

    @jvillafanez
    Member

    Is there a nicer way to achieve the addition of events?

    Other than the usual dependency injection, I don't think so.

    The key point is who is responsible of creating and sending the events. This responsability needs to be clearly defined in one object (maybe more, depending on the events), but definitely not spread into multiple objects.

    The listeners should be registered as part of the initialization of the app that use this. The usual Application class used for the dependency injection can also be used for the event registration.

    (in the app.php file)
    $app = new Application();
    $app->registerListeners();
    

    That should be enough to add unit tests to check if an event is being triggered (probably using the dependency injection, checking if the symfony dispatcher is being called), and also to check the listeners code by triggering the specific event. In both cases, the tests should be pretty isolated.

    What I think it's also important is the use custom events. By using custom events you can wrap an object or multiple objects inside an event and provide limited access to the object:

    • Provide access to specific properties of the wrapped object
    • Provide read-only access to the wrapped object, in case you don't want the listener to modify any property of the object
    • Provide limited access to some "write" methods of the object. Maybe you want to allow setting some properties of the object but not others
    • Provide custom access to some object properties. Maybe you want to conditionally set a property of the object, or set a property only once.
    • Include custom methods in the event to prevent the event propagation and also prevent further code execution of the method in case of the "beforeSomething" events.
  5. PVince81 commented on Jun 27, 2018

    @PVince81
    ContributorAuthor

    @jvillafanez I think the topic here is about the usage of traits and the wrapping of the event emitter, not the event class itself.

    @butonic As far as I remember, the usage of traits was recommended by @DeepDiver1975 when reviewing #29651. Somehow I can't find the commented messages as Github refuses to expand comments from old changes.

    @DeepDiver1975 are you ok with reverting the decision about the trait ?

  6. stale commented on Sep 21, 2021

    @stale

    This issue has been automatically closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions