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, like getModule, 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, the transform block will be called with every Module, in order. The returned Observable, 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 id because updateConfiguration may 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 same id must 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

    transform

    The transformation to be applied to each module.

    Return Value

    An Observable of 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, the transform block will be called with every Module, in order, until one of them returns a non-nil T. The returned Observable, per modules array change, emits nil when 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

    transform

    The transformation to be applied to each module, until one succeeds.

    Return Value

    An Observable of the module, emitting after every modules update with the resolved object if one transformation succeeds, or nil when 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, nil means 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 Observable won’t emit again unless the actual Module instance is different from the last returned one.

    Declaration

    Swift

    public func observeModule<T>(_ module: T.Type = T.self) -> Observable<T?> where T : Module

    Parameters

    module

    The type used to match the module.

    Return Value

    An Observable of 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 Disposable

    Parameters

    transform

    The transformation to be applied to each module, until one succeeds. If not provided, it will just try to cast the Module as T.

    completion

    Called, once the modules are ready, with the transformed module instance or nil if no transformation succeeds.

    Return Value

    A Disposable that can be disposed to avoid the completion being called.