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
typeThe string key identifying the collector (e.g., “TEXT”, “PASSWORD”).
collectorThe 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
typeThe string key identifying the collector (e.g., “TEXT”, “PASSWORD”).
closureA 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:
- A registered metatype in
collectors(deprecated path). - A registered closure in
collectorCreationClosures.
If a collector conforms to
DaVinciAware, the provideddaVinciinstance is injected.Declaration
Swift
public func collector(daVinci: DaVinci, from array: [[String : Any]]) -> CollectorsParameters
daVinciThe DaVinci workflow instance to inject into collectors that are
DaVinciAware.arrayThe array of JSON dictionaries describing each collector. The factory will read
Constants.inputTypefirst, falling back toConstants.type, to identify the collector type.Return Value
An ordered list of collectors constructed from the JSON input.
- A registered metatype in
-
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
continueNodeThe 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()
View on GitHub