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

Dispatcher performance #9

Merged
merged 2 commits into from Feb 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion VueFlux/Internal/Dispatcher.swift
@@ -1,5 +1,5 @@
/// An action dispatcher for subscribed dispatch functions.
struct Dispatcher<State: VueFlux.State> {
final class Dispatcher<State: VueFlux.State> {
typealias Observers = Storage<(State.Action) -> Void>

/// Shared instance associated by generic type of State.
Expand Down
29 changes: 2 additions & 27 deletions VueFlux/Internal/DispatcherContext.swift
Expand Up @@ -2,7 +2,7 @@
struct DispatcherContext {
static let shared = DispatcherContext()

private let dispatchers = AtomicReference([Identifier: Any]())
private let dispatchers = AtomicReference([ObjectIdentifier: Any]())

private init() {}

Expand All @@ -14,7 +14,7 @@ struct DispatcherContext {
/// - Returns: An shared instance of Dispatcher.
func dispatcher<State: VueFlux.State>(for stateType: State.Type) -> Dispatcher<State> {
return dispatchers.modify { dispatchers in
let identifier = Identifier(for: stateType)
let identifier = ObjectIdentifier(stateType)
if let dispatcher = dispatchers[identifier] as? Dispatcher<State> {
return dispatcher
}
Expand All @@ -25,28 +25,3 @@ struct DispatcherContext {
}
}
}

private extension DispatcherContext {
/// Identifier for save dispatcher instance.
struct Identifier: Hashable {
let hashValue: Int

/// Create with arbitrary State protocol conformed type.
/// - Parameters:
/// - stateType: Type of state to make Dispatcher unique.
init<State: VueFlux.State>(for stateType: State.Type) {
hashValue = String(reflecting: stateType).hashValue
}

/// Compare whether two identifiers are equal.
///
/// - Parameters:
/// - lhs: An identifier to compare.
/// - rhs: Another identifier to compare.
///
/// - Returns: A Bool value indicating whether two identifiers are equal.
static func == (lhs: Identifier, rhs: Identifier) -> Bool {
return lhs.hashValue == rhs.hashValue
}
}
}