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
  • Observe an observable of the Module regardless of if the Module is currently enabled or not.

    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 of the Module regardless of if the Module is currently enabled or not.

    Declaration

    Swift

    public func observeModule<Other>(_ keyPath: KeyPath<SpecificModule, Observable<Other>>) -> some Subscribable<Other>

    Parameters

    keyPath

    The KeyPath to the Observable inside of the Module.

    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.

    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`
             }
         }
     }
    

    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")
                      }
                 }
             }
         }
     }
    

    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.