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
Observablederived from theModule, 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
ModuleManageris ready. Once it is, it emits.successwith the value produced bytransformwhile the module is enabled, and.failurewithModuleError.moduleNotEnabled(_:)while the module is disabled or missing - switching back to.successif the module is re-enabled. If theTealiuminstance failed to initialize, or is later shut down, this emits a single.failurewithModuleError.tealiumError(_:)and then completes.Values the module emits while the
Tealiuminstance is shutting down are not delivered: the terminalModuleError.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
transformThe transformation that maps the
Moduleto one of itsObservables.Return Value
A
Subscribablefor the transformedResult. -
Observes an
Observablederived from theModule, 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
Tealiuminstance fails to initialize or is shut down. For state streams, where subscribers must know the module is unavailable, useobserveModuleResult(transform:)instead.Values the module emits while the
Tealiuminstance 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
transformThe transformation that maps the
Moduleto one of it’sObservables.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. -
Retrieves the
Module, providing it in thecompletion.The
completionreceivesnilwhenever the module isn’t available. UseexecuteModuleTask { $0 }to get theModuleErrorthat explains why.Declaration
Swift
public func getModule(completion: @escaping (SpecificModule?) -> Void)Parameters
completionThe block of code to receive the
Modulein, if present, or nil. -
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` } } }Fails with a
ModuleErrorif the task can’t be run or fails: seeModuleErrorfor the meaning of each case.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") } } } } }Fails with a
ModuleErrorif the task can’t be run or fails: seeModuleErrorfor the meaning of each case.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