isomorfeus-preact 10.9.0 → 22.9.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (266) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +14 -3
  3. data/README.md +23 -39
  4. data/lib/browser/delegate_native.rb +94 -72
  5. data/lib/browser/document.rb +38 -0
  6. data/lib/browser/element.rb +160 -176
  7. data/lib/browser/event.rb +99 -94
  8. data/lib/browser/history.rb +40 -20
  9. data/lib/browser/location.rb +59 -0
  10. data/lib/browser/window.rb +58 -0
  11. data/lib/data_uri/open_uri.rb +18 -0
  12. data/lib/data_uri/uri.rb +61 -0
  13. data/lib/data_uri.rb +4 -0
  14. data/lib/isomorfeus/preact/config.rb +42 -54
  15. data/lib/isomorfeus/preact/imports.rb +5 -20
  16. data/lib/isomorfeus/preact/ssr.rb +34 -0
  17. data/lib/isomorfeus/preact/thread_local_component_cache.rb +9 -11
  18. data/lib/isomorfeus/preact/version.rb +5 -0
  19. data/lib/isomorfeus/preact/view_helper.rb +72 -0
  20. data/lib/isomorfeus/props/validator.rb +19 -11
  21. data/lib/isomorfeus/top_level.rb +55 -33
  22. data/lib/isomorfeus-preact.rb +78 -103
  23. data/lib/link.rb +52 -0
  24. data/lib/lucid_app.rb +117 -0
  25. data/lib/lucid_component.rb +154 -0
  26. data/lib/nano_css.rb +282 -465
  27. data/lib/preact/component.rb +222 -0
  28. data/lib/preact/component_resolution.rb +49 -83
  29. data/lib/preact/context.rb +68 -0
  30. data/lib/preact/elements.rb +2 -9
  31. data/lib/preact/module_component_resolution.rb +50 -0
  32. data/lib/preact/prop_declaration_mixin.rb +73 -0
  33. data/lib/preact.rb +1548 -257
  34. data/lib/redirect.rb +34 -0
  35. data/lib/route.rb +15 -0
  36. data/lib/router.rb +145 -0
  37. data/lib/switch.rb +70 -0
  38. data/opal/iso_uri.rb +29 -0
  39. data/opal/uri/common.rb +18 -0
  40. data/opal/uri/generic.rb +47 -0
  41. metadata +45 -309
  42. data/lib/isomorfeus/preact/memcached_component_cache.rb +0 -19
  43. data/lib/isomorfeus/preact/redis_component_cache.rb +0 -19
  44. data/lib/isomorfeus/preact/ssr/history.rb +0 -23
  45. data/lib/isomorfeus/preact/ssr/render_core.rb +0 -117
  46. data/lib/isomorfeus/preact/ssr/top_level.rb +0 -37
  47. data/lib/isomorfeus/preact_view_helper.rb +0 -129
  48. data/lib/isomorfeus_preact/lucid_app/api.rb +0 -38
  49. data/lib/isomorfeus_preact/lucid_app/base.rb +0 -7
  50. data/lib/isomorfeus_preact/lucid_app/mixin.rb +0 -14
  51. data/lib/isomorfeus_preact/lucid_app/native_component_constructor.rb +0 -101
  52. data/lib/isomorfeus_preact/lucid_component/api.rb +0 -123
  53. data/lib/isomorfeus_preact/lucid_component/app_store_proxy.rb +0 -32
  54. data/lib/isomorfeus_preact/lucid_component/base.rb +0 -7
  55. data/lib/isomorfeus_preact/lucid_component/class_store_proxy.rb +0 -37
  56. data/lib/isomorfeus_preact/lucid_component/initializer.rb +0 -9
  57. data/lib/isomorfeus_preact/lucid_component/mixin.rb +0 -13
  58. data/lib/isomorfeus_preact/lucid_component/native_component_constructor.rb +0 -87
  59. data/lib/isomorfeus_preact/lucid_component/styles_wrapper.rb +0 -40
  60. data/lib/isomorfeus_preact/lucid_func/base.rb +0 -7
  61. data/lib/isomorfeus_preact/lucid_func/initializer.rb +0 -8
  62. data/lib/isomorfeus_preact/lucid_func/mixin.rb +0 -10
  63. data/lib/isomorfeus_preact/lucid_func/native_component_constructor.rb +0 -56
  64. data/lib/isomorfeus_preact/preact/function_component/api.rb +0 -164
  65. data/lib/isomorfeus_preact/preact/function_component/base.rb +0 -7
  66. data/lib/isomorfeus_preact/preact/function_component/initializer.rb +0 -6
  67. data/lib/isomorfeus_preact/preact/function_component/mixin.rb +0 -8
  68. data/lib/isomorfeus_preact/preact/function_component/native_component_constructor.rb +0 -47
  69. data/lib/lucid_app/context.rb +0 -24
  70. data/lib/lucid_prop_declaration/mixin.rb +0 -126
  71. data/lib/preact/component/api.rb +0 -137
  72. data/lib/preact/component/base.rb +0 -7
  73. data/lib/preact/component/callbacks.rb +0 -111
  74. data/lib/preact/component/initializer.rb +0 -7
  75. data/lib/preact/component/mixin.rb +0 -11
  76. data/lib/preact/component/native_component_constructor.rb +0 -77
  77. data/lib/preact/context_wrapper.rb +0 -48
  78. data/lib/preact/native_constant_wrapper.rb +0 -29
  79. data/lib/preact/option_hooks.rb +0 -98
  80. data/lib/preact/params.rb +0 -16
  81. data/lib/preact/props.rb +0 -69
  82. data/lib/preact/ref.rb +0 -17
  83. data/lib/preact/state.rb +0 -87
  84. data/lib/preact/version.rb +0 -3
  85. data/node_modules/.package-lock.json +0 -38
  86. data/node_modules/preact/LICENSE +0 -21
  87. data/node_modules/preact/README.md +0 -188
  88. data/node_modules/preact/compat/LICENSE +0 -21
  89. data/node_modules/preact/compat/client.js +0 -19
  90. data/node_modules/preact/compat/client.mjs +0 -17
  91. data/node_modules/preact/compat/dist/compat.js +0 -2
  92. data/node_modules/preact/compat/dist/compat.js.map +0 -1
  93. data/node_modules/preact/compat/dist/compat.mjs +0 -2
  94. data/node_modules/preact/compat/dist/compat.module.js +0 -2
  95. data/node_modules/preact/compat/dist/compat.module.js.map +0 -1
  96. data/node_modules/preact/compat/dist/compat.umd.js +0 -2
  97. data/node_modules/preact/compat/dist/compat.umd.js.map +0 -1
  98. data/node_modules/preact/compat/jsx-dev-runtime.js +0 -3
  99. data/node_modules/preact/compat/jsx-dev-runtime.mjs +0 -3
  100. data/node_modules/preact/compat/jsx-runtime.js +0 -3
  101. data/node_modules/preact/compat/jsx-runtime.mjs +0 -3
  102. data/node_modules/preact/compat/package.json +0 -49
  103. data/node_modules/preact/compat/scheduler.js +0 -15
  104. data/node_modules/preact/compat/scheduler.mjs +0 -23
  105. data/node_modules/preact/compat/server.browser.js +0 -4
  106. data/node_modules/preact/compat/server.js +0 -15
  107. data/node_modules/preact/compat/server.mjs +0 -4
  108. data/node_modules/preact/compat/src/Children.js +0 -21
  109. data/node_modules/preact/compat/src/PureComponent.js +0 -15
  110. data/node_modules/preact/compat/src/forwardRef.js +0 -44
  111. data/node_modules/preact/compat/src/index.d.ts +0 -164
  112. data/node_modules/preact/compat/src/index.js +0 -223
  113. data/node_modules/preact/compat/src/internal.d.ts +0 -47
  114. data/node_modules/preact/compat/src/memo.js +0 -34
  115. data/node_modules/preact/compat/src/portals.js +0 -82
  116. data/node_modules/preact/compat/src/render.js +0 -238
  117. data/node_modules/preact/compat/src/suspense-list.d.ts +0 -14
  118. data/node_modules/preact/compat/src/suspense-list.js +0 -126
  119. data/node_modules/preact/compat/src/suspense.d.ts +0 -15
  120. data/node_modules/preact/compat/src/suspense.js +0 -270
  121. data/node_modules/preact/compat/src/util.js +0 -28
  122. data/node_modules/preact/compat/test-utils.js +0 -1
  123. data/node_modules/preact/debug/LICENSE +0 -21
  124. data/node_modules/preact/debug/dist/debug.js +0 -2
  125. data/node_modules/preact/debug/dist/debug.js.map +0 -1
  126. data/node_modules/preact/debug/dist/debug.mjs +0 -2
  127. data/node_modules/preact/debug/dist/debug.module.js +0 -2
  128. data/node_modules/preact/debug/dist/debug.module.js.map +0 -1
  129. data/node_modules/preact/debug/dist/debug.umd.js +0 -2
  130. data/node_modules/preact/debug/dist/debug.umd.js.map +0 -1
  131. data/node_modules/preact/debug/package.json +0 -26
  132. data/node_modules/preact/debug/src/check-props.js +0 -54
  133. data/node_modules/preact/debug/src/component-stack.js +0 -146
  134. data/node_modules/preact/debug/src/constants.js +0 -3
  135. data/node_modules/preact/debug/src/debug.js +0 -437
  136. data/node_modules/preact/debug/src/index.js +0 -6
  137. data/node_modules/preact/debug/src/internal.d.ts +0 -82
  138. data/node_modules/preact/debug/src/util.js +0 -11
  139. data/node_modules/preact/devtools/LICENSE +0 -21
  140. data/node_modules/preact/devtools/dist/devtools.js +0 -2
  141. data/node_modules/preact/devtools/dist/devtools.js.map +0 -1
  142. data/node_modules/preact/devtools/dist/devtools.mjs +0 -2
  143. data/node_modules/preact/devtools/dist/devtools.module.js +0 -2
  144. data/node_modules/preact/devtools/dist/devtools.module.js.map +0 -1
  145. data/node_modules/preact/devtools/dist/devtools.umd.js +0 -2
  146. data/node_modules/preact/devtools/dist/devtools.umd.js.map +0 -1
  147. data/node_modules/preact/devtools/package.json +0 -25
  148. data/node_modules/preact/devtools/src/devtools.js +0 -10
  149. data/node_modules/preact/devtools/src/index.d.ts +0 -8
  150. data/node_modules/preact/devtools/src/index.js +0 -15
  151. data/node_modules/preact/dist/preact.js +0 -2
  152. data/node_modules/preact/dist/preact.js.map +0 -1
  153. data/node_modules/preact/dist/preact.min.js +0 -2
  154. data/node_modules/preact/dist/preact.min.js.map +0 -1
  155. data/node_modules/preact/dist/preact.mjs +0 -2
  156. data/node_modules/preact/dist/preact.module.js +0 -2
  157. data/node_modules/preact/dist/preact.module.js.map +0 -1
  158. data/node_modules/preact/dist/preact.umd.js +0 -2
  159. data/node_modules/preact/dist/preact.umd.js.map +0 -1
  160. data/node_modules/preact/hooks/LICENSE +0 -21
  161. data/node_modules/preact/hooks/dist/hooks.js +0 -2
  162. data/node_modules/preact/hooks/dist/hooks.js.map +0 -1
  163. data/node_modules/preact/hooks/dist/hooks.mjs +0 -2
  164. data/node_modules/preact/hooks/dist/hooks.module.js +0 -2
  165. data/node_modules/preact/hooks/dist/hooks.module.js.map +0 -1
  166. data/node_modules/preact/hooks/dist/hooks.umd.js +0 -2
  167. data/node_modules/preact/hooks/dist/hooks.umd.js.map +0 -1
  168. data/node_modules/preact/hooks/package.json +0 -35
  169. data/node_modules/preact/hooks/src/index.d.ts +0 -139
  170. data/node_modules/preact/hooks/src/index.js +0 -417
  171. data/node_modules/preact/hooks/src/internal.d.ts +0 -78
  172. data/node_modules/preact/jsx-runtime/LICENSE +0 -21
  173. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.js +0 -2
  174. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.js.map +0 -1
  175. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.mjs +0 -2
  176. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js +0 -2
  177. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js.map +0 -1
  178. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.umd.js +0 -2
  179. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.umd.js.map +0 -1
  180. data/node_modules/preact/jsx-runtime/package.json +0 -28
  181. data/node_modules/preact/jsx-runtime/src/index.d.ts +0 -50
  182. data/node_modules/preact/jsx-runtime/src/index.js +0 -77
  183. data/node_modules/preact/package.json +0 -304
  184. data/node_modules/preact/src/cjs.js +0 -3
  185. data/node_modules/preact/src/clone-element.js +0 -34
  186. data/node_modules/preact/src/component.js +0 -225
  187. data/node_modules/preact/src/constants.js +0 -3
  188. data/node_modules/preact/src/create-context.js +0 -68
  189. data/node_modules/preact/src/create-element.js +0 -98
  190. data/node_modules/preact/src/diff/catch-error.js +0 -40
  191. data/node_modules/preact/src/diff/children.js +0 -335
  192. data/node_modules/preact/src/diff/index.js +0 -533
  193. data/node_modules/preact/src/diff/props.js +0 -158
  194. data/node_modules/preact/src/index.d.ts +0 -317
  195. data/node_modules/preact/src/index.js +0 -13
  196. data/node_modules/preact/src/internal.d.ts +0 -155
  197. data/node_modules/preact/src/jsx.d.ts +0 -1014
  198. data/node_modules/preact/src/options.js +0 -16
  199. data/node_modules/preact/src/render.js +0 -75
  200. data/node_modules/preact/src/util.js +0 -27
  201. data/node_modules/preact/test-utils/dist/testUtils.js +0 -2
  202. data/node_modules/preact/test-utils/dist/testUtils.js.map +0 -1
  203. data/node_modules/preact/test-utils/dist/testUtils.mjs +0 -2
  204. data/node_modules/preact/test-utils/dist/testUtils.module.js +0 -2
  205. data/node_modules/preact/test-utils/dist/testUtils.module.js.map +0 -1
  206. data/node_modules/preact/test-utils/dist/testUtils.umd.js +0 -2
  207. data/node_modules/preact/test-utils/dist/testUtils.umd.js.map +0 -1
  208. data/node_modules/preact/test-utils/package.json +0 -28
  209. data/node_modules/preact/test-utils/src/index.d.ts +0 -3
  210. data/node_modules/preact/test-utils/src/index.js +0 -118
  211. data/node_modules/preact-render-to-string/LICENSE +0 -21
  212. data/node_modules/preact-render-to-string/README.md +0 -102
  213. data/node_modules/preact-render-to-string/dist/commonjs.js +0 -2
  214. data/node_modules/preact-render-to-string/dist/commonjs.js.map +0 -1
  215. data/node_modules/preact-render-to-string/dist/index.d.ts +0 -16
  216. data/node_modules/preact-render-to-string/dist/index.js +0 -1
  217. data/node_modules/preact-render-to-string/dist/index.js.map +0 -1
  218. data/node_modules/preact-render-to-string/dist/index.mjs +0 -2
  219. data/node_modules/preact-render-to-string/dist/index.module.js +0 -2
  220. data/node_modules/preact-render-to-string/dist/index.module.js.map +0 -1
  221. data/node_modules/preact-render-to-string/dist/jsx-entry.js +0 -2
  222. data/node_modules/preact-render-to-string/dist/jsx-entry.js.map +0 -1
  223. data/node_modules/preact-render-to-string/dist/jsx.d.ts +0 -13
  224. data/node_modules/preact-render-to-string/dist/jsx.js +0 -1
  225. data/node_modules/preact-render-to-string/dist/jsx.js.map +0 -1
  226. data/node_modules/preact-render-to-string/dist/jsx.mjs +0 -2
  227. data/node_modules/preact-render-to-string/dist/jsx.modern.js +0 -2
  228. data/node_modules/preact-render-to-string/dist/jsx.modern.js.map +0 -1
  229. data/node_modules/preact-render-to-string/dist/jsx.module.js +0 -2
  230. data/node_modules/preact-render-to-string/dist/jsx.module.js.map +0 -1
  231. data/node_modules/preact-render-to-string/dist/preact-render-to-string-tests.d.ts +0 -1
  232. data/node_modules/preact-render-to-string/jsx.js +0 -1
  233. data/node_modules/preact-render-to-string/package.json +0 -142
  234. data/node_modules/preact-render-to-string/src/index.d.ts +0 -16
  235. data/node_modules/preact-render-to-string/src/index.js +0 -462
  236. data/node_modules/preact-render-to-string/src/jsx.d.ts +0 -13
  237. data/node_modules/preact-render-to-string/src/jsx.js +0 -76
  238. data/node_modules/preact-render-to-string/src/polyfills.js +0 -8
  239. data/node_modules/preact-render-to-string/src/preact-render-to-string-tests.d.ts +0 -1
  240. data/node_modules/preact-render-to-string/src/util.js +0 -78
  241. data/node_modules/preact-render-to-string/typings.json +0 -5
  242. data/node_modules/pretty-format/.npmignore +0 -3
  243. data/node_modules/pretty-format/LICENSE.md +0 -15
  244. data/node_modules/pretty-format/README.md +0 -94
  245. data/node_modules/pretty-format/index.js +0 -343
  246. data/node_modules/pretty-format/package.json +0 -26
  247. data/node_modules/pretty-format/plugins/ReactElement.js +0 -74
  248. data/node_modules/pretty-format/plugins/ReactTestComponent.js +0 -58
  249. data/node_modules/pretty-format/printString.js +0 -7
  250. data/node_modules/wouter-preact/cjs/index.js +0 -180
  251. data/node_modules/wouter-preact/cjs/matcher.js +0 -72
  252. data/node_modules/wouter-preact/cjs/package.json +0 -1
  253. data/node_modules/wouter-preact/cjs/react-deps.js +0 -75
  254. data/node_modules/wouter-preact/cjs/static-location.js +0 -21
  255. data/node_modules/wouter-preact/cjs/use-location.js +0 -94
  256. data/node_modules/wouter-preact/index.d.ts +0 -110
  257. data/node_modules/wouter-preact/index.js +0 -178
  258. data/node_modules/wouter-preact/matcher.d.ts +0 -30
  259. data/node_modules/wouter-preact/matcher.js +0 -66
  260. data/node_modules/wouter-preact/package.json +0 -33
  261. data/node_modules/wouter-preact/react-deps.js +0 -15
  262. data/node_modules/wouter-preact/static-location.d.ts +0 -16
  263. data/node_modules/wouter-preact/static-location.js +0 -17
  264. data/node_modules/wouter-preact/use-location.d.ts +0 -43
  265. data/node_modules/wouter-preact/use-location.js +0 -86
  266. data/package.json +0 -8
data/lib/redirect.rb ADDED
@@ -0,0 +1,34 @@
1
+ class Redirect < Preact::Component
2
+ EVENT_PUSH_STATE = "pushState"
3
+ EVENT_REPLACE_STATE = "replaceState"
4
+
5
+ def initialize(props, context)
6
+ super(props, context)
7
+ if RUBY_ENGINE == 'opal'
8
+ @history = if `typeof window !== "undefined"`
9
+ `window.history`
10
+ else
11
+ `{ length: 1, state: null, scrollRestoration: "auto", pushState: function(e,t,n){}, replaceState: function(e,t,n){} }`
12
+ end
13
+ @base = ''
14
+ end
15
+ end
16
+
17
+ def navigate
18
+ if RUBY_ENGINE == 'opal'
19
+ to = props[:to] || props[:href]
20
+ m = props[:replace] ? EVENT_REPLACE_STATE : EVENT_PUSH_STATE
21
+ `#@history[m](null, "", to[0] === "~" ? to.slice(1) : #@base + to)`
22
+ context[:router][:callback].call if context[:router].key?(:callback)
23
+ end
24
+ end
25
+
26
+ render do
27
+ if RUBY_ENGINE == 'opal'
28
+ @base = context[:router][:base] if context[:router].key?(:base)
29
+ navigate
30
+ end
31
+ nil
32
+ end
33
+ end
34
+ Redirect.context_type = RouterContext
data/lib/route.rb ADDED
@@ -0,0 +1,15 @@
1
+ class Route < Preact::Component
2
+ render do
3
+ route_match = @context[:router][:matcher].call(props[:path], @context[:location])
4
+ matches, params = props[:match] || route_match
5
+ return unless matches
6
+ component = props[:component]
7
+ if component
8
+ component = component.constantize if component.is_a?(String)
9
+ Preact.create_element(component, { params: params })
10
+ else
11
+ props[:children]
12
+ end
13
+ end
14
+ end
15
+ Route.context_type = RouterContext
data/lib/router.rb ADDED
@@ -0,0 +1,145 @@
1
+ class Router < Preact::Component
2
+ EVENT_POP_STATE = "popstate"
3
+ EVENT_PUSH_STATE = "pushState"
4
+ EVENT_REPLACE_STATE = "replaceState"
5
+ EVENTS = [EVENT_POP_STATE, EVENT_PUSH_STATE, EVENT_REPLACE_STATE]
6
+
7
+ def initialize(props, context)
8
+ super(props, context)
9
+ @state = { update: 0 }
10
+ @router = nil
11
+ if RUBY_ENGINE == 'opal'
12
+ @browser = `typeof window !== "undefined"` ? true : false
13
+ rtr = self
14
+ @check_for_updates_n = `function() { #{rtr.call(:check_for_updates).call}; }`
15
+ else
16
+ @browser = false
17
+ end
18
+ @rbase = nil
19
+ @original_location = nil
20
+ @prev_hash = nil
21
+ end
22
+
23
+ def build_router(props, values)
24
+ { base: "",
25
+ matcher: make_matcher(),
26
+ original_location: "/",
27
+ callback: call(:check_for_updates)
28
+ }.merge(props, values)
29
+ end
30
+
31
+ def check_for_updates
32
+ pathname = @browser ? current_pathname(@rbase) : current_pathname(@rbase, @original_location)
33
+ search = @browser ? `location.search` : ""
34
+ hash = pathname + search;
35
+ if @prev_hash != hash
36
+ @prev_hash = hash
37
+ set_state({ update: state[:update] + 1 })
38
+ end
39
+ end
40
+
41
+ def current_pathname(base, path)
42
+ unless path
43
+ path = @browser ? `location.pathname` : ""
44
+ end
45
+ base = "" unless base
46
+ path.downcase.start_with?(base.downcase) ? (path[base.size..-1] || "/") : "~" + path
47
+ end
48
+
49
+ def escape_rx(str)
50
+ str.gsub(/([.+*?=^!:${}()\[\]\|\/\\])/, "\\1")
51
+ end
52
+
53
+ # returns a segment representation in Regexp based on flags
54
+ # adapted and simplified version from path-to-regexp sources
55
+ def rx_for_segment(repeat, optional, prefix)
56
+ capture = repeat ? "((?:[^\\/]+?)(?:\\/(?:[^\\/]+?))*)" : "([^\\/]+?)"
57
+ capture = "(?:\\/" + capture + ")" if (optional && prefix)
58
+ capture + (optional ? "?" : "")
59
+ end
60
+
61
+ def path_to_regexp(pattern)
62
+ group_rx = /:([A-Za-z0-9_]+)([?+*]?)/
63
+
64
+ pos = 0
65
+ keys = []
66
+ result = ""
67
+
68
+ while (match = group_rx.match(pattern, pos))
69
+ mod = match[2]
70
+ segment = match[1]
71
+
72
+ # :foo [1] ( )
73
+ # :foo? [0 - 1] ( o)
74
+ # :foo+ [1 - ∞] (r )
75
+ # :foo* [0 - ∞] (ro)
76
+ repeat = mod === "+" || mod === "*"
77
+ optional = mod === "?" || mod === "*"
78
+ prefix = optional && pattern[match.begin(0) - 1] == "/" ? 1 : 0
79
+
80
+ prev = pattern[pos...(match.begin(0) - prefix)]
81
+ keys.push({ name: segment.to_sym })
82
+ pos = match.end(0)
83
+ if RUBY_ENGINE == 'opal'
84
+ result += "#{escape_rx(prev)}#{rx_for_segment(repeat, optional, prefix)}"
85
+ else
86
+ result << "#{escape_rx(prev)}#{rx_for_segment(repeat, optional, prefix)}"
87
+ end
88
+ end
89
+
90
+ if RUBY_ENGINE == 'opal'
91
+ result += escape_rx(pattern[pos..-1])
92
+ else
93
+ result << escape_rx(pattern[pos..-1])
94
+ end
95
+ [ Regexp.new("^" + result + "(?:\\/)?$", Regexp::IGNORECASE), keys]
96
+ end
97
+
98
+ def get_regexp(cache, pattern)
99
+ # obtains a cached regexp version of the pattern
100
+ cache[pattern] || (cache[pattern] = path_to_regexp(pattern))
101
+ end
102
+
103
+ def make_matcher
104
+ cache = {}
105
+
106
+ proc do |pattern, path|
107
+ regexp, keys = get_regexp(cache, pattern || "")
108
+ out = regexp.match(path)
109
+ if !out
110
+ [false, nil]
111
+ else
112
+ # formats an object with matched params
113
+ i = 0
114
+ params = keys.reduce({}) do |p, key|
115
+ p[key[:name]] = out[i += 1]
116
+ p
117
+ end
118
+ [true, params]
119
+ end
120
+ end
121
+ end
122
+
123
+ render do
124
+ @router = build_router(props, { original_location: @context[:iso_location] }) if !@router
125
+ @rbase = @router[:base]
126
+ @original_location = @router[:original_location]
127
+ loc = @browser ? current_pathname(@rbase) : current_pathname(@rbase, @original_location)
128
+ @prev_hash = @browser ? loc + `location.search` : loc
129
+ Preact.create_element(RouterContext.Provider, { value: { router: @router, location: loc }, children: props[:children] })
130
+ end
131
+
132
+ component_did_mount do
133
+ EVENTS.each do |event|
134
+ `window`.JS.addEventListener(event, @check_for_updates_n)
135
+ end
136
+ check_for_updates
137
+ end
138
+
139
+ component_will_unmount do
140
+ EVENTS.each do |event|
141
+ `window`.JS.removeEventListener(event, @check_for_updates_n)
142
+ end
143
+ end
144
+ end
145
+ Router.context_type = LucidApplicationContext
data/lib/switch.rb ADDED
@@ -0,0 +1,70 @@
1
+ class Switch < Preact::Component
2
+ def initialize(props, context)
3
+ super(props, context)
4
+ if RUBY_ENGINE == 'opal'
5
+ @browser = `typeof window !== "undefined"` ? true : false
6
+ else
7
+ @browser = false
8
+ end
9
+ end
10
+
11
+ def flatten(children)
12
+ if children.is_a?(Array)
13
+ children.compact!
14
+ children.map! do |child|
15
+ if RUBY_ENGINE == 'opal'
16
+ if child.is_a?(Array)
17
+ flatten(child)
18
+ elsif `child.type === Opal.Fragment`
19
+ flatten(child.JS[:props][:children])
20
+ else
21
+ child
22
+ end
23
+ else
24
+ if child.is_a?(Array)
25
+ flatten(child)
26
+ elsif child.type == ::Fragment
27
+ flatten(child.JS[:props][:children])
28
+ else
29
+ child
30
+ end
31
+ end
32
+ end
33
+ else
34
+ [children]
35
+ end
36
+ end
37
+
38
+ render do
39
+ router = @context[:router]
40
+ base = router[:base]
41
+ matcher = router[:matcher]
42
+ originalLocation = router[:originalLocation]
43
+ children = flatten(props[:children])
44
+ location = props[:location]
45
+ child = nil
46
+ if RUBY_ENGINE == 'opal'
47
+ children.each do |vnode|
48
+ match = nil
49
+ # we don't require an element to be of type Route,
50
+ # but we do require it to contain a truthy `path` prop.
51
+ # this allows to use different components that wrap Route
52
+ # inside of a switch, for example <AnimatedRoute />.
53
+ if vnode.is_a?(VNode) && (match = (p = vnode.JS[:props][:path]) ? matcher.call(p, location || @context[:location]) : [true, {}])[0]
54
+ child = Preact.clone_element(vnode, { match: match })
55
+ break
56
+ end
57
+ end
58
+ else
59
+ children.each do |vnode|
60
+ match = nil
61
+ if vnode.is_a?(VNode) && (match = (p = vnode.props[:path]) ? matcher.call(p, location || @context[:location]) : [true, {}])[0]
62
+ child = Preact.clone_element(vnode, { match: match })
63
+ break
64
+ end
65
+ end
66
+ end
67
+ child
68
+ end
69
+ end
70
+ Switch.context_type = RouterContext
data/opal/iso_uri.rb ADDED
@@ -0,0 +1,29 @@
1
+ require "uri/common"
2
+ require "uri/generic"
3
+
4
+ module URI
5
+ @@schemes = {}
6
+
7
+ class InvalidURIError < Exception
8
+ end
9
+
10
+ def self.parse(url)
11
+ # scheme://conn_data/path?query#hash
12
+ match = url.match(%r[(\w+)://([^/]+)([^\?]+)(\?[^\#]+)?(\#.*)?])
13
+
14
+ _, scheme, connection_data, path, query, fragment = match.to_a
15
+
16
+ connection_data = connection_data.split(/@/)
17
+ userinfo = connection_data.size > 1 ? connection_data.first : nil
18
+ host, port = connection_data.last.split(/:/)
19
+
20
+ port = port.to_i
21
+ query = query[1..-1] if query
22
+
23
+ registry = opaque = nil
24
+
25
+ Generic.new(scheme, userinfo, host, port,
26
+ registry, path, opaque, query,
27
+ fragment)
28
+ end
29
+ end
@@ -0,0 +1,18 @@
1
+ module Kernel
2
+
3
+ #
4
+ # Returns +uri+ converted to a URI object.
5
+ #
6
+ def URI(uri)
7
+ if uri.is_a?(URI::Generic)
8
+ uri
9
+ elsif uri = String.try_convert(uri)
10
+ URI.parse(uri)
11
+ else
12
+ raise ArgumentError,
13
+ "bad argument (expected URI object or URI string)"
14
+ end
15
+ end
16
+ module_function :URI
17
+ end
18
+
@@ -0,0 +1,47 @@
1
+ module URI
2
+ class Generic
3
+ attr_reader :scheme, :userinfo, :user, :password, :host, :port, :path,
4
+ :query, :fragment, :opaque, :registry
5
+
6
+ #
7
+ # An Array of the available components for URI::Generic
8
+ #
9
+ COMPONENT = [
10
+ :scheme,
11
+ :userinfo, :host, :port, :registry,
12
+ :path, :opaque,
13
+ :query,
14
+ :fragment
15
+ ]
16
+
17
+ def initialize(scheme,
18
+ userinfo, host, port, registry,
19
+ path, opaque,
20
+ query,
21
+ fragment,
22
+ parser = nil,
23
+ arg_check = false)
24
+ @scheme = scheme
25
+ @userinfo = userinfo
26
+ @host = host
27
+ @port = port
28
+ @path = path
29
+ @query = query
30
+ @fragment = fragment
31
+ @user, @password = userinfo.split(/:/) if userinfo
32
+ end
33
+
34
+ def ==(other)
35
+ self.class == other.class &&
36
+ component_ary == other.component_ary
37
+ end
38
+
39
+ protected
40
+
41
+ def component_ary
42
+ self.class::COMPONENT.collect do |x|
43
+ self.send(x)
44
+ end
45
+ end
46
+ end
47
+ end