Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid adding multiple onEvery listener #2735

Merged
merged 1 commit into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion ios/RCTMGL-v10/RCTMGLImages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ class RCTMGLImages : UIView, RCTMGLMapComponent {
func addToMap(_ map: RCTMGLMapView, style: Style) {
self.style = style
map.images.append(self)
map.setupEvents()

self.addNativeImages(style: style, nativeImages: nativeImageInfos)
self.addRemoteImages(style: style, remoteImages: images)
Expand Down
8 changes: 7 additions & 1 deletion ios/RCTMGL-v10/RCTMGLMapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ open class RCTMGLMapView : MapView {

var handleMapChangedEvents = Set<RCTMGLEvent.EventType>()

var eventListeners : [Cancelable] = []

private var isPendingInitialLayout = true
private var wasGestureActive = false
private var isGestureActive = false
Expand Down Expand Up @@ -347,11 +349,15 @@ open class RCTMGLMapView : MapView {

extension RCTMGLMapView {
private func onEvery<Payload>(event: MapEvents.Event<Payload>, handler: @escaping (RCTMGLMapView, MapEvent<Payload>) -> Void) {
self.mapView.mapboxMap.onEvery(event: event) { [weak self](mapEvent) in
let eventListener = self.mapView.mapboxMap.onEvery(event: event) { [weak self](mapEvent) in
guard let self = self else { return }

handler(self, mapEvent)
}
eventListeners.append(eventListener)
if eventListeners.count > 20 {
Logger.log(level:.warn, message: "RCTMGLMapView.onEvery, too much handler installed");
}
}

private func onNext<Payload>(event: MapEvents.Event<Payload>, handler: @escaping (RCTMGLMapView, MapEvent<Payload>) -> Void) {
Expand Down