|
@@ -2,6 +2,7 @@
|
|
from __future__ import annotations
|
|
from __future__ import annotations
|
|
|
|
|
|
import inspect
|
|
import inspect
|
|
|
|
+import json
|
|
from typing import Any, Callable, Dict, List, Set, Tuple
|
|
from typing import Any, Callable, Dict, List, Set, Tuple
|
|
|
|
|
|
from pynecone.base import Base
|
|
from pynecone.base import Base
|
|
@@ -43,12 +44,29 @@ class EventHandler(Base):
|
|
|
|
|
|
Returns:
|
|
Returns:
|
|
The event spec, containing both the function and args.
|
|
The event spec, containing both the function and args.
|
|
|
|
+
|
|
|
|
+ Raises:
|
|
|
|
+ TypeError: If the arguments are invalid.
|
|
"""
|
|
"""
|
|
# Get the function args.
|
|
# Get the function args.
|
|
fn_args = inspect.getfullargspec(self.fn).args[1:]
|
|
fn_args = inspect.getfullargspec(self.fn).args[1:]
|
|
|
|
|
|
# Construct the payload.
|
|
# Construct the payload.
|
|
- payload = tuple(zip(fn_args, [Var.create(arg).full_name for arg in args])) # type: ignore
|
|
|
|
|
|
+ values = []
|
|
|
|
+ for arg in args:
|
|
|
|
+ # If it is a Var, add the full name.
|
|
|
|
+ if isinstance(arg, Var):
|
|
|
|
+ values.append(arg.full_name)
|
|
|
|
+ continue
|
|
|
|
+
|
|
|
|
+ # Otherwise, convert to JSON.
|
|
|
|
+ try:
|
|
|
|
+ values.append(json.dumps(arg))
|
|
|
|
+ except TypeError:
|
|
|
|
+ raise TypeError(
|
|
|
|
+ f"Arguments to event handlers must be Vars or JSON-serializable. Got {arg} of type {type(arg)}."
|
|
|
|
+ )
|
|
|
|
+ payload = tuple(zip(fn_args, values))
|
|
|
|
|
|
# Return the event spec.
|
|
# Return the event spec.
|
|
return EventSpec(handler=self, args=payload)
|
|
return EventSpec(handler=self, args=payload)
|