Improve parameter names and docstrings on reitit frontend functions

This commit is contained in:
Juho Teperi 2021-09-07 14:38:42 +03:00
parent 8694d312f8
commit ac68f0f726
2 changed files with 106 additions and 49 deletions

View file

@ -10,7 +10,8 @@
;; Differences: ;; Differences:
;; This one automatically removes previous event listeners. ;; This one automatically removes previous event listeners.
(defn start! (defn ^{:see-also ["reitit.frontend.history/start!"]}
start!
"This registers event listeners on HTML5 history and hashchange events. "This registers event listeners on HTML5 history and hashchange events.
Automatically removes previous event listeners so it is safe to call this repeatedly, for example when using Automatically removes previous event listeners so it is safe to call this repeatedly, for example when using
@ -39,28 +40,57 @@
(on-navigate m this)) (on-navigate m this))
opts)) opts))
(defn href (defn
([k] ^{:see-also ["reitit.frontend.history/href"]}
(rfh/href @history k nil nil)) href
([k params] "Generate link href for the given route and parameters
(rfh/href @history k params nil))
([k params query]
(rfh/href @history k params query)))
(defn push-state Note: currently collections in query-parameters are encoded as field-value
"Sets the new route, leaving previous route in history." pairs separated by &, i.e. \"?a=1&a=2\", if you want to encode them
([k] differently, convert the collections to strings first."
(rfh/push-state @history k nil nil)) ([name]
([k params] (rfh/href @history name nil nil))
(rfh/push-state @history k params nil)) ([name path-params]
([k params query] (rfh/href @history name path-params nil))
(rfh/push-state @history k params query))) ([name path-params query-params]
(rfh/href @history name path-params query-params)))
(defn replace-state (defn
"Replaces current route. I.e. current route is not left on history." ^{:see-also ["reitit.frontend.history/push-state"]}
([k] push-state
(rfh/replace-state @history k nil nil)) "Creates url using the given route and parameters, pushes those to
([k params] history stack with pushState and triggers on-navigate callback on the
(rfh/replace-state @history k params nil)) history handler.
([k params query]
(rfh/replace-state @history k params query))) Note: currently collections in query-parameters are encoded as field-value
pairs separated by &, i.e. \"?a=1&a=2\", if you want to encode them
differently, convert the collections to strings first.
See also:
https://developer.mozilla.org/en-US/docs/Web/API/History/pushState"
([name]
(rfh/push-state @history name nil nil))
([name path-params]
(rfh/push-state @history name path-params nil))
([name path-params query-params]
(rfh/push-state @history name path-params query-params)))
(defn
^{:see-also ["reitit.frontend.history/replace-state"]}
replace-state
"Creates url using the given route and parameters, replaces latest entry on
history stack with replaceState and triggers on-navigate callback on the
history handler.
Note: currently collections in query-parameters are encoded as field-value
pairs separated by &, i.e. \"?a=1&a=2\", if you want to encode them
differently, convert the collections to strings first.
See also:
https://developer.mozilla.org/en-US/docs/Web/API/History/replaceState"
([name]
(rfh/replace-state @history name nil nil))
([name path-params]
(rfh/replace-state @history name path-params nil))
([name path-params query-params]
(rfh/replace-state @history name path-params query-params)))

View file

@ -171,40 +171,67 @@
(map->FragmentHistory opts) (map->FragmentHistory opts)
(map->Html5History opts)))))) (map->Html5History opts))))))
(defn stop! [history] (defn stop!
"Stops the given history handler, removing the event handlers."
[history]
(if history (if history
(-stop history))) (-stop history)))
(defn href (defn href
([history k] "Generate link href for the given route and parameters
(href history k nil))
([history k params]
(href history k params nil))
([history k params query]
(let [match (rf/match-by-name! (:router history) k params)]
(-href history (reitit/match->path match query)))))
(defn push-state Note: currently collections in query-parameters are encoded as field-value
"Sets the new route, leaving previous route in history." pairs separated by &, i.e. \"?a=1&a=2\", if you want to encode them
([history k] differently, convert the collections to strings first."
(push-state history k nil nil)) ([history name]
([history k params] (href history name nil))
(push-state history k params nil)) ([history name path-params]
([history k params query] (href history name path-params nil))
(let [match (rf/match-by-name! (:router history) k params) ([history name path-params query-params]
path (reitit/match->path match query)] (let [match (rf/match-by-name! (:router history) name path-params)]
(-href history (reitit/match->path match query-params)))))
(defn
^{:see-also ["reitit.core/match->path"]}
push-state
"Creates url using the given route and parameters, pushes those to
history stack with pushState and triggers on-navigate callback on the
history handler.
Note: currently collections in query-parameters are encoded as field-value
pairs separated by &, i.e. \"?a=1&a=2\", if you want to encode them
differently, convert the collections to strings first.
See also:
https://developer.mozilla.org/en-US/docs/Web/API/History/pushState"
([history name]
(push-state history name nil nil))
([history name path-params]
(push-state history name path-params nil))
([history name path-params query-params]
(let [match (rf/match-by-name! (:router history) name path-params)
path (reitit/match->path match query-params)]
;; pushState and replaceState don't trigger popstate event so call on-navigate manually ;; pushState and replaceState don't trigger popstate event so call on-navigate manually
(.pushState js/window.history nil "" (-href history path)) (.pushState js/window.history nil "" (-href history path))
(-on-navigate history path)))) (-on-navigate history path))))
(defn replace-state (defn replace-state
"Replaces current route. I.e. current route is not left on history." "Creates url using the given route and parameters, replaces latest entry on
([history k] history stack with replaceState and triggers on-navigate callback on the
(replace-state history k nil nil)) history handler.
([history k params]
(replace-state history k params nil)) Note: currently collections in query-parameters are encoded as field-value
([history k params query] pairs separated by &, i.e. \"?a=1&a=2\", if you want to encode them
(let [match (rf/match-by-name! (:router history) k params) differently, convert the collections to strings first.
path (reitit/match->path match query)]
See also:
https://developer.mozilla.org/en-US/docs/Web/API/History/replaceState"
([history name]
(replace-state history name nil nil))
([history name path-params]
(replace-state history name path-params nil))
([history name path-params query-params]
(let [match (rf/match-by-name! (:router history) name path-params)
path (reitit/match->path match query-params)]
(.replaceState js/window.history nil "" (-href history path)) (.replaceState js/window.history nil "" (-href history path))
(-on-navigate history path)))) (-on-navigate history path))))