ComponentDescriptorProviderRegistry.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #include "ComponentDescriptorProviderRegistry.h"
  8. namespace facebook {
  9. namespace react {
  10. void ComponentDescriptorProviderRegistry::add(
  11. ComponentDescriptorProvider provider) const {
  12. std::unique_lock<better::shared_mutex> lock(mutex_);
  13. /*
  14. // TODO: T57583139
  15. The assert is temporarily disabled to reduce the volume of the signal.
  16. assert(
  17. componentDescriptorProviders_.find(provider.handle) ==
  18. componentDescriptorProviders_.end() &&
  19. "Attempt to register an already registered ComponentDescriptorProvider.");
  20. */
  21. if (componentDescriptorProviders_.find(provider.handle) !=
  22. componentDescriptorProviders_.end()) {
  23. // Re-registering a provider makes no sense because it's copyable: already
  24. // registered one is as good as any new can be.
  25. return;
  26. }
  27. componentDescriptorProviders_.insert({provider.handle, provider});
  28. for (auto const &weakRegistry : componentDescriptorRegistries_) {
  29. auto registry = weakRegistry.lock();
  30. if (!registry) {
  31. continue;
  32. }
  33. registry->add(provider);
  34. }
  35. }
  36. void ComponentDescriptorProviderRegistry::setComponentDescriptorProviderRequest(
  37. ComponentDescriptorProviderRequest componentDescriptorProviderRequest)
  38. const {
  39. std::shared_lock<better::shared_mutex> lock(mutex_);
  40. componentDescriptorProviderRequest_ = componentDescriptorProviderRequest;
  41. }
  42. void ComponentDescriptorProviderRegistry::request(
  43. ComponentName componentName) const {
  44. ComponentDescriptorProviderRequest componentDescriptorProviderRequest;
  45. {
  46. std::shared_lock<better::shared_mutex> lock(mutex_);
  47. componentDescriptorProviderRequest = componentDescriptorProviderRequest_;
  48. }
  49. if (componentDescriptorProviderRequest) {
  50. componentDescriptorProviderRequest(componentName);
  51. }
  52. }
  53. ComponentDescriptorRegistry::Shared
  54. ComponentDescriptorProviderRegistry::createComponentDescriptorRegistry(
  55. ComponentDescriptorParameters const &parameters) const {
  56. std::shared_lock<better::shared_mutex> lock(mutex_);
  57. auto registry =
  58. std::make_shared<ComponentDescriptorRegistry const>(parameters, *this);
  59. for (auto const &pair : componentDescriptorProviders_) {
  60. registry->add(pair.second);
  61. }
  62. componentDescriptorRegistries_.push_back(registry);
  63. return registry;
  64. }
  65. } // namespace react
  66. } // namespace facebook