CollectorFactory

public actor CollectorFactory

A factory and registry for Collector types.

CollectorFactory is responsible for:

  • Registering collector types by a string key (typically the server-provided “type” or “inputType”).
  • Creating collectors from JSON dictionaries provided by DaVinci responses.
  • Injecting contextual references (e.g., ContinueNode) into collectors that need them.
  • Resetting the registry (useful in tests).

The actor ensures thread-safe registration and creation across concurrent tasks.

  • The shared singleton instance of the CollectorFactory.

    Declaration

    Swift

    public static let shared: CollectorFactory
  • Registers a Collector metatype for a given key.

    Note

    This API is deprecated. Prefer register(type:closure:) to allow flexible construction.

    Declaration

    Swift

    @available(*, deprecated, message: "Use register(type:closure:﹚ instead")
    public func register(type: String, collector: any Collector.Type)

    Parameters

    type

    The string key identifying the collector (e.g., “TEXT”, “PASSWORD”).

    collector

    The Collector type to instantiate when that key is encountered.

  • Registers a Collector factory closure for a given key.

    Declaration

    Swift

    public func register(type: String, closure: @escaping @Sendable ([String : Any]) -> (any Collector)?)

    Parameters

    type

    The string key identifying the collector (e.g., “TEXT”, “PASSWORD”).

    closure

    A closure that takes the raw JSON dictionary for a collector and returns an instance, or nil if the JSON cannot be parsed for this type.

  • Creates a list of collectors from an array of JSON dictionaries.

    The method attempts construction using:

    1. A registered metatype in collectors (deprecated path).
    2. A registered closure in collectorCreationClosures.

    If a collector conforms to DaVinciAware, the provided daVinci instance is injected.

    Declaration

    Swift

    public func collector(daVinci: DaVinci, from array: [[String : Any]]) -> Collectors

    Parameters

    daVinci

    The DaVinci workflow instance to inject into collectors that are DaVinciAware.

    array

    The array of JSON dictionaries describing each collector. The factory will read Constants.inputType first, falling back to Constants.type, to identify the collector type.

    Return Value

    An ordered list of collectors constructed from the JSON input.

  • Injects the provided ContinueNode into any collectors that conform to ContinueNodeAware.

    This enables collectors to call next() or otherwise interact with their hosting node.

    Declaration

    Swift

    public func inject(continueNode: ContinueNode)

    Parameters

    continueNode

    The node whose collectors will receive the node reference.

  • Clears all registered collectors and factory closures.

    Useful for tests to ensure a clean registry between runs.

    Declaration

    Swift

    public func reset()