Skip to content

Let modules subscribe to other modules' object events (global event dispatcher) #526

Open

Description

@caseysoftware

Current state

  • Dispatchers are private. Every w2p_Core_BaseObject creates its own w2p_System_Dispatcher and subscribes only itself to its own events (BaseObject::_subscribeToEvents()). No module can hear another module's events, such as "a task was deleted" or "a project was deleted". Modules that keep data tied to tasks or projects can't clean up after them.
  • Events carry no data. w2p_System_Event has a $data property but no getter, and BaseObject never passes data (new w2p_System_Event(get_class($this), 'postDeleteEvent'), BaseObject.class.php lines 225-527).
  • Events are named by concrete class. Project deletes remove their tasks through w2p_Actions_BulkTasks (CProject::hook_postDelete()), so those deletes publish as w2p_Actions_BulkTasks, not CTask. A listener for CTask would miss them.
  • Task moves bypass events. CTask::moveTaskBetweenProjects() moves subtasks with raw SQL (updateSubTasksProject()), and only the root task is store()d. No event says "these tasks changed project".
  • Bug: in Dispatcher::publish(), the wildcard-resource loop foreach($this->_listeners['*'] as $event => $listeners) (line 75) overwrites $event with a string key and passes that string to listeners whose parameter is typed w2p_System_Event. That's a TypeError on PHP 8.

Intended solution

  1. Shared dispatcher. Add an app-wide instance, w2p_System_Dispatcher::shared(). BaseObject keeps publishing to its own dispatcher for its hook_* methods, then publishes the same event to the shared one.
  2. Event data. Add w2p_System_Event::getData(). Shared events carry ['object' => $this, 'id' => <key>, 'module' => <module>, 'original' => <row before an update, or null>].
    • Take the original snapshot (one extra SELECT) only when the shared dispatcher has listeners for that class and event. Add hasListeners($resource, $event), so unrelated saves pay nothing.
  3. Publish under the module's resource too. Publish under the base module name (tasks, projects) as well as the concrete class, so subclasses like w2p_Actions_BulkTasks reach listeners for tasks.
  4. A move event. CTask::moveTaskBetweenProjects() publishes postMoveEvent with ['task_id', 'old_project', 'new_project', 'moved_task_ids' => <the task and every subtask>].
  5. Registration. A module's main class can implement a new interface, w2p_Interfaces_Subscriber, with static getSubscriptions(): array, e.g. [['tasks', 'postDeleteEvent'], ['tasks', 'postMoveEvent'], ['projects', 'postDeleteEvent']].
    • Bootstrap reads subscriptions from active modules once and caches them in the session, as tabs already are.
    • Listener objects are only created when a matching event fires.
  6. Isolation. Each shared-listener call is wrapped in try/catch with error_log(), as w2p_System_HookHandler already does. A broken module can't break a core save or delete.
  7. Fix the bug. Fix the $event overwrite in Dispatcher::publish().

First consumer

The Cards module (caseysoftware/modules-cards) needs task delete, task move and project delete to keep its cards consistent.

Other uses

  • Generic tagging can drop tag links when any object is deleted.
  • Files and Links could clean up after task deletes the same way.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions