Skip to content
This repository has been archived by the owner on Jul 7, 2022. It is now read-only.

Commit

Permalink
Add TimelaneCombineX
Browse files Browse the repository at this point in the history
  • Loading branch information
ffittschen committed Aug 6, 2020
1 parent b48f05f commit 82e1925
Show file tree
Hide file tree
Showing 9 changed files with 710 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Sixt SE.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
52 changes: 52 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"object": {
"pins": [
{
"package": "CombineX",
"repositoryURL": "https://github.com/cx-org/CombineX",
"state": {
"branch": null,
"revision": "9ce154b155643ec8939d84cb6ae00c331ff3eca0",
"version": "0.2.1"
}
},
{
"package": "Nimble",
"repositoryURL": "https://github.com/Quick/Nimble.git",
"state": {
"branch": null,
"revision": "f8657642dfdec9973efc79cc68bcef43a653a2bc",
"version": "8.0.2"
}
},
{
"package": "Quick",
"repositoryURL": "https://github.com/Quick/Quick.git",
"state": {
"branch": null,
"revision": "09b3becb37cb2163919a3842a4c5fa6ec7130792",
"version": "2.2.1"
}
},
{
"package": "Semver",
"repositoryURL": "https://github.com/ddddxxx/Semver.git",
"state": {
"branch": null,
"revision": "6094e6f23a02b52b5d211fd114a4750c4f3ecef3",
"version": "0.2.1"
}
},
{
"package": "TimelaneCore",
"repositoryURL": "https://github.com/icanzilb/TimelaneCore",
"state": {
"branch": null,
"revision": "35437d370a6127d14f728065b3d4e3e9cf65e6a0",
"version": "1.0.12"
}
}
]
},
"version": 1
}
37 changes: 37 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// swift-tools-version:5.1
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription

let package = Package(
name: "TimelaneCombineX",
platforms: [
.macOS(.v10_14),
.iOS(.v12),
.tvOS(.v12),
.watchOS(.v5)
],
products: [
.library(
name: "TimelaneCombineX",
targets: ["TimelaneCombineX"]),
],
dependencies: [
.package(url: "https://github.com/icanzilb/TimelaneCore", from: "1.0.0"),
.package(url: "https://github.com/cx-org/CombineX", from: "0.2.1")
],
targets: [
.target(
name: "TimelaneCombineX",
dependencies: [
"TimelaneCore",
"CombineX"
]),
.testTarget(
name: "TimelaneCombineXTests",
dependencies: [
"TimelaneCombineX",
"TimelaneCoreTestUtils"
]),
],
swiftLanguageVersions: [.v5]
)
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,36 @@
# TimelaneCombineX
<h1 align="center"> Timelane + CombineX </h1>
<p align="center">
<img src="https://github.com/icanzilb/TimelaneCombine/blob/master/etc/[email protected]" width="200” max-width="90%" alt="TimelaneCombineX" />
</p>

**TimelaneCombineX** is a port of [TimelaneCombine](https://github.com/icanzilb/TimelaneCombine) to provide CombineX bindings for profiling [CombineX](https://github.com/cx-org/CombineX) code with the [Timelane](http://timelane.tools) Instrument.

#### Table of Contents:

- [Installation](#Installation)
- [Usage](#Usage)
- [License](#License)

# Installation

## Swift Package Manager

I . Automatically in Xcode:

- Click **File > Swift Packages > Add Package Dependency...**
- Use the package URL `https://github.com/Sixt/TimelaneCombineX` to add TimelaneCombineX to your project.

II . Manually in your **Package.swift** file add:

```swift
.package(url: "https://github.com/Sixt/TimelaneCombineX", from: "1.0.0")
```

# Usage

For usage instructions, please refer to the original [TimelaneCombine](https://github.com/icanzilb/TimelaneCombine) repository.

# License

Copyright (c) Sixt SE 2020
This package is provided under the MIT License.
41 changes: 41 additions & 0 deletions Sources/TimelaneCombineX/PublishedOnLane.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import CombineX
import TimelaneCore

/// Property wrapper that offers a publisher for the given property
/// **and** creates a Timelane lane for it.
@propertyWrapper public class PublishedOnLane<Value> {
@Published private var value: Value
private let laneName: String
private let filter: Timelane.LaneTypeOptions

/// Gets or sets the value of this property.
public var wrappedValue: Value {
get { self.value }
set { self.value = newValue }
}

/// Gets the lane-enabled `Publisher` for this property.
public var projectedValue: AnyPublisher<Value, Never> {
return self.$value.lane(laneName, filter: filter).eraseToAnyPublisher()
}

/// Creates a `PublishedOnLane` wrapper.
/// - Parameters:
/// - initialValue: Value to wrap; usually implicit.
/// - name: The name of this lane; defaults to the
/// type of this property if not provided.
public init(wrappedValue initialValue: Value,
_ name: String? = nil,
filter: Timelane.LaneTypeOptions = .all) {
self.value = initialValue
self.laneName = name ?? "\(initialValue.self)"
self.filter = filter
}

public init(wrappedValue initialValue: Value,
_ name: String? = nil) {
self.value = initialValue
self.laneName = name ?? "\(initialValue.self)"
self.filter = .all
}
}
167 changes: 167 additions & 0 deletions Sources/TimelaneCombineX/TimelaneCombineX.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import CombineX
import CXFoundation
import Foundation
import TimelaneCore

extension Publishers {
public class TimelanePublisher<Upstream: Publisher, S>: Publisher where S: Scheduler {
public typealias Output = Upstream.Output
public typealias Failure = Upstream.Failure

private let upstream: Upstream

private let name: String?
private let filter: Timelane.LaneTypeOptions
private let source: String
private let scheduler: S
private let transformValue: (Upstream.Output) -> String
private let logger: Timelane.Logger

public init(upstream: Upstream,
name: String?,
filter: Timelane.LaneTypeOptions,
source: String,
scheduler: S,
transformValue: @escaping (Upstream.Output) -> String,
logger: @escaping Timelane.Logger) {
self.upstream = upstream
self.name = name
self.filter = filter
self.source = source
self.scheduler = scheduler
self.transformValue = transformValue
self.logger = logger
}

public func receive<S: Subscriber>(subscriber: S) where Failure == S.Failure, Output == S.Input {
let filter = self.filter
let source = self.source
let scheduler = self.scheduler
let subscription = Timelane.Subscription(name: name, logger: logger)
let transform = self.transformValue

let sink = AnySubscriber<Upstream.Output, Upstream.Failure>(
receiveSubscription: { sub in
if filter.contains(.subscription) {
subscription.begin(source: source)
}

subscriber.receive(subscription: sub)
},
receiveValue: { value -> Subscribers.Demand in
if filter.contains(.event) {
subscription.event(value: .value(transform(value)), source: source)
}

return subscriber.receive(value)
},
receiveCompletion: { completion in
if filter.contains(.subscription) {
switch completion {
case .finished:
subscription.end(state: .completed)
case .failure(let error):
subscription.end(state: .error(error.localizedDescription))
}
}

if filter.contains(.event) {
switch completion {
case .finished:
subscription.event(value: .completion, source: source)
case .failure(let error):
subscription.event(value: .error(error.localizedDescription), source: source)
}
}

subscriber.receive(completion: completion)
}
)

upstream
.handleEvents(receiveCancel: {
// Sometimes a "cancel" event preceeds "finished" so we seem to
// need this hack below to make sure "finished" goes out first.
scheduler.schedule {
// Cancelling the subscription
if filter.contains(.subscription) {
subscription.end(state: .cancelled)
}
if filter.contains(.event) {
subscription.event(value: .cancelled, source: source)
}
}
})
.subscribe(sink)
}
}
}

extension Publisher {

/// The `lane` operator logs the subscription and its events to the Timelane Instrument.
///
/// - Note: You can download the Timelane Instrument from http://timelane.tools
/// - Parameters:
/// - name: A name for the lane when visualized in Instruments
/// - filter: Which events to log subscriptions or data events.
/// For example for a subscription on a subject you might be interested only in data events.
/// - transformValue: An optional closure to format the subscription values for displaying in Instruments.
/// You can not only prettify the values but also change them completely, e.g. for arrays you can
/// it might be more useful to report the count of elements if there are a lot of them.
/// - value: The value emitted by the subscription
public func lane(_ name: String,
filter: Timelane.LaneTypeOptions = .all,
file: StaticString = #file,
function: StaticString = #function,
line: UInt = #line,
scheduler: CXWrappers.DispatchQueue = DispatchQueue.main.cx,
transformValue: @escaping (_ value: Output) -> String = { String(describing: $0) },
logger: @escaping Timelane.Logger = Timelane.defaultLogger)
-> Publishers.TimelanePublisher<Self, CXWrappers.DispatchQueue> {

let fileName = file.description.components(separatedBy: "/").last!
let source = "\(fileName):\(line) - \(function)"

return Publishers.TimelanePublisher(upstream: self,
name: name,
filter: filter,
source: source,
scheduler: scheduler,
transformValue: transformValue,
logger: logger)
}

/// The `lane` operator logs the subscription and its events to the Timelane Instrument.
///
/// - Note: You can download the Timelane Instrument from http://timelane.tools
/// - Parameters:
/// - name: A name for the lane when visualized in Instruments
/// - filter: Which events to log subscriptions or data events.
/// For example for a subscription on a subject you might be interested only in data events.
/// - transformValue: An optional closure to format the subscription values for displaying in Instruments.
/// You can not only prettify the values but also change them completely, e.g. for arrays you can
/// it might be more useful to report the count of elements if there are a lot of them.
/// - value: The value emitted by the subscription
public func lane<S>(_ name: String,
filter: Timelane.LaneTypeOptions = .all,
file: StaticString = #file,
function: StaticString = #function,
line: UInt = #line,
scheduler: S,
transformValue: @escaping (_ value: Output) -> String = { String(describing: $0) },
logger: @escaping Timelane.Logger = Timelane.defaultLogger)
-> Publishers.TimelanePublisher<Self, S> where S: Scheduler {

let fileName = file.description.components(separatedBy: "/").last!
let source = "\(fileName):\(line) - \(function)"

return Publishers.TimelanePublisher(upstream: self,
name: name,
filter: filter,
source: source,
scheduler: scheduler,
transformValue: transformValue,
logger: logger)
}
}
Loading

0 comments on commit 82e1925

Please sign in to comment.