Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor createStore to remove StateStream usage #4

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
excluded: # paths to ignore during linting. overridden by `included`.
- Carthage
- Pods

disabled_rules: # rule identifiers to exclude from running
- force_cast
128 changes: 108 additions & 20 deletions ReduxKitRxSwift.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Binary file not shown.
22 changes: 18 additions & 4 deletions ReduxKitRxSwift/ObservableAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,34 @@
import ReduxKit
import RxSwift

/**
ObservableActions take a payload type that executes once observed then
dispatches the result if successful.
*/
public protocol ObservableAction: Action {

var rawPayload: Observable<Action> { get set }
func replacePayload(payload: Observable<Action>) -> ObservableAction
// TODO: Refactor mutating functions back to ReduxKit.Action as protocols

/**
Immutabily modify the payload of an ObservableAction

- parameter payload: New payload

- returns: Returns a copy of the current ObservableAction with the
existing payload replaced with the supplied payload
*/
func replacePayload(payload: Observable<Action>) -> Self
}

public extension ObservableAction {

/// Default implementation of the standard action payload and type
public var payload: Any? { return rawPayload }
}

public protocol ObservableActionError: SimpleStandardAction, ErrorType {
}
public protocol ObservableActionError: SimpleStandardAction, ErrorType {}

public extension ObservableActionError {

public var error: Bool { return true }
}
32 changes: 0 additions & 32 deletions ReduxKitRxSwift/ObservableMiddleware.swift

This file was deleted.

65 changes: 0 additions & 65 deletions ReduxKitRxSwift/ReduxKitRxSwift.swift

This file was deleted.

34 changes: 34 additions & 0 deletions ReduxKitRxSwift/ReduxRxDisposable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// ReduxRxDisposable.swift
// ReduxKitRxSwift
//
// Created by Karl Bowden on 27/12/2015.
// Copyright © 2015 Redux. All rights reserved.
//

import RxSwift
import ReduxKit

/**
Simple implementation of ReduxDisposable to wrap RxSwift.Disposable types.

- note: RxSwift.Disposable does not have a `disposed` parameter that can be
exposed. Hence the disposed parameter always returns false.
*/
public struct ReduxRxDisposable: ReduxDisposable {

/// Fallback implementation. Always returns `false`
public let disposed: Bool = false

/// Cancels the observation and disposes of the connection
public let dispose: () -> ()

/**
Create a ReduxDisposable from an RxSwift.Disposable

- parameter disposable: RxSwift.Disposable
*/
public init(disposable: Disposable) {
dispose = disposable.dispose
}
}
37 changes: 37 additions & 0 deletions ReduxKitRxSwift/Store+init.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// Store+init.swift
// ReduxKitRxSwift
//
// Created by Karl Bowden on 27/12/2015.
// Copyright © 2015 Redux. All rights reserved.
//

import RxSwift
import ReduxKit

extension Store {

/**
Initialise a ReduxKit Store from an RxSwift PublishSubject and ReplayAction

- parameter subjectDispatcher: RxSwift.PublishSubject<Action>
- parameter stateSubject: ReplaySubject<State>
*/
public init(subjectDispatcher: PublishSubject<Action>, stateSubject: ReplaySubject<State>) {

dispatch = { action in
subjectDispatcher.onNext(action)
return action
}

subscribe = { subscriber in
return ReduxRxDisposable(disposable: stateSubject.subscribeNext(subscriber))
}

getState = {
var state: State!
stateSubject.take(1).subscribeNext { state = $0 }.dispose()
return state
}
}
}
36 changes: 36 additions & 0 deletions ReduxKitRxSwift/createStore.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// createStore.swift
// ReduxKitRxSwift
//
// Created by Karl Bowden on 27/12/2015.
// Copyright © 2015 Redux. All rights reserved.
//

import RxSwift
import ReduxKit

/**
Create a ReduxKit Store backed by an RxSwift PublishSubject and ReplaySubject.

- parameter reducer: ReduxKit reducer
- parameter state: Optional initial state

- returns: Store<State>
*/
public func createStore<State>(
reducer: ((State?, ReduxKit.Action) -> State),
state: State? = nil)
-> Store<State> {

typealias StoreCreator = (
reducer: ((State?, ReduxKit.Action) -> State),
state: State?)
-> Store<State>

let backing: (
publisher: PublishSubject<Action>,
subject: ReplaySubject<State>,
createStore: StoreCreator) = setupStoreBacking()

return backing.createStore(reducer: reducer, state: state)
}
39 changes: 39 additions & 0 deletions ReduxKitRxSwift/observableMiddleware.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// observableMiddleware.swift
// ReduxKitRxSwift
//
// Created by Karl Bowden on 23/12/2015.
// Copyright © 2015 Redux. All rights reserved.
//

import ReduxKit

/**
ReduxKit middleware that dispatches the result of an ObservableAction payload.

The success and failure states of the ObservableAction are connected to dispatch.

- parameter store: Store supplied by the StoreEnhancer

- returns: Dispatch transformer middleware
*/
public func observableMiddleware<State>(store: Store<State>) -> DispatchTransformer {
return { next in { action in
guard let originalAction = action as? ObservableAction else { return next(action) }

let observable = originalAction.rawPayload
.doOn(
onNext: { successAction in
store.dispatch(successAction)
},
onError: { error in
guard let errorAction = error as? Action else { return }
store.dispatch(errorAction)
})

let newAction = originalAction.replacePayload(observable)

return next(newAction)
}
}
}
52 changes: 52 additions & 0 deletions ReduxKitRxSwift/setupStoreBacking.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// setupStoreBacking.swift
// ReduxKitRxSwift
//
// Created by Karl Bowden on 27/12/2015.
// Copyright © 2015 Redux. All rights reserved.
//

import RxSwift
import ReduxKit

/**
Sets up a createStore function to create a ReduxKit.Store<State> using an
RxSwift PublishSubject and ReplaySubject.

__publisher__: `RxSwift.PublishSubject<Action>`
- Reduces and action and publishes it to the stateSubject

__subject__: `RxSwift.ReplaySubject<State>`
- A subscribable hot signal of resulting states

__createStore__: `(Reducer, State?) -> Store<State>`
- the createStore function to be used when creating a ReduxKit Store

- returns: (
RxSwift.PublishSubject,
RxSwift.ReplaySubject,
StoreCreator(reducer, state) -> Store<State>)
*/
public func setupStoreBacking<State>() -> (
publisher: PublishSubject<Action>,
subject: ReplaySubject<State>,
createStore: (reducer: ((State?, ReduxKit.Action) -> State), state: State?) -> Store<State>) {

typealias Subscriber = State -> ()

let subjectDispatcher: PublishSubject<Action> = PublishSubject()
let stateSubject = ReplaySubject<State>.create(bufferSize: 1)

return (subjectDispatcher, stateSubject, { reducer, state in

let initalState = state ?? reducer(state, DefaultAction())

// have the stateSubject subscribe to the dispatcher
subjectDispatcher
.scan(initalState, accumulator: reducer)
.startWith(initalState)
.subscribe(stateSubject)

return Store(subjectDispatcher: subjectDispatcher, stateSubject: stateSubject)
})
}
Loading