From df6188742b6e8053da521775bfc0134a49a3ace3 Mon Sep 17 00:00:00 2001 From: Ryo Aoyama Date: Fri, 5 Jan 2018 01:40:19 +0900 Subject: [PATCH] Make Storage to RandomAccessCollection --- .swiftlint.yml | 1 + Tests/StorageTests.swift | 6 +++--- VueFlux/Internal/Dispatcher.swift | 2 +- VueFlux/Storage.swift | 24 ++++++++++++++++-------- VueFluxReactive/Internal/Subject.swift | 4 +++- 5 files changed, 24 insertions(+), 13 deletions(-) diff --git a/.swiftlint.yml b/.swiftlint.yml index 47fc9b7..2bf3dfa 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -4,6 +4,7 @@ excluded: disabled_rules: - multiple_closures_with_trailing_closure + - identifier_name trailing_whitespace: ignores_empty_lines: true diff --git a/Tests/StorageTests.swift b/Tests/StorageTests.swift index 1fd91d3..fe51d88 100644 --- a/Tests/StorageTests.swift +++ b/Tests/StorageTests.swift @@ -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) } diff --git a/VueFlux/Internal/Dispatcher.swift b/VueFlux/Internal/Dispatcher.swift index bac8f4e..52da599 100644 --- a/VueFlux/Internal/Dispatcher.swift +++ b/VueFlux/Internal/Dispatcher.swift @@ -18,7 +18,7 @@ struct Dispatcher { /// - 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) } } diff --git a/VueFlux/Storage.swift b/VueFlux/Storage.swift index 7526169..5c5ad30 100644 --- a/VueFlux/Storage.swift +++ b/VueFlux/Storage.swift @@ -28,15 +28,23 @@ public struct Storage { 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 } } diff --git a/VueFluxReactive/Internal/Subject.swift b/VueFluxReactive/Internal/Subject.swift index 79de3fc..47df205 100644 --- a/VueFluxReactive/Internal/Subject.swift +++ b/VueFluxReactive/Internal/Subject.swift @@ -29,7 +29,9 @@ final class Subject: 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) + } } } }