ModuleProxy

public class ModuleProxy<SpecificModule, Failure> where SpecificModule : Module, Failure : Error

A ModuleProxy is to be used for proxying access to modules that are or were available to access from the main Tealium implementation.

Any external Module implementation that provides functionality expected to be used by a developer should wrap their access to Tealium through a ModuleProxy.

  • A synchronous task that can be executed on a module.

    Declaration

    Swift

    public typealias ModuleTask<T> = (_ module: SpecificModule) throws(Failure) -> T
  • An asynchronous task that can be executed on a module.

    Declaration

    Swift

    public typealias AsyncModuleTask<T> = (
        _ module: SpecificModule,
        _ completion: @escaping (Result<T, ModuleError<Failure>>) -> Void
    ) throws(Failure) -> Void
  • Observes an Observable derived from the Module, re-deriving it as the module’s availability changes.

    Use this for state streams, where subscribers must know when the module is unavailable. For event streams, use observeModule(transform:) instead.

    The stream emits nothing until the ModuleManager is ready. Once it is, it emits .success with the value produced by transform while the module is enabled, and .failure with ModuleError.moduleNotEnabled(_:) while the module is disabled or missing - switching back to .success if the module is re-enabled. If the Tealium instance failed to initialize, or is later shut down, this emits a single .failure with ModuleError.tealiumError(_:) and then completes.

    Values the module emits while the Tealium instance is shutting down are not delivered: the terminal ModuleError.tealiumError(_:) is the last value a subscriber receives.

    Declaration

    Swift

    public func observeModuleResult<Other>(
        transform: @escaping (SpecificModule) -> Observable<Other>
    ) -> some Subscribable<Result<Other, ModuleError<Failure>>>

    Parameters

    transform

    The transformation that maps the Module to one of its Observables.

    Return Value

    A Subscribable for the transformed Result.

  • Observes an Observable derived from the Module, re-deriving it as the module’s availability changes.

    Use this for event streams: it emits only while the module is enabled, stays silent while it is disabled or missing, and completes when the Tealium instance fails to initialize or is shut down. For state streams, where subscribers must know the module is unavailable, use observeModuleResult(transform:) instead.

    Values the module emits while the Tealium instance is shutting down are not delivered: the completion is the last event a subscriber receives.

    Declaration

    Swift

    public func observeModule<Other>(transform: @escaping (SpecificModule) -> Observable<Other>) -> some Subscribable<Other>

    Parameters

    transform

    The transformation that maps the Module to one of it’s Observables.

    Return Value

    A Subscribable for the inner Observable.

  • Observe an observable derived from every SpecificModule instance currently registered.

    Use this when a module type may be registered multiple times (for example, multiple MomentsAI instances for different use cases) and the caller needs to react to the full set of instances. The transform receives the current list and returns the Observable to expose; it re-runs whenever the set changes by identity.

    Declaration

    Swift

    public func observeModules<Other>(transform: @escaping ([SpecificModule]) -> Observable<Other>) -> any Subscribable<Other>

    Parameters

    transform

    Maps the current list of instances to an Observable.

    Return Value

    A Subscribable for the inner Observable.

  • Retrieves the Module, providing it in the completion.

    The completion receives nil whenever the module isn’t available. Use executeModuleTask { $0 } to get the ModuleError that explains why.

    Declaration

    Swift

    public func getModule(completion: @escaping (SpecificModule?) -> Void)

    Parameters

    completion

    The block of code to receive the Module in, if present, or nil.

  • Executes a task for the Module and returns a Single with the Result.

    Example of usage inside a custom module wrapper:

     class MyModuleWrapper {
         private let moduleProxy: ModuleProxy<MyModule>
         init(moduleProxy: ModuleProxy<MyModule>) {
             self.moduleProxy = moduleProxy
         }
         func doStuff() -> SingleResult<SomeValue> {
             moduleProxy.executeModuleTask { module in
                 module.doStuff() // returns `SomeValue`
             }
         }
         func throwStuff() -> SingleResult<SomeValue> {
             moduleProxy.executeModuleTask { module in
                 try module.throwStuff() // throws an `Error`
             }
         }
     }
    

    Fails with a ModuleError if the task can’t be run or fails: see ModuleError for the meaning of each case.

    Declaration

    Swift

    public func executeModuleTask<T>(_ task: @escaping ModuleTask<T>) -> SingleResult<T, ModuleError<Failure>>

    Parameters

    task

    the task to be executed if module is enabled

    Return Value

    the Single with the Result

  • Executes an async task for the Module and returns a Single with the Result.

    Example of usage inside a custom module wrapper:

     class MyModuleWrapper {
         private let moduleProxy: ModuleProxy<MyModule>
         init(moduleProxy: ModuleProxy<MyModule>) {
             self.moduleProxy = moduleProxy
         }
         func doStuff() -> SingleResult<SomeValue> {
             moduleProxy.executeAsyncModuleTask { module, completion in
                 module.doStuff(completion: completion) // Completes with a `Result<SomeValue, Error>`
             }
         }
     }
    

    Note that the completion of the module method needs to be with a Result as well or the completion parameter needs to be converted to one like so:

     class MyModuleWrapper {
         private let moduleProxy: ModuleProxy<MyModule>
         init(moduleProxy: ModuleProxy<MyModule>) {
             self.moduleProxy = moduleProxy
         }
         func doStuff() -> SingleResult<Void> {
             moduleProxy.executeAsyncModuleTask { module, completion in
                 module.doStuff { optionalData, optionalError in
                      if let data = optionalData {
                          completion(.success(data))
                      } else if error = optionalError {
                          completion(.failure(error))
                      } else {
                          completion(.failure(TealiumError.genericError("Unexpected completion data")
                      }
                 }
             }
         }
     }
    

    Fails with a ModuleError if the task can’t be run or fails: see ModuleError for the meaning of each case.

    Declaration

    Swift

    public func executeAsyncModuleTask<T>(_ asyncTask: @escaping AsyncModuleTask<T>) -> SingleResult<T, ModuleError<Failure>>

    Parameters

    task

    the task to be executed if module is enabled

    Return Value

    the Single with the Result.