Skip to content

Commit

Permalink
warning if event handlers already registered or has weird argspec
Browse files Browse the repository at this point in the history
  • Loading branch information
kushalkolar committed Dec 22, 2022
1 parent 78aeb2e commit 22f7283
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion fastplotlib/graphics/features/_base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from abc import ABC, abstractmethod
from inspect import getfullargspec
from warnings import warn
from typing import *

import numpy as np
Expand Down Expand Up @@ -63,6 +64,11 @@ def add_event_handler(self, handler: callable):
"""
if not callable(handler):
raise TypeError("event handler must be callable")

if handler in self._event_handlers:
warn(f"Event handler {handler} is already registered.")
return

self._event_handlers.append(handler)

#TODO: maybe this can be implemented right here in the base class
Expand All @@ -73,7 +79,11 @@ def _feature_changed(self, key: Union[int, slice, Tuple[slice]], new_data: Any):

def _call_event_handlers(self, event_data: FeatureEvent):
for func in self._event_handlers:
if len(getfullargspec(func).args) > 0:
try:
if len(getfullargspec(func).args) > 0:
func(event_data)
except:
warn(f"Event handler {func} has an unresolvable argspec, trying it anyways.")
func(event_data)
else:
func()
Expand Down

0 comments on commit 22f7283

Please sign in to comment.