Skip to content

Commit

Permalink
Changed potentially long-running computed properties to functions in …
Browse files Browse the repository at this point in the history
…ClusterManager.
  • Loading branch information
vospennikov committed Oct 1, 2023
1 parent 872a200 commit 94be1e9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
48 changes: 24 additions & 24 deletions Sources/ClusterMap/Public/Core/ClusterManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,9 @@ public final class ClusterManager<Annotation: CoordinateIdentifiable>
self.configuration = configuration
}

/// A collection of all annotations.
public var annotations: [Annotation] {
dispatchQueue.sync {
tree.findAnnotations(in: .world)
}
}

/// A collection of currently visible annotations on the map.
public var visibleAnnotations: [AnnotationType] = []

/// A collection of currently visible nested annotations on the map.
///
/// This includes individual annotations as well as annotations within visible clusters.
public var visibleNestedAnnotations: [Annotation] {
dispatchQueue.sync {
visibleAnnotations.reduce(into: [Annotation]()) { partialResult, annotationType in
switch annotationType {
case .annotation(let annotation):
partialResult.append(annotation)
case .cluster(let clusterAnnotation):
partialResult += clusterAnnotation.memberAnnotations
}
}
}
}


/// Adds a single annotation to the cluster manager.
///
/// - Parameter annotation: The annotation to be added.
Expand Down Expand Up @@ -122,6 +99,29 @@ public final class ClusterManager<Annotation: CoordinateIdentifiable>
self?.tree = QuadTree<Annotation>(rect: .world)
}
}

/// A collection of all annotations.
public func fetchAllAnnotations() -> [Annotation] {
dispatchQueue.sync {
tree.findAnnotations(in: .world)
}
}

/// A collection of currently visible nested annotations on the map.
///
/// This includes individual annotations as well as annotations within visible clusters.
public func fetchVisibleNestedAnnotations() -> [Annotation] {
dispatchQueue.sync {
visibleAnnotations.reduce(into: [Annotation]()) { partialResult, annotationType in
switch annotationType {
case .annotation(let annotation):
partialResult.append(annotation)
case .cluster(let clusterAnnotation):
partialResult += clusterAnnotation.memberAnnotations
}
}
}
}

/// Reloads the annotations on the map based on the current zoom level and visible map region.
///
Expand Down
10 changes: 5 additions & 5 deletions Tests/ClusterMapTests/Core/ClusterManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ final class ClusterManagerTests: XCTestCase {
clusterManager.add(annotations)
await clusterManager.reload(mapViewSize: .mediumMapSize, coordinateRegion: .mediumRegion)

XCTAssertTrue(clusterManager.visibleNestedAnnotations.count == 1000)
XCTAssertTrue(clusterManager.fetchVisibleNestedAnnotations().count == 1000)
}

func testRemoveInvisibleAnnotations() async {
Expand All @@ -68,7 +68,7 @@ final class ClusterManagerTests: XCTestCase {
clusterManager.add(annotations)
await clusterManager.reload(mapViewSize: .mediumMapSize, coordinateRegion: .mediumRegion)

XCTAssertTrue(clusterManager.visibleNestedAnnotations.count == 1000)
XCTAssertTrue(clusterManager.fetchVisibleNestedAnnotations().count == 1000)
}

func testMinCountForClustering() async {
Expand All @@ -78,7 +78,7 @@ final class ClusterManagerTests: XCTestCase {
clusterManager.add(annotations)
await clusterManager.reload(mapViewSize: .mediumMapSize, coordinateRegion: .mediumRegion)

XCTAssertTrue(clusterManager.visibleNestedAnnotations.count == 1000)
XCTAssertTrue(clusterManager.fetchVisibleNestedAnnotations().count == 1000)
}

func testCancelOperation() {
Expand All @@ -101,7 +101,7 @@ final class ClusterManagerTests: XCTestCase {
let result = XCTWaiter.wait(for: [expectation], timeout: 10)

XCTAssertTrue(result == .completed)
XCTAssertTrue(clusterManager.visibleNestedAnnotations.count == 1000)
XCTAssertTrue(clusterManager.fetchVisibleNestedAnnotations().count == 1000)
}

func testMultipleOperations() {
Expand All @@ -123,7 +123,7 @@ final class ClusterManagerTests: XCTestCase {
let result = XCTWaiter.wait(for: [expectation], timeout: 10)

XCTAssertTrue(result == .completed)
XCTAssertTrue(clusterManager.visibleNestedAnnotations.count == 1000)
XCTAssertTrue(clusterManager.fetchVisibleNestedAnnotations().count == 1000)
}
}

Expand Down

0 comments on commit 94be1e9

Please sign in to comment.