WorkerRegistry

The DI seam: resolves a stable task id to a fresh BackgroundWorker per invocation. Two registration shapes feed it:

  • Per-id: register taking a task id + closure. One id, one factory lambda. The closure closes over the user's DI graph.

  • Bulk: register taking a BackgroundWorkerFactory. One factory object owns many ids and resolves the concrete worker lazily.

The library calls the resolved factory each time the platform fires a worker — never caches the worker instance itself.

Resolution order in create: per-id registrations first, then factories in registration order, first non-null wins. Declared id sets must not overlap (factory-vs-factory or factory-vs-per-id) — overlapping registration throws, so resolution is always unambiguous.

Register everything at app launch before BackgroundTaskManager.start(). The registry is sealed at start(); re-registering after that throws.

@OptIn(ExperimentalObjCName::class): Swift-rename annotations so iOS app code calls registry.register(taskId:factory:) (CLAUDE.md §8).

Types

Link copied to clipboard

Thrown when a BackgroundWorkerFactory declares ownership of a taskId in BackgroundWorkerFactory.taskIds but returns null from BackgroundWorkerFactory.create for it — a violation of the factory's sync contract.

Functions

Link copied to clipboard
@ObjCName(swiftName = "factoryDescriptors")
fun factoryDescriptors(): List<FactoryDescriptor>

Inspector-shaped view of every registered factory — one FactoryDescriptor.PerId per closure registration, one FactoryDescriptor.Bulk per BackgroundWorkerFactory.

Link copied to clipboard
@ObjCName(swiftName = "register")
fun register(factory: BackgroundWorkerFactory)
@ObjCName(swiftName = "register")
fun register(taskId: String, factory: () -> BackgroundWorker)

Associate taskId with a factory that builds a fresh BackgroundWorker per dispatch.

Link copied to clipboard
@ObjCName(swiftName = "registeredIds")
fun registeredIds(): Set<String>

Returns the set of task ids currently registered — the union of per-id registrations and every BackgroundWorkerFactory's declared ids. Snapshot — safe to call at any time.