Skip to content

Commit

Permalink
feat: decrease chances of reactive key collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
iisakkirotko committed Mar 26, 2024
1 parent 4705b46 commit 6971464
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions solara/toestand.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,17 @@ def __init__(self, default_value: S, key=None):
self.default_value = default_value
cls = type(default_value)
if key is None:
import inspect

for frame in inspect.stack():
file = frame.filename
if not (file.endswith("solara/toestand.py") or file.endswith("solara/reactive.py")):
break

with KernelStoreValue.scope_lock:
index = self._type_counter[cls]
self._type_counter[cls] += 1
key = cls.__module__ + ":" + cls.__name__ + ":" + str(index)
key = file + ":" + cls.__name__ + ":" + str(index)
super().__init__(key=key)

def initial_value(self) -> S:
Expand All @@ -290,13 +297,18 @@ def __init__(self, factory: Callable[[], S], key=None):
except Exception:
prefix = repr(factory)
if key is None:
import inspect

for frame in inspect.stack():
file = frame.filename
if not (file.endswith("solara/toestand.py") or file.endswith("solara/reactive.py")):
break

with KernelStore.scope_lock:
index = self._type_counter[prefix]
self._type_counter[prefix] += 1
try:
key = factory.__module__ + ":" + prefix + ":" + str(index)
except Exception:
key = prefix + ":" + str(index)

key = file + ":" + prefix + ":" + str(index)
super().__init__(key=key)

def initial_value(self) -> S:
Expand Down

0 comments on commit 6971464

Please sign in to comment.