FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnative/src/pythonnative/native_modules/app_state.py at main · andevsr/pythonnative · GitHub
andevsr
pythonnative
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnative
/
src
/
pythonnative
/
native_modules
/
app_state.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
95 lines (71 loc) · 2.68 KB
Breadcrumbs
pythonnative
/
src
/
pythonnative
/
native_modules
/
app_state.py
Copy path
File metadata and controls
95 lines (71 loc) · 2.68 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""Foreground / background app lifecycle state.
[`AppState`][pythonnative.AppState] exposes the current lifecycle phase
(``"active"``, ``"inactive"``, or ``"background"``) and lets you
subscribe to transitions. The native host (the iOS app delegate /
Android ``Activity``) forwards lifecycle callbacks by calling
[`dispatch_app_state`][pythonnative.native_modules.app_state.dispatch_app_state],
so the same listener machinery works on every platform and in tests.
Prefer the [`use_app_state`][pythonnative.use_app_state] hook inside
components; use the imperative API for non-UI code.
Example:
```python
import pythonnative as pn
@pn.component
def Banner():
state = pn.use_app_state()
return pn.Text(f"App is {state}")
```
"""
from
__future__
import
annotations
from
typing
import
Callable
,
List
from
..
import
diagnostics
from
..
hooks
import
use_effect
,
use_state
AppStateStatus
=
str
# "active" | "inactive" | "background"
_VALID
=
(
"active"
,
"inactive"
,
"background"
)
_current
:
AppStateStatus
=
"active"
_listeners
:
List
[
Callable
[[
AppStateStatus
],
None
]]
=
[]
class
AppState
:
"""App lifecycle state interface."""
@
staticmethod
def
current_state
()
->
AppStateStatus
:
"""Return the current lifecycle phase."""
return
_current
@
staticmethod
def
add_listener
(
callback
:
Callable
[[
AppStateStatus
],
None
])
->
Callable
[[],
None
]:
"""Subscribe to lifecycle changes.
Returns:
A zero-arg function that unsubscribes when called.
"""
_listeners
.
append
(
callback
)
def
_unsubscribe
()
->
None
:
try
:
_listeners
.
remove
(
callback
)
except
ValueError
:
pass
return
_unsubscribe
def
dispatch_app_state
(
state
:
AppStateStatus
)
->
None
:
"""Update the current state and notify every listener.
Called by the native host on lifecycle transitions. Unknown values
are ignored so a misbehaving host can't push garbage into the tree.
"""
global
_current
if
state
not
in
_VALID
or
state
==
_current
:
return
_current
=
state
for
listener
in
list
(
_listeners
):
try
:
listener
(
state
)
except
Exception
:
diagnostics
.
swallowed
(
"app_state.dispatch_app_state"
)
def
use_app_state
()
->
AppStateStatus
:
"""Subscribe a component to [`AppState`][pythonnative.AppState].
Returns:
The current lifecycle phase; the component re-renders whenever
it changes.
"""
state
,
set_state
=
use_state
(
_current
)
def
_subscribe
()
->
Callable
[[],
None
]:
set_state
(
_current
)
return
AppState
.
add_listener
(
set_state
)
use_effect
(
_subscribe
, [])
return
state
Back
|
FazBrowse Home
|
New Git URL