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 -
Declaration
Swift
public func observeModule<Other>(transform: @escaping (SpecificModule) -> Observable<Other>) -> some Subscribable<Other>Parameters
transformThe transformation that maps the
Moduleto one of it’sObservables.Return Value
A
Subscribablefor the innerObservable. -
Declaration
Swift
public func observeModule<Other>(_ keyPath: KeyPath<SpecificModule, Observable<Other>>) -> some Subscribable<Other>Parameters
keyPathThe
KeyPathto theObservableinside of theModule.Return Value
A
Subscribablefor the innerObservable. -
Observe an observable derived from every
SpecificModuleinstance 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
transformreceives the current list and returns theObservableto 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
transformMaps the current list of instances to an
Observable.Return Value
A
Subscribablefor the innerObservable. -
Executes a task for the
Moduleand returns aSinglewith theResult.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
taskthe task to be executed if module is enabled
Return Value
the
Singlewith theResult -
Executes an async task for the
Moduleand returns aSinglewith theResult.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
Resultas 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
taskthe task to be executed if module is enabled
Return Value
the
Singlewith theResult.
View on GitHub