Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An ability to access PubSubHub from outside Flet app #3446

Merged
merged 3 commits into from
Jun 10, 2024
Merged

Conversation

FeodorFitsner
Copy link
Contributor

@FeodorFitsner FeodorFitsner commented Jun 10, 2024

Close #3445

Summary by Sourcery

This pull request introduces a new feature that allows accessing PubSubHub from outside the Flet app. It also includes a refactoring to centralize the management of PubSubHub instances within FletAppManager, enhancing code maintainability.

  • New Features:
    • Introduced the ability to access PubSubHub from outside the Flet app by adding a method to retrieve or create a PubSubHub instance.
  • Enhancements:
    • Refactored the handling of PubSubHub instances to use a centralized method in FletAppManager, improving code maintainability and reducing redundancy.

Copy link
Contributor

sourcery-ai bot commented Jun 10, 2024

Reviewer's Guide by Sourcery

This pull request introduces the ability to access PubSubHub from outside the Flet app by refactoring the way PubSubHub instances are managed and accessed. The changes centralize the PubSubHub management in the FletAppManager class, allowing for more flexible and thread-safe access.

File-Level Changes

Files Changes
sdk/python/packages/flet/src/flet/fastapi/flet_app_manager.py
sdk/python/packages/flet/src/flet/fastapi/flet_app.py
Centralized PubSubHub management in FletAppManager and refactored FletApp to use the new method for accessing PubSubHub instances.

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @FeodorFitsner - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 3 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

Comment on lines 35 to 36
self.__pubsubhubs_lock = threading.Lock()
self.__pubsubhubs = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Consider using an asyncio.Lock instead of threading.Lock

Since the rest of the code appears to be using asyncio constructs, it might be more consistent and potentially safer to use an asyncio.Lock instead of threading.Lock.

Suggested change
self.__pubsubhubs_lock = threading.Lock()
self.__pubsubhubs = {}
self.__pubsubhubs_lock = asyncio.Lock()
self.__pubsubhubs = {}

Comment on lines +42 to +44
def get_pubsubhub(
self, session_handler, loop: Optional[asyncio.AbstractEventLoop] = None
):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Add type hint for session_handler parameter

To improve code readability and maintainability, consider adding a type hint for the session_handler parameter.

Suggested change
def get_pubsubhub(
self, session_handler, loop: Optional[asyncio.AbstractEventLoop] = None
):
def get_pubsubhub(
self, session_handler: SessionHandlerType, loop: Optional[asyncio.AbstractEventLoop] = None
):

Comment on lines +46 to +47
psh = self.__pubsubhubs.get(session_handler, None)
if psh is None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Potential race condition with pubsubhubs dictionary

Although the lock should prevent race conditions, consider using a defaultdict to simplify the logic and reduce the risk of potential issues.

psh = self.__pubsubhubs.get(session_handler, None)
if psh is None:
psh = PubSubHub(
loop=loop if loop else asyncio.get_running_loop(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (code-quality): Replace if-expression with or (or-if-exp-identity)

Suggested change
loop=loop if loop else asyncio.get_running_loop(),
loop=loop or asyncio.get_running_loop(),


ExplanationHere we find ourselves setting a value if it evaluates to True, and otherwise
using a default.

The 'After' case is a bit easier to read and avoids the duplication of
input_currency.

It works because the left-hand side is evaluated first. If it evaluates to
true then currency will be set to this and the right-hand side will not be
evaluated. If it evaluates to false the right-hand side will be evaluated and
currency will be set to DEFAULT_CURRENCY.

@FeodorFitsner FeodorFitsner merged commit 2242157 into main Jun 10, 2024
2 checks passed
@FeodorFitsner FeodorFitsner deleted the pubsub-ext branch June 12, 2024 04:38
zrr1999 pushed a commit to zrr1999/flet that referenced this pull request Jul 17, 2024
* An ability to access PubSubHub from outside Flet app

Close flet-dev#3445

* Fix self.__pubsubhubs_lock

* Use "or" instead of "if-else"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Send pubsub broadcast message from outside Flet
1 participant