Skip to content

Commit

Permalink
Merge pull request #1 from skyfoxa/main
Browse files Browse the repository at this point in the history
Update to LD 6.1
  • Loading branch information
ericlewis authored Nov 6, 2023
2 parents 6e35afb + de76aa6 commit ea7ebdd
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
25 changes: 25 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"object": {
"pins": [
{
"package": "LaunchDarkly",
"repositoryURL": "https://github.com/launchdarkly/ios-client-sdk.git",
"state": {
"branch": null,
"revision": "0fedd1964fef9e3145526dfb9d13a079f588fbfb",
"version": "9.2.1"
}
},
{
"package": "LDSwiftEventSource",
"repositoryURL": "https://github.com/LaunchDarkly/swift-eventsource.git",
"state": {
"branch": null,
"revision": "3d45eacab476f9bb2c58662cfb2d35088140b25b",
"version": "3.1.1"
}
}
]
},
"version": 1
}
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// swift-tools-version:5.3
// swift-tools-version:5.7
// 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),
.iOS(.v15),
.macOS(.v10_15),
.watchOS(.v6),
.tvOS(.v13)
Expand All @@ -17,7 +17,7 @@ let package = Package(
targets: ["LaunchDarkly+Combine"]),
],
dependencies: [
.package(name: "LaunchDarkly", url: "https://github.com/launchdarkly/ios-client-sdk.git", .upToNextMinor(from: "5.1.0"))
.package(name: "LaunchDarkly", url: "https://github.com/launchdarkly/ios-client-sdk.git", .upToNextMajor(from: Version(9, 0, 0)))
],
targets: [
.target(
Expand Down
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ To include LaunchDarkly+Combine in a Swift package, simply add it to the depende

```swift
dependencies: [
.package(url: "https://github.com/ericlewis/LaunchDarkly-Combine.git", .upToNextMinor(from: "1.0.0"))
.package(url: "https://github.com/ericlewis/LaunchDarkly-Combine.git", .upToNextMinor(from: "2.0.0"))
]
```

Expand All @@ -37,10 +37,22 @@ import Combine
import LaunchDarkly_Combine
import LaunchDarkly

func observeFlag<T: Decodable>(key: LDFlagKey) -> AnyPublisher<T, Error> {
LDClient.get()!
.variationPublisher(forKey: key)
.decode()
.eraseToAnyPublisher()
}

func observeMyBoolFlag() -> AnyPublisher<Bool, Never> {
return observeFlag(key: "#FLAG_KEY")
.replaceError(with: false) // Default value
}

LDClient.start(config: LDConfig(mobileKey: "#YOUR_MOBILE_KEY#"))

var cancellable: AnyCancellable?
cancellable = LDClient.get()!.variationPublisher(forKey: key)
cancellable = observeMyBoolFlag()
.sink {
print($0)
}
Expand Down
28 changes: 22 additions & 6 deletions Sources/Extensions/LDClient+variationPublisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@

import Combine
import LaunchDarkly
import Foundation

extension LDClient {
public func variationPublisher<T: LDFlagValueConvertible>(forKey: LDFlagKey) -> LDClient.VariationPublisher<T> {
public func variationPublisher(forKey: LDFlagKey) -> LDClient.VariationPublisher {
VariationPublisher(forKey, client: self)
}
}

extension LDClient {
public struct VariationPublisher<T: LDFlagValueConvertible>: Combine.Publisher {
public typealias Output = T
public struct VariationPublisher: Combine.Publisher {
public typealias Output = LDValue
public typealias Failure = Never

private let key: LDFlagKey
Expand All @@ -33,7 +34,7 @@ extension LDClient {
}
}

fileprivate final class VariationSubscription<SubscriberType: Subscriber>: Combine.Subscription where SubscriberType.Input: LDFlagValueConvertible, SubscriberType.Failure == Never {
fileprivate final class VariationSubscription<SubscriberType: Subscriber>: Combine.Subscription where SubscriberType.Input: Codable, SubscriberType.Failure == Never {
private let subscriber: SubscriberType
private let client: LDClient
private let key: LDFlagKey
Expand All @@ -44,18 +45,33 @@ extension LDClient {
self.client = client

client.observe(key: key, owner: self as LDObserverOwner) { [weak self] in
_ = self?.subscriber.receive($0.newValue as! SubscriberType.Input)
guard let self = self else { return }
_ = self.subscriber.receive($0.newValue as! SubscriberType.Input)
}
}

func request(_ demand: Subscribers.Demand) {
if demand > 0 {
_ = subscriber.receive(client.variation(forKey: key)!)
let value: LDValue = client.jsonVariation(forKey: key, defaultValue: LDValue.null)
_ = subscriber.receive(value as! SubscriberType.Input)
}
}

func cancel() {
client.stopObserving(owner: self as LDObserverOwner)
}

}
}

public extension Publisher where Output == LDValue {

func decode<T: Decodable>(
as type: T.Type = T.self,
using decoder: JSONDecoder = .init()
) -> AnyPublisher<T, Error> {
encode(encoder: JSONEncoder())
.decode(type: type, decoder: decoder)
.eraseToAnyPublisher()
}
}

0 comments on commit ea7ebdd

Please sign in to comment.