ComponentDescriptorProviderRegistry.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 <better/mutex.h>
  9. #include <react/core/ComponentDescriptor.h>
  10. #include <react/uimanager/ComponentDescriptorProvider.h>
  11. #include <react/uimanager/ComponentDescriptorRegistry.h>
  12. namespace facebook {
  13. namespace react {
  14. using ComponentDescriptorProviderRequest =
  15. std::function<void(ComponentName componentName)>;
  16. /*
  17. * Registry of `ComponentDescriptorProvider`s (and managed
  18. * `ComponentDescriptorRegistry`s). The class maintains a list of
  19. * `ComponentDescriptorRegistry`s (retaining pointers weakly) and update them
  20. * accordingly to changes in the provider registry.
  21. */
  22. class ComponentDescriptorProviderRegistry final {
  23. public:
  24. /*
  25. * Adds a `ComponentDescriptorProvider`s and update the managed
  26. * `ComponentDescriptorRegistry`s accordingly.
  27. * The methods can be called on any thread.
  28. */
  29. void add(ComponentDescriptorProvider provider) const;
  30. /*
  31. * ComponenDescriptorRegistry will call the `request` in case if a component
  32. * with given name wasn't registered yet.
  33. * The request handler must register a ComponentDescripor with requested name
  34. * synchronously during handling the request.
  35. * The request can be called on any thread.
  36. * The methods can be called on any thread.
  37. */
  38. void setComponentDescriptorProviderRequest(
  39. ComponentDescriptorProviderRequest request) const;
  40. /*
  41. * Creates managed `ComponentDescriptorRegistry` based on a stored list of
  42. * `ComponentDescriptorProvider`s and given `ComponentDescriptorParameters`.
  43. * The methods can be called on any thread.
  44. */
  45. ComponentDescriptorRegistry::Shared createComponentDescriptorRegistry(
  46. ComponentDescriptorParameters const &parameters) const;
  47. private:
  48. friend class ComponentDescriptorRegistry;
  49. void request(ComponentName componentName) const;
  50. mutable better::shared_mutex mutex_;
  51. mutable std::vector<std::weak_ptr<ComponentDescriptorRegistry const>>
  52. componentDescriptorRegistries_;
  53. mutable better::map<ComponentHandle, ComponentDescriptorProvider const>
  54. componentDescriptorProviders_;
  55. mutable ComponentDescriptorProviderRequest
  56. componentDescriptorProviderRequest_;
  57. };
  58. } // namespace react
  59. } // namespace facebook