GULCCComponent.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <Foundation/Foundation.h>
  17. @class GULCCComponentContainer;
  18. NS_ASSUME_NONNULL_BEGIN
  19. /// Provides a system to clean up cached instances returned from the component system.
  20. NS_SWIFT_NAME(ComponentLifecycleMaintainer)
  21. @protocol GULCCComponentLifecycleMaintainer
  22. /// Clean up any resources as they are about to be deallocated.
  23. - (void)containerWillBeEmptied:(GULCCComponentContainer *)container;
  24. @end
  25. typedef _Nullable id (^GULCCComponentCreationBlock)(GULCCComponentContainer *container,
  26. BOOL *isCacheable)
  27. NS_SWIFT_NAME(ComponentCreationBlock);
  28. @class GULCCDependency;
  29. /// Describes the timing of instantiation. Note: new components should default to lazy unless there
  30. /// is a strong reason to be eager.
  31. typedef NS_ENUM(NSInteger, GULCCInstantiationTiming) {
  32. GULCCInstantiationTimingLazy,
  33. GULCCInstantiationTimingAlwaysEager
  34. } NS_SWIFT_NAME(InstantiationTiming);
  35. /// A component that can be used from other components.
  36. NS_SWIFT_NAME(Component)
  37. @interface GULCCComponent : NSObject
  38. /// The protocol describing functionality provided from the Component.
  39. @property(nonatomic, strong, readonly) Protocol *protocol;
  40. /// The timing of instantiation.
  41. @property(nonatomic, readonly) GULCCInstantiationTiming instantiationTiming;
  42. /// An array of dependencies for the component.
  43. @property(nonatomic, copy, readonly) NSArray<GULCCDependency *> *dependencies;
  44. /// A block to instantiate an instance of the component with the appropriate dependencies.
  45. @property(nonatomic, copy, readonly) GULCCComponentCreationBlock creationBlock;
  46. // There's an issue with long NS_SWIFT_NAMES that causes compilation to fail, disable clang-format
  47. // for the next two methods.
  48. // clang-format off
  49. /// Creates a component with no dependencies that will be lazily initialized.
  50. + (instancetype)componentWithProtocol:(Protocol *)protocol
  51. creationBlock:(GULCCComponentCreationBlock)creationBlock
  52. NS_SWIFT_NAME(init(_:creationBlock:));
  53. /// Creates a component to be registered with the component container.
  54. ///
  55. /// @param protocol - The protocol describing functionality provided by the component.
  56. /// @param instantiationTiming - When the component should be initialized. Use .lazy unless there's
  57. /// a good reason to be instantiated earlier.
  58. /// @param dependencies - Any dependencies the `implementingClass` has, optional or required.
  59. /// @param creationBlock - A block to instantiate the component with a container and a flag to cache
  60. /// the instance created or not.
  61. /// @return A component that can be registered with the component container.
  62. + (instancetype)componentWithProtocol:(Protocol *)protocol
  63. instantiationTiming:(GULCCInstantiationTiming)instantiationTiming
  64. dependencies:(NSArray<GULCCDependency *> *)dependencies
  65. creationBlock:(GULCCComponentCreationBlock)creationBlock
  66. NS_SWIFT_NAME(init(_:instantiationTiming:dependencies:creationBlock:));
  67. // clang-format on
  68. /// Unavailable.
  69. - (instancetype)init NS_UNAVAILABLE;
  70. @end
  71. NS_ASSUME_NONNULL_END