mobility-toolbox-js 3.0.1-beta.0 → 3.0.1-beta.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 (78) hide show
  1. package/README.md +11 -3
  2. package/api/HttpAPI.js +1 -3
  3. package/api/RealtimeAPI.d.ts +47 -47
  4. package/api/RealtimeAPI.js +74 -74
  5. package/api/WebSocketAPI.js +0 -1
  6. package/common/controls/StopFinderControlCommon.d.ts +1 -1
  7. package/common/controls/StopFinderControlCommon.js +1 -1
  8. package/common/styles/realtimeDefaultStyle.js +0 -5
  9. package/common/styles/realtimeHeadingStyle.js +0 -5
  10. package/common/styles/realtimeSimpleStyle.d.ts +0 -1
  11. package/common/styles/realtimeSimpleStyle.js +0 -1
  12. package/common/typedefs.d.ts +0 -117
  13. package/common/typedefs.js +0 -31
  14. package/common/utils/RealtimeEngine.d.ts +214 -0
  15. package/common/utils/RealtimeEngine.js +554 -0
  16. package/common/utils/getLayersAsFlatArray.d.ts +0 -1
  17. package/common/utils/getLayersAsFlatArray.js +0 -1
  18. package/common/utils/getVehiclePosition.js +1 -4
  19. package/common/utils/realtimeConfig.d.ts +1 -1
  20. package/common/utils/realtimeConfig.js +0 -1
  21. package/common/utils/renderTrajectories.d.ts +1 -0
  22. package/common/utils/renderTrajectories.js +1 -0
  23. package/common/utils/sortAndFilterDepartures.d.ts +1 -0
  24. package/common/utils/sortAndFilterDepartures.js +1 -0
  25. package/maplibre/controls/CopyrightControl.d.ts +9 -6
  26. package/maplibre/controls/CopyrightControl.js +11 -8
  27. package/maplibre/layers/Layer.d.ts +7 -6
  28. package/maplibre/layers/Layer.js +1 -2
  29. package/maplibre/layers/RealtimeLayer.d.ts +54 -111
  30. package/maplibre/layers/RealtimeLayer.js +126 -114
  31. package/maplibre/utils/getSourceCoordinates.d.ts +1 -0
  32. package/maplibre/utils/getSourceCoordinates.js +5 -4
  33. package/mbt.js +6960 -14605
  34. package/mbt.js.map +4 -4
  35. package/mbt.min.js +68 -71
  36. package/mbt.min.js.map +4 -4
  37. package/ol/controls/CopyrightControl.d.ts +13 -5
  38. package/ol/controls/CopyrightControl.js +13 -5
  39. package/ol/controls/RoutingControl.d.ts +29 -18
  40. package/ol/controls/RoutingControl.js +44 -56
  41. package/ol/controls/StopFinderControl.d.ts +21 -2
  42. package/ol/controls/StopFinderControl.js +22 -3
  43. package/ol/index.d.ts +0 -1
  44. package/ol/index.js +0 -1
  45. package/ol/layers/Layer.d.ts +17 -92
  46. package/ol/layers/Layer.js +17 -3
  47. package/ol/layers/MaplibreLayer.d.ts +47 -114
  48. package/ol/layers/MaplibreLayer.js +102 -46
  49. package/ol/layers/MaplibreStyleLayer.d.ts +67 -147
  50. package/ol/layers/MaplibreStyleLayer.js +170 -123
  51. package/ol/layers/RealtimeLayer.d.ts +85 -218
  52. package/ol/layers/RealtimeLayer.js +170 -181
  53. package/ol/layers/VectorLayer.d.ts +1 -2
  54. package/ol/layers/VectorLayer.js +7 -6
  55. package/ol/renderers/MaplibreLayerRenderer.d.ts +9 -0
  56. package/ol/renderers/MaplibreLayerRenderer.js +35 -137
  57. package/ol/renderers/MaplibreStyleLayerRenderer.js +2 -2
  58. package/ol/renderers/RealtimeLayerRenderer.d.ts +1 -1
  59. package/ol/renderers/RealtimeLayerRenderer.js +6 -31
  60. package/ol/styles/fullTrajectoryDelayStyle.js +5 -7
  61. package/ol/styles/fullTrajectoryStyle.d.ts +1 -2
  62. package/ol/styles/fullTrajectoryStyle.js +5 -7
  63. package/ol/styles/routingStyle.d.ts +0 -1
  64. package/ol/styles/routingStyle.js +13 -10
  65. package/ol/utils/defineDeprecatedProperties.d.ts +10 -0
  66. package/ol/utils/defineDeprecatedProperties.js +180 -0
  67. package/package.json +40 -39
  68. package/setupTests.js +14 -0
  69. package/types/common.d.ts +4 -27
  70. package/types/realtime.d.ts +7 -2
  71. package/common/mixins/RealtimeLayerMixin.d.ts +0 -267
  72. package/common/mixins/RealtimeLayerMixin.js +0 -751
  73. package/ol/mixins/MobilityLayerMixin.d.ts +0 -96
  74. package/ol/mixins/MobilityLayerMixin.js +0 -6
  75. package/ol/mixins/PropertiesLayerMixin.d.ts +0 -136
  76. package/ol/mixins/PropertiesLayerMixin.js +0 -178
  77. package/ol/mixins/index.d.ts +0 -1
  78. package/ol/mixins/index.js +0 -2
@@ -4,11 +4,11 @@ import { getMapGlCopyrights } from '../../common/utils';
4
4
  */
5
5
  const DEFAULT_SEPARATOR = ' | ';
6
6
  /**
7
- * Display layer's copyrights.
7
+ * Display layer's attributions trying to remove duplicated ones.
8
8
  *
9
9
  * @example
10
- * import { Map } from 'Maplibre-gl';
11
- * import { CopyrightControl } from 'mobility-toolbox-js/Maplibre';
10
+ * import { Map } from 'maplibre-gl';
11
+ * import { CopyrightControl } from 'mobility-toolbox-js/maplibre';
12
12
  *
13
13
  * const map = new Map({
14
14
  * container: 'map',
@@ -19,13 +19,20 @@ const DEFAULT_SEPARATOR = ' | ';
19
19
  * map.addControl(control);
20
20
  *
21
21
  *
22
- * @see <a href="/example/mb-copyright">Maplibre copyright example</a>
22
+ * @see <a href="/example/mb-realtime>MapLibre Realtime layer example</a>
23
23
  *
24
+ * @implements {maplibregl.IControl}
25
+ *
26
+ * @public
24
27
  */
25
28
  class CopyrightControl {
26
29
  constructor(options = {}) {
27
30
  this.options = options;
28
31
  }
32
+ // eslint-disable-next-line class-methods-use-this
33
+ getDefaultPosition() {
34
+ return 'bottom-right';
35
+ }
29
36
  onAdd(map) {
30
37
  this.map = map;
31
38
  if (!this.container) {
@@ -51,10 +58,6 @@ class CopyrightControl {
51
58
  this.map = undefined;
52
59
  return this.container;
53
60
  }
54
- // eslint-disable-next-line class-methods-use-this
55
- getDefaultPosition() {
56
- return 'bottom-right';
57
- }
58
61
  render() {
59
62
  var _a, _b;
60
63
  if (this.map && this.container) {
@@ -1,8 +1,9 @@
1
1
  import { CustomLayerInterface, Evented } from 'maplibre-gl';
2
2
  import type { AnyMapGlMap } from '../../types';
3
- export type LayerOptions = {
3
+ export interface LayerOptions {
4
4
  id?: string;
5
- };
5
+ }
6
+ export type CUSTOM = 'custom';
6
7
  /**
7
8
  * A class representing a layer to display on an Maplibre map.
8
9
  *
@@ -19,10 +20,10 @@ declare class Layer extends Evented implements CustomLayerInterface {
19
20
  id: string;
20
21
  map: AnyMapGlMap | undefined;
21
22
  options: LayerOptions;
22
- type: 'custom';
23
+ type: CUSTOM;
23
24
  constructor(options?: LayerOptions);
24
- onAdd(map: AnyMapGlMap, gl: WebGLRenderingContext | WebGL2RenderingContext): void;
25
- onRemove(map: AnyMapGlMap, gl: WebGLRenderingContext | WebGL2RenderingContext): void;
26
- render(gl: WebGLRenderingContext | WebGL2RenderingContext): void;
25
+ onAdd(map: AnyMapGlMap, gl: WebGL2RenderingContext | WebGLRenderingContext): void;
26
+ onRemove(map: AnyMapGlMap, gl: WebGL2RenderingContext | WebGLRenderingContext): void;
27
+ render(gl: WebGL2RenderingContext | WebGLRenderingContext): void;
27
28
  }
28
29
  export default Layer;
@@ -1,5 +1,5 @@
1
- import { v4 as uuid } from 'uuid';
2
1
  import { Evented } from 'maplibre-gl';
2
+ import { v4 as uuid } from 'uuid';
3
3
  /**
4
4
  * A class representing a layer to display on an Maplibre map.
5
5
  *
@@ -19,7 +19,6 @@ class Layer extends Evented {
19
19
  this.type = 'custom';
20
20
  this.options = options;
21
21
  this.id = options.id || uuid();
22
- this.type = 'custom';
23
22
  }
24
23
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
25
24
  onAdd(map, gl) {
@@ -1,82 +1,8 @@
1
- import { RealtimeLayerMixinOptions } from '../../common/mixins/RealtimeLayerMixin';
1
+ import { CanvasSourceSpecification, LayerSpecification } from 'maplibre-gl';
2
+ import RealtimeEngine, { RealtimeEngineOptions } from '../../common/utils/RealtimeEngine';
2
3
  import Layer, { LayerOptions } from './Layer';
3
- import type { AnyMapGlMap, ViewState } from '../../types';
4
- import type { RealtimeTrajectory } from '../../api/typedefs';
5
- export type RealtimeLayerOptions = LayerOptions & RealtimeLayerMixinOptions;
6
- declare const RealtimeLayer_base: {
7
- new (options: RealtimeLayerMixinOptions): {
8
- [x: string]: any;
9
- [x: symbol]: any;
10
- api: import("..").RealtimeAPI;
11
- bboxParameters?: Record<string, boolean | boolean[] | number | number[] | string | string[]>;
12
- canvas?: import("../../types").AnyCanvas;
13
- debounceRenderTrajectories: (viewState: ViewState, noInterpolate?: boolean) => void;
14
- debug: boolean;
15
- filter?: import("../../common/typedefs").FilterFunction;
16
- format: import("ol/format").GeoJSON;
17
- generalizationLevel?: import("../../types").RealtimeGeneralizationLevel;
18
- generalizationLevelByZoom: import("../../types").RealtimeGeneralizationLevel[];
19
- getGeneralizationLevelByZoom: (zoom: number) => import("../../types").RealtimeGeneralizationLevel;
20
- getMotsByZoom: (zoom: number) => import("../../types").RealtimeMot[];
21
- getRenderTimeIntervalByZoom: (zoom: number) => number;
22
- hoverVehicleId?: import("../../types").RealtimeTrainId;
23
- isUpdateBboxOnMoveEnd: boolean;
24
- live?: boolean;
25
- minZoomInterpolation: number;
26
- mode: import("../../types").RealtimeMode;
27
- mots?: import("../../types").RealtimeMot[];
28
- motsByZoom: import("../../types").RealtimeMot[][];
29
- onStart?: ((realtimeLayer: import("../../types").AnyLayer) => void) | undefined;
30
- onStop?: ((realtimeLayer: import("../../types").AnyLayer) => void) | undefined;
31
- pixelRatio?: number;
32
- renderState?: import("../../types").RealtimeRenderState;
33
- renderTimeIntervalByZoom: number[];
34
- requestId?: number;
35
- selectedVehicle: import("../../types").RealtimeTrajectory;
36
- selectedVehicleId?: import("../../types").RealtimeTrainId;
37
- sort?: import("../../common/typedefs").SortFunction;
38
- speed?: number;
39
- style?: import("../../types").RealtimeStyleFunction;
40
- styleOptions?: import("../../types").RealtimeStyleOptions;
41
- tenant: import("../../types").RealtimeTenant;
42
- throttleRenderTrajectories: (viewState: ViewState, noInterpolate?: boolean) => void;
43
- time?: Date;
44
- trajectories?: Record<import("../../types").RealtimeTrainId, import("../../types").RealtimeTrajectory>;
45
- updateTimeDelay?: number;
46
- updateTimeInterval?: number;
47
- useDebounce?: boolean;
48
- useRequestAnimationFrame?: boolean;
49
- useThrottle?: boolean;
50
- visibilityRef: import("ol/events").EventsKey;
51
- addTrajectory(trajectory: import("../../types").RealtimeTrajectory): void;
52
- attachToMap(map: import("../../types").AnyMap): void;
53
- defineProperties(options: RealtimeLayerMixinOptions): void;
54
- detachFromMap(): void;
55
- getFeatureInfoAtCoordinate(coordinate: import("ol/coordinate").Coordinate, options: import("../../types").LayerGetFeatureInfoOptions): Promise<import("../../types").LayerGetFeatureInfoResponse>;
56
- getRefreshTimeInMs(zoom?: number | undefined): number;
57
- getTrajectoryInfos(id: import("../../types").RealtimeTrainId): Promise<{
58
- fullTrajectory: import("../../api/WebSocketAPI").WebSocketAPIMessageEventData<import("../../types").RealtimeStopSequence[]> | import("../../api/WebSocketAPI").WebSocketAPIMessageEventData<import("../../types").RealtimeFullTrajectory>;
59
- stopSequence: import("../../api/WebSocketAPI").WebSocketAPIMessageEventData<import("../../types").RealtimeStopSequence[]> | import("../../api/WebSocketAPI").WebSocketAPIMessageEventData<import("../../types").RealtimeFullTrajectory>;
60
- }>;
61
- getVehicle(filterFc: import("../../common/typedefs").FilterFunction): import("../../types").RealtimeTrajectory[];
62
- highlightVehicle(id: import("../../types").RealtimeTrainId): void;
63
- onDeleteTrajectoryMessage(data: import("../../api/WebSocketAPI").WebSocketAPIMessageEventData<import("../../types").RealtimeTrainId>): void;
64
- onDocumentVisibilityChange(): void;
65
- onTrajectoryMessage(data: import("../../api/WebSocketAPI").WebSocketAPIMessageEventData<import("../../types").RealtimeTrajectory>): void;
66
- onZoomEnd(): void;
67
- purgeOutOfDateTrajectories(): void;
68
- purgeTrajectory(trajectory: import("../../types").RealtimeTrajectory, extent: [number, number, number, number], zoom: number): boolean;
69
- removeTrajectory(trajectoryOrId: import("../../types").RealtimeTrainId | import("../../types").RealtimeTrajectory): void;
70
- renderTrajectories(viewState: undefined | ViewState, noInterpolate: boolean | undefined): void;
71
- renderTrajectoriesInternal(viewState: ViewState, noInterpolate?: boolean): boolean;
72
- selectVehicle(id: import("../../types").RealtimeTrainId): void;
73
- setBbox(extent: [number, number, number, number], zoom: number): void;
74
- start(): void;
75
- startUpdateTime(): void;
76
- stop(): void;
77
- stopUpdateTime(): void;
78
- };
79
- } & typeof Layer;
4
+ import type { AnyCanvas, AnyMapGlMap } from '../../types';
5
+ export type RealtimeLayerOptions = LayerOptions & RealtimeEngineOptions;
80
6
  /**
81
7
  * A Maplibre layer able to display data from the [geOps Realtime API](https://developer.geops.io/apis/realtime/).
82
8
  *
@@ -100,7 +26,7 @@ declare const RealtimeLayer_base: {
100
26
  *
101
27
  *
102
28
  * @see <a href="/api/class/src/api/RealtimeAPI%20js~RealtimeAPI%20html">RealtimeAPI</a>
103
- * @see <a href="/example/mb-realtime>Live example</a>
29
+ * @see <a href="/example/mb-realtime>MapLibre Realtime layer example</a>
104
30
  *
105
31
  * @implements {maplibregl.CustomLayerInterface}
106
32
  * @extends {maplibregl.Evented}
@@ -113,7 +39,12 @@ declare const RealtimeLayer_base: {
113
39
  s
114
40
  * @public
115
41
  */
116
- declare class RealtimeLayer extends RealtimeLayer_base {
42
+ declare class RealtimeLayer extends Layer {
43
+ #private;
44
+ engine: RealtimeEngine;
45
+ layer: LayerSpecification;
46
+ source: CanvasSourceSpecification;
47
+ sourceId: string;
117
48
  /**
118
49
  * Constructor.
119
50
  *
@@ -128,16 +59,52 @@ declare class RealtimeLayer extends RealtimeLayer_base {
128
59
  * @param {RealtimeTenant} options.tenant Filter trains by its tenant. It filters trains on backend side.
129
60
  * @param {string} [options.url="wss://api.geops.io/tracker-ws/v1/"] The geOps Realtime API url.
130
61
  */
131
- constructor(options?: {});
62
+ constructor(options?: RealtimeLayerOptions);
63
+ /**
64
+ * Return the current view state. Used by the RealtimeEngine.
65
+ * @private
66
+ */
67
+ getViewState(): {
68
+ center?: undefined;
69
+ extent?: undefined;
70
+ pixelRatio?: undefined;
71
+ resolution?: undefined;
72
+ rotation?: undefined;
73
+ size?: undefined;
74
+ visible?: undefined;
75
+ zoom?: undefined;
76
+ } | {
77
+ center: import("ol/coordinate").Coordinate;
78
+ extent: number[];
79
+ pixelRatio: number;
80
+ resolution: number;
81
+ rotation: number;
82
+ size: number[];
83
+ visible: boolean;
84
+ zoom: number;
85
+ };
132
86
  /**
133
87
  * Add sources, layers and listeners to the map.
134
88
  */
135
- onAdd(map: AnyMapGlMap, gl: WebGLRenderingContext | WebGL2RenderingContext): void;
89
+ onAdd(map: AnyMapGlMap, gl: WebGL2RenderingContext | WebGLRenderingContext): void;
90
+ onLoad(): void;
91
+ /**
92
+ * Callback on 'move' event.
93
+ */
94
+ onMove(): void;
95
+ /**
96
+ * Callback on 'moveend' event.
97
+ */
98
+ onMoveEnd(): void;
99
+ /**
100
+ * Callback when the RealtimeEngine has rendered successfully.
101
+ */
102
+ onRealtimeEngineRender(): void;
136
103
  /**
137
104
  * Remove source, layers and listeners from the map.
138
105
  */
139
- onRemove(map: AnyMapGlMap, gl: WebGLRenderingContext | WebGL2RenderingContext): void;
140
- onLoad(): void;
106
+ onRemove(map: AnyMapGlMap, gl: WebGL2RenderingContext | WebGLRenderingContext): void;
107
+ onZoomEnd(): void;
141
108
  /**
142
109
  * Start updating vehicles position.
143
110
  *
@@ -150,32 +117,8 @@ declare class RealtimeLayer extends RealtimeLayer_base {
150
117
  * @public
151
118
  */
152
119
  stop(): void;
153
- /**
154
- * Render the trajectories using current map's size, resolution and rotation.
155
- * @param {boolean} noInterpolate if true, renders the vehicles without interpolating theirs positions.
156
- * @private
157
- */
158
- renderTrajectories(noInterpolate?: boolean): void;
159
- /**
160
- * Return the delay in ms before the next rendering.
161
- */
162
- getRefreshTimeInMs(): number;
163
- /**
164
- * Remove the trajectory form the list if necessary.
165
- */
166
- purgeTrajectory(trajectory: RealtimeTrajectory, extent: [number, number, number, number], zoom: number): boolean;
167
- /**
168
- * Send the current bbox to the websocket
169
- */
170
- setBbox(extent?: [number, number, number, number], zoom?: number): void;
171
- renderTrajectoriesInternal(viewState: ViewState, noInterpolate?: boolean): boolean;
172
- /**
173
- * Callback on 'move' event.
174
- */
175
- onMove(): void;
176
- /**
177
- * Callback on 'moveend' event.
178
- */
179
- onMoveEnd(): void;
120
+ get canvas(): AnyCanvas | undefined;
121
+ get pixelRatio(): number | undefined;
122
+ set pixelRatio(pixelRatio: number | undefined);
180
123
  }
181
124
  export default RealtimeLayer;
@@ -1,12 +1,22 @@
1
- // @ts-nocheck
2
- import { fromLonLat } from 'ol/proj';
3
- import { getWidth, getHeight } from 'ol/extent';
4
- import transformRotate from '@turf/transform-rotate';
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _RealtimeLayer_internalId;
5
13
  import { point } from '@turf/helpers';
6
- import RealtimeLayerMixin from '../../common/mixins/RealtimeLayerMixin';
7
- import Layer from './Layer';
14
+ import transformRotate from '@turf/transform-rotate';
15
+ import { getHeight, getWidth } from 'ol/extent';
16
+ import { fromLonLat } from 'ol/proj';
17
+ import RealtimeEngine from '../../common/utils/RealtimeEngine';
8
18
  import { getSourceCoordinates } from '../utils';
9
- import toMercatorExtent from '../../common/utils/toMercatorExtent';
19
+ import Layer from './Layer';
10
20
  /**
11
21
  * A Maplibre layer able to display data from the [geOps Realtime API](https://developer.geops.io/apis/realtime/).
12
22
  *
@@ -30,7 +40,7 @@ import toMercatorExtent from '../../common/utils/toMercatorExtent';
30
40
  *
31
41
  *
32
42
  * @see <a href="/api/class/src/api/RealtimeAPI%20js~RealtimeAPI%20html">RealtimeAPI</a>
33
- * @see <a href="/example/mb-realtime>Live example</a>
43
+ * @see <a href="/example/mb-realtime>MapLibre Realtime layer example</a>
34
44
  *
35
45
  * @implements {maplibregl.CustomLayerInterface}
36
46
  * @extends {maplibregl.Evented}
@@ -43,7 +53,7 @@ import toMercatorExtent from '../../common/utils/toMercatorExtent';
43
53
  s
44
54
  * @public
45
55
  */
46
- class RealtimeLayer extends RealtimeLayerMixin(Layer) {
56
+ class RealtimeLayer extends Layer {
47
57
  /**
48
58
  * Constructor.
49
59
  *
@@ -60,12 +70,17 @@ class RealtimeLayer extends RealtimeLayerMixin(Layer) {
60
70
  */
61
71
  constructor(options = {}) {
62
72
  var _a;
63
- const canvas = document.createElement('canvas');
64
- super(Object.assign({ canvas, id: 'realtime' }, options));
65
- /** @private */
73
+ const id = (options === null || options === void 0 ? void 0 : options.id) || 'realtime';
74
+ super(Object.assign(Object.assign({}, options), { id: 'realtime-custom-' + id }));
75
+ _RealtimeLayer_internalId.set(this, void 0);
76
+ __classPrivateFieldSet(this, _RealtimeLayer_internalId, id, "f");
77
+ this.engine = new RealtimeEngine(Object.assign({ getViewState: this.getViewState.bind(this), onRender: this.onRealtimeEngineRender.bind(this) }, options));
78
+ this.sourceId = __classPrivateFieldGet(this, _RealtimeLayer_internalId, "f");
66
79
  this.source = {
67
- id: this.id,
68
- type: 'canvas',
80
+ // Set to true if the canvas source is animated. If the canvas is static, animate should be set to false to improve performance.
81
+ animate: true,
82
+ // @ts-expect-error bad type definition
83
+ attribution: (_a = options.attribution) === null || _a === void 0 ? void 0 : _a.join(', '),
69
84
  canvas: this.canvas,
70
85
  // Set a default coordinates, it will be overrides on next data update
71
86
  coordinates: [
@@ -74,108 +89,47 @@ class RealtimeLayer extends RealtimeLayerMixin(Layer) {
74
89
  [2, 2],
75
90
  [0, 0],
76
91
  ],
77
- // Set to true if the canvas source is animated. If the canvas is static, animate should be set to false to improve performance.
78
- animate: true,
79
- attribution: (_a = options.attribution) === null || _a === void 0 ? void 0 : _a.join(', '),
80
92
  loaded: true,
93
+ type: 'canvas',
81
94
  };
82
- /** @private */
83
95
  this.layer = {
84
- id: `${this.id}-raster`,
85
- type: 'raster',
86
- source: this.id,
96
+ id: __classPrivateFieldGet(this, _RealtimeLayer_internalId, "f"),
87
97
  layout: {
88
98
  visibility: 'visible',
89
99
  },
90
100
  paint: {
91
- 'raster-opacity': 1,
92
101
  'raster-fade-duration': 0,
102
+ 'raster-opacity': 1,
93
103
  'raster-resampling': 'nearest', // important otherwise it looks blurry
94
104
  },
105
+ source: this.sourceId,
106
+ type: 'raster',
95
107
  };
96
- /** @private */
97
108
  this.onLoad = this.onLoad.bind(this);
98
- /** @private */
99
109
  this.onMove = this.onMove.bind(this);
100
- /** @private */
101
110
  this.onMoveEnd = this.onMoveEnd.bind(this);
102
- /** @private */
103
111
  this.onZoomEnd = this.onZoomEnd.bind(this);
104
112
  }
105
113
  /**
106
- * Add sources, layers and listeners to the map.
107
- */
108
- onAdd(map, gl) {
109
- super.onAdd(map, gl);
110
- if (map.isStyleLoaded()) {
111
- this.onLoad();
112
- }
113
- map.on('load', this.onLoad);
114
- }
115
- /**
116
- * Remove source, layers and listeners from the map.
117
- */
118
- onRemove(map, gl) {
119
- map.off('load', this.onLoad);
120
- if (map.getLayer(this.layer.id)) {
121
- map.removeLayer(this.layer.id);
122
- }
123
- if (map.getSource(this.id)) {
124
- map.removeSource(this.id);
125
- }
126
- super.onRemove(map, gl);
127
- }
128
- onLoad() {
129
- if (!this.map.getSource(this.id)) {
130
- this.map.addSource(this.id, this.source);
131
- }
132
- if (!this.map.getLayer(this.layer.id)) {
133
- this.map.addLayer(this.layer, this.id);
134
- }
135
- this.start();
136
- }
137
- /**
138
- * Start updating vehicles position.
139
- *
140
- * @public
141
- */
142
- start() {
143
- super.start();
144
- this.map.on('move', this.onMove);
145
- this.map.on('moveend', this.onMoveEnd);
146
- this.map.on('zoomend', this.onZoomEnd);
147
- }
148
- /**
149
- * Stop updating vehicles position.
150
- *
151
- * @public
152
- */
153
- stop() {
154
- var _a, _b, _c;
155
- super.stop();
156
- (_a = this.map) === null || _a === void 0 ? void 0 : _a.off('move', this.onMove);
157
- (_b = this.map) === null || _b === void 0 ? void 0 : _b.off('moveend', this.onMoveEnd);
158
- (_c = this.map) === null || _c === void 0 ? void 0 : _c.off('zoomend', this.onZoomEnd);
159
- }
160
- /**
161
- * Render the trajectories using current map's size, resolution and rotation.
162
- * @param {boolean} noInterpolate if true, renders the vehicles without interpolating theirs positions.
114
+ * Return the current view state. Used by the RealtimeEngine.
163
115
  * @private
164
116
  */
165
- renderTrajectories(noInterpolate = false) {
117
+ getViewState() {
166
118
  if (!this.map) {
167
- return;
119
+ return {};
168
120
  }
169
121
  if (!this.pixelRatio) {
170
122
  this.pixelRatio = 1;
171
123
  }
172
- const { width, height } = this.map.getCanvas();
124
+ const { height, width } = this.map.getCanvas();
173
125
  const center = this.map.getCenter();
174
126
  // We use turf here to have good transform.
127
+ // @ts-expect-error bad type definition
175
128
  const leftBottom = this.map.unproject({
176
129
  x: 0,
177
130
  y: height / this.pixelRatio,
178
131
  }); // southWest
132
+ // @ts-expect-error bad type definition
179
133
  const rightTop = this.map.unproject({
180
134
  x: width / this.pixelRatio,
181
135
  y: 0,
@@ -191,60 +145,118 @@ class RealtimeLayer extends RealtimeLayerMixin(Layer) {
191
145
  const yResolution = getHeight(bounds) / (height / this.pixelRatio);
192
146
  const res = Math.max(xResolution, yResolution);
193
147
  // Coordinate of trajectories are in mercator so we have to pass the proper resolution and center in mercator.
194
- const viewState = {
195
- size: [width / this.pixelRatio, height / this.pixelRatio],
148
+ return {
196
149
  center: fromLonLat([center.lng, center.lat]),
197
150
  extent: bounds,
151
+ pixelRatio: this.pixelRatio,
198
152
  resolution: res,
199
- zoom: this.map.getZoom() - 1,
200
153
  rotation: -(this.map.getBearing() * Math.PI) / 180,
201
- pixelRatio: this.pixelRatio,
154
+ size: [width / this.pixelRatio, height / this.pixelRatio],
155
+ visible: true,
156
+ zoom: this.map.getZoom() - 1,
202
157
  };
203
- super.renderTrajectories(viewState, noInterpolate);
204
158
  }
205
159
  /**
206
- * Return the delay in ms before the next rendering.
160
+ * Add sources, layers and listeners to the map.
207
161
  */
208
- getRefreshTimeInMs() {
209
- return super.getRefreshTimeInMs(this.map.getZoom());
162
+ onAdd(map, gl) {
163
+ super.onAdd(map, gl);
164
+ this.engine.attachToMap();
165
+ if (map.isStyleLoaded()) {
166
+ this.onLoad();
167
+ }
168
+ map.on('load', this.onLoad);
169
+ }
170
+ onLoad() {
171
+ var _a, _b, _c, _d;
172
+ if (!((_a = this.map) === null || _a === void 0 ? void 0 : _a.getSource(this.sourceId))) {
173
+ (_b = this.map) === null || _b === void 0 ? void 0 : _b.addSource(this.sourceId, this.source);
174
+ }
175
+ if (!((_c = this.map) === null || _c === void 0 ? void 0 : _c.getLayer(this.layer.id))) {
176
+ (_d = this.map) === null || _d === void 0 ? void 0 : _d.addLayer(this.layer, this.id);
177
+ }
178
+ this.start();
210
179
  }
211
180
  /**
212
- * Remove the trajectory form the list if necessary.
181
+ * Callback on 'move' event.
213
182
  */
214
- purgeTrajectory(trajectory, extent, zoom) {
215
- return super.purgeTrajectory(trajectory, extent || toMercatorExtent(this.map.getBounds()), zoom || Math.floor(this.map.getZoom() - 1));
183
+ onMove() {
184
+ this.engine.renderTrajectories();
216
185
  }
217
186
  /**
218
- * Send the current bbox to the websocket
187
+ * Callback on 'moveend' event.
219
188
  */
220
- setBbox(extent, zoom) {
221
- super.setBbox(extent || toMercatorExtent(this.map.getBounds()), zoom || this.map.getZoom() - 1);
189
+ onMoveEnd() {
190
+ this.engine.renderTrajectories();
191
+ if (this.engine.isUpdateBboxOnMoveEnd) {
192
+ this.engine.setBbox();
193
+ }
222
194
  }
223
- renderTrajectoriesInternal(viewState, noInterpolate = false) {
224
- const render = super.renderTrajectoriesInternal(viewState, noInterpolate);
225
- if (render && this.map.style) {
195
+ /**
196
+ * Callback when the RealtimeEngine has rendered successfully.
197
+ */
198
+ onRealtimeEngineRender() {
199
+ var _a;
200
+ if ((_a = this.map) === null || _a === void 0 ? void 0 : _a.style) {
226
201
  const extent = getSourceCoordinates(this.map, this.pixelRatio);
227
- const source = this.map.getSource(this.id);
202
+ const source = this.map.getSource(this.sourceId);
228
203
  if (source) {
204
+ // @ts-expect-error bad type definition
229
205
  source.setCoordinates(extent);
230
206
  }
231
207
  }
232
- return render;
233
208
  }
234
209
  /**
235
- * Callback on 'move' event.
210
+ * Remove source, layers and listeners from the map.
236
211
  */
237
- onMove() {
238
- this.renderTrajectories();
212
+ onRemove(map, gl) {
213
+ this.engine.detachFromMap();
214
+ this.stop();
215
+ map.off('load', this.onLoad);
216
+ if (map.getLayer(this.layer.id)) {
217
+ map.removeLayer(this.layer.id);
218
+ }
219
+ if (map.getSource(this.sourceId)) {
220
+ map.removeSource(this.sourceId);
221
+ }
222
+ super.onRemove(map, gl);
223
+ }
224
+ onZoomEnd() {
225
+ this.engine.onZoomEnd();
239
226
  }
240
227
  /**
241
- * Callback on 'moveend' event.
228
+ * Start updating vehicles position.
229
+ *
230
+ * @public
242
231
  */
243
- onMoveEnd() {
244
- this.renderTrajectories();
245
- if (this.isUpdateBboxOnMoveEnd) {
246
- this.setBbox();
247
- }
232
+ start() {
233
+ var _a, _b, _c;
234
+ this.engine.start();
235
+ (_a = this.map) === null || _a === void 0 ? void 0 : _a.on('move', this.onMove);
236
+ (_b = this.map) === null || _b === void 0 ? void 0 : _b.on('moveend', this.onMoveEnd);
237
+ (_c = this.map) === null || _c === void 0 ? void 0 : _c.on('zoomend', this.onZoomEnd);
238
+ }
239
+ /**
240
+ * Stop updating vehicles position.
241
+ *
242
+ * @public
243
+ */
244
+ stop() {
245
+ var _a, _b, _c;
246
+ this.engine.stop();
247
+ (_a = this.map) === null || _a === void 0 ? void 0 : _a.off('move', this.onMove);
248
+ (_b = this.map) === null || _b === void 0 ? void 0 : _b.off('moveend', this.onMoveEnd);
249
+ (_c = this.map) === null || _c === void 0 ? void 0 : _c.off('zoomend', this.onZoomEnd);
250
+ }
251
+ get canvas() {
252
+ return this.engine.canvas;
253
+ }
254
+ get pixelRatio() {
255
+ return this.engine.pixelRatio || 1;
256
+ }
257
+ set pixelRatio(pixelRatio) {
258
+ this.engine.pixelRatio = pixelRatio || 1;
248
259
  }
249
260
  }
261
+ _RealtimeLayer_internalId = new WeakMap();
250
262
  export default RealtimeLayer;
@@ -2,6 +2,7 @@ import { AnyMapGlMap } from '../../types';
2
2
  /**
3
3
  * Get the canvas source coordinates of the current map's extent.
4
4
  * @param {maplibregl.Map} map A map object.
5
+ * @param {number} [pixelRatio=1] The pixel ratio.
5
6
  */
6
7
  export declare const getSourceCoordinates: (map: AnyMapGlMap, pixelRatio?: number) => number[][];
7
8
  export default getSourceCoordinates;
@@ -1,20 +1,21 @@
1
1
  /**
2
2
  * Get the canvas source coordinates of the current map's extent.
3
3
  * @param {maplibregl.Map} map A map object.
4
+ * @param {number} [pixelRatio=1] The pixel ratio.
4
5
  */
5
6
  export const getSourceCoordinates = (map, pixelRatio = 1) => {
6
7
  // Requesting getBounds is not enough when we rotate the map, so we request manually each corner.
7
8
  const { height, width } = map.getCanvas();
8
- // @ts-expect-error
9
+ // @ts-expect-error - to fix
9
10
  const leftTop = map.unproject({ x: 0, y: 0 });
10
- // @ts-expect-error
11
+ // @ts-expect-error - to fix
11
12
  const leftBottom = map.unproject({ x: 0, y: height / pixelRatio }); // southWest
12
- // @ts-expect-error
13
+ // @ts-expect-error - to fix
13
14
  const rightBottom = map.unproject({
14
15
  x: width / pixelRatio,
15
16
  y: height / pixelRatio,
16
17
  });
17
- // @ts-expect-error
18
+ // @ts-expect-error - to fix
18
19
  const rightTop = map.unproject({ x: width / pixelRatio, y: 0 }); // north east
19
20
  return [
20
21
  [leftTop.lng, leftTop.lat],