-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from day-23/dev
release: Version 1.0.5
- Loading branch information
Showing
56 changed files
with
1,568 additions
and
1,102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// Action.swift | ||
// Haru | ||
// | ||
// Created by 최정민 on 11/19/23. | ||
// | ||
|
||
import Foundation | ||
|
||
final class Action<T> { | ||
private(set) var key: String | ||
private(set) var updater: (T) -> T | ||
|
||
init(key: String, updater: @escaping (T) -> T) { | ||
self.key = key | ||
self.updater = updater | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// | ||
// Dispatcher.swift | ||
// Haru | ||
// | ||
// Created by 최정민 on 11/19/23. | ||
// | ||
|
||
import Foundation | ||
|
||
final class FluxDispatcher { | ||
private init() {} | ||
public static let `default`: FluxDispatcher = .init() | ||
|
||
private var stores: [String: Any] = [:] | ||
|
||
public func register<T, A: Action>(store: Store<T, A>) { | ||
if stores.keys.contains(store.id) { | ||
return | ||
} | ||
|
||
stores[store.id] = store | ||
} | ||
|
||
public func unregister(storeId id: String) { | ||
stores.removeValue(forKey: id) | ||
} | ||
|
||
public func get<T, A: Action>( | ||
for type: T.Type, | ||
actionType: A.Type) -> [Store<T, A>] | ||
{ | ||
var res: [Store<T, A>] = [] | ||
|
||
for store in stores.values { | ||
if let store = store as? Store<T, A> { | ||
res.append(store) | ||
} | ||
} | ||
|
||
return res | ||
} | ||
|
||
public func get<T, A: Action>( | ||
storeId id: String, | ||
for type: T.Type, | ||
actionType: A.Type) -> Store<T, A>? | ||
{ | ||
for store in get(for: type, actionType: actionType) { | ||
if store.id == id { | ||
return store | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
public func dispatch<T, A: Action>( | ||
action: A, | ||
params: UpdaterParameters = [:], | ||
for type: T.Type) | ||
{ | ||
for store in stores.values { | ||
if let store = store as? Store<T, A> { | ||
store.update(action: action, params: params) | ||
} | ||
} | ||
} | ||
|
||
public func dispatch<T, A: Action>( | ||
action: A, | ||
params: UpdaterParameters = [:], | ||
storeId id: String, | ||
for type: T.Type) | ||
{ | ||
for store in stores.values { | ||
if let store = store as? Store<T, A> { | ||
if store.id == id { | ||
store.update(action: action, params: params) | ||
return | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
let Dispatcher = FluxDispatcher.default |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// Reducer.swift | ||
// Haru | ||
// | ||
// Created by 최정민 on 11/20/23. | ||
// | ||
|
||
import Foundation | ||
|
||
final class Reducer<T> { | ||
private(set) var actions: Action<T> | ||
|
||
init(actions: Action<T>) { | ||
self.actions = actions | ||
} | ||
|
||
public func update(key: String) { | ||
switch key {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// Store.swift | ||
// Haru | ||
// | ||
// Created by 최정민 on 11/19/23. | ||
// | ||
|
||
import Foundation | ||
|
||
typealias Action = Hashable & RawRepresentable | ||
typealias UpdaterParameters = Any | ||
typealias Updater<T> = (T, UpdaterParameters) -> T | ||
typealias Reducer<T, A: Action> = [A: Updater<T>] | ||
|
||
final class Store<T, A: Action>: ObservableObject { | ||
public let id: String | ||
@Published private(set) var state: T | ||
private(set) var reducer: Reducer<T, A> = [:] | ||
|
||
public func update(action: A, params: UpdaterParameters) { | ||
guard let updater = reducer[action] else { | ||
return | ||
} | ||
|
||
state = updater(state, params) | ||
} | ||
|
||
init(id: String = UUID().uuidString, initialState state: T, reducer: [A: Updater<T>]) { | ||
self.id = id | ||
self.state = state | ||
self.reducer = reducer | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.