Skip to content

Commit

Permalink
chore: create
Browse files Browse the repository at this point in the history
  • Loading branch information
ericlewis committed Oct 7, 2020
1 parent 8afa050 commit cc99915
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Package.swift
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")
]
)
45 changes: 45 additions & 0 deletions README.md
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.

110 changes: 110 additions & 0 deletions Sources/Extensions/LDClient+Combine.swift
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)
}
}
}

0 comments on commit cc99915

Please sign in to comment.