mobility-toolbox-js 2.0.0-beta.30 → 2.0.0-beta.34

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 (225) hide show
  1. package/api/RealtimeAPI.d.ts +268 -0
  2. package/api/RealtimeAPI.d.ts.map +1 -0
  3. package/api/RealtimeAPI.js +626 -343
  4. package/api/RoutingAPI.d.ts +33 -0
  5. package/api/RoutingAPI.d.ts.map +1 -0
  6. package/api/RoutingAPI.js +65 -15
  7. package/api/StopsAPI.d.ts +36 -0
  8. package/api/StopsAPI.d.ts.map +1 -0
  9. package/api/StopsAPI.js +70 -12
  10. package/api/index.d.ts +5 -0
  11. package/api/index.d.ts.map +1 -0
  12. package/api/index.js +10 -3
  13. package/api/typedefs.d.ts +105 -0
  14. package/api/typedefs.d.ts.map +1 -0
  15. package/api/typedefs.js +72 -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 +82 -30
  19. package/common/api/WebSocketAPI.d.ts +95 -0
  20. package/common/api/WebSocketAPI.d.ts.map +1 -0
  21. package/common/api/WebSocketAPI.js +313 -168
  22. package/common/controls/Control.d.ts +74 -0
  23. package/common/controls/Control.d.ts.map +1 -0
  24. package/common/controls/Control.js +167 -78
  25. package/common/index.d.ts +3 -0
  26. package/common/index.d.ts.map +1 -0
  27. package/common/index.js +18 -2
  28. package/common/layers/Layer.d.ts +80 -0
  29. package/common/layers/Layer.d.ts.map +1 -0
  30. package/common/layers/Layer.js +251 -134
  31. package/common/mixins/CopyrightMixin.d.ts +22 -0
  32. package/common/mixins/CopyrightMixin.d.ts.map +1 -0
  33. package/common/mixins/CopyrightMixin.js +70 -22
  34. package/common/mixins/MapboxLayerMixin.d.ts +27 -0
  35. package/common/mixins/MapboxLayerMixin.d.ts.map +1 -0
  36. package/common/mixins/MapboxLayerMixin.js +240 -0
  37. package/common/mixins/RealtimeLayerMixin.d.ts +58 -0
  38. package/common/mixins/RealtimeLayerMixin.d.ts.map +1 -0
  39. package/common/mixins/RealtimeLayerMixin.js +698 -429
  40. package/common/mixins/StopFinderMixin.d.ts +40 -0
  41. package/common/mixins/StopFinderMixin.d.ts.map +1 -0
  42. package/common/mixins/StopFinderMixin.js +195 -107
  43. package/common/mixins/UserInteractionsLayerMixin.d.ts +42 -0
  44. package/common/mixins/UserInteractionsLayerMixin.d.ts.map +1 -0
  45. package/common/mixins/UserInteractionsLayerMixin.js +222 -121
  46. package/common/styles/index.d.ts +5 -0
  47. package/common/styles/index.d.ts.map +1 -0
  48. package/common/styles/index.js +24 -4
  49. package/common/styles/realtimeDefaultStyle.d.ts +15 -0
  50. package/common/styles/realtimeDefaultStyle.d.ts.map +1 -0
  51. package/common/styles/realtimeDefaultStyle.js +236 -190
  52. package/common/styles/realtimeDelayStyle.d.ts +11 -0
  53. package/common/styles/realtimeDelayStyle.d.ts.map +1 -0
  54. package/common/styles/realtimeDelayStyle.js +25 -7
  55. package/common/styles/realtimeSimpleStyle.d.ts +3 -0
  56. package/common/styles/realtimeSimpleStyle.d.ts.map +1 -0
  57. package/common/styles/realtimeSimpleStyle.js +23 -17
  58. package/common/typedefs.d.ts +50 -0
  59. package/common/typedefs.d.ts.map +1 -0
  60. package/common/typedefs.js +21 -0
  61. package/common/utils/cleanStopTime.d.ts +7 -0
  62. package/common/utils/cleanStopTime.d.ts.map +1 -0
  63. package/common/utils/cleanStopTime.js +28 -17
  64. package/common/utils/compareDepartures.d.ts +9 -0
  65. package/common/utils/compareDepartures.d.ts.map +1 -0
  66. package/common/utils/compareDepartures.js +34 -22
  67. package/common/utils/createCanvas.d.ts +9 -0
  68. package/common/utils/createCanvas.d.ts.map +1 -0
  69. package/common/utils/createCanvas.js +28 -16
  70. package/common/utils/createTrackerFilters.d.ts +12 -0
  71. package/common/utils/createTrackerFilters.d.ts.map +1 -0
  72. package/common/utils/createTrackerFilters.js +75 -54
  73. package/common/utils/getLayersAsFlatArray.d.ts +3 -0
  74. package/common/utils/getLayersAsFlatArray.d.ts.map +1 -0
  75. package/common/utils/getLayersAsFlatArray.js +15 -13
  76. package/common/utils/getMapboxMapCopyrights.d.ts +8 -0
  77. package/common/utils/getMapboxMapCopyrights.d.ts.map +1 -0
  78. package/common/utils/getMapboxMapCopyrights.js +24 -17
  79. package/common/utils/getMapboxRender.d.ts +5 -0
  80. package/common/utils/getMapboxRender.d.ts.map +1 -0
  81. package/common/utils/getMapboxRender.js +77 -0
  82. package/common/utils/getMaplibreRender.d.ts +5 -0
  83. package/common/utils/getMaplibreRender.d.ts.map +1 -0
  84. package/common/utils/getMaplibreRender.js +38 -0
  85. package/common/utils/getRealtimeModeSuffix.d.ts +8 -0
  86. package/common/utils/getRealtimeModeSuffix.d.ts.map +1 -0
  87. package/common/utils/getRealtimeModeSuffix.js +11 -2
  88. package/common/utils/getUrlWithParams.d.ts +9 -0
  89. package/common/utils/getUrlWithParams.d.ts.map +1 -0
  90. package/common/utils/getUrlWithParams.js +20 -10
  91. package/common/utils/getVehiclePosition.d.ts +14 -0
  92. package/common/utils/getVehiclePosition.d.ts.map +1 -0
  93. package/common/utils/getVehiclePosition.js +63 -39
  94. package/common/utils/index.d.ts +12 -0
  95. package/common/utils/index.d.ts.map +1 -0
  96. package/common/utils/index.js +37 -9
  97. package/common/utils/removeDuplicate.d.ts +10 -0
  98. package/common/utils/removeDuplicate.d.ts.map +1 -0
  99. package/common/utils/removeDuplicate.js +29 -7
  100. package/common/utils/renderTrajectories.d.ts +20 -0
  101. package/common/utils/renderTrajectories.d.ts.map +1 -0
  102. package/common/utils/renderTrajectories.js +111 -78
  103. package/common/utils/sortByDelay.d.ts +3 -0
  104. package/common/utils/sortByDelay.d.ts.map +1 -0
  105. package/common/utils/sortByDelay.js +21 -17
  106. package/common/utils/timeUtils.d.ts +5 -0
  107. package/common/utils/timeUtils.d.ts.map +1 -0
  108. package/common/utils/timeUtils.js +47 -18
  109. package/common/utils/trackerConfig.d.ts +24 -0
  110. package/common/utils/trackerConfig.d.ts.map +1 -0
  111. package/common/utils/trackerConfig.js +171 -118
  112. package/iife.d.ts +3 -0
  113. package/iife.d.ts.map +1 -0
  114. package/iife.js +7 -0
  115. package/index.d.ts +6 -0
  116. package/index.d.ts.map +1 -0
  117. package/index.js +10 -7
  118. package/mapbox/controls/CopyrightControl.d.ts +32 -0
  119. package/mapbox/controls/CopyrightControl.d.ts.map +1 -0
  120. package/mapbox/controls/CopyrightControl.js +72 -28
  121. package/mapbox/controls/index.d.ts +2 -0
  122. package/mapbox/controls/index.d.ts.map +1 -0
  123. package/mapbox/controls/index.js +6 -1
  124. package/mapbox/index.d.ts +5 -0
  125. package/mapbox/index.d.ts.map +1 -0
  126. package/mapbox/index.js +20 -4
  127. package/mapbox/layers/Layer.d.ts +47 -0
  128. package/mapbox/layers/Layer.d.ts.map +1 -0
  129. package/mapbox/layers/Layer.js +137 -54
  130. package/mapbox/layers/RealtimeLayer.d.ts +118 -0
  131. package/mapbox/layers/RealtimeLayer.d.ts.map +1 -0
  132. package/mapbox/layers/RealtimeLayer.js +310 -183
  133. package/mapbox/layers/index.d.ts +3 -0
  134. package/mapbox/layers/index.d.ts.map +1 -0
  135. package/mapbox/layers/index.js +7 -2
  136. package/mapbox/utils.d.ts +8 -0
  137. package/mapbox/utils.d.ts.map +1 -0
  138. package/mapbox/utils.js +54 -29
  139. package/mbt.js +2052 -2109
  140. package/mbt.js.map +3 -3
  141. package/mbt.min.js +18 -18
  142. package/mbt.min.js.map +3 -3
  143. package/ol/controls/CopyrightControl.d.ts +31 -0
  144. package/ol/controls/CopyrightControl.d.ts.map +1 -0
  145. package/ol/controls/CopyrightControl.js +89 -41
  146. package/ol/controls/RoutingControl.d.ts +180 -0
  147. package/ol/controls/RoutingControl.d.ts.map +1 -0
  148. package/ol/controls/RoutingControl.js +666 -371
  149. package/ol/controls/StopFinderControl.d.ts +32 -0
  150. package/ol/controls/StopFinderControl.d.ts.map +1 -0
  151. package/ol/controls/StopFinderControl.js +59 -10
  152. package/ol/controls/index.d.ts +4 -0
  153. package/ol/controls/index.d.ts.map +1 -0
  154. package/ol/controls/index.js +9 -3
  155. package/ol/index.d.ts +6 -0
  156. package/ol/index.d.ts.map +1 -0
  157. package/ol/index.js +21 -5
  158. package/ol/layers/Layer.d.ts +49 -0
  159. package/ol/layers/Layer.d.ts.map +1 -0
  160. package/ol/layers/Layer.js +178 -81
  161. package/ol/layers/MapboxLayer.d.ts +42 -0
  162. package/ol/layers/MapboxLayer.d.ts.map +1 -0
  163. package/ol/layers/MapboxLayer.js +131 -198
  164. package/ol/layers/MapboxStyleLayer.d.ts +146 -0
  165. package/ol/layers/MapboxStyleLayer.d.ts.map +1 -0
  166. package/ol/layers/MapboxStyleLayer.js +382 -186
  167. package/ol/layers/MaplibreLayer.d.ts +27 -0
  168. package/ol/layers/MaplibreLayer.d.ts.map +1 -0
  169. package/ol/layers/MaplibreLayer.js +67 -138
  170. package/ol/layers/RealtimeLayer.d.ts +119 -0
  171. package/ol/layers/RealtimeLayer.d.ts.map +1 -0
  172. package/ol/layers/RealtimeLayer.js +327 -184
  173. package/ol/layers/RoutingLayer.d.ts +24 -0
  174. package/ol/layers/RoutingLayer.d.ts.map +1 -0
  175. package/ol/layers/RoutingLayer.js +111 -56
  176. package/ol/layers/VectorLayer.d.ts +23 -0
  177. package/ol/layers/VectorLayer.d.ts.map +1 -0
  178. package/ol/layers/VectorLayer.js +71 -21
  179. package/ol/layers/WMSLayer.d.ts +38 -0
  180. package/ol/layers/WMSLayer.d.ts.map +1 -0
  181. package/ol/layers/WMSLayer.js +105 -37
  182. package/ol/layers/index.d.ts +9 -0
  183. package/ol/layers/index.d.ts.map +1 -0
  184. package/ol/layers/index.js +19 -8
  185. package/ol/styles/fullTrajectoryDelayStyle.d.ts +4 -0
  186. package/ol/styles/fullTrajectoryDelayStyle.d.ts.map +1 -0
  187. package/ol/styles/fullTrajectoryDelayStyle.js +32 -28
  188. package/ol/styles/fullTrajectoryStyle.d.ts +4 -0
  189. package/ol/styles/fullTrajectoryStyle.d.ts.map +1 -0
  190. package/ol/styles/fullTrajectoryStyle.js +44 -40
  191. package/ol/styles/index.d.ts +3 -0
  192. package/ol/styles/index.d.ts.map +1 -0
  193. package/ol/styles/index.js +7 -2
  194. package/package.json +14 -36
  195. package/setupTests.d.ts +2 -0
  196. package/setupTests.d.ts.map +1 -0
  197. package/setupTests.js +15 -0
  198. package/types/index.d.ts +7 -6
  199. package/types/routing.d.ts +25 -25
  200. package/types/stops.d.ts +19 -19
  201. package/api/RealtimeAPI.test.js +0 -67
  202. package/api/RoutingAPI.test.js +0 -25
  203. package/api/StopsAPI.test.js +0 -22
  204. package/common/api/HttpAPI.test.js +0 -50
  205. package/common/api/WebSocketAPI.test.js +0 -311
  206. package/common/controls/Control.test.js +0 -87
  207. package/common/layers/Layer.test.js +0 -134
  208. package/common/mixins/UserInteractionsLayerMixin.test.js +0 -199
  209. package/common/utils/createTrackerFilters.test.js +0 -79
  210. package/common/utils/getMapboxMapCopyrights.test.js +0 -40
  211. package/common/utils/removeDuplicate.test.js +0 -19
  212. package/common/utils/timeUtils.test.js +0 -10
  213. package/common/utils/trackerConfig.test.js +0 -23
  214. package/mapbox/layers/Layer.test.js +0 -182
  215. package/mapbox/layers/RealtimeLayer.test.js +0 -10
  216. package/ol/controls/CopyrightControl.test.js +0 -165
  217. package/ol/controls/RoutingControl.test.js +0 -146
  218. package/ol/controls/StopFinderControl.test.js +0 -48
  219. package/ol/layers/Layer.test.js +0 -175
  220. package/ol/layers/MapboxLayer.test.js +0 -160
  221. package/ol/layers/MapboxStyleLayer.test.js +0 -226
  222. package/ol/layers/RealtimeLayer.test.js +0 -67
  223. package/ol/layers/RoutingLayer.test.js +0 -39
  224. package/ol/layers/VectorLayer.test.js +0 -76
  225. package/ol/layers/WMSLayer.test.js +0 -57
@@ -0,0 +1,95 @@
1
+ export default WebSocketAPI;
2
+ /**
3
+ * Class used to facilitate connection to a WebSocket and
4
+ * also to manage properly messages send to the WebSocket.
5
+ * This class must not contain any specific implementation.
6
+ */
7
+ declare class WebSocketAPI {
8
+ /**
9
+ * Get the websocket request string.
10
+ *
11
+ * @param {string} method Request mehtod {GET, SUB}.
12
+ * @param {Object} params Request parameters.
13
+ * @param {string} params.channel Channel name
14
+ * @param {string} [params.args] Request arguments
15
+ * @param {Number} [params.id] Request identifier
16
+ * @return {string} request string
17
+ * @private
18
+ */
19
+ private static getRequestString;
20
+ defineProperties(): void;
21
+ /**
22
+ * (Re)connect the websocket.
23
+ *
24
+ * @param {strin} url Websocket url.
25
+ * @param {function} onOpen Callback called when the websocket connection is opened and before subscriptions of previous subscriptions.
26
+ * @private
27
+ */
28
+ private connect;
29
+ /** @ignore */
30
+ websocket: WebSocket;
31
+ /**
32
+ * Close the websocket definitively.
33
+ *
34
+ * @private
35
+ */
36
+ private close;
37
+ messagesOnOpen: any[];
38
+ /**
39
+ * Sends a message to the websocket.
40
+ *
41
+ * @param {message} message Message to send.
42
+ * @private
43
+ */
44
+ private send;
45
+ /**
46
+ * Listen to websocket messages.
47
+ *
48
+ * @param {Object} params Parameters for the websocket get request
49
+ * @param {function} cb callback on listen
50
+ * @param {function} errorCb Callback on error
51
+ * @return {{onMessage: function, errorCb: function}} Object with onMessage and error callbacks
52
+ * @private
53
+ */
54
+ private listen;
55
+ /**
56
+ * Unlisten websocket messages.
57
+ *
58
+ * @param {Object} params Parameters for the websocket get request.
59
+ * @param {function} cb Callback used when listen.
60
+ * @private
61
+ */
62
+ private unlisten;
63
+ /**
64
+ * Sends a get request to the websocket.
65
+ *
66
+ * @param {Object} params Parameters for the websocket get request
67
+ * @param {function} cb callback on listen
68
+ * @param {function} errorCb Callback on error
69
+ * @private
70
+ */
71
+ private get;
72
+ /**
73
+ * Subscribe to a given channel.
74
+ *
75
+ * @param {Object} params Parameters for the websocket get request
76
+ * @param {function} cb callback on listen
77
+ * @param {function} errorCb Callback on error
78
+ * @param {boolean} quiet if false, no GET or SUB requests are send, only the callback is registered.
79
+ * @private
80
+ */
81
+ private subscribe;
82
+ /**
83
+ * Unsubscribe from a channel.
84
+ * @param {string} source source to unsubscribe from
85
+ * @param {function} cb Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
86
+ * @private
87
+ */
88
+ private unsubscribe;
89
+ subscriptions: any;
90
+ /**
91
+ * After an auto reconnection we need to re-subscribe to the channels.
92
+ */
93
+ subscribePreviousSubscriptions(): void;
94
+ }
95
+ //# sourceMappingURL=WebSocketAPI.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"WebSocketAPI.d.ts","sourceRoot":"","sources":["../../../src/common/api/WebSocketAPI.js"],"names":[],"mappings":";AAAA;;;;GAIG;AACH;IAiEE;;;;;;;;;;OAUG;IACH,gCAKC;IA5ED,yBA0DC;IAoBD;;;;;;OAMG;IACH,gBAiBC;IAZC,cAAc;IACd,qBAAmC;IAarC;;;;OAIG;IACH,cAOC;IAFG,sBAAwB;IAI5B;;;;;OAKG;IACH,aAsBC;IAED;;;;;;;;OAQG;IACH,eAsCC;IAED;;;;;;OAMG;IACH,iBAiBC;IAED;;;;;;;OAOG;IACH,YAIC;IAED;;;;;;;;OAQG;IACH,kBAqBC;IAED;;;;;OAKG;IACH,oBA8BC;IAfC,mBAEC;IAeH;;OAEG;IACH,uCAYC;CACF"}
@@ -1,175 +1,320 @@
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();
43
- }
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();
53
- }
54
- }
55
- close() {
56
- if (this.websocket) {
57
- this.websocket.onclose = null;
58
- this.websocket.close();
59
- this.websocket = null;
60
- this.messagesOnOpen = [];
1
+ "use strict";
2
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
+ if (ar || !(i in from)) {
5
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
6
+ ar[i] = from[i];
7
+ }
61
8
  }
62
- }
63
- send(message) {
64
- if (!this.websocket) {
65
- return;
9
+ return to.concat(ar || Array.prototype.slice.call(from));
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ /**
13
+ * Class used to facilitate connection to a WebSocket and
14
+ * also to manage properly messages send to the WebSocket.
15
+ * This class must not contain any specific implementation.
16
+ */
17
+ var WebSocketAPI = /** @class */ (function () {
18
+ function WebSocketAPI() {
19
+ this.defineProperties();
66
20
  }
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 = [];
21
+ WebSocketAPI.prototype.defineProperties = function () {
22
+ var _this = this;
23
+ Object.defineProperties(this, {
24
+ closed: {
25
+ get: function () {
26
+ return !!(_this.websocket &&
27
+ _this.websocket.readyState === _this.websocket.CLOSED);
28
+ },
29
+ },
30
+ closing: {
31
+ get: function () {
32
+ return !!(_this.websocket &&
33
+ _this.websocket.readyState === _this.websocket.CLOSING);
34
+ },
35
+ },
36
+ connecting: {
37
+ get: function () {
38
+ return !!(_this.websocket &&
39
+ _this.websocket.readyState === _this.websocket.CONNECTING);
40
+ },
41
+ },
42
+ open: {
43
+ get: function () {
44
+ return !!(_this.websocket && _this.websocket.readyState === _this.websocket.OPEN);
45
+ },
46
+ },
47
+ /**
48
+ * Array of message to send on open.
49
+ * @type {Array<string>}
50
+ * @private
51
+ */
52
+ messagesOnOpen: {
53
+ value: [],
54
+ writable: true,
55
+ },
56
+ /**
57
+ * Array of subscriptions.
58
+ * @type {Array<subscription>}
59
+ * @private
60
+ */
61
+ subscriptions: {
62
+ value: [],
63
+ writable: true,
64
+ },
65
+ /**
66
+ * List of channels subscribed.
67
+ * @type {Array<subscription>}
68
+ * @private
69
+ */
70
+ subscribed: {
71
+ value: {},
72
+ writable: true,
73
+ },
79
74
  });
80
- }
81
- } else if (!this.messagesOnOpen.includes(message)) {
82
- send();
83
- }
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);
75
+ };
76
+ /**
77
+ * Get the websocket request string.
78
+ *
79
+ * @param {string} method Request mehtod {GET, SUB}.
80
+ * @param {Object} params Request parameters.
81
+ * @param {string} params.channel Channel name
82
+ * @param {string} [params.args] Request arguments
83
+ * @param {Number} [params.id] Request identifier
84
+ * @return {string} request string
85
+ * @private
86
+ */
87
+ WebSocketAPI.getRequestString = function (method, params) {
88
+ var reqStr = "".concat(method, " ").concat(params.channel);
89
+ reqStr += params.args ? " ".concat(params.args) : '';
90
+ reqStr += params.id ? " ".concat(params.id) : '';
91
+ return reqStr.trim();
92
+ };
93
+ /**
94
+ * (Re)connect the websocket.
95
+ *
96
+ * @param {strin} url Websocket url.
97
+ * @param {function} onOpen Callback called when the websocket connection is opened and before subscriptions of previous subscriptions.
98
+ * @private
99
+ */
100
+ WebSocketAPI.prototype.connect = function (url, onOpen) {
101
+ var _this = this;
102
+ if (onOpen === void 0) { onOpen = function () { }; }
103
+ if (this.websocket && !this.closed) {
104
+ this.websocket.close();
105
+ }
106
+ /** @ignore */
107
+ this.websocket = new WebSocket(url);
108
+ if (!this.open) {
109
+ this.websocket.addEventListener('open', function () {
110
+ onOpen();
111
+ _this.subscribePreviousSubscriptions();
112
+ });
113
+ }
114
+ else {
115
+ onOpen();
116
+ this.subscribePreviousSubscriptions();
100
117
  }
101
- });
102
118
  };
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
- }
109
- }
110
- return { onMessageCb: onMessage, onErrorCb: errorCb };
111
- }
112
- unlisten(params, cb) {
113
- if (!this.websocket) {
114
- return;
115
- }
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);
119
+ /**
120
+ * Close the websocket definitively.
121
+ *
122
+ * @private
123
+ */
124
+ WebSocketAPI.prototype.close = function () {
125
+ if (this.websocket) {
126
+ this.websocket.onclose = null;
127
+ this.websocket.close();
128
+ this.websocket = null;
129
+ this.messagesOnOpen = [];
122
130
  }
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);
140
- }
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;
147
- }
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);
131
+ };
132
+ /**
133
+ * Sends a message to the websocket.
134
+ *
135
+ * @param {message} message Message to send.
136
+ * @private
137
+ */
138
+ WebSocketAPI.prototype.send = function (message) {
139
+ var _this = this;
140
+ if (!this.websocket) {
141
+ return;
157
142
  }
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;
164
- }
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
- }
175
- export default WebSocketAPI;
143
+ var send = function () {
144
+ _this.websocket.send(message);
145
+ };
146
+ if (!this.open) {
147
+ // This 'if' avoid sending 2 identical BBOX message on open,
148
+ if (!this.messagesOnOpen.includes(message)) {
149
+ this.messagesOnOpen.push(message);
150
+ this.websocket.addEventListener('open', function () {
151
+ _this.messagesOnOpen = [];
152
+ send();
153
+ });
154
+ this.websocket.addEventListener('close', function () {
155
+ _this.messagesOnOpen = [];
156
+ });
157
+ }
158
+ }
159
+ else if (!this.messagesOnOpen.includes(message)) {
160
+ send();
161
+ }
162
+ };
163
+ /**
164
+ * Listen to websocket messages.
165
+ *
166
+ * @param {Object} params Parameters for the websocket get request
167
+ * @param {function} cb callback on listen
168
+ * @param {function} errorCb Callback on error
169
+ * @return {{onMessage: function, errorCb: function}} Object with onMessage and error callbacks
170
+ * @private
171
+ */
172
+ WebSocketAPI.prototype.listen = function (params, cb, errorCb) {
173
+ // Remove the previous identical callback
174
+ this.unlisten(params, cb);
175
+ var onMessage = function (evt) {
176
+ var data = {};
177
+ try {
178
+ data = JSON.parse(evt.data);
179
+ }
180
+ catch (err) {
181
+ // eslint-disable-next-line no-console
182
+ console.error('WebSocket: unable to parse JSON data', err, evt.data);
183
+ }
184
+ var source = params.channel;
185
+ source += params.args ? " ".concat(params.args) : '';
186
+ // Buffer channel message return a list of other channels to propagate to proper callbacks.
187
+ var contents = data.source === 'buffer' ? data.content : [data];
188
+ contents.forEach(function (content) {
189
+ // Because of backend optimization, the last content is null.
190
+ if ((content === null || content === void 0 ? void 0 : content.source) === source &&
191
+ (!params.id || params.id === data.client_reference)) {
192
+ cb(content);
193
+ }
194
+ });
195
+ };
196
+ if (this.websocket) {
197
+ this.websocket.addEventListener('message', onMessage);
198
+ if (errorCb) {
199
+ this.websocket.addEventListener('error', errorCb);
200
+ this.websocket.addEventListener('close', errorCb);
201
+ }
202
+ }
203
+ return { onMessageCb: onMessage, onErrorCb: errorCb };
204
+ };
205
+ /**
206
+ * Unlisten websocket messages.
207
+ *
208
+ * @param {Object} params Parameters for the websocket get request.
209
+ * @param {function} cb Callback used when listen.
210
+ * @private
211
+ */
212
+ WebSocketAPI.prototype.unlisten = function (params, cb) {
213
+ var _this = this;
214
+ if (!this.websocket) {
215
+ return;
216
+ }
217
+ this.subscriptions
218
+ .filter(function (s) { return s.params.channel === params.channel && (!cb || s.cb === cb); })
219
+ .forEach(function (_a) {
220
+ var onMessageCb = _a.onMessageCb, onErrorCb = _a.onErrorCb;
221
+ if (_this.websocket) {
222
+ _this.websocket.removeEventListener('message', onMessageCb);
223
+ if (onErrorCb) {
224
+ _this.websocket.removeEventListener('error', onErrorCb);
225
+ _this.websocket.removeEventListener('close', onErrorCb);
226
+ }
227
+ }
228
+ });
229
+ };
230
+ /**
231
+ * Sends a get request to the websocket.
232
+ *
233
+ * @param {Object} params Parameters for the websocket get request
234
+ * @param {function} cb callback on listen
235
+ * @param {function} errorCb Callback on error
236
+ * @private
237
+ */
238
+ WebSocketAPI.prototype.get = function (params, cb, errorCb) {
239
+ var reqStr = WebSocketAPI.getRequestString('GET', params);
240
+ this.send(reqStr);
241
+ this.listen(params, cb, errorCb);
242
+ };
243
+ /**
244
+ * Subscribe to a given channel.
245
+ *
246
+ * @param {Object} params Parameters for the websocket get request
247
+ * @param {function} cb callback on listen
248
+ * @param {function} errorCb Callback on error
249
+ * @param {boolean} quiet if false, no GET or SUB requests are send, only the callback is registered.
250
+ * @private
251
+ */
252
+ WebSocketAPI.prototype.subscribe = function (params, cb, errorCb, quiet) {
253
+ if (quiet === void 0) { quiet = false; }
254
+ var _a = this.listen(params, cb, errorCb), onMessageCb = _a.onMessageCb, onErrorCb = _a.onErrorCb;
255
+ var reqStr = WebSocketAPI.getRequestString('', params);
256
+ var index = this.subscriptions.findIndex(function (subcr) { return params.channel === subcr.params.channel && cb === subcr.cb; });
257
+ var newSubscr = { params: params, cb: cb, errorCb: errorCb, onMessageCb: onMessageCb, onErrorCb: onErrorCb, quiet: quiet };
258
+ if (index > -1) {
259
+ this.subscriptions[index] = newSubscr;
260
+ }
261
+ else {
262
+ this.subscriptions.push(newSubscr);
263
+ }
264
+ if (!this.subscribed[reqStr]) {
265
+ if (!newSubscr.quiet) {
266
+ this.send("GET ".concat(reqStr));
267
+ this.send("SUB ".concat(reqStr));
268
+ }
269
+ this.subscribed[reqStr] = true;
270
+ }
271
+ };
272
+ /**
273
+ * Unsubscribe from a channel.
274
+ * @param {string} source source to unsubscribe from
275
+ * @param {function} cb Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
276
+ * @private
277
+ */
278
+ WebSocketAPI.prototype.unsubscribe = function (source, cb) {
279
+ var _this = this;
280
+ var toRemove = this.subscriptions.filter(function (s) { return s.params.channel === source && (!cb || s.cb === cb); });
281
+ toRemove.forEach(function (_a) {
282
+ var onMessageCb = _a.onMessageCb, onErrorCb = _a.onErrorCb;
283
+ if (_this.websocket) {
284
+ _this.websocket.removeEventListener('message', onMessageCb);
285
+ if (onErrorCb) {
286
+ _this.websocket.removeEventListener('error', onErrorCb);
287
+ _this.websocket.removeEventListener('close', onErrorCb);
288
+ }
289
+ }
290
+ });
291
+ this.subscriptions = this.subscriptions.filter(function (s) { return s.params.channel !== source || (cb && s.cb !== cb); });
292
+ // If there is no more subscriptions to this channel, and the removed subscriptions didn't register quietly,
293
+ // we DEL it.
294
+ if (source &&
295
+ this.subscribed[source] &&
296
+ !this.subscriptions.find(function (s) { return s.params.channel === source; }) &&
297
+ toRemove.find(function (subscr) { return !subscr.quiet; })) {
298
+ this.send("DEL ".concat(source));
299
+ this.subscribed[source] = false;
300
+ }
301
+ };
302
+ /**
303
+ * After an auto reconnection we need to re-subscribe to the channels.
304
+ */
305
+ WebSocketAPI.prototype.subscribePreviousSubscriptions = function () {
306
+ var _this = this;
307
+ // Before to subscribe previous subscriptions we make sure they
308
+ // are all defined as unsubscribed, because this code is asynchrone
309
+ // and a subscription could have been added in between.
310
+ Object.keys(this.subscribed).forEach(function (key) {
311
+ _this.subscribed[key] = false;
312
+ });
313
+ // Subscribe all previous subscriptions.
314
+ __spreadArray([], this.subscriptions, true).forEach(function (s) {
315
+ _this.subscribe(s.params, s.cb, s.errorCb, s.quiet);
316
+ });
317
+ };
318
+ return WebSocketAPI;
319
+ }());
320
+ exports.default = WebSocketAPI;
@@ -0,0 +1,74 @@
1
+ export default Control;
2
+ /**
3
+ * A class representing a control to display on map.
4
+ *
5
+ * @example
6
+ * const control = new Control();
7
+ *
8
+ * @classproperty {ol/Map~Map|mapboxgl.Map} map - The map which the control refers to.
9
+ * @classproperty {boolean} active - Active the control.
10
+ * @classproperty {HTMLElement} element - The HTML element used to render the control.
11
+ * @classproperty {HTMLElement} target - The HTML element where to render the element property. Default is the map's element. Read only.
12
+ */
13
+ declare class Control extends BaseObject {
14
+ /**
15
+ * Constructor
16
+ *
17
+ * @param {Object} [options] Control options.
18
+ * @param {boolean} [options.active = true] Whether the control is active or not.
19
+ * @param {HTMLElement} [options.element] The HTML element used to render the control.
20
+ * @param {HTMLElement} [options.target] The HTML element where to render the element property. Default is the map's element.
21
+ * @param {function} [options.render] Render function called whenever the control needs to be rerendered.
22
+ */
23
+ constructor(options?: {
24
+ active?: boolean;
25
+ element?: HTMLElement;
26
+ target?: HTMLElement;
27
+ render?: Function;
28
+ });
29
+ /**
30
+ * @ignore
31
+ */
32
+ active: boolean;
33
+ /**
34
+ * Define control's properties.
35
+ *
36
+ * @private
37
+ */
38
+ private defineProperties;
39
+ /**
40
+ * Attach the control to the map. Add events, html element ...
41
+ */
42
+ attachToMap(map: any): void;
43
+ map: any;
44
+ /**
45
+ * Detach the control From the map. Remove events, html element ..
46
+ */
47
+ detachFromMap(): void;
48
+ /**
49
+ * Add listeners then renders the control.
50
+ * To be defined in inherited classes.
51
+ */
52
+ activate(): void;
53
+ /**
54
+ * Remove listeners added by activate() function then renders the control.
55
+ * To be defined in inherited classes.
56
+ */
57
+ deactivate(): void;
58
+ /**
59
+ * The default render function. It renders content in the HTML element.
60
+ * To be defined in inherited classes.
61
+ *
62
+ * @private
63
+ */
64
+ private render;
65
+ /**
66
+ * The default element to display if this.element is not defined.
67
+ * To be defined in inherited classes.
68
+ *
69
+ * @private
70
+ */
71
+ private createDefaultElement;
72
+ }
73
+ import BaseObject from "ol/Object";
74
+ //# sourceMappingURL=Control.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Control.d.ts","sourceRoot":"","sources":["../../../src/common/controls/Control.js"],"names":[],"mappings":";AAEA;;;;;;;;;;GAUG;AACH;IACE;;;;;;;;OAQG;IACH;QAL6B,MAAM,GAAxB,OAAO;QACe,OAAO,GAA7B,WAAW;QACW,MAAM,GAA5B,WAAW;QACQ,MAAM;OAenC;IAJC;;OAEG;IACH,gBAAoB;IAGtB;;;;OAIG;IACH,yBAmEC;IAED;;OAEG;IACH,4BAEC;IADC,SAAc;IAGhB;;OAEG;IACH,sBAEC;IAED;;;OAGG;IACH,iBAEC;IAED;;;OAGG;IAEH,mBAAe;IAEf;;;;;OAKG;IACH,eAAW;IAEX;;;;;OAKG;IAEH,6BAAyB;CAC1B"}