@maplibre/maplibre-react-native 9.1.0 → 10.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. package/.eslintrc.js +10 -2
  2. package/.husky/pre-commit +5 -0
  3. package/.yarn/plugins/@yarnpkg/plugin-typescript.cjs +9 -0
  4. package/.yarn/sdks/eslint/bin/eslint.js +20 -0
  5. package/.yarn/sdks/eslint/lib/api.js +20 -0
  6. package/.yarn/sdks/eslint/package.json +6 -0
  7. package/.yarn/sdks/integrations.yml +5 -0
  8. package/.yarn/sdks/prettier/index.js +20 -0
  9. package/.yarn/sdks/prettier/package.json +6 -0
  10. package/.yarn/sdks/typescript/bin/tsc +20 -0
  11. package/.yarn/sdks/typescript/bin/tsserver +20 -0
  12. package/.yarn/sdks/typescript/lib/tsc.js +20 -0
  13. package/.yarn/sdks/typescript/lib/tsserver.js +225 -0
  14. package/.yarn/sdks/typescript/lib/tsserverlibrary.js +225 -0
  15. package/.yarn/sdks/typescript/lib/typescript.js +20 -0
  16. package/.yarn/sdks/typescript/package.json +6 -0
  17. package/CHANGELOG.md +18 -0
  18. package/android/rctmgl/build.gradle +7 -7
  19. package/android/rctmgl/src/main/java/com/mapbox/rctmgl/location/LocationManager.java +2 -2
  20. package/android/rctmgl/src/main/java/com/mapbox/rctmgl/modules/RCTMGLOfflineModule.java +21 -26
  21. package/docs/Annotation.md +8 -9
  22. package/docs/BackgroundLayer.md +2 -10
  23. package/docs/Callout.md +5 -5
  24. package/docs/Camera.md +18 -47
  25. package/docs/CircleLayer.md +2 -10
  26. package/docs/FillExtrusionLayer.md +2 -10
  27. package/docs/FillLayer.md +2 -10
  28. package/docs/HeatmapLayer.md +2 -10
  29. package/docs/ImageSource.md +3 -2
  30. package/docs/Images.md +5 -3
  31. package/docs/Light.md +1 -1
  32. package/docs/LineLayer.md +2 -10
  33. package/docs/MapView.md +53 -37
  34. package/docs/MarkerView.md +4 -2
  35. package/docs/NativeUserLocation.md +2 -2
  36. package/docs/PointAnnotation.md +5 -8
  37. package/docs/RasterLayer.md +2 -10
  38. package/docs/RasterSource.md +3 -2
  39. package/docs/ShapeSource.md +12 -7
  40. package/docs/Style.md +1 -1
  41. package/docs/SymbolLayer.md +3 -10
  42. package/docs/UserLocation.md +8 -10
  43. package/docs/VectorSource.md +6 -5
  44. package/docs/coordinates.md +6 -0
  45. package/docs/docs.json +518 -1272
  46. package/docs/location.md +6 -0
  47. package/docs/snapshotManager.md +3 -3
  48. package/javascript/@types/assets.d.ts +4 -0
  49. package/javascript/MGLModule.ts +37 -0
  50. package/javascript/Maplibre.ts +65 -0
  51. package/javascript/components/AbstractLayer.tsx +107 -0
  52. package/javascript/components/AbstractSource.tsx +27 -0
  53. package/javascript/components/BackgroundLayer.tsx +43 -0
  54. package/javascript/components/{Callout.js → Callout.tsx} +67 -65
  55. package/javascript/components/{Camera.js → Camera.tsx} +266 -251
  56. package/javascript/components/CircleLayer.tsx +46 -0
  57. package/javascript/components/FillExtrusionLayer.tsx +47 -0
  58. package/javascript/components/FillLayer.tsx +41 -0
  59. package/javascript/components/{HeadingIndicator.js → HeadingIndicator.tsx} +8 -8
  60. package/javascript/components/HeatmapLayer.tsx +44 -0
  61. package/javascript/components/ImageSource.tsx +78 -0
  62. package/javascript/components/Images.tsx +134 -0
  63. package/javascript/components/Light.tsx +67 -0
  64. package/javascript/components/LineLayer.tsx +42 -0
  65. package/javascript/components/{MapView.js → MapView.tsx} +365 -354
  66. package/javascript/components/MarkerView.tsx +104 -0
  67. package/javascript/components/{NativeBridgeComponent.js → NativeBridgeComponent.tsx} +44 -13
  68. package/javascript/components/NativeUserLocation.tsx +33 -0
  69. package/javascript/components/PointAnnotation.tsx +221 -0
  70. package/javascript/components/RasterLayer.tsx +41 -0
  71. package/javascript/components/RasterSource.tsx +117 -0
  72. package/javascript/components/ShapeSource.tsx +383 -0
  73. package/javascript/components/{Style.js → Style.tsx} +129 -38
  74. package/javascript/components/SymbolLayer.tsx +72 -0
  75. package/javascript/components/{UserLocation.js → UserLocation.tsx} +111 -110
  76. package/javascript/components/VectorSource.tsx +207 -0
  77. package/javascript/components/annotations/Annotation.tsx +134 -0
  78. package/javascript/index.ts +4 -0
  79. package/javascript/modules/location/{locationManager.js → locationManager.ts} +71 -10
  80. package/javascript/modules/offline/{OfflineCreatePackOptions.js → OfflineCreatePackOptions.ts} +20 -4
  81. package/javascript/modules/offline/{OfflinePack.js → OfflinePack.ts} +23 -7
  82. package/javascript/modules/offline/{offlineManager.js → offlineManager.ts} +81 -24
  83. package/javascript/modules/snapshot/{SnapshotOptions.js → SnapshotOptions.ts} +43 -6
  84. package/javascript/modules/snapshot/{snapshotManager.js → snapshotManager.ts} +5 -5
  85. package/javascript/requestAndroidLocationPermissions.ts +29 -0
  86. package/javascript/types/BaseProps.ts +5 -0
  87. package/javascript/types/OnPressEvent.ts +13 -0
  88. package/javascript/types/index.ts +7 -0
  89. package/javascript/utils/BridgeValue.ts +90 -0
  90. package/javascript/utils/{Logger.js → Logger.ts} +37 -18
  91. package/javascript/utils/MaplibreStyles.d.ts +1486 -0
  92. package/javascript/utils/StyleValue.ts +49 -0
  93. package/javascript/utils/animated/AbstractAnimatedCoordinates.ts +134 -0
  94. package/javascript/utils/animated/AnimatedCoordinatesArray.ts +112 -0
  95. package/javascript/utils/animated/{AnimatedExtractCoordinateFromArray.js → AnimatedExtractCoordinateFromArray.ts} +5 -5
  96. package/javascript/utils/animated/{AnimatedPoint.js → AnimatedPoint.ts} +20 -12
  97. package/javascript/utils/animated/{AnimatedRouteCoordinatesArray.js → AnimatedRouteCoordinatesArray.ts} +39 -14
  98. package/javascript/utils/animated/{AnimatedShape.js → AnimatedShape.ts} +31 -10
  99. package/javascript/utils/{deprecation.js → deprecation.ts} +9 -6
  100. package/javascript/utils/filterUtils.ts +9 -0
  101. package/javascript/utils/geoUtils.ts +79 -0
  102. package/javascript/utils/{index.js → index.ts} +44 -24
  103. package/javascript/utils/styleMap.ts +264 -0
  104. package/package.json +35 -27
  105. package/plugin/build/withMapLibre.js +18 -3
  106. package/scripts/autogenHelpers/globals.js +1 -1
  107. package/scripts/autogenerate.js +14 -6
  108. package/scripts/download-style-spec.sh +1 -1
  109. package/scripts/templates/MaplibreStyles.ts.ejs +99 -0
  110. package/scripts/templates/index.d.ts.ejs +71 -42
  111. package/scripts/templates/{styleMap.js.ejs → styleMap.ts.ejs} +3 -33
  112. package/setup-jest.js +1 -10
  113. package/tsconfig.json +20 -61
  114. package/index.d.ts +0 -955
  115. package/javascript/components/AbstractLayer.js +0 -75
  116. package/javascript/components/AbstractSource.js +0 -15
  117. package/javascript/components/BackgroundLayer.js +0 -97
  118. package/javascript/components/CircleLayer.js +0 -101
  119. package/javascript/components/FillExtrusionLayer.js +0 -98
  120. package/javascript/components/FillLayer.js +0 -94
  121. package/javascript/components/HeatmapLayer.js +0 -99
  122. package/javascript/components/ImageSource.js +0 -82
  123. package/javascript/components/Images.js +0 -119
  124. package/javascript/components/Light.js +0 -47
  125. package/javascript/components/LineLayer.js +0 -94
  126. package/javascript/components/MarkerView.js +0 -87
  127. package/javascript/components/NativeUserLocation.js +0 -41
  128. package/javascript/components/PointAnnotation.js +0 -216
  129. package/javascript/components/RasterLayer.js +0 -95
  130. package/javascript/components/RasterSource.js +0 -124
  131. package/javascript/components/ShapeSource.js +0 -372
  132. package/javascript/components/SymbolLayer.js +0 -120
  133. package/javascript/components/VectorSource.js +0 -200
  134. package/javascript/components/annotations/Annotation.js +0 -122
  135. package/javascript/index.js +0 -149
  136. package/javascript/utils/BridgeValue.js +0 -81
  137. package/javascript/utils/animated/AnimatedCoordinatesArray.js +0 -191
  138. package/javascript/utils/filterUtils.js +0 -7
  139. package/javascript/utils/geoUtils.js +0 -73
  140. package/javascript/utils/styleMap.js +0 -1932
  141. /package/javascript/utils/animated/{Animated.js → Animated.ts} +0 -0
@@ -0,0 +1,134 @@
1
+ import SymbolLayer from '../SymbolLayer';
2
+ import Animated from '../../utils/animated/Animated';
3
+ import AnimatedMapPoint from '../../utils/animated/AnimatedPoint';
4
+ import OnPressEvent from '../../types/OnPressEvent';
5
+ import {SymbolLayerStyleProps} from '../../utils/MaplibreStyles';
6
+
7
+ import React, {ReactElement} from 'react';
8
+ import {Animated as RNAnimated, Easing} from 'react-native';
9
+
10
+ interface AnnotationProps {
11
+ id: string;
12
+ animated?: boolean;
13
+ animationDuration?: number;
14
+ animationEasingFunction?(x: number): number;
15
+ coordinates?: number[];
16
+ onPress?(event: OnPressEvent): void;
17
+ children?: ReactElement | ReactElement[];
18
+ style?: object;
19
+ icon?: string | number | object;
20
+ }
21
+
22
+ type AnnotationState = {
23
+ shape: AnimatedMapPoint | GeoJSON.Point | null;
24
+ };
25
+
26
+ class Annotation extends React.Component<AnnotationProps, AnnotationState> {
27
+ static defaultProps = {
28
+ animated: false,
29
+ animationDuration: 1000,
30
+ animationEasingFunction: Easing.linear,
31
+ };
32
+
33
+ constructor(props: AnnotationProps) {
34
+ super(props);
35
+
36
+ const shape = this._getShapeFromProps(props);
37
+
38
+ this.state = {
39
+ shape: props.animated ? new AnimatedMapPoint(shape) : shape,
40
+ };
41
+
42
+ this.onPress = this.onPress.bind(this);
43
+ }
44
+
45
+ componentDidUpdate(prevProps: AnnotationProps): void {
46
+ if (!Array.isArray(this.props.coordinates)) {
47
+ this.setState({shape: null});
48
+ return;
49
+ }
50
+
51
+ const hasCoordChanged =
52
+ prevProps.coordinates?.[0] !== this.props.coordinates?.[0] ||
53
+ prevProps.coordinates?.[1] !== this.props.coordinates?.[1];
54
+
55
+ if (!hasCoordChanged) {
56
+ return;
57
+ }
58
+
59
+ if (this.props.animated && this.state.shape) {
60
+ // flush current animations
61
+ (this.state.shape as AnimatedMapPoint).stopAnimation();
62
+
63
+ (this.state.shape as AnimatedMapPoint)
64
+ .timing({
65
+ coordinates: this.props.coordinates,
66
+ easing: this.props.animationEasingFunction,
67
+ duration: this.props.animationDuration,
68
+ })
69
+ .start();
70
+ } else if (!this.state.shape || !this.props.animated) {
71
+ const shape = this._getShapeFromProps();
72
+
73
+ this.setState({
74
+ shape: this.props.animated ? new AnimatedMapPoint(shape) : shape,
75
+ });
76
+ }
77
+ }
78
+
79
+ onPress(event: OnPressEvent): void {
80
+ if (this.props.onPress) {
81
+ this.props.onPress(event);
82
+ }
83
+ }
84
+
85
+ _getShapeFromProps(props: Partial<AnnotationProps> = {}): GeoJSON.Point {
86
+ const lng = props.coordinates?.[0] || 0;
87
+ const lat = props.coordinates?.[1] || 0;
88
+ return {type: 'Point', coordinates: [lng, lat]};
89
+ }
90
+
91
+ get symbolStyle(): SymbolLayerStyleProps | undefined {
92
+ if (!this.props.icon) {
93
+ return undefined;
94
+ }
95
+ return Object.assign({}, this.props.style, {
96
+ iconImage: this.props.icon,
97
+ });
98
+ }
99
+
100
+ render(): ReactElement | null {
101
+ if (!this.props.coordinates) {
102
+ return null;
103
+ }
104
+
105
+ const children = [];
106
+
107
+ if (this.symbolStyle) {
108
+ children.push(
109
+ <SymbolLayer id={`${this.props.id}-symbol`} style={this.symbolStyle} />,
110
+ );
111
+ }
112
+
113
+ if (this.props.children) {
114
+ if (Array.isArray(this.props.children)) {
115
+ children.push(...this.props.children);
116
+ } else {
117
+ children.push(this.props.children);
118
+ }
119
+ }
120
+
121
+ return (
122
+ <Animated.ShapeSource
123
+ id={this.props.id}
124
+ onPress={this.onPress}
125
+ shape={
126
+ this.state.shape as RNAnimated.WithAnimatedObject<GeoJSON.Point>
127
+ }>
128
+ {children}
129
+ </Animated.ShapeSource>
130
+ );
131
+ }
132
+ }
133
+
134
+ export default Annotation;
@@ -0,0 +1,4 @@
1
+ export * from './Maplibre';
2
+ import * as Maplibre from './Maplibre';
3
+
4
+ export default Maplibre;
@@ -1,4 +1,8 @@
1
- import {NativeModules, NativeEventEmitter} from 'react-native';
1
+ import {
2
+ NativeModules,
3
+ NativeEventEmitter,
4
+ EmitterSubscription,
5
+ } from 'react-native';
2
6
 
3
7
  const MapLibreGL = NativeModules.MGLModule;
4
8
  const MapLibreGLLocationManager = NativeModules.MGLLocationModule;
@@ -7,7 +11,64 @@ export const LocationModuleEventEmitter = new NativeEventEmitter(
7
11
  MapLibreGLLocationManager,
8
12
  );
9
13
 
14
+ /**
15
+ * Location sent by locationManager
16
+ */
17
+ export interface Location {
18
+ coords: Coordinates;
19
+ timestamp?: number;
20
+ }
21
+
22
+ /**
23
+ * Coorinates sent by locationManager
24
+ */
25
+ interface Coordinates {
26
+ /**
27
+ * The heading (measured in degrees) relative to true north.
28
+ * Heading is used to describe the direction the device is pointing to (the value of the compass).
29
+ * Note that on Android this is incorrectly reporting the course value as mentioned in issue https://github.com/rnmapbox/maps/issues/1213
30
+ * and will be corrected in a future update.
31
+ */
32
+ heading?: number;
33
+
34
+ /**
35
+ * The direction in which the device is traveling, measured in degrees and relative to due north.
36
+ * The course refers to the direction the device is actually moving (not the same as heading).
37
+ */
38
+ course?: number;
39
+
40
+ /**
41
+ * The instantaneous speed of the device, measured in meters per second.
42
+ */
43
+ speed?: number;
44
+
45
+ /**
46
+ * The latitude in degrees.
47
+ */
48
+ latitude: number;
49
+
50
+ /**
51
+ * The longitude in degrees.
52
+ */
53
+ longitude: number;
54
+
55
+ /**
56
+ * The radius of uncertainty for the location, measured in meters.
57
+ */
58
+ accuracy?: number;
59
+
60
+ /**
61
+ * The altitude, measured in meters.
62
+ */
63
+ altitude?: number;
64
+ }
65
+
10
66
  class LocationManager {
67
+ _listeners: ((location: Location) => void)[];
68
+ _lastKnownLocation: Location | null;
69
+ _isListening: boolean;
70
+ subscription: EmitterSubscription | null;
71
+
11
72
  constructor() {
12
73
  this._listeners = [];
13
74
  this._lastKnownLocation = null;
@@ -16,7 +77,7 @@ class LocationManager {
16
77
  this.subscription = null;
17
78
  }
18
79
 
19
- async getLastKnownLocation() {
80
+ async getLastKnownLocation(): Promise<Location | null> {
20
81
  if (!this._lastKnownLocation) {
21
82
  let lastKnownLocation;
22
83
 
@@ -39,7 +100,7 @@ class LocationManager {
39
100
  return this._lastKnownLocation;
40
101
  }
41
102
 
42
- addListener(listener) {
103
+ addListener(listener: (location: Location) => void): void {
43
104
  if (!this._isListening) {
44
105
  this.start();
45
106
  }
@@ -52,19 +113,19 @@ class LocationManager {
52
113
  }
53
114
  }
54
115
 
55
- removeListener(listener) {
116
+ removeListener(listener: (location: Location) => void): void {
56
117
  this._listeners = this._listeners.filter(l => l !== listener);
57
118
  if (this._listeners.length === 0) {
58
119
  this.stop();
59
120
  }
60
121
  }
61
122
 
62
- removeAllListeners() {
123
+ removeAllListeners(): void {
63
124
  this._listeners = [];
64
125
  this.stop();
65
126
  }
66
127
 
67
- start(displacement = 0) {
128
+ start(displacement = 0): void {
68
129
  if (!this._isListening) {
69
130
  MapLibreGLLocationManager.start(displacement);
70
131
 
@@ -77,21 +138,21 @@ class LocationManager {
77
138
  }
78
139
  }
79
140
 
80
- stop() {
141
+ stop(): void {
81
142
  MapLibreGLLocationManager.stop();
82
143
 
83
144
  if (this._isListening) {
84
- this.subscription.remove();
145
+ this.subscription?.remove();
85
146
  }
86
147
 
87
148
  this._isListening = false;
88
149
  }
89
150
 
90
- setMinDisplacement(minDisplacement) {
151
+ setMinDisplacement(minDisplacement: number): void {
91
152
  MapLibreGLLocationManager.setMinDisplacement(minDisplacement);
92
153
  }
93
154
 
94
- onUpdate(location) {
155
+ onUpdate(location: Location): void {
95
156
  this._lastKnownLocation = location;
96
157
 
97
158
  this._listeners.forEach(l => l(location));
@@ -1,8 +1,24 @@
1
1
  import {makeLatLngBounds} from '../../utils/geoUtils';
2
2
  import {toJSONString} from '../../utils';
3
3
 
4
+ export interface OfflineCreatePackInputOptions {
5
+ name: string;
6
+ styleURL: string;
7
+ bounds: [GeoJSON.Position, GeoJSON.Position];
8
+ minZoom?: number;
9
+ maxZoom?: number;
10
+ metadata?: Record<string, any>;
11
+ }
12
+
4
13
  class OfflineCreatePackOptions {
5
- constructor(options = {}) {
14
+ name: string;
15
+ styleURL: string;
16
+ bounds: string;
17
+ minZoom?: number;
18
+ maxZoom?: number;
19
+ metadata: string;
20
+
21
+ constructor(options: OfflineCreatePackInputOptions) {
6
22
  this._assert(options);
7
23
 
8
24
  this.name = options.name;
@@ -13,7 +29,7 @@ class OfflineCreatePackOptions {
13
29
  this.metadata = this._makeMetadata(options.metadata);
14
30
  }
15
31
 
16
- _assert(options) {
32
+ _assert(options: OfflineCreatePackInputOptions): void {
17
33
  if (!options.styleURL) {
18
34
  throw new Error(
19
35
  'Style URL must be provided for creating an offline pack',
@@ -29,13 +45,13 @@ class OfflineCreatePackOptions {
29
45
  }
30
46
  }
31
47
 
32
- _makeLatLngBounds(bounds) {
48
+ _makeLatLngBounds(bounds: [GeoJSON.Position, GeoJSON.Position]): string {
33
49
  const ne = bounds[0];
34
50
  const sw = bounds[1];
35
51
  return toJSONString(makeLatLngBounds(ne, sw));
36
52
  }
37
53
 
38
- _makeMetadata(metadata) {
54
+ _makeMetadata(metadata?: Record<string, any>): string {
39
55
  return JSON.stringify({
40
56
  ...metadata,
41
57
  name: this.name,
@@ -1,38 +1,54 @@
1
+ import OfflineCreatePackOptions from './OfflineCreatePackOptions';
2
+
1
3
  import {NativeModules} from 'react-native';
2
4
 
3
5
  const MapLibreGLOfflineManager = NativeModules.MGLOfflineModule;
4
6
 
7
+ type OfflinePackStatus = {
8
+ name: string;
9
+ state: number;
10
+ percentage: number;
11
+ completedResourceCount: number;
12
+ completedResourceSize: number;
13
+ completedTileSize: number;
14
+ completedTileCount: number;
15
+ requiredResourceCount: number;
16
+ };
17
+
5
18
  class OfflinePack {
6
- constructor(pack) {
19
+ private pack: OfflineCreatePackOptions;
20
+ private _metadata: Record<string, any> | null;
21
+
22
+ constructor(pack: OfflineCreatePackOptions) {
7
23
  this.pack = pack;
8
24
  this._metadata = null;
9
25
  }
10
26
 
11
- get name() {
27
+ get name(): string | null {
12
28
  const {metadata} = this;
13
29
  return metadata && metadata.name;
14
30
  }
15
31
 
16
- get bounds() {
32
+ get bounds(): string {
17
33
  return this.pack.bounds;
18
34
  }
19
35
 
20
- get metadata() {
36
+ get metadata(): Record<string, any> | null {
21
37
  if (!this._metadata && this.pack.metadata) {
22
38
  this._metadata = JSON.parse(this.pack.metadata);
23
39
  }
24
40
  return this._metadata;
25
41
  }
26
42
 
27
- status() {
43
+ status(): Promise<OfflinePackStatus> {
28
44
  return MapLibreGLOfflineManager.getPackStatus(this.name);
29
45
  }
30
46
 
31
- resume() {
47
+ resume(): Promise<void> {
32
48
  return MapLibreGLOfflineManager.resumePackDownload(this.name);
33
49
  }
34
50
 
35
- pause() {
51
+ pause(): Promise<void> {
36
52
  return MapLibreGLOfflineManager.pausePackDownload(this.name);
37
53
  }
38
54
  }
@@ -1,9 +1,15 @@
1
1
  import {isUndefined, isFunction, isAndroid} from '../../utils';
2
2
 
3
- import OfflineCreatePackOptions from './OfflineCreatePackOptions';
3
+ import OfflineCreatePackOptions, {
4
+ OfflineCreatePackInputOptions,
5
+ } from './OfflineCreatePackOptions';
4
6
  import OfflinePack from './OfflinePack';
5
7
 
6
- import {NativeModules, NativeEventEmitter} from 'react-native';
8
+ import {
9
+ NativeModules,
10
+ NativeEventEmitter,
11
+ EventSubscription,
12
+ } from 'react-native';
7
13
 
8
14
  const MapLibreGL = NativeModules.MGLModule;
9
15
  const MapLibreGLOfflineManager = NativeModules.MGLOfflineModule;
@@ -11,12 +17,49 @@ export const OfflineModuleEventEmitter = new NativeEventEmitter(
11
17
  MapLibreGLOfflineManager,
12
18
  );
13
19
 
20
+ export type OfflineProgressStatus = {
21
+ name: string;
22
+ state: number;
23
+ percentage: number;
24
+ completedResourceSize: number;
25
+ completedTileCount: number;
26
+ completedResourceCount: number;
27
+ requiredResourceCount: number;
28
+ completedTileSize: number;
29
+ };
30
+
31
+ export type OfflinePackError = {
32
+ name: string;
33
+ message: string;
34
+ };
35
+
36
+ type ErrorEvent = {
37
+ payload: OfflinePackError;
38
+ };
39
+
40
+ type ProgressEvent = {
41
+ payload: OfflineProgressStatus;
42
+ };
43
+
44
+ type ProgressListener = (
45
+ pack: OfflinePack,
46
+ status: OfflineProgressStatus,
47
+ ) => void;
48
+ type ErrorListener = (pack: OfflinePack, err: OfflinePackError) => void;
49
+
14
50
  /**
15
51
  * OfflineManager implements a singleton (shared object) that manages offline packs.
16
52
  * All of this class’s instance methods are asynchronous, reflecting the fact that offline resources are stored in a database.
17
53
  * The shared object maintains a canonical collection of offline packs.
18
54
  */
19
55
  class OfflineManager {
56
+ _hasInitialized: boolean;
57
+ _offlinePacks: Record<string, OfflinePack>;
58
+ _progressListeners: Record<string, ProgressListener>;
59
+ _errorListeners: Record<string, ErrorListener>;
60
+ subscriptionProgress: EventSubscription | null;
61
+ subscriptionError: EventSubscription | null;
62
+
20
63
  constructor() {
21
64
  this._hasInitialized = false;
22
65
  this._offlinePacks = {};
@@ -52,7 +95,11 @@ class OfflineManager {
52
95
  * @param {Callback=} errorListener Callback that listens for status events while downloading the offline resource.
53
96
  * @return {void}
54
97
  */
55
- async createPack(options, progressListener, errorListener) {
98
+ async createPack(
99
+ options: OfflineCreatePackInputOptions,
100
+ progressListener: ProgressListener,
101
+ errorListener: ErrorListener,
102
+ ): Promise<void> {
56
103
  await this._initialize();
57
104
 
58
105
  const packOptions = new OfflineCreatePackOptions(options);
@@ -64,9 +111,8 @@ class OfflineManager {
64
111
  }
65
112
 
66
113
  this.subscribe(packOptions.name, progressListener, errorListener);
67
- const nativeOfflinePack = await MapLibreGLOfflineManager.createPack(
68
- packOptions,
69
- );
114
+ const nativeOfflinePack =
115
+ await MapLibreGLOfflineManager.createPack(packOptions);
70
116
  this._offlinePacks[packOptions.name] = new OfflinePack(nativeOfflinePack);
71
117
  }
72
118
 
@@ -81,7 +127,7 @@ class OfflineManager {
81
127
  * @param {String} name Name of the offline pack.
82
128
  * @return {void}
83
129
  */
84
- async invalidatePack(name) {
130
+ async invalidatePack(name: string): Promise<void> {
85
131
  if (!name) {
86
132
  return;
87
133
  }
@@ -103,7 +149,7 @@ class OfflineManager {
103
149
  * @param {String} name Name of the offline pack.
104
150
  * @return {void}
105
151
  */
106
- async deletePack(name) {
152
+ async deletePack(name: string): Promise<void> {
107
153
  if (!name) {
108
154
  return;
109
155
  }
@@ -128,7 +174,7 @@ class OfflineManager {
128
174
  *
129
175
  * @return {void}
130
176
  */
131
- async invalidateAmbientCache() {
177
+ async invalidateAmbientCache(): Promise<void> {
132
178
  await this._initialize();
133
179
  await MapLibreGLOfflineManager.invalidateAmbientCache();
134
180
  }
@@ -142,7 +188,7 @@ class OfflineManager {
142
188
  *
143
189
  * @return {void}
144
190
  */
145
- async clearAmbientCache() {
191
+ async clearAmbientCache(): Promise<void> {
146
192
  await this._initialize();
147
193
  await MapLibreGLOfflineManager.clearAmbientCache();
148
194
  }
@@ -157,7 +203,7 @@ class OfflineManager {
157
203
  * @param {Number} size Size of ambient cache.
158
204
  * @return {void}
159
205
  */
160
- async setMaximumAmbientCacheSize(size) {
206
+ async setMaximumAmbientCacheSize(size: number): Promise<void> {
161
207
  await this._initialize();
162
208
  await MapLibreGLOfflineManager.setMaximumAmbientCacheSize(size);
163
209
  }
@@ -170,7 +216,7 @@ class OfflineManager {
170
216
  *
171
217
  * @return {void}
172
218
  */
173
- async resetDatabase() {
219
+ async resetDatabase(): Promise<void> {
174
220
  await this._initialize();
175
221
  await MapLibreGLOfflineManager.resetDatabase();
176
222
  }
@@ -183,7 +229,7 @@ class OfflineManager {
183
229
  *
184
230
  * @return {Array<OfflinePack>}
185
231
  */
186
- async getPacks() {
232
+ async getPacks(): Promise<OfflinePack[]> {
187
233
  await this._initialize();
188
234
  return Object.keys(this._offlinePacks).map(
189
235
  name => this._offlinePacks[name],
@@ -199,7 +245,7 @@ class OfflineManager {
199
245
  * @param {String} name Name of the offline pack.
200
246
  * @return {OfflinePack}
201
247
  */
202
- async getPack(name) {
248
+ async getPack(name: string): Promise<OfflinePack> {
203
249
  await this._initialize();
204
250
  return this._offlinePacks[name];
205
251
  }
@@ -213,7 +259,7 @@ class OfflineManager {
213
259
  * @param {String} path Path to offline tile db on file system.
214
260
  * @return {void}
215
261
  */
216
- async mergeOfflineRegions(path) {
262
+ async mergeOfflineRegions(path: string): Promise<void> {
217
263
  await this._initialize();
218
264
  return MapLibreGLOfflineManager.mergeOfflineRegions(path);
219
265
  }
@@ -228,7 +274,7 @@ class OfflineManager {
228
274
  * @param {Number} limit Map tile limit count.
229
275
  * @return {void}
230
276
  */
231
- setTileCountLimit(limit) {
277
+ setTileCountLimit(limit: number): void {
232
278
  MapLibreGLOfflineManager.setTileCountLimit(limit);
233
279
  }
234
280
 
@@ -242,7 +288,7 @@ class OfflineManager {
242
288
  * @param {Number} throttleValue event throttle value in ms.
243
289
  * @return {void}
244
290
  */
245
- setProgressEventThrottle(throttleValue) {
291
+ setProgressEventThrottle(throttleValue: number): void {
246
292
  MapLibreGLOfflineManager.setProgressEventThrottle(throttleValue);
247
293
  }
248
294
 
@@ -260,7 +306,11 @@ class OfflineManager {
260
306
  * @param {Callback} errorListener Callback that listens for status events while downloading the offline resource.
261
307
  * @return {void}
262
308
  */
263
- async subscribe(packName, progressListener, errorListener) {
309
+ async subscribe(
310
+ packName: string,
311
+ progressListener: ProgressListener,
312
+ errorListener: ErrorListener,
313
+ ): Promise<void> {
264
314
  const totalProgressListeners = Object.keys(this._progressListeners).length;
265
315
  if (isFunction(progressListener)) {
266
316
  if (totalProgressListeners === 0) {
@@ -305,7 +355,7 @@ class OfflineManager {
305
355
  * @param {String} packName Name of the offline pack.
306
356
  * @return {void}
307
357
  */
308
- unsubscribe(packName) {
358
+ unsubscribe(packName: string): void {
309
359
  delete this._progressListeners[packName];
310
360
  delete this._errorListeners[packName];
311
361
 
@@ -324,7 +374,7 @@ class OfflineManager {
324
374
  }
325
375
  }
326
376
 
327
- async _initialize() {
377
+ async _initialize(): Promise<boolean> {
328
378
  if (this._hasInitialized) {
329
379
  return true;
330
380
  }
@@ -333,14 +383,16 @@ class OfflineManager {
333
383
 
334
384
  for (const nativeOfflinePack of nativeOfflinePacks) {
335
385
  const offlinePack = new OfflinePack(nativeOfflinePack);
336
- this._offlinePacks[offlinePack.name] = offlinePack;
386
+ if (offlinePack.name) {
387
+ this._offlinePacks[offlinePack.name] = offlinePack;
388
+ }
337
389
  }
338
390
 
339
391
  this._hasInitialized = true;
340
392
  return true;
341
393
  }
342
394
 
343
- _onProgress(e) {
395
+ _onProgress(e: ProgressEvent): void {
344
396
  const {name, state} = e.payload;
345
397
 
346
398
  if (!this._hasListeners(name, this._progressListeners)) {
@@ -356,7 +408,7 @@ class OfflineManager {
356
408
  }
357
409
  }
358
410
 
359
- _onError(e) {
411
+ _onError(e: ErrorEvent): void {
360
412
  const {name} = e.payload;
361
413
 
362
414
  if (!this._hasListeners(name, this._errorListeners)) {
@@ -367,7 +419,12 @@ class OfflineManager {
367
419
  this._errorListeners[name](pack, e.payload);
368
420
  }
369
421
 
370
- _hasListeners(name, listenerMap) {
422
+ _hasListeners(
423
+ name: string,
424
+ listenerMap:
425
+ | Record<string, ProgressListener>
426
+ | Record<string, ErrorListener>,
427
+ ): boolean {
371
428
  return (
372
429
  !isUndefined(this._offlinePacks[name]) && isFunction(listenerMap[name])
373
430
  );