Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
95 lines (74 sloc) 2.05 KB
//
// Redux.swift
// ReduxApp
//
// Created by Armin Kroll on 12/12/2015.
// Copyright © 2015 jtribe. All rights reserved.
//
import Foundation
// MARK: Usually implemented by the app delegate
protocol Reduxable {
var store : Store? { get set }
}
// MARK: State
typealias State = Dictionary<String , Any>
// MARK: Actions
protocol ActionType {
var type : String { get }
var data : Any { get }
}
struct InitialAction : ActionType {
var type : String
var data : Any { get {return ""} }
init() {
self.type = "Initial"
}
}
// MARK: Subscriber
// Listeners are updatable and have an identity so they can be compared
protocol Subscriber
{
func update(_ state : State)
var identifier : String { get set }
}
// helper that can be use in implementations of Updatable to make it unique and identifieable so it can be filtered.
func generateIdentifier() -> String {
return UUID().uuidString
}
// Equatable for Updatables. This will allow us to filter Updatables (basically subscribers)
func !=(lhs: Subscriber, rhs: Subscriber) -> Bool {
return lhs.identifier != rhs.identifier
}
// MARK: Store
// Something that wants to transfer a state into another state
protocol Reducable
{
var reducer : (_ state : State? , _ action : ActionType) -> State? { get }
}
// Simple Redux Store implementation
struct Store : Reducable
{
var state : State?
var reducer : (_ state : State? , _ action : ActionType) -> State?
var subscribers : Array<Subscriber>
init(reducer: @escaping (State?, ActionType) -> State?)
{
self.reducer = reducer
print("call initial")
self.state = reducer(nil, InitialAction())
self.subscribers = []
}
mutating func dispatch(_ action: ActionType) {
self.state = self.reducer(self.state, action)
print("Current State:\n \(self.state)")
if let state = self.state {
self.subscribers.forEach { $0.update(state) }
}
}
mutating func subscribe(_ listener: Subscriber) {
self.subscribers.append(listener)
}
mutating func unsubscribe(_ listener: Subscriber ) {
self.subscribers = self.subscribers.filter({ $0 != listener })
}
}