Skip to content

Commit

Permalink
Ensure the channel is the same as the widget's ID, if no default chan…
Browse files Browse the repository at this point in the history
…nel is given at init time.
  • Loading branch information
ntoll committed Jun 27, 2024
1 parent b77297a commit 26bcfd3
Showing 1 changed file with 34 additions and 29 deletions.
63 changes: 34 additions & 29 deletions src/invent/ui/core/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,35 +476,6 @@ def update_attribute(self, attribute_name, attribute_value):
else:
self.element.removeAttribute(attribute_name)

def when(self, subject, to_channel=None, do=None):
"""
Convenience method for wrapping subscriptions.
If no "do" handler is given, we assume this function is decorating the
handler to "do" the stuff.
The subject and to_channel can be either individual strings or a
list of strings to indicate the channel[s] and message subject[s] to
match.
"""
if not to_channel:
to_channel = self.id

if do:
invent.subscribe(
handler=do, to_channel=to_channel, when_subject=subject
)
else:

def inner_function(handler):
invent.subscribe(
handler=handler,
to_channel=to_channel,
when_subject=subject,
)

return inner_function


class Widget(Component):
"""
Expand All @@ -530,6 +501,11 @@ class Widget(Component):
default_value=None,
)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.channel is None:
self.channel = self.id

def publish(self, blueprint, **kwargs):
"""
Given the name of one of the class's MessageBlueprints, publish
Expand All @@ -549,6 +525,35 @@ def publish(self, blueprint, **kwargs):
)
invent.publish(message, to_channel=channels)

def when(self, subject, to_channel=None, do=None):
"""
Convenience method for wrapping subscriptions.
If no "do" handler is given, we assume this function is decorating the
handler to "do" the stuff.
The subject and to_channel can be either individual strings or a
list of strings to indicate the channel[s] and message subject[s] to
match.
"""
if not to_channel:
to_channel = self.id

if do:
invent.subscribe(
handler=do, to_channel=to_channel, when_subject=subject
)
else:

def inner_function(handler):
invent.subscribe(
handler=handler,
to_channel=to_channel,
when_subject=subject,
)

return inner_function


class Container(Component):
"""
Expand Down

0 comments on commit 26bcfd3

Please sign in to comment.