-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// swift-tools-version:5.3 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "LaunchDarkly+Combine", | ||
platforms: [ | ||
.iOS(.v13), | ||
.macOS(.v10_15), | ||
.watchOS(.v6), | ||
.tvOS(.v13) | ||
], | ||
products: [ | ||
.library( | ||
name: "LaunchDarkly+Combine", | ||
targets: ["LaunchDarkly+Combine"]), | ||
], | ||
dependencies: [ | ||
.package(name: "LaunchDarkly", url: "https://github.com/launchdarkly/ios-client-sdk.git", .upToNextMinor(from: "5.1.0")) | ||
], | ||
targets: [ | ||
.target( | ||
name: "LaunchDarkly+Combine", | ||
dependencies: ["LaunchDarkly"], | ||
path: "Sources") | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Launchly | ||
|
||
An easy to use [LaunchDarkly](https://launchdarkly.com) framework that adds support for Combine. Supports iOS 13+, macOS 10.15+, watchOS 6+, tvOS 13+. | ||
|
||
# Example | ||
```swift | ||
import Combine | ||
import Launchly | ||
import LaunchDarkly | ||
|
||
LDClient.start(config: LDConfig(mobileKey: "#YOUR_MOBILE_KEY#")) | ||
|
||
var cancellable: AnyCancellable? | ||
cancellable = LDClient.get()!.variationPublisher(forKey: key) | ||
.sink { | ||
print($0) | ||
} | ||
``` | ||
|
||
# Project Structure | ||
|
||
## Extensions | ||
- Contains the code for extending `LDClient` with Combine. | ||
|
||
# License | ||
Copyright (c) 2020 Eric Lewis | ||
|
||
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// | ||
// LDClient+Combine.swift | ||
// | ||
// Created by Eric Lewis on 10/7/20. | ||
// | ||
|
||
import Combine | ||
import LaunchDarkly | ||
|
||
// MARK: LDClient Publishers | ||
|
||
extension LDClient { | ||
public func variationPublisher<T: LDFlagValueConvertible>(forKey: LDFlagKey) -> LDClient.VariationPublisher<T> { | ||
VariationPublisher(forKey, client: self) | ||
} | ||
|
||
public func currentConnectionModePublisher() -> LDClient.ConnectionModePublisher { | ||
ConnectionModePublisher(client: self) | ||
} | ||
} | ||
|
||
// MARK: variationPublisher | ||
|
||
extension LDClient { | ||
public struct VariationPublisher<T: LDFlagValueConvertible>: Combine.Publisher { | ||
public typealias Output = T | ||
public typealias Failure = Never | ||
|
||
private let key: LDFlagKey | ||
private let client: LDClient | ||
|
||
init(_ key: LDFlagKey, client: LDClient) { | ||
self.key = key | ||
self.client = client | ||
} | ||
|
||
public func receive<S>(subscriber: S) where S : Subscriber, Failure == S.Failure, Output == S.Input { | ||
let subscription = LDClient.VariationSubscription(subscriber: subscriber, key: key, client: client) | ||
subscriber.receive(subscription: subscription) | ||
} | ||
} | ||
|
||
fileprivate final class VariationSubscription<SubscriberType: Subscriber>: Combine.Subscription where SubscriberType.Input: LDFlagValueConvertible, SubscriberType.Failure == Never { | ||
private let subscriber: SubscriberType | ||
private let client: LDClient | ||
private let key: LDFlagKey | ||
|
||
init(subscriber: SubscriberType, key: LDFlagKey, client: LDClient) { | ||
self.subscriber = subscriber | ||
self.key = key | ||
self.client = client | ||
|
||
client.observe(key: key, owner: self as LDObserverOwner) { [weak self] in | ||
_ = self?.subscriber.receive($0.newValue as! SubscriberType.Input) | ||
} | ||
} | ||
|
||
func request(_ demand: Subscribers.Demand) { | ||
if demand > 0 { | ||
_ = subscriber.receive(client.variation(forKey: key)!) | ||
} | ||
} | ||
|
||
func cancel() { | ||
client.stopObserving(owner: self as LDObserverOwner) | ||
} | ||
} | ||
} | ||
|
||
// MARK: currentConnectionModePublisher | ||
|
||
extension LDClient { | ||
public struct ConnectionModePublisher: Combine.Publisher { | ||
public typealias Output = ConnectionInformation.ConnectionMode | ||
public typealias Failure = Never | ||
|
||
private let client: LDClient | ||
|
||
init(client: LDClient) { | ||
self.client = client | ||
} | ||
|
||
public func receive<S>(subscriber: S) where S : Subscriber, Failure == S.Failure, Output == S.Input { | ||
let subscription = LDClient.ConnectionModeSubscription(subscriber: subscriber, client: client) | ||
subscriber.receive(subscription: subscription) | ||
} | ||
} | ||
|
||
fileprivate final class ConnectionModeSubscription<SubscriberType: Subscriber>: Combine.Subscription where SubscriberType.Input == ConnectionInformation.ConnectionMode, SubscriberType.Failure == Never { | ||
private let subscriber: SubscriberType | ||
private let client: LDClient | ||
|
||
init(subscriber: SubscriberType, client: LDClient) { | ||
self.subscriber = subscriber | ||
self.client = client | ||
|
||
client.observeCurrentConnectionMode(owner: self as LDObserverOwner) { [weak self] in | ||
_ = self?.subscriber.receive($0) | ||
} | ||
} | ||
|
||
func request(_ demand: Subscribers.Demand) { | ||
// NOOP | ||
} | ||
|
||
func cancel() { | ||
client.stopObserving(owner: self as LDObserverOwner) | ||
} | ||
} | ||
} |