ModuleManager
public class ModuleManager
Manages the lifecycle and configuration of modules within the Tealium SDK.
Warning
This class is not thread safe. The methods that do the work, likegetModule, must be
called from the Tealium queue - which is where a Module runs, so context.moduleManager can be
used directly from a module initialization or updateConfiguration methods.
The methods that only return an Observable, like observeModule, can be called from any queue,
but that Observable must be subscribed from the Tealium queue and emits from it: to consume it
elsewhere, chain subscribeOn(_:) with the Tealium queue followed by observeOn(_:) with yours.
-
Observes the objects created by the successful transformations of each module, regardless of whether that module is currently enabled.
While the modules are still being created, or updated, a subscriber receives no value at all rather than a premature empty array
[]. Once the modules are ready, thetransformblock will be called with everyModule, in order. The returnedObservable, per modules array change, emits a compact map of all the transformations succeeded.The array is only emitted again when the set of modules whose transformation succeeded changes, by identity or order. A value that varies over time for the same module is not re-emitted: observe it through the module itself.
Comparison is by identity rather than
idbecauseupdateConfigurationmay mutate a module in place and return the same instance - which must not be re-emitted - while a module replaced by a new instance with the sameidmust be. It is order-sensitive because module order is significant.Casting to a protocol is the basic use-case for this method.
moduleManager.observeModules { $0 as? SomeProtocol } .subscribe { myProtocols in for myProtocol in myProtocols { myProtocol.protocolMethod() } }Declaration
Swift
public func observeModules<T>(_ transform: @escaping (Module) -> T?) -> Observable<[T]>Parameters
transformThe transformation to be applied to each module.
Return Value
An
Observableof the transformed modules, emitting after every modules update with the objects of all the transformations that succeeded, in order, or an empty array when none of them did. -
Observes the object created by the transformation of a module, regardless of whether that module is currently enabled.
While the modules are still being created, or updated, a subscriber receives no value at all rather than a premature
nil. Once the modules are ready, thetransformblock will be called with everyModule, in order, until one of them returns a non-nilT. The returnedObservable, per modules array change, emitsnilwhen none of the transformations succeeded, or the first object successfully returned by a transformation block.The object is only emitted again when the module whose transformation succeeded changes, by identity. A value that varies over time for the same module is not re-emitted: observe it through the module itself.
Casting to a protocol is the basic use-case for this method.
moduleManager.observeModule { $0 as? SomeProtocol } .subscribe { myProtocol in myProtocol.protocolMethod() }Declaration
Swift
public func observeModule<T>(_ transform: @escaping (Module) -> T?) -> Observable<T?>Parameters
transformThe transformation to be applied to each module, until one succeeds.
Return Value
An
Observableof the module, emitting after every modules update with the resolved object if one transformation succeeds, ornilwhen no transformation succeeds. -
Observes the module of the given type, regardless of whether it is currently enabled.
While the modules are still being created, or updated, a subscriber receives no value at all rather than a premature
nil. Once this starts emitting,nilmeans the module is not enabled - it never means that the modules are still being created.In case multiple modules of this type are enabled at the same time, the first one, ordered by
[ModuleSettings.order`](doc:ModuleSettings/order), will be returned.Upon changes of the modules list this
Observablewon’t emit again unless the actualModuleinstance is different from the last returned one.Declaration
Swift
public func observeModule<T>(_ module: T.Type = T.self) -> Observable<T?> where T : ModuleParameters
moduleThe type used to match the module.
Return Value
An
Observableof the module, emitting after every modules update whenever the resolved module changes by identity. -
Asynchronously retrieves the object created by the transformation of a module. The completion is called once, after the modules are ready.
The transformation is applied in order by
[ModuleSettings.order`](doc:ModuleSettings/order) and upon first successful transformation, the completion is called immediately and no other transformation is applied.Casting to a protocol is the basic use-case for this method.
moduleManager.getModule(transform: { $0 as? SomeProtocol }, completion: { $0?.protocolMethod() })Declaration
Swift
@discardableResult public func getModule<T>(transform: @escaping (Module) -> T? = { $0 as? T }, completion: @escaping (T?) -> Void) -> any DisposableParameters
transformThe transformation to be applied to each module, until one succeeds. If not provided, it will just try to cast the
ModuleasT.completionCalled, once the modules are ready, with the transformed module instance or nil if no transformation succeeds.
Return Value
A
Disposablethat can be disposed to avoid the completion being called.
View on GitHub