ComponentDescriptorRegistry.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #pragma once
  8. #include <memory>
  9. #include <better/map.h>
  10. #include <better/mutex.h>
  11. #include <react/core/ComponentDescriptor.h>
  12. #include <react/uimanager/ComponentDescriptorProvider.h>
  13. namespace facebook {
  14. namespace react {
  15. class ComponentDescriptorProviderRegistry;
  16. class ComponentDescriptorRegistry;
  17. using SharedComponentDescriptorRegistry =
  18. std::shared_ptr<const ComponentDescriptorRegistry>;
  19. /*
  20. * Registry of particular `ComponentDescriptor`s.
  21. */
  22. class ComponentDescriptorRegistry {
  23. public:
  24. using Shared = std::shared_ptr<const ComponentDescriptorRegistry>;
  25. /*
  26. * Creates an object with stored `ComponentDescriptorParameters` which will
  27. * be used later to create `ComponentDescriptor`s.
  28. */
  29. ComponentDescriptorRegistry(
  30. ComponentDescriptorParameters const &parameters,
  31. ComponentDescriptorProviderRegistry const &providerRegistry);
  32. /*
  33. * This is broken. Please do not use.
  34. * If you requesting a ComponentDescriptor and unsure that it's there, you are
  35. * doing something wrong.
  36. */
  37. ComponentDescriptor const *
  38. findComponentDescriptorByHandle_DO_NOT_USE_THIS_IS_BROKEN(
  39. ComponentHandle componentHandle) const;
  40. ComponentDescriptor const &at(std::string const &componentName) const;
  41. ComponentDescriptor const &at(ComponentHandle componentHandle) const;
  42. ShadowNode::Shared createNode(
  43. Tag tag,
  44. std::string const &viewName,
  45. SurfaceId surfaceId,
  46. folly::dynamic const &props,
  47. SharedEventTarget const &eventTarget) const;
  48. void setFallbackComponentDescriptor(SharedComponentDescriptor descriptor);
  49. ComponentDescriptor::Shared getFallbackComponentDescriptor() const;
  50. private:
  51. friend class ComponentDescriptorProviderRegistry;
  52. void registerComponentDescriptor(
  53. SharedComponentDescriptor componentDescriptor) const;
  54. /*
  55. * Creates a `ComponentDescriptor` using specified
  56. * `ComponentDescriptorProvider` and stored `ComponentDescriptorParameters`,
  57. * and then adds that to the registry.
  58. * To be used by `ComponentDescriptorProviderRegistry` only.
  59. * Thread safe.
  60. */
  61. void add(ComponentDescriptorProvider componentDescriptorProvider) const;
  62. mutable better::shared_mutex mutex_;
  63. mutable better::map<ComponentHandle, SharedComponentDescriptor>
  64. _registryByHandle;
  65. mutable better::map<std::string, SharedComponentDescriptor> _registryByName;
  66. ComponentDescriptor::Shared _fallbackComponentDescriptor;
  67. ComponentDescriptorParameters parameters_{};
  68. ComponentDescriptorProviderRegistry const &providerRegistry_;
  69. };
  70. } // namespace react
  71. } // namespace facebook