Komtet / mediator

A library implements the Mediator pattern to make your code extensible
MIT License
19 stars 3 forks source link

Using Mediator in Multiple Files #7

Open mmarab opened 6 years ago

mmarab commented 6 years ago

Hi, this maybe a simple question, but i'm pretty new to python and Mediator. I'm currently using Mediator, but i will be dispatching events from different api controllers which live in different python files, i would like to use a singleton instance of Mediator so i'm not having to new up all the listeners etc with each request, how would you do this with your framework? I thought defining Mediator as a static from a setup class could work...

ghost commented 6 years ago

Hi @mmarab! Sorry for delay.

I thought defining Mediator as a static from a setup class could work...

And what's the problem?

For example, you have in app.py:

mediator = Mediator()
# and then register your listeners...

view1.py:

from app import mediator

def view(req):
    mediator.dispatch(event1)

view2.py:

from app import mediator

def view(req):
    mediator.dispatch(event2)

This setup should work fine.

If you have a different files for listeners, you can use venusian's scanner or import them manually.