Skip to content

Commit db7fde9

Browse files
committed
fix(ios): support setRunOnMainThread (true by default)
1 parent bc61e1b commit db7fde9

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

src/ui/index.ios.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { restrictedPanningProperty } from './cssproperties';
1818
import { MapOptions } from '.';
1919
import { CartoViewBase, Layers, MapClickedEvent, MapIdleEvent, MapMovedEvent, MapReadyEvent, MapStableEvent, isLicenseKeyRegistered, setLicenseKeyRegistered } from './index.common';
2020
import { ImageSource } from '@nativescript/core';
21+
import { executeOnMainThread } from '@nativescript/core/utils';
2122

2223
export { MapClickedEvent, MapIdleEvent, MapMovedEvent, MapReadyEvent, MapStableEvent, setLicenseKeyRegistered };
2324

@@ -46,6 +47,21 @@ export function getLicenseKey() {
4647
return licenseKey;
4748
}
4849

50+
let runOnMainThread = true;
51+
function mainThread(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
52+
const originalMethod = descriptor.value;
53+
//wrapping the original method
54+
descriptor.value = function (...args: any[]) {
55+
if (runOnMainThread) {
56+
executeOnMainThread(() => {
57+
originalMethod.apply(this, args);
58+
});
59+
} else {
60+
originalMethod.apply(this, args);
61+
}
62+
};
63+
}
64+
4965
@NativeClass
5066
class NTMapEventListenerImpl extends NTMapEventListener {
5167
private _owner: WeakRef<CartoMap<any>>;
@@ -56,13 +72,15 @@ class NTMapEventListenerImpl extends NTMapEventListener {
5672
return delegate;
5773
}
5874

75+
@mainThread
5976
public onMapIdle() {
6077
const owner = this._owner.get();
6178
if (owner && owner.hasListeners(MapIdleEvent)) {
6279
owner.notify({ eventName: MapIdleEvent, object: owner });
6380
}
6481
}
6582

83+
@mainThread
6684
public onMapMoved() {
6785
const owner = this._owner.get();
6886
if (owner && owner.hasListeners(MapMovedEvent)) {
@@ -73,6 +91,7 @@ class NTMapEventListenerImpl extends NTMapEventListener {
7391
});
7492
}
7593
}
94+
@mainThread
7695
public onMapStable() {
7796
const owner = this._owner.get();
7897

@@ -83,6 +102,7 @@ class NTMapEventListenerImpl extends NTMapEventListener {
83102
owner.userAction = false;
84103
}
85104
}
105+
@mainThread
86106
public onMapClicked(mapClickInfo: NTMapClickInfo) {
87107
const owner = this._owner.get();
88108
if (owner && owner.hasListeners(MapClickedEvent)) {
@@ -126,11 +146,9 @@ class NTRendererCaptureListenerImpl extends NTRendererCaptureListener {
126146
}
127147
}
128148
}
129-
130149
@NativeClass
131150
class MapView extends NTMapView {
132151
owner: CartoMap;
133-
public static setRunOnMainThread(value: boolean) {}
134152
touchesBeganWithEvent(touches, event) {
135153
super.touchesBeganWithEvent(touches, event);
136154
if (this.owner) {
@@ -172,6 +190,9 @@ export class CartoMap<T = DefaultLatLonKeys> extends CartoViewBase {
172190
}
173191
}
174192

193+
public static setRunOnMainThread(value: boolean) {
194+
runOnMainThread = value;
195+
}
175196
get mapView() {
176197
return this.nativeViewProtected as NTMapView;
177198
}
@@ -205,12 +226,12 @@ export class CartoMap<T = DefaultLatLonKeys> extends CartoViewBase {
205226
// When nativeView is tapped we get the owning JS object through this field.
206227
this.nativeView.owner = this;
207228
super.initNativeView();
229+
if (!this.projection) {
230+
this.projection = new EPSG4326();
231+
}
208232
this.mapView.setMapEventListener(NTMapEventListenerImpl.initWithOwner(new WeakRef(this)));
209233

210234
// const options = this.nativeViewProtected.getOptions();
211-
if (!this.projection) {
212-
this.projection = CartoMap.projection;
213-
}
214235
}
215236

216237
disposeNativeView(): void {

0 commit comments

Comments
 (0)