Skip to content

Commit

Permalink
Introduce RxStore.State
Browse files Browse the repository at this point in the history
  • Loading branch information
denizcoskun committed Mar 28, 2021
1 parent 39d2f24 commit 299c597
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ It is a naive implementation of [Redux](https://redux.js.org/) inspired by [@ngr

// Define your app store, it can have multiple sub states
class AppStore: RxStore {
var counterState = RxStoreSubject(0)
var counterState = RxStore.State(0)
}

// Define actions
Expand Down Expand Up @@ -58,8 +58,8 @@ appStore.dispatch(action: CounterAction.Increment)

// Define your app store, it can have multiple sub states
class AppStore: RxStore {
var counterState = RxStoreSubject(0)
var loadingState = RxStoreSubject(false)
var counterState = RxStore.State(0)
var loadingState = RxStore.State(false)
}

// Define actions
Expand Down Expand Up @@ -133,8 +133,8 @@ struct Todo {
typealias TodosState = Dictionary<Int, Todo>

class AppStore: RxStore {
var todosState = RxStoreSubject<TodosState>([:])
var loadingState = RxStoreSubject(false)
var todosState = RxStore.State<TodosState>([:])
var loadingState = RxStore.State(false)
}

enum Action: RxStore.Action {
Expand Down Expand Up @@ -193,8 +193,8 @@ let todoList = [mockTodo, mockTodo2]
let userTodoIds: Dictionary<Int, [Int]> = [userId:[mockTodo.id], userId2: [mockTodo2.id]]

class AppStore: RxStore {
var todos = RxStoreSubject(todoList)
var userTodos = RxStoreSubject(userTodoIds)
var todos = RxStore.State(todoList)
var userTodos = RxStore.State(userTodoIds)
}

let store = AppStore().initialize()
Expand Down
5 changes: 3 additions & 2 deletions Sources/RxStore/RxStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Combine



final public class RxStoreSubject<T: Equatable & Codable>: Subject {
public final class RxStoreSubject<T: Equatable & Codable>: Subject {

public typealias Output = T
public typealias Failure = Never
Expand Down Expand Up @@ -49,7 +49,7 @@ public protocol RxStoreAction {}
public protocol RxStoreProtocol : AnyObject {
typealias Action = RxStoreAction
typealias Reducer<T> = (T, Action) -> T
typealias State = Equatable & Codable
typealias State<T: Equatable & Codable> = RxStoreSubject<T>
typealias ActionSubject = PassthroughSubject<Action, Never>
var actions: PassthroughSubject<Action, Never>{ get }
var stream: AnyPublisher<Action, Never> {get set}
Expand Down Expand Up @@ -137,6 +137,7 @@ extension RxStoreProtocol {
public func select<R>(_ selector: @escaping Selector<R>) -> AnyPublisher<R, Never> {
return selector(self)
}

}


Expand Down
38 changes: 20 additions & 18 deletions Tests/RxStoreTests/RxStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ final class RxStoreTests: XCTestCase {
// results.


class TestStore: RxStore {
var counterState = RxStoreSubject(0)
class AppStore: RxStore {
var counterState = RxStore.State(0)
}


let reducer : RxStore.Reducer<Int> = {state, action in
let reducer : AppStore.Reducer<Int> = {state, action in
switch action {
case CounterAction.Increment:
return state + 1
Expand All @@ -32,7 +32,7 @@ final class RxStoreTests: XCTestCase {
}
}

let store = TestStore().registerReducer(for: \.counterState, reducer)
let store = AppStore().registerReducer(for: \.counterState, reducer)
.initialize()

store.dispatch(action: CounterAction.Increment)
Expand All @@ -45,7 +45,7 @@ final class RxStoreTests: XCTestCase {

func testEmptyActionsIgnored() {
class TestStore: RxStore {
let emptyState = RxStoreSubject(false)
let emptyState = RxStore.State(false)
}

let store = TestStore().registerReducer(for: \.emptyState, {state, action in
Expand Down Expand Up @@ -131,14 +131,25 @@ final class RxStoreTests: XCTestCase {

func testSelector() {
class AppStore: RxStore {
var todos = RxStoreSubject([mockTodo])
var userTodoIds = RxStoreSubject<Dictionary<Int, [Int]>>([userId:[mockTodo.id], userId2: [mockTodo2.id]])
var counter = RxStoreSubject(0)
var todos = RxStore.State([mockTodo])
var userTodoIds = RxStore.State<Dictionary<Int, [Int]>>([userId:[mockTodo.id], userId2: [mockTodo2.id]])
var counter = RxStore.State(0)
}
enum Action: RxStore.Action {
case AddTodo(Todo)
}

func counterReducer(_ state: Int, action: RxStore.Action) -> Int {
switch action {
case CounterAction.Increment:
return state + 1
case CounterAction.Decrement:
return state - 1
default:
return state
}
}

let store = AppStore()
.registerReducer(for: \.todos, {state, action in
if case Action.AddTodo(let todo) = action {
Expand All @@ -148,16 +159,7 @@ final class RxStoreTests: XCTestCase {
}
return state
})
.registerReducer(for: \.counter, {state, action in
switch action {
case CounterAction.Increment:
return state + 1
case CounterAction.Decrement:
return state - 1
default:
return state
}
})
.registerReducer(for: \.counter, counterReducer)
.initialize()

func getTodosForSelectedUser(_ userId: Int) -> AppStore.Selector<[Todo]> {
Expand Down

0 comments on commit 299c597

Please sign in to comment.