26
26
27
27
# pylint: disable=too-few-public-methods
28
28
class _Hook (_tx .Generic [HookDataType ]):
29
- def __init__ (self , func , priority , final = False , data : HookDataType = None ):
29
+ def __init__ (self , func , priority = 0 , final = False , data : HookDataType = None ):
30
30
self .func = func
31
31
self .final = final
32
32
self .data = data
@@ -51,24 +51,32 @@ def __init__(self) -> None:
51
51
self ._clientside_callbacks : _t .List [
52
52
_t .Tuple [ClientsideFuncType , _t .Any , _t .Any ]
53
53
] = []
54
+
55
+ # final hooks are a single hook added to the end of regular hooks.
54
56
self ._finals = {}
55
57
56
58
def add_hook (
57
- self , hook : str , func : _t .Callable , priority = None , final = False , data = None
59
+ self ,
60
+ hook : str ,
61
+ func : _t .Callable ,
62
+ priority : _t .Optional [int ] = None ,
63
+ final = False ,
64
+ data = None ,
58
65
):
59
66
if final :
60
67
existing = self ._finals .get (hook )
61
68
if existing :
62
69
raise HookError ("Final hook already present" )
63
- self ._finals [hook ] = _Hook (func , priority , final , data = data )
70
+ self ._finals [hook ] = _Hook (func , final , data = data )
64
71
return
65
72
hks = self ._ns .get (hook , [])
73
+
74
+ p = 0
66
75
if not priority and len (hks ):
67
76
priority_max = max (h .priority for h in hks )
68
- priority = priority_max - 1
69
- elif not priority :
70
- priority = 0
71
- hks .append (_Hook (func , priority = priority , data = data ))
77
+ p = priority_max - 1
78
+
79
+ hks .append (_Hook (func , priority = p , data = data ))
72
80
self ._ns [hook ] = sorted (hks , reverse = True , key = lambda h : h .priority )
73
81
74
82
def get_hooks (self , hook : str ) -> _t .List [_Hook ]:
@@ -106,7 +114,7 @@ def route(
106
114
self ,
107
115
name : _t .Optional [str ] = None ,
108
116
methods : _t .Sequence [str ] = ("GET" ,),
109
- priority = None ,
117
+ priority : _t . Optional [ int ] = None ,
110
118
final = False ,
111
119
):
112
120
"""
@@ -126,7 +134,7 @@ def wrap(func: _t.Callable[[], _f.Response]):
126
134
127
135
return wrap
128
136
129
- def error (self , priority = None , final = False ):
137
+ def error (self , priority : _t . Optional [ int ] = None , final = False ):
130
138
"""Automatically add an error handler to the dash app."""
131
139
132
140
def _error (func : _t .Callable [[Exception ], _t .Any ]):
@@ -135,7 +143,7 @@ def _error(func: _t.Callable[[Exception], _t.Any]):
135
143
136
144
return _error
137
145
138
- def callback (self , * args , priority = None , final = False , ** kwargs ):
146
+ def callback (self , * args , priority : _t . Optional [ int ] = None , final = False , ** kwargs ):
139
147
"""
140
148
Add a callback to all the apps with the hook installed.
141
149
"""
@@ -168,7 +176,7 @@ def stylesheet(self, distribution: _t.List[ResourceType]):
168
176
"""Add stylesheets to the page."""
169
177
self ._css_dist .extend (distribution )
170
178
171
- def index (self , priority = None , final = False ):
179
+ def index (self , priority : _t . Optional [ int ] = None , final = False ):
172
180
"""Modify the index of the apps."""
173
181
174
182
def wrap (func ):
@@ -187,6 +195,7 @@ def wrap(func):
187
195
188
196
189
197
class HooksManager :
198
+ # Flag to only run `register_setuptools` once
190
199
_registered = False
191
200
hooks = hooks
192
201
0 commit comments