22from typing import Any , Optional , Union
33from urllib .parse import urlparse
44
5- from idom import client
65from idom .core .vdom import VdomDict , ImportSourceDict , make_vdom_constructor
76
7+ from .protocol import client_implementation as client
8+
89
910class Module :
1011 """A Javascript module
1112
1213 Parameters:
13- path :
14+ url_or_name :
1415 The URL to an ECMAScript module which exports React components
1516 (*with* a ``.js`` file extension) or name of a module installed in the
1617 built-in client application (*without* a ``.js`` file extension).
1718 source_file:
18- Only applicable if running on the built-in client app. Dynamically install
19- the code in the give file as a single-file module. The built-in client will
20- inject this module adjacent to other installed modules which means they can
21- be imported via a relative path (e.g. ``./some-other-installed-module.js``).
22-
23- .. warning::
24-
25- Do not use the ``source_file`` parameter if not running with the client app
26- distributed with ``idom``.
27-
28- Examples:
29- .. testcode::
30-
31- import idom
32-
19+ Only applicable if running on a client app which supports this feature.
20+ Dynamically install the code in the give file as a single-file module. The
21+ built-in client will inject this module adjacent to other installed modules
22+ which means they can be imported via a relative path like
23+ ``./some-other-installed-module.js``.
24+
25+ Attributes:
26+ installed:
27+ Whether or not this module has been installed into the built-in client app.
28+ url:
29+ The URL this module will be imported from.
30+
31+ Notes:
32+ To allow for other client implementations, you can set the current client
33+ implementation
34+ following private methods to support serving dynamically registered source
35+ files or loading modules that have been installed by some other means:
3336 """
3437
35- __slots__ = "_module " , "_installed "
38+ __slots__ = "url " , "installed "
3639
3740 def __init__ (
3841 self ,
39- path : str ,
42+ url_or_name : str ,
4043 source_file : Optional [Union [str , Path ]] = None ,
4144 ) -> None :
42- self ._installed = False
45+ self .installed = False
4346 if source_file is not None :
44- self ._module = client .register_web_module (path , source_file )
45- self ._installed = True
46- elif client .web_module_exists (path ):
47- self ._module = client .web_module_url (path )
48- self ._installed = True
49- elif not _is_url (path ):
47+ self .url = client .current .register_web_module (url_or_name , source_file )
48+ self .installed = True
49+ elif client .current .web_module_exists (url_or_name ):
50+ self .url = client .current .web_module_url (url_or_name )
51+ self .installed = True
52+ elif _is_url (url_or_name ):
53+ self .url = url_or_name
54+ else :
5055 raise ValueError (
51- f"{ path !r} is not installed - "
56+ f"{ url_or_name !r} is not installed - "
5257 "only installed modules can omit a file extension."
5358 )
54- else :
55- self ._module = path
56-
57- @property
58- def installed (self ) -> bool :
59- """Whether or not this module has been installed into the built-in client app."""
60- return self ._installed
61-
62- @property
63- def url (self ) -> str :
64- """The path this module will be imported from"""
65- return self ._module
6659
6760 def Import (self , name : str , * args : Any , ** kwargs : Any ) -> "Import" :
6861 """Return an :class:`Import` for the given :class:`Module` and ``name``
@@ -76,10 +69,10 @@ def Import(self, name: str, *args: Any, **kwargs: Any) -> "Import":
7669 Where ``name`` is the given name, and ``module`` is the :attr:`Module.url` of
7770 this :class:`Module` instance.
7871 """
79- return Import (self ._module , name , * args , ** kwargs )
72+ return Import (self .url , name , * args , ** kwargs )
8073
8174 def __repr__ (self ) -> str : # pragma: no cover
82- return f"{ type (self ).__name__ } ({ self ._module !r} )"
75+ return f"{ type (self ).__name__ } ({ self .url !r} )"
8376
8477
8578class Import :
0 commit comments