diff --git a/docs/Killswitches.md b/docs/Killswitches.md index 41935318..85275819 100644 --- a/docs/Killswitches.md +++ b/docs/Killswitches.md @@ -76,8 +76,15 @@ Plugins may use the killswitch system simply by hosting their own version of the using `killswitch.get_kill_switches(target='https://example.com/myplugin_killswitches.json')`. The returned object can be used to query the kill switch set, see the docstrings for more information on specifying versions. -The version of the JSON file will be automatically upgraded if possible by the code KillSwitch code. No behaviour changes will occur--Any killswitches defined in older - versions will simply become unconditional kills in the new version. +A helper method `killswitch.get_kill_switch_thread` is provided to allow for simple nonblocking requests for +KillSwitches. It starts a new thread, performs the HTTP request, and sends the results to the given callback. + +**Note that your callback is invoked off-thread. Take precaution for locking if required, and do _NOT_ use tkinter** +**methods** + +The version of the JSON file will be automatically upgraded if possible by the code KillSwitch code. +No behaviour changes will occur--Any killswitches defined in older versions will simply become unconditional kills in +the new version. ## Currently supported killswitch strings diff --git a/killswitch.py b/killswitch.py index b63bb5ea..c89d7ace 100644 --- a/killswitch.py +++ b/killswitch.py @@ -1,10 +1,11 @@ """Fetch kill switches from EDMC Repo.""" from __future__ import annotations +import threading from copy import deepcopy from typing import ( - TYPE_CHECKING, Any, Dict, List, Mapping, MutableMapping, MutableSequence, NamedTuple, Optional, Sequence, Tuple, - TypedDict, Union, cast + TYPE_CHECKING, Any, Callable, Dict, List, Mapping, MutableMapping, MutableSequence, NamedTuple, Optional, Sequence, + Tuple, TypedDict, Union, cast ) import requests @@ -414,6 +415,22 @@ def get_kill_switches(target=DEFAULT_KILLSWITCH_URL, fallback: Optional[str] = N return KillSwitchSet(parse_kill_switches(data)) +def get_kill_switches_thread( + target, callback: Callable[[Optional[KillSwitchSet]], None], fallback: Optional[str] = None, +) -> None: + """ + Threaded version of get_kill_switches. Request is performed off thread, and callback is called when it is available. + + :param target: Target killswitch file + :param callback: The callback to pass the newly created KillSwitchSet + :param fallback: Fallback killswitch file, if any, defaults to None + """ + def make_request(): + callback(get_kill_switches(target, fallback=fallback)) + + threading.Thread(target=make_request, daemon=True).start() + + active: KillSwitchSet = KillSwitchSet([])