You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RouteRegistry.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* @flow */
  2. import { Component } from 'react';
  3. /**
  4. * Object describing application route.
  5. *
  6. * @typedef {Object} Route
  7. * @property {Component} component - React Component constructor.
  8. * @property {string} path - URL route, required for web routing.
  9. */
  10. type Route = {
  11. component: Class<Component<*>>, // eslint-disable-line no-undef
  12. path: string
  13. };
  14. /**
  15. * A registry for Navigator routes, allowing features to register themselves
  16. * without needing to create additional inter-feature dependencies.
  17. */
  18. class RouteRegistry {
  19. _elements: Array<Route>;
  20. /**
  21. * Initializes a new RouteRegistry instance.
  22. */
  23. constructor() {
  24. /**
  25. * The set of registered routes.
  26. *
  27. * @private
  28. * @type {Route[]}
  29. */
  30. this._elements = [];
  31. }
  32. /**
  33. * Determines whether two specific Routes are equal i.e. they describe one
  34. * and the same abstract route.
  35. *
  36. * @param {Object} a - The Route to compare to b.
  37. * @param {Object} b - The Route to compare to a.
  38. * @returns {boolean} True if the specified a and b describe one and the
  39. * same abstract route; otherwise, false.
  40. */
  41. areRoutesEqual(a: Route, b: Route) {
  42. if (a === b) { // reflexive
  43. return true;
  44. }
  45. if (!a) {
  46. return !b;
  47. }
  48. if (!b) {
  49. return !a;
  50. }
  51. const aKeys = Object.keys(a);
  52. const bKeys = Object.keys(b);
  53. return (
  54. aKeys.length === bKeys.length /* symmetric */
  55. && aKeys.every(key => a[key] === b[key]));
  56. }
  57. /**
  58. * Returns all registered routes.
  59. *
  60. * @returns {Route[]}
  61. */
  62. getRoutes() {
  63. // We use the destructuring operator to 'clone' the route object to
  64. // prevent modifications from outside (e.g. React Native's Navigator
  65. // extends it with additional properties).
  66. return this._elements.map(r => {
  67. return { ...r };
  68. });
  69. }
  70. /* eslint-disable no-undef */
  71. /**
  72. * Returns registered route by name if any.
  73. *
  74. * @param {Component} component - The React Component (class) of the route
  75. * to retrieve.
  76. * @returns {Route|null}
  77. */
  78. getRouteByComponent(component: Class<Component<*>>) {
  79. /* eslint-enable no-undef */
  80. const route = this._elements.find(r => r.component === component);
  81. // We use destructuring operator to 'clone' route object to prevent
  82. // modifications from outside (e.g. React Native's Navigator extends
  83. // it with some additional properties).
  84. return route ? { ...route } : null;
  85. }
  86. /**
  87. * Adds a route to this registry.
  88. *
  89. * @param {Route} route - Route definition object.
  90. * @returns {void}
  91. */
  92. register(route: Route) {
  93. if (this._elements.includes(route)) {
  94. throw new Error(
  95. `Route ${String(route.component)} is registered already!`);
  96. }
  97. this._elements.push(route);
  98. }
  99. }
  100. /**
  101. * The public singleton instance of the RouteRegistry class.
  102. */
  103. export default new RouteRegistry();