Skip to content

Commit f0a4ec7

Browse files
committed
fix(android): allow correct object disposal with dispose()
1 parent 139b8d5 commit f0a4ec7

File tree

6 files changed

+51
-24
lines changed

6 files changed

+51
-24
lines changed

src/ui-carto/BaseNative.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@ export abstract class BaseNative<T, U extends {}> extends Observable {
2626
createNative(options: U): T {
2727
return null;
2828
}
29+
dispose() {
30+
this.native = null;
31+
}
2932
}

src/ui-carto/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export abstract class BaseNative<T, U extends {}> extends Observable {
1717
constructor(options?: U, native?: T);
1818
initNativeView(native: T, options: U): void;
1919
getNative(): T;
20+
dispose();
2021
}
2122
export interface NativePropertyOptions {
2223
converter?: {

src/ui-carto/ui/index.d.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ export interface MapInteractionInfo {
4949
isZoomAction: boolean;
5050
}
5151

52-
5352
export class MapOptions {
5453
/**
5554
* @returns native Carto Color
@@ -185,7 +184,6 @@ export class MapOptions {
185184

186185
setWatermarkScale(scale: number): void;
187186
setZoomGestures(enabled: boolean): void;
188-
setRotationGestures(enabled: boolean): void;
189187
/**
190188
* @param zoomRange native Carto MapRange
191189
*/
@@ -244,4 +242,4 @@ export class CartoMap<T = DefaultLatLonKeys> extends View {
244242
clearPreloadingCaches();
245243
cancelAllTasks();
246244
captureRendering(wait?: boolean): Promise<ImageSource>;
247-
}
245+
}

src/ui-carto/utils/index.android.ts

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -123,41 +123,52 @@ export function setShowError(value: boolean) {
123123

124124
export class ZippedAssetPackage extends BaseNative<com.carto.utils.ZippedAssetPackage, ZippedAssetPackageOptions> {
125125
mInterface: com.akylas.carto.additions.AKAssetPackage.Interface;
126+
mBaseAssetPackage: com.akylas.carto.additions.AKAssetPackage;
127+
mAssetPackage: com.akylas.carto.additions.AKAssetPackage;
128+
mVectorTileStyleSetData: com.carto.core.BinaryData;
126129
constructor(options) {
127130
super(options);
128-
for (const property of ['mInterface']) {
129-
const descriptor = Object.getOwnPropertyDescriptor(DirAssetPackage.prototype, property);
130-
if (descriptor) {
131-
descriptor.enumerable = false;
132-
}
133-
}
131+
// Object.defineProperty(this, 'mInterface', { enumerable: false });
132+
// Object.defineProperty(this, 'mBaseAssetPackage', { enumerable: false });
133+
// Object.defineProperty(this, 'mAssetPackage', { enumerable: false });
134+
// Object.defineProperty(this, 'mVectorTileStyleSetData', { enumerable: false });
135+
// for (const property of ['mInterface']) {
136+
// const descriptor = Object.getOwnPropertyDescriptor(DirAssetPackage.prototype, property);
137+
// if (descriptor) {
138+
// descriptor.enumerable = false;
139+
// }
140+
// }
141+
}
142+
dispose(): void {
143+
super.dispose();
144+
this.mInterface = null;
145+
this.mBaseAssetPackage = null;
146+
this.mAssetPackage = null;
147+
this.mVectorTileStyleSetData = null;
134148
}
135149
createNative(options: ZippedAssetPackageOptions) {
136150
// if (File.exists(options.zipPath)) {
137-
let vectorTileStyleSetData: com.carto.core.BinaryData;
138151
if (options.liveReload === true) {
139152
const data = File.fromPath(getFileName(options.zipPath)).readSync();
140-
vectorTileStyleSetData = new com.carto.core.BinaryData(data);
153+
this.mVectorTileStyleSetData = new com.carto.core.BinaryData(data);
141154
} else {
142155
const zipPath = getRelativePathToApp(options.zipPath);
143-
console.log('zipPath', zipPath);
144-
vectorTileStyleSetData = com.carto.utils.AssetUtils.loadAsset(zipPath);
156+
this.mVectorTileStyleSetData = com.carto.utils.AssetUtils.loadAsset(zipPath);
145157
}
146-
let assetPackage: com.akylas.carto.additions.AKAssetPackage;
147158
if (options.basePack) {
148-
assetPackage = options.basePack.getNative();
159+
this.mBaseAssetPackage = options.basePack.getNative();
149160
}
150161
if (options.loadAsset && options.getAssetNames) {
151162
this.mInterface = new com.akylas.carto.additions.AKAssetPackage.Interface({
152163
getAssetNames: options.getAssetNames,
153164
loadAsset: options.loadAsset
154165
});
155-
assetPackage = new com.akylas.carto.additions.AKAssetPackage(this.mInterface, assetPackage);
166+
this.mAssetPackage = new com.akylas.carto.additions.AKAssetPackage(this.mInterface, this.mBaseAssetPackage);
156167
}
157-
if (assetPackage) {
158-
return new com.carto.utils.ZippedAssetPackage(vectorTileStyleSetData, assetPackage);
168+
if (this.mBaseAssetPackage || this.mAssetPackage) {
169+
return new com.carto.utils.ZippedAssetPackage(this.mVectorTileStyleSetData, this.mAssetPackage || this.mBaseAssetPackage);
159170
} else {
160-
return new com.carto.utils.ZippedAssetPackage(vectorTileStyleSetData);
171+
return new com.carto.utils.ZippedAssetPackage(this.mVectorTileStyleSetData);
161172
}
162173
// } else {
163174
// console.error(`could not find zip file: ${options.zipPath}`);
@@ -182,20 +193,28 @@ function walkDir(dirPath: string, cb: (str: string) => void, currentSubDir?: str
182193
});
183194
}
184195
export class DirAssetPackage extends BaseNative<com.akylas.carto.additions.AKAssetPackage, DirAssetPackageOptions> {
185-
assetNames: com.carto.core.StringVector;
196+
mAssetNames: com.carto.core.StringVector;
186197
mDirPath: string;
187198
mCartoDirPath: string;
188199
loadUsingNS = false;
189200
mInterface: com.akylas.carto.additions.AKAssetPackage.Interface;
190201
constructor(options) {
191202
super(options);
203+
204+
// Object.defineProperty(this, 'mInterface', { enumerable: false });
205+
// Object.defineProperty(this, 'mAssetNames', { enumerable: false });
192206
for (const property of ['mInterface']) {
193207
const descriptor = Object.getOwnPropertyDescriptor(DirAssetPackage.prototype, property);
194208
if (descriptor) {
195209
descriptor.enumerable = false;
196210
}
197211
}
198212
}
213+
dispose(): void {
214+
this.mInterface = null;
215+
this.mAssetNames = null;
216+
super.dispose();
217+
}
199218
createNative(options: DirAssetPackageOptions) {
200219
if (Folder.exists(getFileName(options.dirPath))) {
201220
const dirPath = options.dirPath;
@@ -227,15 +246,15 @@ export class DirAssetPackage extends BaseNative<com.akylas.carto.additions.AKAss
227246
return result;
228247
}
229248
public getAssetNames() {
230-
if (!this.assetNames) {
249+
if (!this.mAssetNames) {
231250
try {
232-
this.assetNames = new com.carto.core.StringVector();
251+
this.mAssetNames = new com.carto.core.StringVector();
233252
walkDir(this.mDirPath, (fileRelPath: string) => {
234-
this.assetNames.add(fileRelPath);
253+
this.mAssetNames.add(fileRelPath);
235254
});
236255
} catch (e) {}
237256
}
238-
return this.assetNames;
257+
return this.mAssetNames;
239258
}
240259
}
241260

src/ui-carto/vectorelements/index.android.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,9 @@ export abstract class BillboardStyleBuilder<T extends com.carto.styles.Billboard
165165

166166
mBuildStyle: com.carto.styles.Style;
167167
abstract buildStyle();
168+
169+
dispose(): void {
170+
this.mBuildStyle = null;
171+
super.dispose();
172+
}
168173
}

src/ui-carto/vectortiles/index.android.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export class MBVectorTileDecoder extends BaseVectorTileDecoder<com.carto.vectort
3838
return new com.carto.vectortiles.MBVectorTileDecoder(new com.carto.styles.CartoCSSStyleSet(options.cartoCss));
3939
}
4040
} else if (pack) {
41+
console.log('new com.carto.styles.CompiledStyleSet', pack, options.style, options);
4142
const vectorTileStyleSet = new com.carto.styles.CompiledStyleSet(pack, options.style);
4243
const result = new com.carto.vectortiles.MBVectorTileDecoder(vectorTileStyleSet);
4344
return result;

0 commit comments

Comments
 (0)