Skip to content

Commit

Permalink
Improve sending changed settings back to the launcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Garulf committed Nov 23, 2023
1 parent a6c312a commit baf04e3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
12 changes: 4 additions & 8 deletions pyflowlauncher/method.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
from __future__ import annotations
from typing import Any, Iterable, TypedDict
from typing import Any, Dict, Optional
from abc import ABC, abstractmethod

from .result import Result, JsonRPCAction, send_results
from .result import Result, JsonRPCAction, ResultResponse, send_results
from .shared import logger


class ResultResponse(TypedDict):
result: Iterable[dict[str, Any]]


class Method(ABC):

def __init__(self) -> None:
Expand All @@ -19,8 +15,8 @@ def __init__(self) -> None:
def add_result(self, result: Result) -> None:
self._results.append(result)

def return_results(self) -> ResultResponse:
return send_results(self._results)
def return_results(self, settings: Optional[Dict[str, Any]] = None) -> ResultResponse:
return send_results(self._results, settings)

@abstractmethod
def __call__(self, *args, **kwargs) -> ResultResponse | JsonRPCAction:
Expand Down
3 changes: 3 additions & 0 deletions pyflowlauncher/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,7 @@ def run(self) -> None:
method = request.get('method')
parameters = request.get('parameters', [])
feedback = self._event_handler(method, *parameters)
# Inject settings if changed
if 'result' in feedback and self._settings is not None:
feedback['SettingsChange'] = self.settings
self._client.send(feedback)
8 changes: 5 additions & 3 deletions pyflowlauncher/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
Optional,
Union,
Dict,
List
)

if sys.version_info < (3, 11):
Expand Down Expand Up @@ -62,9 +63,10 @@ def add_action(self, method: Method,


class ResultResponse(TypedDict):
result: Iterable[Dict[str, Any]]
result: List[Dict[str, Any]]
SettingsChange: NotRequired[Optional[Dict[str, Any]]]


def send_results(results: Iterable[Result]) -> ResultResponse:
def send_results(results: Iterable[Result], settings: Optional[Dict[str, Any]] = None) -> ResultResponse:
"""Formats and returns results as a JsonRPCResponse"""
return {'result': [result.as_dict() for result in results]}
return {'result': [result.as_dict() for result in results], 'SettingsChange': settings}

0 comments on commit baf04e3

Please sign in to comment.