
from typing import Optional

from ...events.board import BoardPostingCreated, BoardTopicCreated
from ...events.news import NewsItemPublished
from ...signals import board as board_signals
from ...signals import news as news_signals
from ...util.jobqueue import enqueue

from . import board, news


# board


@board_signals.topic_created.connect
def _on_board_topic_created(
    sender, *, event: Optional[BoardTopicCreated] = None
) -> None:
    enqueue(board.announce_board_topic_created, event)


@board_signals.posting_created.connect
def _on_board_posting_created(
    sender, *, event: Optional[BoardPostingCreated] = None
) -> None:
    enqueue(board.announce_board_posting_created, event)


# news


@news_signals.item_published.connect
def _on_news_item_published(
    sender, *, event: Optional[NewsItemPublished] = None
) -> None:
    enqueue(news.announce_news_item_published, event)


from typing import Optional

from ...events.base import _BaseEvent
from ...events.board import BoardPostingCreated, BoardTopicCreated
from ...events.news import NewsItemPublished
from ...signals import board as board_signals
from ...signals import news as news_signals
from ...util.jobqueue import enqueue

from . import board, news


EVENT_TYPES_TO_HANDLERS = {
    BoardTopicCreated: board.announce_board_topic_created,
    BoardPostingCreated: board.announce_board_posting_created,
    NewsItemPublished: news.announce_news_item_published,
}


@board_signals.topic_created.connect
@board_signals.posting_created.connect
@news_signals.item_published.connect
def _on_event(sender, *, event: Optional[_BaseEvent] = None) -> None:
    event_type = type(event)

    handler = EVENT_TYPES_TO_HANDLERS.get(event_type)
    if handler is None:
        return None

    enqueue(handler, event)

Compress Discord event connectors into single function
Compress Discord event connectors into single function

  
  from typing import Optional
  
+ from ...events.base import _BaseEvent
  from ...events.board import BoardPostingCreated, BoardTopicCreated
  from ...events.news import NewsItemPublished
  from ...signals import board as board_signals
  from ...signals import news as news_signals
  from ...util.jobqueue import enqueue
  
  from . import board, news
  
  
- # board
+ EVENT_TYPES_TO_HANDLERS = {
+     BoardTopicCreated: board.announce_board_topic_created,
+     BoardPostingCreated: board.announce_board_posting_created,
+     NewsItemPublished: news.announce_news_item_published,
+ }
  
  
  @board_signals.topic_created.connect
- def _on_board_topic_created(
-     sender, *, event: Optional[BoardTopicCreated] = None
- ) -> None:
-     enqueue(board.announce_board_topic_created, event)
+ @board_signals.posting_created.connect
+ @news_signals.item_published.connect
+ def _on_event(sender, *, event: Optional[_BaseEvent] = None) -> None:
+     event_type = type(event)
  
+     handler = EVENT_TYPES_TO_HANDLERS.get(event_type)
+     if handler is None:
+         return None
  
+     enqueue(handler, event)
- @board_signals.posting_created.connect
- def _on_board_posting_created(
-     sender, *, event: Optional[BoardPostingCreated] = None
- ) -> None:
-     enqueue(board.announce_board_posting_created, event)
  
- 
- # news
- 
- 
- @news_signals.item_published.connect
- def _on_news_item_published(
-     sender, *, event: Optional[NewsItemPublished] = None
- ) -> None:
-     enqueue(news.announce_news_item_published, event)
- 
Compress Discord event connectors into single function
## Code Before:

from typing import Optional

from ...events.board import BoardPostingCreated, BoardTopicCreated
from ...events.news import NewsItemPublished
from ...signals import board as board_signals
from ...signals import news as news_signals
from ...util.jobqueue import enqueue

from . import board, news


# board


@board_signals.topic_created.connect
def _on_board_topic_created(
    sender, *, event: Optional[BoardTopicCreated] = None
) -> None:
    enqueue(board.announce_board_topic_created, event)


@board_signals.posting_created.connect
def _on_board_posting_created(
    sender, *, event: Optional[BoardPostingCreated] = None
) -> None:
    enqueue(board.announce_board_posting_created, event)


# news


@news_signals.item_published.connect
def _on_news_item_published(
    sender, *, event: Optional[NewsItemPublished] = None
) -> None:
    enqueue(news.announce_news_item_published, event)

## Instruction:
Compress Discord event connectors into single function
## Code After:

from typing import Optional

from ...events.base import _BaseEvent
from ...events.board import BoardPostingCreated, BoardTopicCreated
from ...events.news import NewsItemPublished
from ...signals import board as board_signals
from ...signals import news as news_signals
from ...util.jobqueue import enqueue

from . import board, news


EVENT_TYPES_TO_HANDLERS = {
    BoardTopicCreated: board.announce_board_topic_created,
    BoardPostingCreated: board.announce_board_posting_created,
    NewsItemPublished: news.announce_news_item_published,
}


@board_signals.topic_created.connect
@board_signals.posting_created.connect
@news_signals.item_published.connect
def _on_event(sender, *, event: Optional[_BaseEvent] = None) -> None:
    event_type = type(event)

    handler = EVENT_TYPES_TO_HANDLERS.get(event_type)
    if handler is None:
        return None

    enqueue(handler, event)
