Skip to content

Commit

Permalink
Make Storage to RandomAccessCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
ra1028 committed Jan 4, 2018
1 parent ff1da9f commit df61887
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
1 change: 1 addition & 0 deletions .swiftlint.yml
Expand Up @@ -4,6 +4,7 @@ excluded:

disabled_rules:
- multiple_closures_with_trailing_closure
- identifier_name

trailing_whitespace:
ignores_empty_lines: true
Expand Down
6 changes: 3 additions & 3 deletions Tests/StorageTests.swift
Expand Up @@ -17,19 +17,19 @@ final class StorageTests: XCTestCase {
let key1 = storage.add(f1)
let key2 = storage.add(f2)

storage.forEach { f in f() }
for f in storage { f() }

XCTAssertEqual(value, 11)

storage.remove(for: key1)

storage.forEach { f in f() }
for f in storage { f() }

XCTAssertEqual(value, 21)

storage.remove(for: key2)

storage.forEach { f in f() }
for f in storage { f() }

XCTAssertEqual(value, 21)
}
Expand Down
2 changes: 1 addition & 1 deletion VueFlux/Internal/Dispatcher.swift
Expand Up @@ -18,7 +18,7 @@ struct Dispatcher<State: VueFlux.State> {
/// - action: An Action to be dispatch.
func dispatch(action: State.Action) {
observers.synchronized { observers in
observers.forEach { observer in
for observer in observers {
observer(action)
}
}
Expand Down
24 changes: 16 additions & 8 deletions VueFlux/Storage.swift
Expand Up @@ -28,15 +28,23 @@ public struct Storage<Element> {
buffer.remove(at: index)
}
}
}

extension Storage: RandomAccessCollection {
public var startIndex: Int {
return buffer.startIndex
}

/// Calls the given function on each element in the collection.
///
/// - Parameters:
/// - body: A function that takes an element of the collection as a parameter.
public func forEach(_ body: (Element) -> Void) {
for (_, element) in buffer {
body(element)
}
public var endIndex: Int {
return buffer.endIndex
}

public func index(after i: Int) -> Int {
return i + 1
}

public subscript(position: Int) -> Element {
return buffer[position].element
}
}

Expand Down
4 changes: 3 additions & 1 deletion VueFluxReactive/Internal/Subject.swift
Expand Up @@ -29,7 +29,9 @@ final class Subject<Value>: Subscribable {
/// - value: Value to send to all observers.
func send(value: Value) {
observers.synchronized { observers in
observers.forEach { $0(value) }
for observer in observers {
observer(value)
}
}
}
}

0 comments on commit df61887

Please sign in to comment.