Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add processor phases feature #91

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions esper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
from typing import Iterable as _Iterable
from typing import Optional as _Optional
from typing import overload as _overload
from typing import Union as _Union

from weakref import ref as _ref
from weakref import WeakMethod as _WeakMethod

from enum import IntFlag as _IntFlag

version = '2.5'

Expand Down Expand Up @@ -123,6 +125,7 @@ def process(self):

priority = 0
world: "World"
phases: _Union[_IntFlag, int] = ~0

def process(self, *args: _Any, **kwargs: _Any) -> None:
raise NotImplementedError
Expand Down Expand Up @@ -159,7 +162,7 @@ def clear_database(self) -> None:
self._next_entity_id = 0
self.clear_cache()

def add_processor(self, processor_instance: Processor, priority: int = 0) -> None:
def add_processor(self, processor_instance: Processor, priority: int = 0, phases: _Optional[_IntFlag] = None) -> None:
"""Add a Processor instance to the World.

All processors should subclass :py:class:`esper.Processor`.
Expand All @@ -169,6 +172,12 @@ def add_processor(self, processor_instance: Processor, priority: int = 0) -> Non
"""
processor_instance.priority = priority
processor_instance.world = self

if phases is None:
processor_instance.phases = ~0
else:
processor_instance.phases = phases

self._processors.append(processor_instance)
self._processors.sort(key=lambda proc: proc.priority, reverse=True)

Expand Down Expand Up @@ -424,9 +433,10 @@ def _clear_dead_entities(self) -> None:
self._dead_entities.clear()
self.clear_cache()

def _process(self, *args: _Any, **kwargs: _Any) -> None:
def _process(self, phases: _Union[_IntFlag, int], *args: _Any, **kwargs: _Any) -> None:
for processor in self._processors:
processor.process(*args, **kwargs)
if processor.phases & phases:
processor.process(*args, **kwargs)

def process(self, *args: _Any, **kwargs: _Any) -> None:
"""Call the process method on all Processors, in order of their priority.
Expand All @@ -437,8 +447,11 @@ def process(self, *args: _Any, **kwargs: _Any) -> None:
at the start of this call.
"""
self._clear_dead_entities()
self._process(*args, **kwargs)
self._process(~0, *args, **kwargs)

def process_phase(self, phases: _IntFlag, *args: _Any, **kwargs: _Any) -> None:
self._clear_dead_entities()
self._process(phases, *args, **kwargs)

class TimedWorld(World):
def __init__(self) -> None:
Expand Down