Bulk is a library for buffering the objects. Pipeline(Sink) receives the object and emits the object bulked.
To pack a lot of elements would be helpful in several cases. For example, sending the analytics events for your products with in house API.
We call the pipeline that receives the object as Sink
.
Sink
receives the objects and emits the bulked object to multiple targets.
To create a bulk, we can choose several types of the buffer.
Currently, Bulk provides 2 types below.
In this tutorial, we select MemoryBuffer.
Target receives the bulked object from Sink.
Bulk does not privides default implemented Target.
For now, you need to create it.
public protocol TargetType {
associatedtype Element
func write(items: [Element])
}
struct MyTarget<Element>: TargetType {
func write(items: [Element]) {
print(items)
}
}
And finally, create BulkSink object.
let sink = BulkSink<String>(
buffer: MemoryBuffer.init(size: 10).asAny(),
targets: [
MyTarget<String>().asAny()
]
)
Send the objects
sink.send("A")
sink.send("B")
sink.send("C")
sink
sends the bulked object when Buffer receives the objects up to the specified size (10 elements in the example).
The Logger as a library on top of Bulk.
BulkLogger provies Logger
object that wraps Sink
inside.
let logger = Logger(context: "", sinks: [
BulkSink<LogData>(
buffer: MemoryBuffer.init(size: 10).asAny(),
targets: [
TargetUmbrella.init(
transform: LogBasicFormatter().format,
targets: [
LogConsoleTarget.init().asAny()
]
).asAny(),
OSLogTarget(subsystem: "BulkDemo", category: "Demo").asAny()
]
)
.asAny()
])
Logger object send the log data to 2 targets (LogConsoleTarget and OSLogTarget)
logger.verbose("Hello")
Bulk Framework is released under the MIT License.
link |
Stars: 59 |
Last commit: 4 days ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco