Skip to content

Commit 4bcb55f

Browse files
committed
More robust keys for web module caching mechanism
1 parent 0c52710 commit 4bcb55f

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

src/reactpy/web/module.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ def import_js_from_url(
6565
allow_children:
6666
Whether or not these components can have children.
6767
"""
68-
if url in _URL_WEB_MODULE_CACHE:
69-
module = _URL_WEB_MODULE_CACHE[url]
68+
key = f"{url}{resolve_exports}{resolve_exports_depth}{unmount_before_update}"
69+
if key in _URL_WEB_MODULE_CACHE:
70+
module = _URL_WEB_MODULE_CACHE[key]
7071
else:
7172
module = _module_from_url(
7273
url,
@@ -75,7 +76,7 @@ def import_js_from_url(
7576
resolve_exports_depth=resolve_exports_depth,
7677
unmount_before_update=unmount_before_update,
7778
)
78-
_URL_WEB_MODULE_CACHE[url] = module
79+
_URL_WEB_MODULE_CACHE[key] = module
7980
return _vdom_from_web_module(module, export_names, fallback, allow_children)
8081

8182

@@ -117,8 +118,9 @@ def import_js_from_file(
117118
allow_children:
118119
Whether or not these components can have children.
119120
"""
120-
if name in _FILE_WEB_MODULE_CACHE:
121-
module = _FILE_WEB_MODULE_CACHE[name]
121+
key = f"{name}{resolve_exports}{resolve_exports_depth}{unmount_before_update}"
122+
if key in _FILE_WEB_MODULE_CACHE:
123+
module = _FILE_WEB_MODULE_CACHE[key]
122124
else:
123125
module = _module_from_file(
124126
name,
@@ -129,7 +131,7 @@ def import_js_from_file(
129131
unmount_before_update=unmount_before_update,
130132
symlink=symlink,
131133
)
132-
_FILE_WEB_MODULE_CACHE[name] = module
134+
_FILE_WEB_MODULE_CACHE[key] = module
133135
return _vdom_from_web_module(module, export_names, fallback, allow_children)
134136

135137

@@ -168,8 +170,9 @@ def import_js_from_string(
168170
allow_children:
169171
Whether or not these components can have children.
170172
"""
171-
if name in _STRING_WEB_MODULE_CACHE:
172-
module = _STRING_WEB_MODULE_CACHE[name]
173+
key = f"{name}{resolve_exports}{resolve_exports_depth}{unmount_before_update}"
174+
if key in _STRING_WEB_MODULE_CACHE:
175+
module = _STRING_WEB_MODULE_CACHE[key]
173176
else:
174177
module = _module_from_string(
175178
name,
@@ -179,7 +182,7 @@ def import_js_from_string(
179182
resolve_exports_depth=resolve_exports_depth,
180183
unmount_before_update=unmount_before_update,
181184
)
182-
_STRING_WEB_MODULE_CACHE[name] = module
185+
_STRING_WEB_MODULE_CACHE[key] = module
183186
return _vdom_from_web_module(module, export_names, fallback, allow_children)
184187

185188

tests/test_web/test_module.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -474,12 +474,15 @@ def test_import_js_from_url_caching():
474474

475475
# First import
476476
reactpy.web.import_js_from_url(url, "Component", resolve_exports=False)
477-
assert url in reactpy.web.module._URL_WEB_MODULE_CACHE
478-
module1 = reactpy.web.module._URL_WEB_MODULE_CACHE[url]
477+
# Find the key that contains the 'url' substring
478+
key = next(x for x in reactpy.web.module._URL_WEB_MODULE_CACHE.keys() if url in x)
479+
module1 = reactpy.web.module._URL_WEB_MODULE_CACHE[key]
480+
assert module1
481+
initial_length = len(reactpy.web.module._URL_WEB_MODULE_CACHE)
479482

480483
# Second import
481484
reactpy.web.import_js_from_url(url, "Component", resolve_exports=False)
482-
assert reactpy.web.module._URL_WEB_MODULE_CACHE[url] is module1
485+
assert len(reactpy.web.module._URL_WEB_MODULE_CACHE) == initial_length
483486

484487

485488
def test_import_js_from_file_caching(tmp_path):
@@ -489,11 +492,13 @@ def test_import_js_from_file_caching(tmp_path):
489492
reactpy.web.module._FILE_WEB_MODULE_CACHE.clear()
490493

491494
reactpy.web.import_js_from_file(name, file, "Component")
492-
assert name in reactpy.web.module._FILE_WEB_MODULE_CACHE
493-
module1 = reactpy.web.module._FILE_WEB_MODULE_CACHE[name]
495+
key = next(x for x in reactpy.web.module._FILE_WEB_MODULE_CACHE.keys() if name in x)
496+
module1 = reactpy.web.module._FILE_WEB_MODULE_CACHE[key]
497+
assert module1
498+
initial_length = len(reactpy.web.module._FILE_WEB_MODULE_CACHE)
494499

495500
reactpy.web.import_js_from_file(name, file, "Component")
496-
assert reactpy.web.module._FILE_WEB_MODULE_CACHE[name] is module1
501+
assert len(reactpy.web.module._FILE_WEB_MODULE_CACHE) == initial_length
497502

498503

499504
def test_import_js_from_string_caching():
@@ -502,8 +507,12 @@ def test_import_js_from_string_caching():
502507
reactpy.web.module._STRING_WEB_MODULE_CACHE.clear()
503508

504509
reactpy.web.import_js_from_string(name, content, "Component")
505-
assert name in reactpy.web.module._STRING_WEB_MODULE_CACHE
506-
module1 = reactpy.web.module._STRING_WEB_MODULE_CACHE[name]
510+
key = next(
511+
x for x in reactpy.web.module._STRING_WEB_MODULE_CACHE.keys() if name in x
512+
)
513+
module1 = reactpy.web.module._STRING_WEB_MODULE_CACHE[key]
514+
assert module1
515+
initial_length = len(reactpy.web.module._STRING_WEB_MODULE_CACHE)
507516

508517
reactpy.web.import_js_from_string(name, content, "Component")
509-
assert reactpy.web.module._STRING_WEB_MODULE_CACHE[name] is module1
518+
assert len(reactpy.web.module._STRING_WEB_MODULE_CACHE) == initial_length

0 commit comments

Comments
 (0)