address linter warnings

This commit is contained in:
Igor Kravchenko 2020-09-30 16:11:35 +03:00
parent 386350ab1f
commit 53d2c27082
2 changed files with 63 additions and 58 deletions

View File

@ -90,7 +90,7 @@ extension ComponentTypeProvider: ComponentProvider {
/// This component provider always returns the same instance of the component. The instance
/// is created when first required and is of the type passed in to the initializer.
public class ComponentSingletonProvider {
lazy private var instance: Component = {
private lazy var instance: Component = {
componentType.init()
}()
@ -125,16 +125,16 @@ extension ComponentSingletonProvider: ComponentProvider {
public class DynamicComponentProvider {
/// Wrapper for closure to make it hashable
public class Closure {
let closure: () -> Component
let provideComponent: () -> Component
/// Initializer
/// - Parameter closure: Swift closure returning component of the appropriate type
public init(closure: @escaping () -> Component) {
self.closure = closure
/// - Parameter provideComponent: Swift closure returning component of the appropriate type
public init(provideComponent: @escaping () -> Component) {
self.provideComponent = provideComponent
}
}
private let closure: Closure
private let closure: Closure
/// Initializer
/// - Parameter closure: Instance of Closure class. A wrapper around closure that will
@ -155,7 +155,7 @@ extension DynamicComponentProvider: ComponentProvider {
/// Used to request a component from this provider
/// - Returns: The instance returned by calling the closure
public func getComponent() -> Component {
closure.closure()
closure.provideComponent()
}
}
@ -172,7 +172,8 @@ public class EntityState {
/// map a component type to the provider that provides the component.
/// - Parameter type: The type of component to be mapped
/// - Returns: The component mapping to use when setting the provider for the component
@discardableResult public func add(_ type: ComponentInitializable.Type) -> StateComponentMapping {
@discardableResult
public func add(_ type: ComponentInitializable.Type) -> StateComponentMapping {
StateComponentMapping(creatingState: self, type: type)
}
@ -215,7 +216,8 @@ public class StateComponentMapping {
/// ComponentInstanceProvider is used for the mapping.
/// - Parameter component: The component instance to use for the mapping
/// - Returns: This ComponentMapping, so more modifications can be applied
@discardableResult public func withInstance(_ component: Component) -> StateComponentMapping {
@discardableResult
public func withInstance(_ component: Component) -> StateComponentMapping {
setProvider(ComponentInstanceProvider(instance: component))
return self
}
@ -225,7 +227,8 @@ public class StateComponentMapping {
/// is used for the mapping.
/// - Parameter type: The type of components to be created by this mapping
/// - Returns: This ComponentMapping, so more modifications can be applied
@discardableResult public func withType(_ type: ComponentInitializable.Type) -> Self {
@discardableResult
public func withType(_ type: ComponentInitializable.Type) -> Self {
setProvider(ComponentTypeProvider(type: type))
return self
}
@ -237,7 +240,8 @@ public class StateComponentMapping {
/// - Parameter type: The type of the single instance to be created. If omitted, the type of the
/// mapping is used.
/// - Returns: This ComponentMapping, so more modifications can be applied
@discardableResult public func withSingleton(_ type: ComponentInitializable.Type?) -> Self {
@discardableResult
public func withSingleton(_ type: ComponentInitializable.Type?) -> Self {
setProvider(ComponentSingletonProvider(type: type ?? componentType))
return self
}
@ -246,7 +250,8 @@ public class StateComponentMapping {
/// DynamicComponentProvider is used for the mapping.
/// - Parameter method: The method to return the component instance
/// - Returns: This ComponentMapping, so more modifications can be applied
@discardableResult public func withMethod(_ closure: DynamicComponentProvider.Closure) -> Self {
@discardableResult
public func withMethod(_ closure: DynamicComponentProvider.Closure) -> Self {
setProvider(DynamicComponentProvider(closure: closure))
return self
}
@ -254,7 +259,8 @@ public class StateComponentMapping {
/// Creates a mapping for the component type to any ComponentProvider.
/// - Parameter provider: The component provider to use.
/// - Returns: This ComponentMapping, so more modifications can be applied.
@discardableResult public func withProvider(_ provider: ComponentProvider) -> Self {
@discardableResult
public func withProvider(_ provider: ComponentProvider) -> Self {
setProvider(provider)
return self
}
@ -299,7 +305,8 @@ public class EntityStateMachine<StateName: Hashable> {
/// - Parameter name: The name of this state - used to identify it later in the changeState method call.
/// - Parameter state: The state.
/// - Returns: This state machine, so methods can be chained.
@discardableResult public func addState(name: StateName, state: EntityState) -> Self {
@discardableResult
public func addState(name: StateName, state: EntityState) -> Self {
states[name] = state
return self
}
@ -314,7 +321,6 @@ public class EntityStateMachine<StateName: Hashable> {
return state
}
/// Change to a new state. The components from the old state will be removed and the components
/// for the new state will be added.
/// - Parameter name: The name of the state to change to.
@ -331,24 +337,24 @@ public class EntityStateMachine<StateName: Hashable> {
if let currentState = currentState {
toAdd = .init()
for t in newState.providers {
toAdd[t.key] = t.value
for (identifier, provider) in newState.providers {
toAdd[identifier] = provider
}
for t in currentState.providers {
if let other = toAdd[t.key], let current = currentState.providers[t.key],
current.identifier == other.identifier {
toAdd[t.key] = nil
for (identifier, _) in currentState.providers {
if let other = toAdd[identifier], let current = currentState.providers[identifier],
current.identifier == other.identifier {
toAdd[identifier] = nil
} else {
entity.remove(t.key)
entity.remove(identifier)
}
}
} else {
toAdd = newState.providers
}
for t in toAdd {
guard let component = toAdd[t.key]?.getComponent() else {
for (identifier, _) in toAdd {
guard let component = toAdd[identifier]?.getComponent() else {
continue
}
entity.assign(component)

View File

@ -72,7 +72,6 @@ class ComponentTypeProviderTests: XCTestCase {
self.value = false
}
}
}
class ComponentSingletonProviderTests: XCTestCase {