Skip to content

Commit

Permalink
Swiftlint autocorrect
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianvarela authored and minuscorp committed Sep 5, 2019
1 parent 18bdd03 commit bef760c
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 125 deletions.
2 changes: 1 addition & 1 deletion Sources/MiniSwift/Action.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extension Action {
public var innerTag: String {
return String(describing: type(of: self))
}

/**
Static method to retrieve the name of the action as a tag.action.

Expand Down
8 changes: 4 additions & 4 deletions Sources/MiniSwift/ActionReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ public class Reducer<A: Action>: Disposable {
public let action: A.Type
public let dispatcher: Dispatcher
public let reducer: (A) -> Void

private var disposable: Disposable!

public init(of action: A.Type, on dispatcher: Dispatcher, reducer: @escaping (A) -> Void) {
self.action = action
self.dispatcher = dispatcher
self.reducer = reducer
self.disposable = build()
}

private func build() -> Disposable {
let disposable = dispatcher.subscribe(tag: action.tag) {
self.reducer($0)
}
return disposable
}

public func dispose() {
disposable.dispose()
}
Expand Down
59 changes: 29 additions & 30 deletions Sources/MiniSwift/Dispatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@ import NIOConcurrencyHelpers
public typealias SubscriptionMap = SharedDictionary<String, OrderedSet<DispatcherSubscription>?>

final public class Dispatcher {

public struct DispatchMode {
// swiftlint:disable:next type_name nesting
public enum UI {
case sync, async
}
}

public var subscriptionCount: Int {
return subscriptionMap.innerDictionary.mapValues { set -> Int in
guard let setValue = set else { return 0 }
return setValue.count
}
.reduce(0, { $0 + $1.value })
}

public static let defaultPriority = 100

private let internalQueue = DispatchQueue(label: "MiniSwift", qos: .userInitiated)
private var subscriptionMap = SubscriptionMap()
private var middleware = [Middleware]()
Expand All @@ -47,27 +47,27 @@ final public class Dispatcher {
private var chain: Chain
private var dispatching: Bool = false
private var subscriptionCounter: Atomic<Int> = Atomic<Int>(value: 0)

public init() {
root = RootChain(map: subscriptionMap)
chain = root
}

private func build() -> Chain {
return middleware.reduce(root, { (chain: Chain, middleware: Middleware) -> Chain in
return ForwardingChain { action in
middleware.perform(action, chain)
}
})
}

public func add(middleware: Middleware) {
internalQueue.sync {
self.middleware.append(middleware)
self.chain = build()
}
}

public func remove(middleware: Middleware) {
internalQueue.sync {
if let index = self.middleware.firstIndex(where: { middleware.id == $0.id }) {
Expand All @@ -76,21 +76,21 @@ final public class Dispatcher {
chain = build()
}
}

public func register(service: Service) {
internalQueue.sync {
self.service.append(service)
}
}

public func unregister(service: Service) {
internalQueue.sync {
if let index = self.service.firstIndex(where: { service.id == $0.id }) {
self.service.remove(at: index)
}
}
}

public func subscribe(priority: Int, tag: String, completion: @escaping (Action) -> Void) -> DispatcherSubscription {
let subscription = DispatcherSubscription(
dispatcher: self,
Expand All @@ -100,7 +100,7 @@ final public class Dispatcher {
completion: completion)
return registerInternal(subscription: subscription)
}

public func registerInternal(subscription: DispatcherSubscription) -> DispatcherSubscription {
internalQueue.sync {
if let map = subscriptionMap[subscription.tag, orPut: OrderedSet<DispatcherSubscription>()] {
Expand All @@ -109,7 +109,7 @@ final public class Dispatcher {
}
return subscription
}

public func unregisterInternal(subscription: DispatcherSubscription) {
internalQueue.sync {
var removed = false
Expand All @@ -121,13 +121,13 @@ final public class Dispatcher {
assert(removed, "Failed to remove DispatcherSubscription, multiple dispose calls?")
}
}

public func subscribe<T: Action>(completion: @escaping (T) -> Void) -> DispatcherSubscription {
return subscribe(tag: T.tag, completion: { (action: T) -> Void in
completion(action)
})
}

public func subscribe<T: Action>(tag: String, completion: @escaping (T) -> Void) -> DispatcherSubscription {
return subscribe(tag: tag, completion: { object in
if let action = object as? T {
Expand All @@ -137,11 +137,11 @@ final public class Dispatcher {
}
})
}

public func subscribe(tag: String, completion: @escaping (Action) -> Void) -> DispatcherSubscription {
return subscribe(priority: Dispatcher.defaultPriority, tag: tag, completion: completion)
}

public func dispatch(_ action: Action, mode: Dispatcher.DispatchMode.UI) {
switch mode {
case .sync:
Expand All @@ -158,7 +158,7 @@ final public class Dispatcher {
}
}
}

private func dispatch(_ action: Action) {
assert(DispatchQueue.isMain)
internalQueue.sync {
Expand All @@ -176,21 +176,21 @@ final public class Dispatcher {
}
}
}

private func getNewSubscriptionId() -> Int {
return subscriptionCounter.add(1)
}
}

public final class DispatcherSubscription: Comparable, Disposable {

private let dispatcher: Dispatcher
public let id: Int
private let priority: Int
private let completion: (Action) -> Void

public let tag: String

public init (dispatcher: Dispatcher,
id: Int,
priority: Int,
Expand All @@ -202,33 +202,32 @@ public final class DispatcherSubscription: Comparable, Disposable {
self.tag = tag
self.completion = completion
}

public func dispose() {
dispatcher.unregisterInternal(subscription: self)
}

public func on(_ action: Action) {
completion(action)
}

public static func == (lhs: DispatcherSubscription, rhs: DispatcherSubscription) -> Bool {
return lhs.id == rhs.id
}

public static func > (lhs: DispatcherSubscription, rhs: DispatcherSubscription) -> Bool {
return lhs.priority > rhs.priority
}

public static func < (lhs: DispatcherSubscription, rhs: DispatcherSubscription) -> Bool {
return lhs.priority < rhs.priority
}

public static func >= (lhs: DispatcherSubscription, rhs: DispatcherSubscription) -> Bool {
return lhs.priority >= rhs.priority
}

public static func <= (lhs: DispatcherSubscription, rhs: DispatcherSubscription) -> Bool {
return lhs.priority <= rhs.priority
}
}

12 changes: 6 additions & 6 deletions Sources/MiniSwift/Middleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,24 @@ public protocol Middleware {
}

public final class ForwardingChain: Chain {

private let next: Next

public var proceed: Next {
return { action in
return self.next(action)
}
}

public init(next: @escaping Next) {
self.next = next
}
}

public final class RootChain: Chain {

private let map: SubscriptionMap

public var proceed: Next {
return { action in
if let set = self.map[action.innerTag] {
Expand All @@ -57,7 +57,7 @@ public final class RootChain: Chain {
return action
}
}

public init(map: SubscriptionMap) {
self.map = map
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/MiniSwift/ReducerGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public protocol Group: Disposable {
}

public class ReducerGroup: Group {

public let disposeBag = CompositeDisposable()

init(_ builder: () -> [Disposable]) {
let disposable = builder()
disposable.forEach { _ = disposeBag.insert($0) }
}

public func dispose() {
disposeBag.dispose()
}
Expand Down
30 changes: 15 additions & 15 deletions Sources/MiniSwift/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import RxSwift
public protocol StoreType {
associatedtype State: StateType
associatedtype StoreController: Disposable

var state: State { get set }
var dispatcher: Dispatcher { get }
var reducerGroup: ReducerGroup { get }
Expand Down Expand Up @@ -52,24 +52,24 @@ extension StoreType {
}

public class Store<State: StateType, StoreController: Disposable>: ObservableType, StoreType {

public typealias Element = State

public typealias State = State
public typealias StoreController = StoreController

public typealias ObjectWillChangePublisher = BehaviorSubject<State>

public var objectWillChange: ObjectWillChangePublisher

private var _initialState: State
public let dispatcher: Dispatcher
private var storeController: StoreController

private let queue = DispatchQueue(label: "atomic state")

private var _state: State

public var state: State {
get {
return _state
Expand All @@ -83,11 +83,11 @@ public class Store<State: StateType, StoreController: Disposable>: ObservableTyp
}
}
}

public var initialState: State {
return _initialState
}

public init(_ state: State,
dispatcher: Dispatcher,
storeController: StoreController) {
Expand All @@ -98,21 +98,21 @@ public class Store<State: StateType, StoreController: Disposable>: ObservableTyp
self.storeController = storeController
self.state = _initialState
}

public var reducerGroup: ReducerGroup {
return ReducerGroup {
[]
}
}

public func replayOnce() {
objectWillChange.onNext(state)
}

public func reset() {
state = initialState
}

public func subscribe<Observer: ObserverType>(_ observer: Observer) -> Disposable where Observer.Element == Store.Element {
return objectWillChange.subscribe(observer)
}
Expand Down
Loading

0 comments on commit bef760c

Please # to comment.