mobility-toolbox-js 2.0.1-beta.13 → 2.2.0-beta.0

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 (259) hide show
  1. package/api/RealtimeAPI.d.ts +281 -0
  2. package/api/RealtimeAPI.d.ts.map +1 -0
  3. package/api/RealtimeAPI.js +490 -0
  4. package/api/RoutingAPI.d.ts +37 -0
  5. package/api/RoutingAPI.d.ts.map +1 -0
  6. package/api/RoutingAPI.js +32 -12
  7. package/api/StopsAPI.d.ts +38 -0
  8. package/api/StopsAPI.d.ts.map +1 -0
  9. package/api/StopsAPI.js +33 -9
  10. package/api/index.d.ts +4 -0
  11. package/api/index.d.ts.map +1 -0
  12. package/api/index.js +3 -3
  13. package/api/typedefs.d.ts +179 -0
  14. package/api/typedefs.d.ts.map +1 -0
  15. package/api/typedefs.js +75 -0
  16. package/common/api/HttpAPI.d.ts +31 -0
  17. package/common/api/HttpAPI.d.ts.map +1 -0
  18. package/common/api/HttpAPI.js +54 -27
  19. package/common/api/WebSocketAPI.d.ts +153 -0
  20. package/common/api/WebSocketAPI.d.ts.map +1 -0
  21. package/common/api/WebSocketAPI.js +330 -164
  22. package/common/controls/ControlCommon.d.ts +76 -0
  23. package/common/controls/ControlCommon.d.ts.map +1 -0
  24. package/common/controls/ControlCommon.js +150 -0
  25. package/common/controls/CopyrightControlCommon.d.ts +13 -0
  26. package/common/controls/CopyrightControlCommon.d.ts.map +1 -0
  27. package/common/controls/CopyrightControlCommon.js +34 -0
  28. package/common/controls/StopFinderControlCommon.d.ts +55 -0
  29. package/common/controls/StopFinderControlCommon.d.ts.map +1 -0
  30. package/common/controls/StopFinderControlCommon.js +144 -0
  31. package/common/index.d.ts +3 -0
  32. package/common/index.d.ts.map +1 -0
  33. package/common/index.js +2 -4
  34. package/common/layers/LayerCommon.d.ts +94 -0
  35. package/common/layers/LayerCommon.d.ts.map +1 -0
  36. package/common/layers/LayerCommon.js +244 -0
  37. package/common/mixins/RealtimeLayerMixin.d.ts +286 -0
  38. package/common/mixins/RealtimeLayerMixin.d.ts.map +1 -0
  39. package/common/mixins/RealtimeLayerMixin.js +779 -0
  40. package/common/mixins/UserInteractionsLayerMixin.d.ts +60 -0
  41. package/common/mixins/UserInteractionsLayerMixin.d.ts.map +1 -0
  42. package/common/mixins/UserInteractionsLayerMixin.js +241 -0
  43. package/common/styles/index.d.ts +5 -0
  44. package/common/styles/index.d.ts.map +1 -0
  45. package/common/styles/index.js +4 -4
  46. package/common/styles/realtimeDefaultStyle.d.ts +36 -0
  47. package/common/styles/realtimeDefaultStyle.d.ts.map +1 -0
  48. package/common/styles/realtimeDefaultStyle.js +275 -0
  49. package/common/styles/realtimeDelayStyle.d.ts +12 -0
  50. package/common/styles/realtimeDelayStyle.d.ts.map +1 -0
  51. package/common/styles/realtimeDelayStyle.js +13 -0
  52. package/common/styles/realtimeHeadingStyle.d.ts +12 -0
  53. package/common/styles/realtimeHeadingStyle.d.ts.map +1 -0
  54. package/common/styles/realtimeHeadingStyle.js +85 -0
  55. package/common/styles/realtimeSimpleStyle.d.ts +4 -0
  56. package/common/styles/realtimeSimpleStyle.d.ts.map +1 -0
  57. package/common/styles/realtimeSimpleStyle.js +23 -0
  58. package/common/typedefs.d.ts +111 -0
  59. package/common/typedefs.d.ts.map +1 -0
  60. package/common/typedefs.js +52 -0
  61. package/common/utils/compareDepartures.d.ts +11 -0
  62. package/common/utils/compareDepartures.d.ts.map +1 -0
  63. package/common/utils/compareDepartures.js +35 -0
  64. package/common/utils/createCanvas.d.ts +10 -0
  65. package/common/utils/createCanvas.d.ts.map +1 -0
  66. package/common/utils/createCanvas.js +27 -0
  67. package/common/utils/createRealtimeFilters.d.ts +13 -0
  68. package/common/utils/createRealtimeFilters.d.ts.map +1 -0
  69. package/common/utils/createRealtimeFilters.js +74 -0
  70. package/common/utils/debounceDeparturesMessages.d.ts +12 -0
  71. package/common/utils/debounceDeparturesMessages.d.ts.map +1 -0
  72. package/common/utils/debounceDeparturesMessages.js +24 -0
  73. package/common/utils/debounceWebsocketMessages.d.ts +11 -0
  74. package/common/utils/debounceWebsocketMessages.d.ts.map +1 -0
  75. package/common/utils/debounceWebsocketMessages.js +29 -0
  76. package/common/utils/getLayersAsFlatArray.d.ts +3 -0
  77. package/common/utils/getLayersAsFlatArray.d.ts.map +1 -0
  78. package/common/utils/getLayersAsFlatArray.js +15 -0
  79. package/common/utils/getMapboxMapCopyrights.d.ts +18 -0
  80. package/common/utils/getMapboxMapCopyrights.d.ts.map +1 -0
  81. package/common/utils/getMapboxMapCopyrights.js +26 -15
  82. package/common/utils/getMapboxRender.d.ts +7 -0
  83. package/common/utils/getMapboxRender.d.ts.map +1 -0
  84. package/common/utils/getMapboxRender.js +87 -0
  85. package/common/utils/getMaplibreRender.d.ts +7 -0
  86. package/common/utils/getMaplibreRender.d.ts.map +1 -0
  87. package/common/utils/getMaplibreRender.js +38 -0
  88. package/common/utils/getRealtimeModeSuffix.d.ts +10 -0
  89. package/common/utils/getRealtimeModeSuffix.d.ts.map +1 -0
  90. package/common/utils/getRealtimeModeSuffix.js +7 -0
  91. package/common/utils/getUrlWithParams.d.ts +9 -0
  92. package/common/utils/getUrlWithParams.d.ts.map +1 -0
  93. package/common/utils/getUrlWithParams.js +18 -0
  94. package/common/utils/getVehiclePosition.d.ts +16 -0
  95. package/common/utils/getVehiclePosition.d.ts.map +1 -0
  96. package/common/utils/getVehiclePosition.js +67 -37
  97. package/common/utils/index.d.ts +17 -0
  98. package/common/utils/index.d.ts.map +1 -0
  99. package/common/utils/index.js +17 -5
  100. package/common/utils/realtimeConfig.d.ts +49 -0
  101. package/common/utils/realtimeConfig.d.ts.map +1 -0
  102. package/common/utils/realtimeConfig.js +173 -0
  103. package/common/utils/removeDuplicate.d.ts +10 -0
  104. package/common/utils/removeDuplicate.d.ts.map +1 -0
  105. package/common/utils/removeDuplicate.js +12 -5
  106. package/common/utils/renderTrajectories.d.ts +17 -0
  107. package/common/utils/renderTrajectories.d.ts.map +1 -0
  108. package/common/utils/renderTrajectories.js +110 -0
  109. package/common/utils/sortAndFilterDepartures.d.ts +16 -0
  110. package/common/utils/sortAndFilterDepartures.d.ts.map +1 -0
  111. package/common/utils/sortAndFilterDepartures.js +58 -0
  112. package/common/utils/sortByDelay.d.ts +3 -0
  113. package/common/utils/sortByDelay.d.ts.map +1 -0
  114. package/common/utils/sortByDelay.js +17 -15
  115. package/common/utils/timeUtils.d.ts +24 -0
  116. package/common/utils/timeUtils.d.ts.map +1 -0
  117. package/common/utils/timeUtils.js +34 -15
  118. package/iife.d.ts +3 -0
  119. package/iife.d.ts.map +1 -0
  120. package/iife.js +5 -0
  121. package/index.d.ts +10 -0
  122. package/index.d.ts.map +1 -0
  123. package/index.js +8 -6
  124. package/mapbox/controls/CopyrightControl.d.ts +29 -0
  125. package/mapbox/controls/CopyrightControl.d.ts.map +1 -0
  126. package/mapbox/controls/CopyrightControl.js +44 -25
  127. package/mapbox/controls/index.d.ts +2 -0
  128. package/mapbox/controls/index.d.ts.map +1 -0
  129. package/mapbox/controls/index.js +2 -1
  130. package/mapbox/index.d.ts +6 -0
  131. package/mapbox/index.d.ts.map +1 -0
  132. package/mapbox/index.js +5 -4
  133. package/mapbox/layers/Layer.d.ts +59 -0
  134. package/mapbox/layers/Layer.d.ts.map +1 -0
  135. package/mapbox/layers/Layer.js +99 -55
  136. package/mapbox/layers/RealtimeLayer.d.ts +181 -0
  137. package/mapbox/layers/RealtimeLayer.d.ts.map +1 -0
  138. package/mapbox/layers/RealtimeLayer.js +276 -0
  139. package/mapbox/layers/index.d.ts +3 -0
  140. package/mapbox/layers/index.d.ts.map +1 -0
  141. package/mapbox/layers/index.js +2 -2
  142. package/mapbox/utils/getMercatorResolution.d.ts +9 -0
  143. package/mapbox/utils/getMercatorResolution.d.ts.map +1 -0
  144. package/mapbox/utils/getMercatorResolution.js +18 -0
  145. package/mapbox/utils/getSourceCoordinates.d.ts +9 -0
  146. package/mapbox/utils/getSourceCoordinates.d.ts.map +1 -0
  147. package/mapbox/utils/getSourceCoordinates.js +27 -0
  148. package/mapbox/utils/index.d.ts +3 -0
  149. package/mapbox/utils/index.d.ts.map +1 -0
  150. package/mapbox/utils/index.js +2 -0
  151. package/mbt.js +26074 -16501
  152. package/mbt.js.map +4 -4
  153. package/mbt.min.js +205 -126
  154. package/mbt.min.js.map +4 -4
  155. package/ol/controls/CopyrightControl.d.ts +31 -0
  156. package/ol/controls/CopyrightControl.d.ts.map +1 -0
  157. package/ol/controls/CopyrightControl.js +62 -36
  158. package/ol/controls/RoutingControl.d.ts +193 -0
  159. package/ol/controls/RoutingControl.d.ts.map +1 -0
  160. package/ol/controls/RoutingControl.js +601 -357
  161. package/ol/controls/StopFinderControl.d.ts +30 -0
  162. package/ol/controls/StopFinderControl.d.ts.map +1 -0
  163. package/ol/controls/StopFinderControl.js +30 -8
  164. package/ol/controls/index.d.ts +4 -0
  165. package/ol/controls/index.d.ts.map +1 -0
  166. package/ol/controls/index.js +3 -3
  167. package/ol/index.d.ts +6 -0
  168. package/ol/index.d.ts.map +1 -0
  169. package/ol/index.js +5 -5
  170. package/ol/layers/Layer.d.ts +86 -0
  171. package/ol/layers/Layer.d.ts.map +1 -0
  172. package/ol/layers/Layer.js +163 -77
  173. package/ol/layers/MapGlLayer.d.ts +67 -0
  174. package/ol/layers/MapGlLayer.d.ts.map +1 -0
  175. package/ol/layers/MapGlLayer.js +218 -0
  176. package/ol/layers/MapboxLayer.d.ts +50 -0
  177. package/ol/layers/MapboxLayer.d.ts.map +1 -0
  178. package/ol/layers/MapboxLayer.js +99 -193
  179. package/ol/layers/MapboxStyleLayer.d.ts +129 -0
  180. package/ol/layers/MapboxStyleLayer.d.ts.map +1 -0
  181. package/ol/layers/MapboxStyleLayer.js +362 -171
  182. package/ol/layers/MaplibreLayer.d.ts +28 -0
  183. package/ol/layers/MaplibreLayer.d.ts.map +1 -0
  184. package/ol/layers/MaplibreLayer.js +30 -135
  185. package/ol/layers/RealtimeLayer.d.ts +202 -0
  186. package/ol/layers/RealtimeLayer.d.ts.map +1 -0
  187. package/ol/layers/RealtimeLayer.js +340 -0
  188. package/ol/layers/RoutingLayer.d.ts +34 -0
  189. package/ol/layers/RoutingLayer.d.ts.map +1 -0
  190. package/ol/layers/RoutingLayer.js +72 -48
  191. package/ol/layers/VectorLayer.d.ts +25 -0
  192. package/ol/layers/VectorLayer.d.ts.map +1 -0
  193. package/ol/layers/VectorLayer.js +34 -18
  194. package/ol/layers/WMSLayer.d.ts +42 -0
  195. package/ol/layers/WMSLayer.d.ts.map +1 -0
  196. package/ol/layers/WMSLayer.js +84 -34
  197. package/ol/layers/index.d.ts +9 -0
  198. package/ol/layers/index.d.ts.map +1 -0
  199. package/ol/layers/index.js +8 -8
  200. package/ol/styles/fullTrajectoryDelayStyle.d.ts +4 -0
  201. package/ol/styles/fullTrajectoryDelayStyle.d.ts.map +1 -0
  202. package/ol/styles/fullTrajectoryDelayStyle.js +26 -24
  203. package/ol/styles/fullTrajectoryStyle.d.ts +5 -0
  204. package/ol/styles/fullTrajectoryStyle.d.ts.map +1 -0
  205. package/ol/styles/fullTrajectoryStyle.js +40 -39
  206. package/ol/styles/index.d.ts +3 -0
  207. package/ol/styles/index.d.ts.map +1 -0
  208. package/ol/styles/index.js +2 -2
  209. package/package.json +81 -133
  210. package/setupTests.d.ts +2 -0
  211. package/setupTests.d.ts.map +1 -0
  212. package/setupTests.js +26 -0
  213. package/types/common.d.ts +122 -0
  214. package/types/index.d.ts +13 -0
  215. package/types/realtime.d.ts +320 -0
  216. package/types/routing.d.ts +206 -0
  217. package/types/stops.d.ts +143 -0
  218. package/README.md +0 -23
  219. package/api/RoutingAPI.test.js +0 -25
  220. package/api/StopsAPI.test.js +0 -22
  221. package/api/TralisAPI.js +0 -359
  222. package/api/TralisAPI.test.js +0 -67
  223. package/api/TralisAPIUtils.js +0 -43
  224. package/common/Tracker.js +0 -93
  225. package/common/api/HttpAPI.test.js +0 -50
  226. package/common/api/WebSocketAPI.test.js +0 -311
  227. package/common/controls/Control.js +0 -81
  228. package/common/controls/Control.test.js +0 -87
  229. package/common/layers/Layer.js +0 -213
  230. package/common/layers/Layer.test.js +0 -526
  231. package/common/mixins/CopyrightMixin.js +0 -24
  232. package/common/mixins/SearchMixin.js +0 -110
  233. package/common/mixins/TralisLayerMixin.js +0 -479
  234. package/common/styles/trackerDefaultStyle.js +0 -197
  235. package/common/styles/trackerDelayStyle.js +0 -8
  236. package/common/styles/trackerSimpleStyle.js +0 -18
  237. package/common/trackerConfig.js +0 -152
  238. package/common/trackerConfig.test.js +0 -23
  239. package/common/utils/createTrackerFilters.js +0 -56
  240. package/common/utils/createTrackerFilters.test.js +0 -79
  241. package/common/utils/getMapboxMapCopyrights.test.js +0 -40
  242. package/common/utils/getMapboxStyleUrl.js +0 -22
  243. package/common/utils/removeDuplicate.test.js +0 -19
  244. package/common/utils/timeUtils.test.js +0 -10
  245. package/mapbox/layers/Layer.test.js +0 -182
  246. package/mapbox/layers/TralisLayer.js +0 -182
  247. package/mapbox/layers/TralisLayer.test.js +0 -38
  248. package/mapbox/utils.js +0 -32
  249. package/ol/controls/CopyrightControl.test.js +0 -165
  250. package/ol/controls/RoutingControl.test.js +0 -151
  251. package/ol/controls/StopFinderControl.test.js +0 -48
  252. package/ol/layers/Layer.test.js +0 -174
  253. package/ol/layers/MapboxLayer.test.js +0 -160
  254. package/ol/layers/MapboxStyleLayer.test.js +0 -231
  255. package/ol/layers/RoutingLayer.test.js +0 -40
  256. package/ol/layers/TralisLayer.js +0 -185
  257. package/ol/layers/TralisLayer.test.js +0 -79
  258. package/ol/layers/VectorLayer.test.js +0 -87
  259. package/ol/layers/WMSLayer.test.js +0 -76
@@ -1,175 +1,341 @@
1
- class WebSocketApi {
2
- constructor() {
3
- this.defineProperties();
4
- }
5
- defineProperties() {
6
- Object.defineProperties(this, {
7
- closed: {
8
- get: () => !!(this.websocket && this.websocket.readyState === this.websocket.CLOSED)
9
- },
10
- closing: {
11
- get: () => !!(this.websocket && this.websocket.readyState === this.websocket.CLOSING)
12
- },
13
- connecting: {
14
- get: () => !!(this.websocket && this.websocket.readyState === this.websocket.CONNECTING)
15
- },
16
- open: {
17
- get: () => !!(this.websocket && this.websocket.readyState === this.websocket.OPEN)
18
- },
19
- messagesOnOpen: {
20
- value: [],
21
- writable: true
22
- },
23
- subscriptions: {
24
- value: [],
25
- writable: true
26
- },
27
- subscribed: {
28
- value: {},
29
- writable: true
30
- }
31
- });
32
- }
33
- static getRequestString(method, params) {
34
- let reqStr = `${method} ${params.channel}`;
35
- reqStr += params.args ? ` ${params.args}` : "";
36
- reqStr += params.id ? ` ${params.id}` : "";
37
- return reqStr.trim();
38
- }
39
- connect(url, onOpen = () => {
40
- }) {
41
- if (this.websocket && !this.closed) {
42
- this.websocket.close();
1
+ /**
2
+ * Class used to facilitate connection to a WebSocketAPI and
3
+ * also to manage properly messages send to the WebSocketAPI.
4
+ * This class must not contain any specific implementation.
5
+ */
6
+ class WebSocketAPI {
7
+ constructor() {
8
+ this.defineProperties();
43
9
  }
44
- this.websocket = new WebSocket(url);
45
- if (!this.open) {
46
- this.websocket.addEventListener("open", () => {
47
- onOpen();
48
- this.subscribePreviousSubscriptions();
49
- });
50
- } else {
51
- onOpen();
52
- this.subscribePreviousSubscriptions();
10
+ defineProperties() {
11
+ Object.defineProperties(this, {
12
+ closed: {
13
+ get: () => !!(!this.websocket ||
14
+ this.websocket.readyState === this.websocket.CLOSED),
15
+ },
16
+ closing: {
17
+ get: () => !!(this.websocket &&
18
+ this.websocket.readyState === this.websocket.CLOSING),
19
+ },
20
+ connecting: {
21
+ get: () => !!(this.websocket &&
22
+ this.websocket.readyState === this.websocket.CONNECTING),
23
+ },
24
+ open: {
25
+ get: () => !!(this.websocket && this.websocket.readyState === this.websocket.OPEN),
26
+ },
27
+ /**
28
+ * Array of message to send on open.
29
+ * @type {Array<string>}
30
+ * @private
31
+ */
32
+ messagesOnOpen: {
33
+ value: [],
34
+ writable: true,
35
+ },
36
+ /**
37
+ * Array of subscriptions.
38
+ * @type {Array<WebSocketSubscription>}
39
+ * @private
40
+ */
41
+ subscriptions: {
42
+ value: [],
43
+ writable: true,
44
+ },
45
+ /**
46
+ * List of channels subscribed.
47
+ * @type {WebSocketSubscribed}
48
+ * @private
49
+ */
50
+ subscribed: {
51
+ value: {},
52
+ writable: true,
53
+ },
54
+ });
53
55
  }
54
- }
55
- close() {
56
- if (this.websocket) {
57
- this.websocket.onclose = null;
58
- this.websocket.close();
59
- this.websocket = null;
60
- this.messagesOnOpen = [];
56
+ /**
57
+ * Get the websocket request string.
58
+ *
59
+ * @param {string} method Request mehtod {GET, SUB}.
60
+ * @param {WebSocketParameters} params Request parameters.
61
+ * @param {string} params.channel Channel name
62
+ * @param {string} [params.args] Request arguments
63
+ * @param {Number|string} [params.id] Request identifier
64
+ * @return {string} request string
65
+ * @private
66
+ */
67
+ static getRequestString(method, params = {}) {
68
+ let reqStr = `${method} ${params.channel}`;
69
+ reqStr += params.args ? ` ${params.args}` : '';
70
+ reqStr += params.id ? ` ${params.id}` : '';
71
+ return reqStr.trim();
61
72
  }
62
- }
63
- send(message) {
64
- if (!this.websocket) {
65
- return;
73
+ /**
74
+ * (Re)connect the websocket.
75
+ *
76
+ * @param {string} url Websocket url.
77
+ * @param {function} onOpen Callback called when the websocket connection is opened and before subscriptions of previous subscriptions.
78
+ * @private
79
+ */
80
+ connect(url, onOpen = () => { }) {
81
+ var _a;
82
+ // if no url specify, close the current websocket and do nothing.
83
+ if (!url) {
84
+ (_a = this.websocket) === null || _a === void 0 ? void 0 : _a.close();
85
+ return;
86
+ }
87
+ // Behavior when a websocket already exists.
88
+ if (this.websocket) {
89
+ // If the current websocket has the same url and is open or is connecting, do nothing.
90
+ if (this.websocket.url === url && (this.open || this.connecting)) {
91
+ return;
92
+ }
93
+ // If the current websocket has not the same url and is open or is connecting, close it.
94
+ if (this.websocket.url !== url && (this.open || this.connecting)) {
95
+ this.websocket.close();
96
+ }
97
+ }
98
+ /** @ignore */
99
+ this.websocket = new WebSocket(url);
100
+ if (!this.open) {
101
+ this.websocket.addEventListener('open', () => {
102
+ onOpen();
103
+ this.subscribePreviousSubscriptions();
104
+ });
105
+ }
106
+ else {
107
+ onOpen();
108
+ this.subscribePreviousSubscriptions();
109
+ }
66
110
  }
67
- const send = () => {
68
- this.websocket.send(message);
69
- };
70
- if (!this.open) {
71
- if (!this.messagesOnOpen.includes(message)) {
72
- this.messagesOnOpen.push(message);
73
- this.websocket.addEventListener("open", () => {
74
- this.messagesOnOpen = [];
75
- send();
76
- });
77
- this.websocket.addEventListener("close", () => {
78
- this.messagesOnOpen = [];
79
- });
80
- }
81
- } else if (!this.messagesOnOpen.includes(message)) {
82
- send();
111
+ /**
112
+ * Close the websocket definitively.
113
+ *
114
+ * @private
115
+ */
116
+ close() {
117
+ if (this.websocket && (this.open || this.connecting)) {
118
+ this.websocket.onclose = () => { };
119
+ this.websocket.close();
120
+ this.messagesOnOpen = [];
121
+ }
122
+ }
123
+ /**
124
+ * Sends a message to the websocket.
125
+ *
126
+ * @param {message} message Message to send.
127
+ * @private
128
+ */
129
+ send(message) {
130
+ if (!this.websocket || this.closed || this.closing) {
131
+ return;
132
+ }
133
+ const send = () => {
134
+ var _a;
135
+ (_a = this.websocket) === null || _a === void 0 ? void 0 : _a.send(message);
136
+ };
137
+ if (!this.open) {
138
+ // This 'if' avoid sending 2 identical BBOX message on open,
139
+ if (!this.messagesOnOpen.includes(message)) {
140
+ this.messagesOnOpen.push(message);
141
+ this.websocket.addEventListener('open', () => {
142
+ this.messagesOnOpen = [];
143
+ send();
144
+ });
145
+ this.websocket.addEventListener('close', () => {
146
+ this.messagesOnOpen = [];
147
+ });
148
+ }
149
+ }
150
+ else if (!this.messagesOnOpen.includes(message)) {
151
+ send();
152
+ }
153
+ }
154
+ addEvents(onMessage, onError) {
155
+ if (this.websocket) {
156
+ this.websocket.addEventListener('message', onMessage);
157
+ if (onError) {
158
+ this.websocket.addEventListener('error', onError);
159
+ this.websocket.addEventListener('close', onError);
160
+ }
161
+ }
83
162
  }
84
- }
85
- listen(params, cb, errorCb) {
86
- this.unlisten(params, cb);
87
- const onMessage = (evt) => {
88
- let data = {};
89
- try {
90
- data = JSON.parse(evt.data);
91
- } catch (err) {
92
- console.error("WebSocket: unable to parse JSON data", err, evt.data);
93
- }
94
- let source = params.channel;
95
- source += params.args ? ` ${params.args}` : "";
96
- const contents = data.source === "buffer" ? data.content : [data];
97
- contents.forEach((content) => {
98
- if (content?.source === source && (!params.id || params.id === data.client_reference)) {
99
- cb(content);
100
- }
101
- });
102
- };
103
- if (this.websocket) {
104
- this.websocket.addEventListener("message", onMessage);
105
- if (errorCb) {
106
- this.websocket.addEventListener("error", errorCb);
107
- this.websocket.addEventListener("close", errorCb);
108
- }
163
+ removeEvents(onMessage, onError) {
164
+ if (this.websocket) {
165
+ this.websocket.removeEventListener('message', onMessage);
166
+ if (onError) {
167
+ this.websocket.removeEventListener('error', onError);
168
+ this.websocket.removeEventListener('close', onError);
169
+ }
170
+ }
171
+ }
172
+ /**
173
+ * Listen to websocket messages.
174
+ *
175
+ * @param {WebSocketParameters} params Parameters for the websocket get request
176
+ * @param {function} cb callback on listen
177
+ * @param {function} errorCb Callback on error
178
+ * @return {{onMessage: function, errorCb: function}} Object with onMessage and error callbacks
179
+ * @private
180
+ */
181
+ listen(params, cb, errorCb) {
182
+ // Remove the previous identical callback
183
+ this.unlisten(params, cb);
184
+ // We wrap the message callback to be sure we only propagate the message if it is for the right channel.
185
+ const onMessage = (evt) => {
186
+ let data;
187
+ try {
188
+ data = JSON.parse(evt.data);
189
+ }
190
+ catch (err) {
191
+ // eslint-disable-next-line no-console
192
+ console.error('WebSocket: unable to parse JSON data', err, evt.data);
193
+ return;
194
+ }
195
+ let source = params.channel;
196
+ source += params.args ? ` ${params.args}` : '';
197
+ // Buffer channel message return a list of other channels to propagate to proper callbacks.
198
+ let contents;
199
+ if (data.source === 'buffer') {
200
+ contents = data
201
+ .content;
202
+ }
203
+ else {
204
+ contents = [data];
205
+ }
206
+ contents.forEach((content) => {
207
+ // Because of backend optimization, the last content is null.
208
+ if ((content === null || content === void 0 ? void 0 : content.source) === source &&
209
+ (!params.id || params.id === data.client_reference)) {
210
+ cb(content);
211
+ }
212
+ });
213
+ };
214
+ this.addEvents(onMessage, errorCb);
215
+ return { onMessageCb: onMessage, onErrorCb: errorCb };
216
+ }
217
+ /**
218
+ * Unlisten websocket messages.
219
+ *
220
+ * @param {Object} params Parameters for the websocket get request.
221
+ * @param {function} cb Callback used when listen.
222
+ * @private
223
+ */
224
+ unlisten(params, cb) {
225
+ [...(this.subscriptions || []), ...(this.requests || [])]
226
+ .filter((s) => s.params.channel === params.channel && (!cb || s.cb === cb))
227
+ .forEach(({ onMessageCb, onErrorCb }) => {
228
+ this.removeEvents(onMessageCb, onErrorCb);
229
+ });
109
230
  }
110
- return { onMessageCb: onMessage, onErrorCb: errorCb };
111
- }
112
- unlisten(params, cb) {
113
- if (!this.websocket) {
114
- return;
231
+ /**
232
+ * Sends a get request to the websocket.
233
+ * The callback is called only once, when the response is received or when the call returns an error.
234
+ *
235
+ * @param {Object} params Parameters for the websocket get request
236
+ * @param {function} onMessage callback on message event
237
+ * @param {function} onError Callback on error and close event
238
+ * @private
239
+ */
240
+ get(params, cb, errorCb) {
241
+ const requestString = WebSocketAPI.getRequestString('GET', params);
242
+ this.send(requestString);
243
+ // We wrap the callbacks to make sure they are called only once.
244
+ const once = (callback) =>
245
+ // @ts-ignore: Spread error
246
+ (...args) => {
247
+ // @ts-ignore: Spread error
248
+ callback(...args);
249
+ const index = this.requests.findIndex((request) => requestString === request.requestString && cb === request.cb);
250
+ const { onMessageCb, onErrorCb } = this.requests[index];
251
+ this.removeEvents(onMessageCb, onErrorCb);
252
+ this.requests.splice(index, 1);
253
+ };
254
+ const { onMessageCb, onErrorCb } = this.listen(params, once(cb), errorCb && once(errorCb));
255
+ // Store requests and callbacks to be able to remove them.
256
+ if (!this.requests) {
257
+ this.requests = [];
258
+ }
259
+ const index = this.requests.findIndex((request) => requestString === request.requestString && cb === request.cb);
260
+ const newReq = {
261
+ params,
262
+ requestString,
263
+ cb,
264
+ errorCb,
265
+ onMessageCb,
266
+ onErrorCb,
267
+ };
268
+ if (index > -1) {
269
+ this.requests[index] = newReq;
270
+ }
271
+ else {
272
+ this.requests.push(newReq);
273
+ }
115
274
  }
116
- this.subscriptions.filter((s) => s.params.channel === params.channel && (!cb || s.cb === cb)).forEach(({ onMessageCb, onErrorCb }) => {
117
- if (this.websocket) {
118
- this.websocket.removeEventListener("message", onMessageCb);
119
- if (onErrorCb) {
120
- this.websocket.removeEventListener("error", onErrorCb);
121
- this.websocket.removeEventListener("close", onErrorCb);
122
- }
123
- }
124
- });
125
- }
126
- get(params, cb, errorCb) {
127
- const reqStr = WebSocketApi.getRequestString("GET", params);
128
- this.send(reqStr);
129
- this.listen(params, cb, errorCb);
130
- }
131
- subscribe(params, cb, errorCb, quiet = false) {
132
- const { onMessageCb, onErrorCb } = this.listen(params, cb, errorCb);
133
- const reqStr = WebSocketApi.getRequestString("", params);
134
- const index = this.subscriptions.findIndex((subcr) => params.channel === subcr.params.channel && cb === subcr.cb);
135
- const newSubscr = { params, cb, errorCb, onMessageCb, onErrorCb, quiet };
136
- if (index > -1) {
137
- this.subscriptions[index] = newSubscr;
138
- } else {
139
- this.subscriptions.push(newSubscr);
275
+ /**
276
+ * Subscribe to a given channel.
277
+ *
278
+ * @param {Object} params Parameters for the websocket get request
279
+ * @param {function} cb callback on listen
280
+ * @param {function} errorCb Callback on error
281
+ * @param {boolean} quiet if false, no GET or SUB requests are send, only the callback is registered.
282
+ * @private
283
+ */
284
+ subscribe(params, cb, errorCb, quiet = false) {
285
+ const { onMessageCb, onErrorCb } = this.listen(params, cb, errorCb);
286
+ const reqStr = WebSocketAPI.getRequestString('', params);
287
+ const index = this.subscriptions.findIndex((subcr) => params.channel === subcr.params.channel && cb === subcr.cb);
288
+ const newSubscr = { params, cb, errorCb, onMessageCb, onErrorCb, quiet };
289
+ if (index > -1) {
290
+ this.subscriptions[index] = newSubscr;
291
+ }
292
+ else {
293
+ this.subscriptions.push(newSubscr);
294
+ }
295
+ if (!this.subscribed[reqStr]) {
296
+ if (!newSubscr.quiet) {
297
+ this.send(`GET ${reqStr}`);
298
+ this.send(`SUB ${reqStr}`);
299
+ }
300
+ this.subscribed[reqStr] = true;
301
+ }
140
302
  }
141
- if (!this.subscribed[reqStr]) {
142
- if (!newSubscr.quiet) {
143
- this.send(`GET ${reqStr}`);
144
- this.send(`SUB ${reqStr}`);
145
- }
146
- this.subscribed[reqStr] = true;
303
+ /**
304
+ * Unsubscribe from a channel.
305
+ * @param {string} source source to unsubscribe from
306
+ * @param {function} cb Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
307
+ * @private
308
+ */
309
+ unsubscribe(source, cb) {
310
+ const toRemove = this.subscriptions.filter((s) => s.params.channel === source && (!cb || s.cb === cb));
311
+ toRemove.forEach(({ onMessageCb, onErrorCb }) => {
312
+ this.removeEvents(onMessageCb, onErrorCb);
313
+ });
314
+ this.subscriptions = this.subscriptions.filter((s) => s.params.channel !== source || (cb && s.cb !== cb));
315
+ // If there is no more subscriptions to this channel, and the removed subscriptions didn't register quietly,
316
+ // we DEL it.
317
+ if (source &&
318
+ this.subscribed[source] &&
319
+ !this.subscriptions.find((s) => s.params.channel === source) &&
320
+ toRemove.find((subscr) => !subscr.quiet)) {
321
+ this.send(`DEL ${source}`);
322
+ this.subscribed[source] = false;
323
+ }
147
324
  }
148
- }
149
- unsubscribe(source, cb) {
150
- const toRemove = this.subscriptions.filter((s) => s.params.channel === source && (!cb || s.cb === cb));
151
- toRemove.forEach(({ onMessageCb, onErrorCb }) => {
152
- if (this.websocket) {
153
- this.websocket.removeEventListener("message", onMessageCb);
154
- if (onErrorCb) {
155
- this.websocket.removeEventListener("error", onErrorCb);
156
- this.websocket.removeEventListener("close", onErrorCb);
157
- }
158
- }
159
- });
160
- this.subscriptions = this.subscriptions.filter((s) => s.params.channel !== source || cb && s.cb !== cb);
161
- if (source && this.subscribed[source] && !this.subscriptions.find((s) => s.params.channel === source) && toRemove.find((subscr) => !subscr.quiet)) {
162
- this.send(`DEL ${source}`);
163
- this.subscribed[source] = false;
325
+ /**
326
+ * After an auto reconnection we need to re-subscribe to the channels.
327
+ */
328
+ subscribePreviousSubscriptions() {
329
+ // Before to subscribe previous subscriptions we make sure they
330
+ // are all defined as unsubscribed, because this code is asynchrone
331
+ // and a subscription could have been added in between.
332
+ Object.keys(this.subscribed).forEach((key) => {
333
+ this.subscribed[key] = false;
334
+ });
335
+ // Subscribe all previous subscriptions.
336
+ [...this.subscriptions].forEach((s) => {
337
+ this.subscribe(s.params, s.cb, s.errorCb, s.quiet);
338
+ });
164
339
  }
165
- }
166
- subscribePreviousSubscriptions() {
167
- Object.keys(this.subscribed).forEach((key) => {
168
- this.subscribed[key] = false;
169
- });
170
- [...this.subscriptions].forEach((s) => {
171
- this.subscribe(s.params, s.cb, s.errorCb, s.quiet);
172
- });
173
- }
174
340
  }
175
- export default WebSocketApi;
341
+ export default WebSocketAPI;
@@ -0,0 +1,76 @@
1
+ import BaseObject from 'ol/Object';
2
+ import type { AnyMap } from '../../types';
3
+ export type ControlCommonOptions = {
4
+ active?: Boolean;
5
+ element?: HTMLElement;
6
+ target?: HTMLElement;
7
+ render?: () => void;
8
+ };
9
+ /**
10
+ * A class representing a control to display on map.
11
+ *
12
+ * @example
13
+ * const control = new Control();
14
+ *
15
+ * @classproperty {ol/Map~Map|mapboxgl.Map} map - The map which the control refers to.
16
+ * @classproperty {boolean} active - Active the control.
17
+ * @classproperty {HTMLElement} element - The HTML element used to render the control.
18
+ * @classproperty {HTMLElement} target - The HTML element where to render the element property. Default is the map's element. Read only.
19
+ */
20
+ declare class ControlCommon extends BaseObject {
21
+ active: Boolean;
22
+ map?: AnyMap;
23
+ target?: HTMLElement;
24
+ element?: HTMLElement;
25
+ /**
26
+ * Constructor
27
+ *
28
+ * @param {Object} [options] Control options.
29
+ * @param {boolean} [options.active = true] Whether the control is active or not.
30
+ * @param {HTMLElement} [options.element] The HTML element used to render the control.
31
+ * @param {HTMLElement} [options.target] The HTML element where to render the element property. Default is the map's element.
32
+ * @param {function} [options.render] Render function called whenever the control needs to be rerendered.
33
+ */
34
+ constructor(options?: ControlCommonOptions);
35
+ /**
36
+ * Define control's properties.
37
+ *
38
+ * @private
39
+ * @ignore
40
+ */
41
+ defineProperties(options: ControlCommonOptions): void;
42
+ /**
43
+ * Attach the control to the map. Add events, html element ...
44
+ */
45
+ attachToMap(map: AnyMap): void;
46
+ /**
47
+ * Detach the control From the map. Remove events, html element ..
48
+ */
49
+ detachFromMap(): void;
50
+ /**
51
+ * Add listeners then renders the control.
52
+ * To be defined in inherited classes.
53
+ */
54
+ activate(): void;
55
+ /**
56
+ * Remove listeners added by activate() function then renders the control.
57
+ * To be defined in inherited classes.
58
+ */
59
+ deactivate(): void;
60
+ /**
61
+ * The default render function. It renders content in the HTML element.
62
+ * To be defined in inherited classes.
63
+ *
64
+ * @private
65
+ */
66
+ render(options?: any): void;
67
+ /**
68
+ * The default element to display if this.element is not defined.
69
+ * To be defined in inherited classes.
70
+ *
71
+ * @private
72
+ */
73
+ createDefaultElement(): void;
74
+ }
75
+ export default ControlCommon;
76
+ //# sourceMappingURL=ControlCommon.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ControlCommon.d.ts","sourceRoot":"","sources":["../../../src/common/controls/ControlCommon.ts"],"names":[],"mappings":"AACA,OAAO,UAAU,MAAM,WAAW,CAAC;AACnC,OAAO,KAAK,EAAE,MAAM,EAAgB,MAAM,aAAa,CAAC;AAExD,MAAM,MAAM,oBAAoB,GAAG;IACjC,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,OAAO,CAAC,EAAE,WAAW,CAAC;IACtB,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,MAAM,CAAC,EAAE,MAAM,IAAI,CAAC;CACrB,CAAC;AAEF;;;;;;;;;;GAUG;AACH,cAAM,aAAc,SAAQ,UAAU;IACpC,MAAM,EAAE,OAAO,CAAC;IAEhB,GAAG,CAAC,EAAE,MAAM,CAAC;IAEb,MAAM,CAAC,EAAE,WAAW,CAAC;IAErB,OAAO,CAAC,EAAE,WAAW,CAAC;IAEtB;;;;;;;;OAQG;gBACS,OAAO,GAAE,oBAAyB;IAe9C;;;;;OAKG;IACH,gBAAgB,CAAC,OAAO,EAAE,oBAAoB;IAuE9C;;OAEG;IACH,WAAW,CAAC,GAAG,EAAE,MAAM;IAIvB;;OAEG;IACH,aAAa;IAIb;;;OAGG;IACH,QAAQ;IAIR;;;OAGG;IAEH,UAAU;IAOV;;;;;OAKG;IAEH,MAAM,CAAC,OAAO,CAAC,EAAE,GAAG;IAKpB;;;;;OAKG;IAEH,oBAAoB;CAMrB;AAED,eAAe,aAAa,CAAC"}