Skip to content

Commit 6780a33

Browse files
committed
### Added documentation/internal cleanup
--------------------------------------- * hasID -> hasIndex (because that's what we're looking for) * Removed `hasStored` since we only use it in one place. * Added documentation for the public facing parts.
1 parent 61e964f commit 6780a33

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

HeavyCore/Common/Entities/BehaviorStore.swift

+20-13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
// Copyright © 2016 Heavy. All rights reserved.
77
//
88

9+
/// Used for storing different behavior types so they can be retrieved easily.
10+
/// - Todo: Add pooling
911
public struct BehaviorStore {
1012
typealias BehaviorID = Int
1113
typealias Behaviors = [Behavior]
@@ -16,32 +18,34 @@ public struct BehaviorStore {
1618
public init() {}
1719

1820
private func has(type: Behavior.Type) -> BehaviorID? {
19-
guard let id = hasID(type) where hasStored(id) else { return nil }
21+
guard let id = hasIndex(type) where storage[id] != nil else { return nil }
2022
return id
2123
}
2224

23-
private func hasID(type: Behavior.Type) -> BehaviorID? {
25+
private func hasIndex(type: Behavior.Type) -> BehaviorID? {
2426
guard let id = storageIDs.indexOf({$0 == type}) else { return nil }
2527
return id
2628
}
2729

28-
private func hasStored(id: BehaviorID) -> Bool {
29-
guard let results = storage[id] else { return false }
30-
return !results.isEmpty
31-
}
32-
30+
/// Find all of the `Behavior.Type` this particular store contains.
31+
///
32+
/// - parameter type: The `Behavior.Type` you wish to find.
33+
///
34+
/// - returns: A collection of the `Behavior.Type` you are looking for or `nil`.
3335
public func find<T: Behavior>(type: T.Type) -> [T]? {
34-
guard let id = has(type),
35-
let store = storage[id]
36-
else { return nil }
36+
guard let id = has(type),
37+
store = storage[id] else { return nil }
3738
return store.flatMap { $0 as? T }
3839
}
3940

41+
/// Add a reference to a given `Behavior` to the store.
42+
///
43+
/// - parameter behavior: The `Behavior` reference you wish to store.
4044
public mutating func add<T: Behavior>(behavior: T) {
4145
let type = behavior.dynamicType
42-
let doesHaveID = hasID(type)
43-
let id = doesHaveID ?? storageIDs.count
44-
if doesHaveID == nil {
46+
let index = hasIndex(type)
47+
let id = index ?? storageIDs.count
48+
if index == nil {
4549
storageIDs.append(type)
4650
}
4751
if storage[id] == nil {
@@ -50,6 +54,9 @@ public struct BehaviorStore {
5054
storage[id]?.append(behavior)
5155
}
5256

57+
/// Remove this `Behavior` from the store.
58+
///
59+
/// - parameter behavior: A reference to the behavior you wish to remove.
5360
public mutating func remove(behavior: Behavior) {
5461
guard let id = has(behavior.dynamicType)
5562
where storage[id]?.count > 0

0 commit comments

Comments
 (0)