Skip to content

Commit

Permalink
sort _hooks statically on each hook register
Browse files Browse the repository at this point in the history
  • Loading branch information
EnriqueSoria committed May 3, 2022
1 parent 6f7febf commit 9ecce7d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
5 changes: 4 additions & 1 deletion django_lifecycle/decorators.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from functools import wraps
from operator import itemgetter
from typing import List, Optional

from django_lifecycle import NotSet

from .hooks import VALID_HOOKS
from .priority import DEFAULT_PRIORITY

Expand Down Expand Up @@ -101,6 +101,9 @@ def func(*args, **kwargs):
}
)

# Sort hooked methods by priority
func._hooked = sorted(func._hooked, key=itemgetter("priority"))

return func

return decorator
6 changes: 1 addition & 5 deletions django_lifecycle/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,6 @@ def _watched_fk_model_fields(cls) -> List[str]:
def _watched_fk_models(cls) -> List[str]:
return [_.split(".")[0] for _ in cls._watched_fk_model_fields()]

@classmethod
def _sort_callback_specs_by_priority(cls, callback_specs: Iterable[dict]) -> List[dict]:
return sorted(callback_specs, key=itemgetter("priority"))

def _get_hooked_methods(self, hook: str, **kwargs) -> List[HookedMethod]:
"""
Expand All @@ -240,8 +237,7 @@ def _get_hooked_methods(self, hook: str, **kwargs) -> List[HookedMethod]:
hooked_methods = []

for method in self._potentially_hooked_methods():
callback_specs_sorted_by_priority = self._sort_callback_specs_by_priority(method._hooked)
for callback_specs in callback_specs_sorted_by_priority:
for callback_specs in method._hooked:
if callback_specs["hook"] != hook:
continue

Expand Down

0 comments on commit 9ecce7d

Please sign in to comment.