Skip to content

Commit 6740933

Browse files
sumanthvraoneiljp
authored andcommitted
refactor: helper: Merge parameters passed into _set_count_[model/view].
Since the functions only need to iterate through each message, we pass in a list of messages by merging id_list and message dict.
1 parent 2567f87 commit 6740933

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

zulipterminal/helper.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,14 @@ def wrapper(*args: Any, **kwargs: Any) -> Any:
8484
return wrapper
8585

8686

87-
def _set_count_in_model(id_list: List[int], new_count: int,
88-
messages: Dict[int, Message],
87+
def _set_count_in_model(new_count: int, changed_messages: List[Message],
8988
unread_counts: UnreadCounts) -> None:
9089
"""
9190
This function doesn't explicitly set counts in model,
9291
but updates `unread_counts` (which can update the model
9392
if it's passed in, but is not tied to it).
9493
"""
95-
for id in id_list:
96-
message = messages[id]
97-
94+
for message in changed_messages:
9895
if message['type'] == 'stream':
9996
key = (message['stream_id'], message['subject'])
10097
unreads = unread_counts['unread_topics']
@@ -119,8 +116,8 @@ def _set_count_in_model(id_list: List[int], new_count: int,
119116
unreads[key] = new_count
120117

121118

122-
def _set_count_in_view(id_list: List[int], controller: Any, new_count: int,
123-
messages: Dict[int, Message],
119+
def _set_count_in_view(controller: Any, new_count: int,
120+
changed_messages: List[Message],
124121
unread_counts: UnreadCounts) -> None:
125122
"""
126123
This function for the most part contains the logic for setting the
@@ -136,8 +133,7 @@ def _set_count_in_view(id_list: List[int], controller: Any, new_count: int,
136133
user_buttons_log = controller.view.user_w.log
137134
all_msg = controller.view.home_button
138135
all_pm = controller.view.pm_button
139-
for id in id_list:
140-
message = messages[id]
136+
for message in changed_messages:
141137
user_id = message['sender_id']
142138

143139
# If we sent this message, don't increase the count
@@ -188,14 +184,14 @@ def set_count(id_list: List[int], controller: Any, new_count: int) -> None:
188184
assert new_count == 1 or new_count == -1
189185
messages = controller.model.index['messages']
190186
unread_counts = controller.model.unread_counts # type: UnreadCounts
191-
192-
_set_count_in_model(id_list, new_count, messages, unread_counts)
187+
changed_messages = [messages[id] for id in id_list]
188+
_set_count_in_model(new_count, changed_messages, unread_counts)
193189

194190
# if view is not yet loaded. Usually the case when first message is read.
195191
while not hasattr(controller, 'view'):
196192
time.sleep(0.1)
197193

198-
_set_count_in_view(id_list, controller, new_count, messages, unread_counts)
194+
_set_count_in_view(controller, new_count, changed_messages, unread_counts)
199195

200196
while not hasattr(controller, 'loop'):
201197
time.sleep(0.1)

0 commit comments

Comments
 (0)