1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-06-27 02:12:35 +00:00

refactor(core): add maybe_get_context

[no changelog]

(cherry picked from commit 3bb3b774c1)
This commit is contained in:
Martin Milata 2025-04-15 11:32:13 +02:00 committed by M1nd3r
parent 092c86083c
commit 607d73b7da
2 changed files with 10 additions and 4 deletions

View File

@ -10,16 +10,15 @@ if TYPE_CHECKING:
async def unpair(msg: BleUnpair) -> None:
from trezor.messages import Success
from trezor.ui.layouts import confirm_action
from trezor.wire.context import get_context
from trezor.wire.context import maybe_get_context
if msg.all:
await confirm_action("erase bonds", TR.ble__unpair_title, TR.ble__unpair_all)
else:
await confirm_action("unpair", TR.ble__unpair_title, TR.ble__unpair_current)
ctx = get_context()
await ctx.write(Success(message="Erasing.."))
if ctx := maybe_get_context():
await ctx.write(Success(message="Erasing..."))
if msg.all:
ble.erase_bonds()

View File

@ -107,6 +107,13 @@ def get_context() -> Context:
return CURRENT_CONTEXT
def maybe_get_context() -> Context | None:
"""Variant of the function above, same limitations apply. Needed in special cases
where a handler might be invoked over wire or from a homescreen menu (e.g. wipe).
"""
return CURRENT_CONTEXT
def with_context(ctx: Context, workflow: loop.Task) -> Generator:
"""Run a workflow in a particular context.