compiler.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. """Compiler variables."""
  2. from enum import Enum
  3. from types import SimpleNamespace
  4. from reflex.constants import Dirs
  5. from reflex.utils.imports import ImportVar
  6. # The prefix used to create setters for state vars.
  7. SETTER_PREFIX = "set_"
  8. # The file used to specify no compilation.
  9. NOCOMPILE_FILE = ".web/nocompile"
  10. class Ext(SimpleNamespace):
  11. """Extension used in Reflex."""
  12. # The extension for JS files.
  13. JS = ".js"
  14. # The extension for python files.
  15. PY = ".py"
  16. # The extension for css files.
  17. CSS = ".css"
  18. # The extension for zip files.
  19. ZIP = ".zip"
  20. class CompileVars(SimpleNamespace):
  21. """The variables used during compilation."""
  22. # The expected variable name where the rx.App is stored.
  23. APP = "app"
  24. # The expected variable name where the API object is stored for deployment.
  25. API = "api"
  26. # The name of the router variable.
  27. ROUTER = "router"
  28. # The name of the socket variable.
  29. SOCKET = "socket"
  30. # The name of the variable to hold API results.
  31. RESULT = "result"
  32. # The name of the final variable.
  33. FINAL = "final"
  34. # The name of the process variable.
  35. PROCESSING = "processing"
  36. # The name of the state variable.
  37. STATE = "state"
  38. # The name of the events variable.
  39. EVENTS = "events"
  40. # The name of the initial hydrate event.
  41. HYDRATE = "hydrate"
  42. # The name of the is_hydrated variable.
  43. IS_HYDRATED = "is_hydrated"
  44. # The name of the function to add events to the queue.
  45. ADD_EVENTS = "addEvents"
  46. # The name of the var storing any connection error.
  47. CONNECT_ERROR = "connectError"
  48. # The name of the function for converting a dict to an event.
  49. TO_EVENT = "Event"
  50. class PageNames(SimpleNamespace):
  51. """The name of basic pages deployed in NextJS."""
  52. # The name of the index page.
  53. INDEX_ROUTE = "index"
  54. # The name of the app root page.
  55. APP_ROOT = "_app"
  56. # The root stylesheet filename.
  57. STYLESHEET_ROOT = "styles"
  58. # The name of the document root page.
  59. DOCUMENT_ROOT = "_document"
  60. # The name of the theme page.
  61. THEME = "theme"
  62. class ComponentName(Enum):
  63. """Component names."""
  64. BACKEND = "Backend"
  65. FRONTEND = "Frontend"
  66. def zip(self):
  67. """Give the zip filename for the component.
  68. Returns:
  69. The lower-case filename with zip extension.
  70. """
  71. return self.value.lower() + Ext.ZIP
  72. class Imports(SimpleNamespace):
  73. """Common sets of import vars."""
  74. EVENTS = {
  75. "react": {ImportVar(tag="useContext")},
  76. f"/{Dirs.CONTEXTS_PATH}": {ImportVar(tag="EventLoopContext")},
  77. f"/{Dirs.STATE_PATH}": {ImportVar(tag=CompileVars.TO_EVENT)},
  78. }
  79. class Hooks(SimpleNamespace):
  80. """Common sets of hook declarations."""
  81. EVENTS = f"const [{CompileVars.ADD_EVENTS}, {CompileVars.CONNECT_ERROR}] = useContext(EventLoopContext);"