mobility-toolbox-js 2.3.9 → 2.3.10
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.
- package/.esdoc.js +18 -0
- package/.eslintignore +1 -0
- package/.eslintrc.js +25 -0
- package/.fixpackrc +20 -0
- package/.github/workflows/build.yml +16 -0
- package/.github/workflows/conventional-pr-title.yml +16 -0
- package/.github/workflows/cypress.yml +101 -0
- package/.github/workflows/test.yml +17 -0
- package/.husky/commit-msg +4 -0
- package/.husky/post-checkout +4 -0
- package/.husky/post-merge +4 -0
- package/.husky/post-rebase +4 -0
- package/.husky/pre-commit +4 -0
- package/.lintstagedrc.js +10 -0
- package/.nvmrc +1 -0
- package/.prettierrc.js +5 -0
- package/.stylelintrc.js +4 -0
- package/CHANGELOG.md +132 -0
- package/LICENSE +21 -0
- package/MIGRATION-V2.md +248 -0
- package/README.md +42 -0
- package/__mocks__/mapbox-gl.js +81 -0
- package/__mocks__/maplibre-gl.js +81 -0
- package/babel.config.js +9 -0
- package/commitlint.config.js +1 -0
- package/cypress/e2e/examples/api.cy.js +7 -0
- package/cypress/e2e/examples/examples.cy.js +7 -0
- package/cypress/e2e/examples/navigation.cy.js +29 -0
- package/cypress/fixtures/example.json +5 -0
- package/cypress/plugins/index.js +21 -0
- package/cypress/support/commands.js +25 -0
- package/cypress/support/e2e.js +20 -0
- package/cypress.config.ts +15 -0
- package/data/fetchRoute.json +292 -0
- package/data/fetchTrajectories.json +18 -0
- package/data/fetchTrajectoryById.json +3 -0
- package/data/fetchTrajectoryStations.json +18 -0
- package/data/stopsSearch.json +15 -0
- package/dependabot.yml +15 -0
- package/doc/.eslintrc.json +1 -0
- package/doc/README.md +34 -0
- package/doc/next.config.js +16 -0
- package/doc/package.json +41 -0
- package/doc/pages/404.js +5 -0
- package/doc/pages/_app.js +61 -0
- package/doc/pages/_document.js +64 -0
- package/doc/pages/doc/[...slug].js +23 -0
- package/doc/pages/doc.js +23 -0
- package/doc/pages/example/[example].js +52 -0
- package/doc/pages/examples.js +34 -0
- package/doc/pages/index.js +25 -0
- package/doc/public/README.md +15 -0
- package/doc/public/favicon.ico +0 -0
- package/doc/public/static/assets/Lato-Black.ttf +0 -0
- package/doc/public/static/assets/Lato-BlackItalic.ttf +0 -0
- package/doc/public/static/assets/Lato-Bold.ttf +0 -0
- package/doc/public/static/assets/Lato-BoldItalic.ttf +0 -0
- package/doc/public/static/assets/Lato-Italic.ttf +0 -0
- package/doc/public/static/assets/Lato-Light.ttf +0 -0
- package/doc/public/static/assets/Lato-LightItalic.ttf +0 -0
- package/doc/public/static/assets/Lato-Regular.ttf +0 -0
- package/doc/public/static/assets/Lato-Thin.ttf +0 -0
- package/doc/public/static/assets/Lato-ThinItalic.ttf +0 -0
- package/doc/public/static/assets/OFL.txt +93 -0
- package/doc/public/static/examples/assets/tralis-live-map/index.js +11 -0
- package/doc/public/static/examples/assets/tralis-live-map/s1kreis.svg +105 -0
- package/doc/public/static/examples/assets/tralis-live-map/s20kreis.svg +101 -0
- package/doc/public/static/examples/assets/tralis-live-map/s2kreis.svg +95 -0
- package/doc/public/static/examples/assets/tralis-live-map/s3kreis.svg +95 -0
- package/doc/public/static/examples/assets/tralis-live-map/s4kreis.svg +95 -0
- package/doc/public/static/examples/assets/tralis-live-map/s6kreis.svg +95 -0
- package/doc/public/static/examples/assets/tralis-live-map/s7kreis.svg +95 -0
- package/doc/public/static/examples/assets/tralis-live-map/s8kreis.svg +93 -0
- package/doc/public/static/examples/assets/tralis-live-map/unknown.svg +107 -0
- package/doc/public/static/examples/layers.html +11 -0
- package/doc/public/static/examples/layers.js +97 -0
- package/doc/public/static/examples/mb-copyright.html +26 -0
- package/doc/public/static/examples/mb-copyright.js +37 -0
- package/doc/public/static/examples/mb-tracker.html +1 -0
- package/doc/public/static/examples/mb-tracker.js +40 -0
- package/doc/public/static/examples/mb-tracker.md +1 -0
- package/doc/public/static/examples/mb-tralis.html +1 -0
- package/doc/public/static/examples/mb-tralis.js +34 -0
- package/doc/public/static/examples/ol-copyright.html +26 -0
- package/doc/public/static/examples/ol-copyright.js +43 -0
- package/doc/public/static/examples/ol-mapbox-layer.html +1 -0
- package/doc/public/static/examples/ol-mapbox-layer.js +28 -0
- package/doc/public/static/examples/ol-mapbox-style-layer.html +12 -0
- package/doc/public/static/examples/ol-mapbox-style-layer.js +48 -0
- package/doc/public/static/examples/ol-query.html +32 -0
- package/doc/public/static/examples/ol-query.js +83 -0
- package/doc/public/static/examples/ol-routing.html +26 -0
- package/doc/public/static/examples/ol-routing.js +65 -0
- package/doc/public/static/examples/ol-routing.md +1 -0
- package/doc/public/static/examples/ol-stop-finder.html +15 -0
- package/doc/public/static/examples/ol-stop-finder.js +35 -0
- package/doc/public/static/examples/ol-stop-finder.md +1 -0
- package/doc/public/static/examples/ol-tracker.html +1 -0
- package/doc/public/static/examples/ol-tracker.js +41 -0
- package/doc/public/static/examples/ol-tracker.md +1 -0
- package/doc/public/static/examples/ol-tralis.html +5 -0
- package/doc/public/static/examples/ol-tralis.js +62 -0
- package/doc/public/static/examples/tralis-live-map.html +1 -0
- package/doc/public/static/examples/tralis-live-map.js +67 -0
- package/doc/public/static/examples/tralis-live-map.md +3 -0
- package/doc/public/static/img/live_tracker_mb.jpg +0 -0
- package/doc/public/static/img/live_tracker_munich.jpg +0 -0
- package/doc/public/static/img/live_tracker_ol.jpg +0 -0
- package/doc/public/static/img/mapbox.jpg +0 -0
- package/doc/public/static/img/mapbox_style.jpg +0 -0
- package/doc/public/static/img/ol-copyright.png +0 -0
- package/doc/public/static/img/query_objects.jpg +0 -0
- package/doc/public/static/img/routing.jpg +0 -0
- package/doc/public/static/img/simple_map.jpg +0 -0
- package/doc/public/static/img/stops.jpg +0 -0
- package/doc/public/vercel.svg +4 -0
- package/doc/src/components/CodeSandboxButton.js +102 -0
- package/doc/src/components/Documentation.js +42 -0
- package/doc/src/components/Esdoc/Anchor.js +57 -0
- package/doc/src/components/Esdoc/ClassDoc.js +272 -0
- package/doc/src/components/Esdoc/DeprecatedHTML.js +16 -0
- package/doc/src/components/Esdoc/DetailDocs.js +279 -0
- package/doc/src/components/Esdoc/DetailHTML.js +33 -0
- package/doc/src/components/Esdoc/DirectSubclassHTML.js +30 -0
- package/doc/src/components/Esdoc/DocBuilderUtils.js +697 -0
- package/doc/src/components/Esdoc/DocLinkHTML.js +63 -0
- package/doc/src/components/Esdoc/DocsLinkHTML.js +38 -0
- package/doc/src/components/Esdoc/Esdoc.js +63 -0
- package/doc/src/components/Esdoc/EsdocContent.js +56 -0
- package/doc/src/components/Esdoc/EsdocNavigation.js +13 -0
- package/doc/src/components/Esdoc/EsdocSearch.js +78 -0
- package/doc/src/components/Esdoc/ExperimentalHTML.js +17 -0
- package/doc/src/components/Esdoc/ExtendsChainHTML.js +32 -0
- package/doc/src/components/Esdoc/FileDocLinkHTML.js +62 -0
- package/doc/src/components/Esdoc/IdentifiersDoc.js +113 -0
- package/doc/src/components/Esdoc/IndirectSubclassHTML.js +30 -0
- package/doc/src/components/Esdoc/InheritedSummaryDoc.js +70 -0
- package/doc/src/components/Esdoc/InheritedSummaryHTML.js +38 -0
- package/doc/src/components/Esdoc/MixinClassesHTML.js +29 -0
- package/doc/src/components/Esdoc/NavDoc.js +112 -0
- package/doc/src/components/Esdoc/OverrideMethod.js +44 -0
- package/doc/src/components/Esdoc/OverrideMethodDescription.js +35 -0
- package/doc/src/components/Esdoc/Properties.js +89 -0
- package/doc/src/components/Esdoc/README.md +45 -0
- package/doc/src/components/Esdoc/SignatureHTML.js +123 -0
- package/doc/src/components/Esdoc/SingleDoc.js +31 -0
- package/doc/src/components/Esdoc/SummaryDoc.js +160 -0
- package/doc/src/components/Esdoc/SummaryHTML.js +96 -0
- package/doc/src/components/Esdoc/TypeDocLinkHTML.js +251 -0
- package/doc/src/components/Esdoc/index.js +7 -0
- package/doc/src/components/Esdoc/taffydb.js +2070 -0
- package/doc/src/components/Example.js +200 -0
- package/doc/src/components/ExampleCard.js +126 -0
- package/doc/src/components/Examples.js +78 -0
- package/doc/src/components/Home.js +121 -0
- package/doc/src/components/TrackerExample.js +39 -0
- package/doc/src/examples.js +117 -0
- package/doc/styles/App.scss +53 -0
- package/doc/styles/identifiers.css +38 -0
- package/doc/styles/search.css +76 -0
- package/doc/styles/style.css +603 -0
- package/esdoc/README.md +27 -0
- package/esdoc/plugins/MyPlugin.js +69 -0
- package/esdoc/plugins/dynamic-property-plugin/Plugin.js +50 -0
- package/esdoc/plugins/externals-plugin/Plugin.js +45 -0
- package/esdoc/plugins/externals-plugin/externals.js +93 -0
- package/esdoc/plugins/optional-chaining-plugin/Plugin.js +8 -0
- package/global-setup.js +3 -0
- package/jest.config.js +24 -0
- package/package.json +18 -18
- package/pull_request_template.md +17 -0
- package/scripts/read-pkg-json.js +22 -0
- package/src/api/RealtimeAPI.test.js +144 -0
- package/src/api/RealtimeAPI.ts +712 -0
- package/src/api/RoutingAPI.test.js +41 -0
- package/src/api/RoutingAPI.ts +47 -0
- package/src/api/StopsAPI.test.js +34 -0
- package/src/api/StopsAPI.ts +45 -0
- package/{api → src/api}/typedefs.js +11 -1
- package/src/common/api/HttpAPI.test.js +68 -0
- package/src/common/api/HttpAPI.ts +77 -0
- package/src/common/api/WebSocketAPI.test.js +421 -0
- package/src/common/api/WebSocketAPI.ts +515 -0
- package/src/common/controls/ControlCommon.test.js +106 -0
- package/src/common/controls/ControlCommon.ts +194 -0
- package/src/common/controls/CopyrightControlCommon.ts +41 -0
- package/src/common/controls/StopFinderControlCommon.ts +192 -0
- package/src/common/layers/LayerCommon.test.js +158 -0
- package/src/common/layers/LayerCommon.ts +321 -0
- package/src/common/mixins/RealtimeLayerMixin.ts +1168 -0
- package/src/common/mixins/UserInteractionsLayerMixin.test.js +227 -0
- package/src/common/mixins/UserInteractionsLayerMixin.ts +352 -0
- package/src/common/styles/realtimeDefaultStyle.ts +436 -0
- package/src/common/styles/realtimeDelayStyle.ts +27 -0
- package/src/common/styles/realtimeHeadingStyle.ts +138 -0
- package/src/common/styles/realtimeSimpleStyle.ts +25 -0
- package/{common → src/common}/typedefs.js +1 -1
- package/src/common/utils/compareDepartures.ts +46 -0
- package/src/common/utils/createCanvas.ts +33 -0
- package/src/common/utils/createRealtimeFilters.test.js +100 -0
- package/src/common/utils/createRealtimeFilters.ts +96 -0
- package/src/common/utils/debounceDeparturesMessages.ts +52 -0
- package/src/common/utils/debounceWebsocketMessages.ts +47 -0
- package/src/common/utils/getLayersAsFlatArray.ts +17 -0
- package/src/common/utils/getMapboxMapCopyrights.test.js +47 -0
- package/src/common/utils/getMapboxMapCopyrights.ts +52 -0
- package/src/common/utils/getMapboxRender.ts +104 -0
- package/src/common/utils/getMaplibreRender.ts +54 -0
- package/{common/utils/getRealtimeModeSuffix.d.ts → src/common/utils/getRealtimeModeSuffix.ts} +4 -2
- package/src/common/utils/getUrlWithParams.ts +21 -0
- package/src/common/utils/getVehiclePosition.ts +92 -0
- package/{common → src/common}/utils/index.js +1 -2
- package/src/common/utils/realtimeConfig.test.js +57 -0
- package/src/common/utils/realtimeConfig.ts +228 -0
- package/src/common/utils/removeDuplicate.test.js +22 -0
- package/src/common/utils/removeDuplicate.ts +22 -0
- package/src/common/utils/renderTrajectories.ts +194 -0
- package/src/common/utils/sortAndFilterDepartures.ts +78 -0
- package/src/common/utils/sortByDelay.ts +29 -0
- package/src/common/utils/timeUtils.test.js +16 -0
- package/src/common/utils/timeUtils.ts +45 -0
- package/{iife.js → src/iife.js} +3 -1
- package/src/index.js +10 -0
- package/{mapbox/controls/CopyrightControl.js → src/mapbox/controls/CopyrightControl.ts} +26 -21
- package/src/mapbox/layers/Layer.test.js +217 -0
- package/src/mapbox/layers/Layer.ts +144 -0
- package/src/mapbox/layers/RealtimeLayer.test.js +13 -0
- package/src/mapbox/layers/RealtimeLayer.ts +350 -0
- package/src/mapbox/utils/getMercatorResolution.ts +21 -0
- package/src/mapbox/utils/getSourceCoordinates.ts +34 -0
- package/src/ol/README.md +0 -0
- package/src/ol/controls/CopyrightControl.test.js +211 -0
- package/src/ol/controls/CopyrightControl.ts +82 -0
- package/src/ol/controls/RoutingControl.test.js +205 -0
- package/src/ol/controls/RoutingControl.ts +869 -0
- package/src/ol/controls/StopFinderControl.test.js +59 -0
- package/{ol/controls/StopFinderControl.js → src/ol/controls/StopFinderControl.ts} +10 -7
- package/src/ol/controls/snapshots/RoutingControlRouteGen10.json +58 -0
- package/src/ol/controls/snapshots/RoutingControlRouteGen100.json +292 -0
- package/src/ol/controls/snapshots/RoutingControlRouteGen30.json +69 -0
- package/src/ol/controls/snapshots/RoutingControlRouteGen5.json +58 -0
- package/src/ol/controls/snapshots/RoutingControlRouteOSM.json +759 -0
- package/src/ol/controls/snapshots/RoutingControlStation1.json +60 -0
- package/src/ol/controls/snapshots/RoutingControlStation2.json +49 -0
- package/src/ol/layers/Layer.test.js +212 -0
- package/src/ol/layers/Layer.ts +252 -0
- package/src/ol/layers/MapGlLayer.ts +294 -0
- package/src/ol/layers/MapboxLayer.test.js +190 -0
- package/src/ol/layers/MapboxLayer.ts +136 -0
- package/src/ol/layers/MapboxStyleLayer.test.js +258 -0
- package/src/ol/layers/MapboxStyleLayer.ts +466 -0
- package/{ol/layers/MaplibreLayer.d.ts → src/ol/layers/MaplibreLayer.ts} +21 -10
- package/src/ol/layers/RealtimeLayer.test.js +84 -0
- package/src/ol/layers/RealtimeLayer.ts +465 -0
- package/src/ol/layers/RoutingLayer.test.js +48 -0
- package/src/ol/layers/RoutingLayer.ts +113 -0
- package/src/ol/layers/VectorLayer.test.js +87 -0
- package/src/ol/layers/VectorLayer.ts +48 -0
- package/src/ol/layers/WMSLayer.test.js +65 -0
- package/src/ol/layers/WMSLayer.ts +114 -0
- package/src/ol/styles/fullTrajectoryDelayStyle.ts +37 -0
- package/src/ol/styles/fullTrajectoryStyle.ts +56 -0
- package/{setupTests.js → src/setupTests.js} +12 -7
- package/tsconfig.json +23 -0
- package/api/RealtimeAPI.d.ts +0 -290
- package/api/RealtimeAPI.d.ts.map +0 -1
- package/api/RealtimeAPI.js +0 -483
- package/api/RoutingAPI.d.ts +0 -37
- package/api/RoutingAPI.d.ts.map +0 -1
- package/api/RoutingAPI.js +0 -35
- package/api/StopsAPI.d.ts +0 -38
- package/api/StopsAPI.d.ts.map +0 -1
- package/api/StopsAPI.js +0 -36
- package/api/index.d.ts +0 -4
- package/api/index.d.ts.map +0 -1
- package/api/typedefs.d.ts +0 -179
- package/api/typedefs.d.ts.map +0 -1
- package/common/api/HttpAPI.d.ts +0 -31
- package/common/api/HttpAPI.d.ts.map +0 -1
- package/common/api/HttpAPI.js +0 -57
- package/common/api/WebSocketAPI.d.ts +0 -153
- package/common/api/WebSocketAPI.d.ts.map +0 -1
- package/common/api/WebSocketAPI.js +0 -341
- package/common/controls/ControlCommon.d.ts +0 -76
- package/common/controls/ControlCommon.d.ts.map +0 -1
- package/common/controls/ControlCommon.js +0 -150
- package/common/controls/CopyrightControlCommon.d.ts +0 -13
- package/common/controls/CopyrightControlCommon.d.ts.map +0 -1
- package/common/controls/CopyrightControlCommon.js +0 -34
- package/common/controls/StopFinderControlCommon.d.ts +0 -55
- package/common/controls/StopFinderControlCommon.d.ts.map +0 -1
- package/common/controls/StopFinderControlCommon.js +0 -144
- package/common/index.d.ts +0 -3
- package/common/index.d.ts.map +0 -1
- package/common/layers/LayerCommon.d.ts +0 -94
- package/common/layers/LayerCommon.d.ts.map +0 -1
- package/common/layers/LayerCommon.js +0 -244
- package/common/mixins/RealtimeLayerMixin.d.ts +0 -288
- package/common/mixins/RealtimeLayerMixin.d.ts.map +0 -1
- package/common/mixins/RealtimeLayerMixin.js +0 -779
- package/common/mixins/UserInteractionsLayerMixin.d.ts +0 -60
- package/common/mixins/UserInteractionsLayerMixin.d.ts.map +0 -1
- package/common/mixins/UserInteractionsLayerMixin.js +0 -241
- package/common/styles/index.d.ts +0 -5
- package/common/styles/index.d.ts.map +0 -1
- package/common/styles/realtimeDefaultStyle.d.ts +0 -36
- package/common/styles/realtimeDefaultStyle.d.ts.map +0 -1
- package/common/styles/realtimeDefaultStyle.js +0 -276
- package/common/styles/realtimeDelayStyle.d.ts +0 -12
- package/common/styles/realtimeDelayStyle.d.ts.map +0 -1
- package/common/styles/realtimeDelayStyle.js +0 -13
- package/common/styles/realtimeHeadingStyle.d.ts +0 -12
- package/common/styles/realtimeHeadingStyle.d.ts.map +0 -1
- package/common/styles/realtimeHeadingStyle.js +0 -87
- package/common/styles/realtimeSimpleStyle.d.ts +0 -4
- package/common/styles/realtimeSimpleStyle.d.ts.map +0 -1
- package/common/styles/realtimeSimpleStyle.js +0 -23
- package/common/typedefs.d.ts +0 -183
- package/common/typedefs.d.ts.map +0 -1
- package/common/utils/compareDepartures.d.ts +0 -11
- package/common/utils/compareDepartures.d.ts.map +0 -1
- package/common/utils/compareDepartures.js +0 -35
- package/common/utils/createCanvas.d.ts +0 -11
- package/common/utils/createCanvas.d.ts.map +0 -1
- package/common/utils/createCanvas.js +0 -28
- package/common/utils/createRealtimeFilters.d.ts +0 -13
- package/common/utils/createRealtimeFilters.d.ts.map +0 -1
- package/common/utils/createRealtimeFilters.js +0 -74
- package/common/utils/debounceDeparturesMessages.d.ts +0 -13
- package/common/utils/debounceDeparturesMessages.d.ts.map +0 -1
- package/common/utils/debounceDeparturesMessages.js +0 -28
- package/common/utils/debounceWebsocketMessages.d.ts +0 -12
- package/common/utils/debounceWebsocketMessages.d.ts.map +0 -1
- package/common/utils/debounceWebsocketMessages.js +0 -30
- package/common/utils/getLayersAsFlatArray.d.ts +0 -4
- package/common/utils/getLayersAsFlatArray.d.ts.map +0 -1
- package/common/utils/getLayersAsFlatArray.js +0 -16
- package/common/utils/getMapboxMapCopyrights.d.ts +0 -18
- package/common/utils/getMapboxMapCopyrights.d.ts.map +0 -1
- package/common/utils/getMapboxMapCopyrights.js +0 -30
- package/common/utils/getMapboxRender.d.ts +0 -8
- package/common/utils/getMapboxRender.d.ts.map +0 -1
- package/common/utils/getMapboxRender.js +0 -88
- package/common/utils/getMaplibreRender.d.ts +0 -9
- package/common/utils/getMaplibreRender.d.ts.map +0 -1
- package/common/utils/getMaplibreRender.js +0 -40
- package/common/utils/getRealtimeModeSuffix.d.ts.map +0 -1
- package/common/utils/getRealtimeModeSuffix.js +0 -7
- package/common/utils/getUrlWithParams.d.ts +0 -9
- package/common/utils/getUrlWithParams.d.ts.map +0 -1
- package/common/utils/getUrlWithParams.js +0 -18
- package/common/utils/getVehiclePosition.d.ts +0 -16
- package/common/utils/getVehiclePosition.d.ts.map +0 -1
- package/common/utils/getVehiclePosition.js +0 -72
- package/common/utils/index.d.ts +0 -18
- package/common/utils/index.d.ts.map +0 -1
- package/common/utils/realtimeConfig.d.ts +0 -53
- package/common/utils/realtimeConfig.d.ts.map +0 -1
- package/common/utils/realtimeConfig.js +0 -202
- package/common/utils/removeDuplicate.d.ts +0 -10
- package/common/utils/removeDuplicate.d.ts.map +0 -1
- package/common/utils/removeDuplicate.js +0 -15
- package/common/utils/renderTrajectories.d.ts +0 -17
- package/common/utils/renderTrajectories.d.ts.map +0 -1
- package/common/utils/renderTrajectories.js +0 -110
- package/common/utils/sortAndFilterDepartures.d.ts +0 -16
- package/common/utils/sortAndFilterDepartures.d.ts.map +0 -1
- package/common/utils/sortAndFilterDepartures.js +0 -58
- package/common/utils/sortByDelay.d.ts +0 -4
- package/common/utils/sortByDelay.d.ts.map +0 -1
- package/common/utils/sortByDelay.js +0 -21
- package/common/utils/timeUtils.d.ts +0 -24
- package/common/utils/timeUtils.d.ts.map +0 -1
- package/common/utils/timeUtils.js +0 -39
- package/iife.d.ts +0 -3
- package/iife.d.ts.map +0 -1
- package/index.d.ts +0 -10
- package/index.d.ts.map +0 -1
- package/index.js +0 -10
- package/mapbox/controls/CopyrightControl.d.ts +0 -29
- package/mapbox/controls/CopyrightControl.d.ts.map +0 -1
- package/mapbox/controls/index.d.ts +0 -2
- package/mapbox/controls/index.d.ts.map +0 -1
- package/mapbox/index.d.ts +0 -6
- package/mapbox/index.d.ts.map +0 -1
- package/mapbox/layers/Layer.d.ts +0 -59
- package/mapbox/layers/Layer.d.ts.map +0 -1
- package/mapbox/layers/Layer.js +0 -101
- package/mapbox/layers/RealtimeLayer.d.ts +0 -180
- package/mapbox/layers/RealtimeLayer.d.ts.map +0 -1
- package/mapbox/layers/RealtimeLayer.js +0 -270
- package/mapbox/layers/index.d.ts +0 -3
- package/mapbox/layers/index.d.ts.map +0 -1
- package/mapbox/utils/getMercatorResolution.d.ts +0 -9
- package/mapbox/utils/getMercatorResolution.d.ts.map +0 -1
- package/mapbox/utils/getMercatorResolution.js +0 -18
- package/mapbox/utils/getSourceCoordinates.d.ts +0 -9
- package/mapbox/utils/getSourceCoordinates.d.ts.map +0 -1
- package/mapbox/utils/getSourceCoordinates.js +0 -27
- package/mapbox/utils/index.d.ts +0 -3
- package/mapbox/utils/index.d.ts.map +0 -1
- package/mbt.js +0 -64706
- package/mbt.js.map +0 -7
- package/mbt.min.js +0 -1090
- package/mbt.min.js.map +0 -7
- package/ol/controls/CopyrightControl.d.ts +0 -31
- package/ol/controls/CopyrightControl.d.ts.map +0 -1
- package/ol/controls/CopyrightControl.js +0 -68
- package/ol/controls/RoutingControl.d.ts +0 -193
- package/ol/controls/RoutingControl.d.ts.map +0 -1
- package/ol/controls/RoutingControl.js +0 -631
- package/ol/controls/StopFinderControl.d.ts +0 -30
- package/ol/controls/StopFinderControl.d.ts.map +0 -1
- package/ol/controls/index.d.ts +0 -4
- package/ol/controls/index.d.ts.map +0 -1
- package/ol/index.d.ts +0 -6
- package/ol/index.d.ts.map +0 -1
- package/ol/layers/Layer.d.ts +0 -86
- package/ol/layers/Layer.d.ts.map +0 -1
- package/ol/layers/Layer.js +0 -174
- package/ol/layers/MapGlLayer.d.ts +0 -67
- package/ol/layers/MapGlLayer.d.ts.map +0 -1
- package/ol/layers/MapGlLayer.js +0 -218
- package/ol/layers/MapboxLayer.d.ts +0 -50
- package/ol/layers/MapboxLayer.d.ts.map +0 -1
- package/ol/layers/MapboxLayer.js +0 -109
- package/ol/layers/MapboxStyleLayer.d.ts +0 -130
- package/ol/layers/MapboxStyleLayer.d.ts.map +0 -1
- package/ol/layers/MapboxStyleLayer.js +0 -378
- package/ol/layers/MaplibreLayer.d.ts.map +0 -1
- package/ol/layers/MaplibreLayer.js +0 -34
- package/ol/layers/RealtimeLayer.d.ts +0 -202
- package/ol/layers/RealtimeLayer.d.ts.map +0 -1
- package/ol/layers/RealtimeLayer.js +0 -332
- package/ol/layers/RoutingLayer.d.ts +0 -35
- package/ol/layers/RoutingLayer.d.ts.map +0 -1
- package/ol/layers/RoutingLayer.js +0 -85
- package/ol/layers/VectorLayer.d.ts +0 -25
- package/ol/layers/VectorLayer.d.ts.map +0 -1
- package/ol/layers/VectorLayer.js +0 -38
- package/ol/layers/WMSLayer.d.ts +0 -42
- package/ol/layers/WMSLayer.d.ts.map +0 -1
- package/ol/layers/WMSLayer.js +0 -88
- package/ol/layers/index.d.ts +0 -9
- package/ol/layers/index.d.ts.map +0 -1
- package/ol/styles/fullTrajectoryDelayStyle.d.ts +0 -4
- package/ol/styles/fullTrajectoryDelayStyle.d.ts.map +0 -1
- package/ol/styles/fullTrajectoryDelayStyle.js +0 -33
- package/ol/styles/fullTrajectoryStyle.d.ts +0 -5
- package/ol/styles/fullTrajectoryStyle.d.ts.map +0 -1
- package/ol/styles/fullTrajectoryStyle.js +0 -44
- package/ol/styles/index.d.ts +0 -3
- package/ol/styles/index.d.ts.map +0 -1
- package/setupTests.d.ts +0 -2
- package/setupTests.d.ts.map +0 -1
- /package/{api → src/api}/index.js +0 -0
- /package/{common → src/common}/index.js +0 -0
- /package/{common → src/common}/styles/index.js +0 -0
- /package/{mapbox → src/mapbox}/controls/index.js +0 -0
- /package/{mapbox → src/mapbox}/index.js +0 -0
- /package/{mapbox → src/mapbox}/layers/index.js +0 -0
- /package/{mapbox → src/mapbox}/utils/index.js +0 -0
- /package/{ol → src/ol}/controls/index.js +0 -0
- /package/{ol → src/ol}/index.js +0 -0
- /package/{ol → src/ol}/layers/index.js +0 -0
- /package/{ol → src/ol}/styles/index.js +0 -0
- /package/{types → src/types}/common.d.ts +0 -0
- /package/{types → src/types}/index.d.ts +0 -0
- /package/{types → src/types}/realtime.d.ts +0 -0
- /package/{types → src/types}/routing.d.ts +0 -0
- /package/{types → src/types}/stops.d.ts +0 -0
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
import { RealtimeTrajectoryResponse } from '../../types';
|
|
2
|
+
|
|
3
|
+
export declare type WebSocketAPIParameters = {
|
|
4
|
+
channel?: string;
|
|
5
|
+
args?: string | number;
|
|
6
|
+
id?: string | number;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export declare type WebSocketAPIMessageEventData<T> = {
|
|
10
|
+
timestamp: number;
|
|
11
|
+
source: string;
|
|
12
|
+
content: T;
|
|
13
|
+
// | T
|
|
14
|
+
// | string
|
|
15
|
+
// | Feature
|
|
16
|
+
// | RealtimeTrajectoryResponse[]
|
|
17
|
+
// | RealtimeDeparture
|
|
18
|
+
// | RealtimeNews[]
|
|
19
|
+
// | RealtimeStation
|
|
20
|
+
// | RealtimeExtraGeom
|
|
21
|
+
// | RealtimeTrajectory
|
|
22
|
+
// | RealtimeStopSequence[]
|
|
23
|
+
// | RealtimeFullTrajectory;
|
|
24
|
+
client_reference: string | number | null;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type WebSocketAPIBufferMessageEventData = WebSocketAPIMessageEventData<
|
|
28
|
+
RealtimeTrajectoryResponse[]
|
|
29
|
+
> & {
|
|
30
|
+
source: 'buffer';
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type WebSocketAPIMessageEvent = Event & {
|
|
34
|
+
data: string;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export interface WebSocketAPIMessageEventListener {
|
|
38
|
+
(evt: WebSocketAPIMessageEvent): void;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* This type represents a function that has been call with each feature returned by the websocket.
|
|
43
|
+
*/
|
|
44
|
+
export interface WebSocketAPIMessageCallback<T> {
|
|
45
|
+
(data: WebSocketAPIMessageEventData<T>): void;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export declare type WebSocketAPISubscription = {
|
|
49
|
+
params: WebSocketAPIParameters;
|
|
50
|
+
cb: WebSocketAPIMessageCallback<any>;
|
|
51
|
+
errorCb?: EventListener;
|
|
52
|
+
onMessageCb: WebSocketAPIMessageEventListener;
|
|
53
|
+
onErrorCb?: EventListener;
|
|
54
|
+
quiet: boolean;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export declare type WebSocketAPISubscribed = {
|
|
58
|
+
[index: string]: boolean;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export declare type WebSocketAPIRequest = {
|
|
62
|
+
params: WebSocketAPIParameters;
|
|
63
|
+
cb: WebSocketAPIMessageCallback<any>;
|
|
64
|
+
errorCb?: EventListener;
|
|
65
|
+
onMessageCb: WebSocketAPIMessageEventListener;
|
|
66
|
+
onErrorCb?: EventListener;
|
|
67
|
+
requestString: String;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Class used to facilitate connection to a WebSocketAPI and
|
|
71
|
+
* also to manage properly messages send to the WebSocketAPI.
|
|
72
|
+
* This class must not contain any specific implementation.
|
|
73
|
+
*/
|
|
74
|
+
class WebSocketAPI {
|
|
75
|
+
websocket?: WebSocket;
|
|
76
|
+
|
|
77
|
+
closed?: boolean;
|
|
78
|
+
|
|
79
|
+
closing?: boolean;
|
|
80
|
+
|
|
81
|
+
connecting?: boolean;
|
|
82
|
+
|
|
83
|
+
open?: boolean;
|
|
84
|
+
|
|
85
|
+
messagesOnOpen!: Array<string>;
|
|
86
|
+
|
|
87
|
+
subscriptions!: Array<WebSocketAPISubscription>;
|
|
88
|
+
|
|
89
|
+
subscribed!: WebSocketAPISubscribed;
|
|
90
|
+
|
|
91
|
+
requests!: Array<WebSocketAPIRequest>;
|
|
92
|
+
|
|
93
|
+
constructor() {
|
|
94
|
+
this.defineProperties();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
defineProperties() {
|
|
98
|
+
Object.defineProperties(this, {
|
|
99
|
+
closed: {
|
|
100
|
+
get: () =>
|
|
101
|
+
!!(
|
|
102
|
+
!this.websocket ||
|
|
103
|
+
this.websocket.readyState === this.websocket.CLOSED
|
|
104
|
+
),
|
|
105
|
+
},
|
|
106
|
+
closing: {
|
|
107
|
+
get: () =>
|
|
108
|
+
!!(
|
|
109
|
+
this.websocket &&
|
|
110
|
+
this.websocket.readyState === this.websocket.CLOSING
|
|
111
|
+
),
|
|
112
|
+
},
|
|
113
|
+
connecting: {
|
|
114
|
+
get: () =>
|
|
115
|
+
!!(
|
|
116
|
+
this.websocket &&
|
|
117
|
+
this.websocket.readyState === this.websocket.CONNECTING
|
|
118
|
+
),
|
|
119
|
+
},
|
|
120
|
+
open: {
|
|
121
|
+
get: () =>
|
|
122
|
+
!!(
|
|
123
|
+
this.websocket && this.websocket.readyState === this.websocket.OPEN
|
|
124
|
+
),
|
|
125
|
+
},
|
|
126
|
+
/**
|
|
127
|
+
* Array of message to send on open.
|
|
128
|
+
* @type {Array<string>}
|
|
129
|
+
* @private
|
|
130
|
+
*/
|
|
131
|
+
messagesOnOpen: {
|
|
132
|
+
value: [],
|
|
133
|
+
writable: true,
|
|
134
|
+
},
|
|
135
|
+
/**
|
|
136
|
+
* Array of subscriptions.
|
|
137
|
+
* @type {Array<WebSocketSubscription>}
|
|
138
|
+
* @private
|
|
139
|
+
*/
|
|
140
|
+
subscriptions: {
|
|
141
|
+
value: [],
|
|
142
|
+
writable: true,
|
|
143
|
+
},
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* List of channels subscribed.
|
|
147
|
+
* @type {WebSocketSubscribed}
|
|
148
|
+
* @private
|
|
149
|
+
*/
|
|
150
|
+
subscribed: {
|
|
151
|
+
value: {},
|
|
152
|
+
writable: true,
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Get the websocket request string.
|
|
159
|
+
*
|
|
160
|
+
* @param {string} method Request mehtod {GET, SUB}.
|
|
161
|
+
* @param {WebSocketParameters} params Request parameters.
|
|
162
|
+
* @param {string} params.channel Channel name
|
|
163
|
+
* @param {string} [params.args] Request arguments
|
|
164
|
+
* @param {Number|string} [params.id] Request identifier
|
|
165
|
+
* @return {string} request string
|
|
166
|
+
* @private
|
|
167
|
+
*/
|
|
168
|
+
static getRequestString(method: string, params: WebSocketAPIParameters = {}) {
|
|
169
|
+
let reqStr = `${method} ${params.channel}`;
|
|
170
|
+
reqStr += params.args ? ` ${params.args}` : '';
|
|
171
|
+
reqStr += params.id ? ` ${params.id}` : '';
|
|
172
|
+
return reqStr.trim();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* (Re)connect the websocket.
|
|
177
|
+
*
|
|
178
|
+
* @param {string} url Websocket url.
|
|
179
|
+
* @param {function} onOpen Callback called when the websocket connection is opened and before subscriptions of previous subscriptions.
|
|
180
|
+
* @private
|
|
181
|
+
*/
|
|
182
|
+
connect(url: string, onOpen = () => {}) {
|
|
183
|
+
// if no url specify, close the current websocket and do nothing.
|
|
184
|
+
if (!url) {
|
|
185
|
+
this.websocket?.close();
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Behavior when a websocket already exists.
|
|
190
|
+
if (this.websocket) {
|
|
191
|
+
// If the current websocket has the same url and is open or is connecting, do nothing.
|
|
192
|
+
if (this.websocket.url === url && (this.open || this.connecting)) {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// If the current websocket has not the same url and is open or is connecting, close it.
|
|
197
|
+
if (this.websocket.url !== url && (this.open || this.connecting)) {
|
|
198
|
+
this.websocket.close();
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** @private */
|
|
203
|
+
this.websocket = new WebSocket(url);
|
|
204
|
+
|
|
205
|
+
if (!this.open) {
|
|
206
|
+
this.websocket.addEventListener('open', () => {
|
|
207
|
+
onOpen();
|
|
208
|
+
this.subscribePreviousSubscriptions();
|
|
209
|
+
});
|
|
210
|
+
} else {
|
|
211
|
+
onOpen();
|
|
212
|
+
this.subscribePreviousSubscriptions();
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Close the websocket definitively.
|
|
218
|
+
*
|
|
219
|
+
* @private
|
|
220
|
+
*/
|
|
221
|
+
close() {
|
|
222
|
+
if (this.websocket && (this.open || this.connecting)) {
|
|
223
|
+
this.websocket.onclose = () => {};
|
|
224
|
+
this.websocket.close();
|
|
225
|
+
this.messagesOnOpen = [];
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Sends a message to the websocket.
|
|
231
|
+
*
|
|
232
|
+
* @param {message} message Message to send.
|
|
233
|
+
* @private
|
|
234
|
+
*/
|
|
235
|
+
send(message: string) {
|
|
236
|
+
if (!this.websocket || this.closed || this.closing) {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
const send = () => {
|
|
240
|
+
this.websocket?.send(message);
|
|
241
|
+
};
|
|
242
|
+
if (!this.open) {
|
|
243
|
+
// This 'if' avoid sending 2 identical BBOX message on open,
|
|
244
|
+
if (!this.messagesOnOpen.includes(message)) {
|
|
245
|
+
this.messagesOnOpen.push(message);
|
|
246
|
+
this.websocket.addEventListener('open', () => {
|
|
247
|
+
this.messagesOnOpen = [];
|
|
248
|
+
send();
|
|
249
|
+
});
|
|
250
|
+
this.websocket.addEventListener('close', () => {
|
|
251
|
+
this.messagesOnOpen = [];
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
} else if (!this.messagesOnOpen.includes(message)) {
|
|
255
|
+
send();
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
addEvents(
|
|
260
|
+
onMessage: WebSocketAPIMessageEventListener,
|
|
261
|
+
onError?: EventListener,
|
|
262
|
+
) {
|
|
263
|
+
if (this.websocket) {
|
|
264
|
+
this.websocket.addEventListener('message', onMessage);
|
|
265
|
+
|
|
266
|
+
if (onError) {
|
|
267
|
+
this.websocket.addEventListener('error', onError);
|
|
268
|
+
this.websocket.addEventListener('close', onError);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
removeEvents(
|
|
274
|
+
onMessage: WebSocketAPIMessageEventListener,
|
|
275
|
+
onError?: EventListener,
|
|
276
|
+
) {
|
|
277
|
+
if (this.websocket) {
|
|
278
|
+
this.websocket.removeEventListener('message', onMessage);
|
|
279
|
+
|
|
280
|
+
if (onError) {
|
|
281
|
+
this.websocket.removeEventListener('error', onError);
|
|
282
|
+
this.websocket.removeEventListener('close', onError);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Listen to websocket messages.
|
|
289
|
+
*
|
|
290
|
+
* @param {WebSocketParameters} params Parameters for the websocket get request
|
|
291
|
+
* @param {function} cb callback on listen
|
|
292
|
+
* @param {function} errorCb Callback on error
|
|
293
|
+
* @return {{onMessage: function, errorCb: function}} Object with onMessage and error callbacks
|
|
294
|
+
* @private
|
|
295
|
+
*/
|
|
296
|
+
listen(
|
|
297
|
+
params: WebSocketAPIParameters,
|
|
298
|
+
cb: WebSocketAPIMessageCallback<any>,
|
|
299
|
+
errorCb?: EventListener,
|
|
300
|
+
): {
|
|
301
|
+
onMessageCb: WebSocketAPIMessageEventListener;
|
|
302
|
+
onErrorCb?: EventListener;
|
|
303
|
+
} {
|
|
304
|
+
// Remove the previous identical callback
|
|
305
|
+
this.unlisten(params, cb);
|
|
306
|
+
|
|
307
|
+
// We wrap the message callback to be sure we only propagate the message if it is for the right channel.
|
|
308
|
+
const onMessage: WebSocketAPIMessageEventListener = (
|
|
309
|
+
evt: WebSocketAPIMessageEvent,
|
|
310
|
+
) => {
|
|
311
|
+
let data: WebSocketAPIMessageEventData<any>;
|
|
312
|
+
try {
|
|
313
|
+
data = JSON.parse(evt.data);
|
|
314
|
+
} catch (err) {
|
|
315
|
+
// eslint-disable-next-line no-console
|
|
316
|
+
console.error('WebSocket: unable to parse JSON data', err, evt.data);
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
let source = params.channel;
|
|
320
|
+
source += params.args ? ` ${params.args}` : '';
|
|
321
|
+
|
|
322
|
+
// Buffer channel message return a list of other channels to propagate to proper callbacks.
|
|
323
|
+
let contents: Array<WebSocketAPIMessageEventData<any>>;
|
|
324
|
+
|
|
325
|
+
if (data.source === 'buffer') {
|
|
326
|
+
contents = (data as unknown as WebSocketAPIBufferMessageEventData)
|
|
327
|
+
.content;
|
|
328
|
+
} else {
|
|
329
|
+
contents = [data];
|
|
330
|
+
}
|
|
331
|
+
contents.forEach((content: WebSocketAPIMessageEventData<any>) => {
|
|
332
|
+
// Because of backend optimization, the last content is null.
|
|
333
|
+
if (
|
|
334
|
+
content?.source === source &&
|
|
335
|
+
(!params.id || params.id === data.client_reference)
|
|
336
|
+
) {
|
|
337
|
+
cb(content);
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
this.addEvents(onMessage, errorCb);
|
|
343
|
+
|
|
344
|
+
return { onMessageCb: onMessage, onErrorCb: errorCb };
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Unlisten websocket messages.
|
|
349
|
+
*
|
|
350
|
+
* @param {Object} params Parameters for the websocket get request.
|
|
351
|
+
* @param {function} cb Callback used when listen.
|
|
352
|
+
* @private
|
|
353
|
+
*/
|
|
354
|
+
unlisten(
|
|
355
|
+
params: WebSocketAPIParameters,
|
|
356
|
+
cb: WebSocketAPIMessageCallback<any>,
|
|
357
|
+
) {
|
|
358
|
+
[...(this.subscriptions || []), ...(this.requests || [])]
|
|
359
|
+
.filter(
|
|
360
|
+
(s) => s.params.channel === params.channel && (!cb || s.cb === cb),
|
|
361
|
+
)
|
|
362
|
+
.forEach(({ onMessageCb, onErrorCb }) => {
|
|
363
|
+
this.removeEvents(onMessageCb, onErrorCb);
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Sends a get request to the websocket.
|
|
369
|
+
* The callback is called only once, when the response is received or when the call returns an error.
|
|
370
|
+
*
|
|
371
|
+
* @param {Object} params Parameters for the websocket get request
|
|
372
|
+
* @param {function} onMessage callback on message event
|
|
373
|
+
* @param {function} onError Callback on error and close event
|
|
374
|
+
* @private
|
|
375
|
+
*/
|
|
376
|
+
get(
|
|
377
|
+
params: WebSocketAPIParameters,
|
|
378
|
+
cb: WebSocketAPIMessageCallback<any>,
|
|
379
|
+
errorCb?: EventListener,
|
|
380
|
+
) {
|
|
381
|
+
const requestString = WebSocketAPI.getRequestString('GET', params);
|
|
382
|
+
this.send(requestString);
|
|
383
|
+
|
|
384
|
+
// We wrap the callbacks to make sure they are called only once.
|
|
385
|
+
const once =
|
|
386
|
+
(callback: WebSocketAPIMessageCallback<any> | EventListener) =>
|
|
387
|
+
// @ts-ignore: Spread error
|
|
388
|
+
(...args) => {
|
|
389
|
+
// @ts-ignore: Spread error
|
|
390
|
+
callback(...args);
|
|
391
|
+
const index = this.requests.findIndex(
|
|
392
|
+
(request) =>
|
|
393
|
+
requestString === request.requestString && cb === request.cb,
|
|
394
|
+
);
|
|
395
|
+
const { onMessageCb, onErrorCb } = this.requests[index];
|
|
396
|
+
this.removeEvents(onMessageCb, onErrorCb);
|
|
397
|
+
this.requests.splice(index, 1);
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
const { onMessageCb, onErrorCb } = this.listen(
|
|
401
|
+
params,
|
|
402
|
+
once(cb),
|
|
403
|
+
errorCb && once(errorCb),
|
|
404
|
+
);
|
|
405
|
+
|
|
406
|
+
// Store requests and callbacks to be able to remove them.
|
|
407
|
+
if (!this.requests) {
|
|
408
|
+
this.requests = [];
|
|
409
|
+
}
|
|
410
|
+
const index = this.requests.findIndex(
|
|
411
|
+
(request) => requestString === request.requestString && cb === request.cb,
|
|
412
|
+
);
|
|
413
|
+
const newReq = {
|
|
414
|
+
params,
|
|
415
|
+
requestString,
|
|
416
|
+
cb,
|
|
417
|
+
errorCb,
|
|
418
|
+
onMessageCb,
|
|
419
|
+
onErrorCb,
|
|
420
|
+
};
|
|
421
|
+
if (index > -1) {
|
|
422
|
+
this.requests[index] = newReq;
|
|
423
|
+
} else {
|
|
424
|
+
this.requests.push(newReq);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Subscribe to a given channel.
|
|
430
|
+
*
|
|
431
|
+
* @param {Object} params Parameters for the websocket get request
|
|
432
|
+
* @param {function} cb callback on listen
|
|
433
|
+
* @param {function} errorCb Callback on error
|
|
434
|
+
* @param {boolean} quiet if false, no GET or SUB requests are send, only the callback is registered.
|
|
435
|
+
* @private
|
|
436
|
+
*/
|
|
437
|
+
subscribe(
|
|
438
|
+
params: WebSocketAPIParameters,
|
|
439
|
+
cb: WebSocketAPIMessageCallback<any>,
|
|
440
|
+
errorCb?: EventListener,
|
|
441
|
+
quiet = false,
|
|
442
|
+
) {
|
|
443
|
+
const { onMessageCb, onErrorCb } = this.listen(params, cb, errorCb);
|
|
444
|
+
const reqStr = WebSocketAPI.getRequestString('', params);
|
|
445
|
+
|
|
446
|
+
const index = this.subscriptions.findIndex(
|
|
447
|
+
(subcr) => params.channel === subcr.params.channel && cb === subcr.cb,
|
|
448
|
+
);
|
|
449
|
+
const newSubscr = { params, cb, errorCb, onMessageCb, onErrorCb, quiet };
|
|
450
|
+
if (index > -1) {
|
|
451
|
+
this.subscriptions[index] = newSubscr;
|
|
452
|
+
} else {
|
|
453
|
+
this.subscriptions.push(newSubscr);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
if (!this.subscribed[reqStr]) {
|
|
457
|
+
if (!newSubscr.quiet) {
|
|
458
|
+
this.send(`GET ${reqStr}`);
|
|
459
|
+
this.send(`SUB ${reqStr}`);
|
|
460
|
+
}
|
|
461
|
+
this.subscribed[reqStr] = true;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* Unsubscribe from a channel.
|
|
467
|
+
* @param {string} source source to unsubscribe from
|
|
468
|
+
* @param {function} cb Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
|
|
469
|
+
* @private
|
|
470
|
+
*/
|
|
471
|
+
unsubscribe(source: string, cb?: WebSocketAPIMessageCallback<any>) {
|
|
472
|
+
const toRemove = this.subscriptions.filter(
|
|
473
|
+
(s) => s.params.channel === source && (!cb || s.cb === cb),
|
|
474
|
+
);
|
|
475
|
+
|
|
476
|
+
toRemove.forEach(({ onMessageCb, onErrorCb }) => {
|
|
477
|
+
this.removeEvents(onMessageCb, onErrorCb);
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
this.subscriptions = this.subscriptions.filter(
|
|
481
|
+
(s) => s.params.channel !== source || (cb && s.cb !== cb),
|
|
482
|
+
);
|
|
483
|
+
|
|
484
|
+
// If there is no more subscriptions to this channel, and the removed subscriptions didn't register quietly,
|
|
485
|
+
// we DEL it.
|
|
486
|
+
if (
|
|
487
|
+
source &&
|
|
488
|
+
this.subscribed[source] &&
|
|
489
|
+
!this.subscriptions.find((s) => s.params.channel === source) &&
|
|
490
|
+
toRemove.find((subscr) => !subscr.quiet)
|
|
491
|
+
) {
|
|
492
|
+
this.send(`DEL ${source}`);
|
|
493
|
+
this.subscribed[source] = false;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* After an auto reconnection we need to re-subscribe to the channels.
|
|
499
|
+
*/
|
|
500
|
+
subscribePreviousSubscriptions() {
|
|
501
|
+
// Before to subscribe previous subscriptions we make sure they
|
|
502
|
+
// are all defined as unsubscribed, because this code is asynchrone
|
|
503
|
+
// and a subscription could have been added in between.
|
|
504
|
+
Object.keys(this.subscribed).forEach((key) => {
|
|
505
|
+
this.subscribed[key] = false;
|
|
506
|
+
});
|
|
507
|
+
|
|
508
|
+
// Subscribe all previous subscriptions.
|
|
509
|
+
[...this.subscriptions].forEach((s) => {
|
|
510
|
+
this.subscribe(s.params, s.cb, s.errorCb, s.quiet);
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
export default WebSocketAPI;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import ControlCommon from './ControlCommon';
|
|
2
|
+
|
|
3
|
+
// We Crate a class to extend to avoid having console error messages.
|
|
4
|
+
class Control extends ControlCommon {
|
|
5
|
+
// eslint-disable-next-line class-methods-use-this
|
|
6
|
+
deactivate() {}
|
|
7
|
+
|
|
8
|
+
render() {}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
describe('Control', () => {
|
|
12
|
+
test('should be activated by default', () => {
|
|
13
|
+
const control = new Control();
|
|
14
|
+
expect(control.active).toBe(true);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test('should not be activated if set to false in the options', () => {
|
|
18
|
+
const control = new Control({ active: false });
|
|
19
|
+
expect(control.active).toBe(false);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test('should call activate/deactivate when active is set to true/false', () => {
|
|
23
|
+
const control = new Control();
|
|
24
|
+
const spy1 = jest.spyOn(control, 'activate');
|
|
25
|
+
const spy2 = jest.spyOn(control, 'deactivate');
|
|
26
|
+
const spy3 = jest.spyOn(control, 'render');
|
|
27
|
+
control.active = false;
|
|
28
|
+
expect(spy1).toBeCalledTimes(0);
|
|
29
|
+
expect(spy2).toBeCalledTimes(1);
|
|
30
|
+
expect(spy3).toBeCalledTimes(1);
|
|
31
|
+
control.active = true;
|
|
32
|
+
expect(spy1).toBeCalledTimes(1);
|
|
33
|
+
expect(spy2).toBeCalledTimes(2);
|
|
34
|
+
expect(spy3).toBeCalledTimes(2);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test('should append/remove the element to the map container when map is set', () => {
|
|
38
|
+
const element = document.createElement('div');
|
|
39
|
+
const target = document.createElement('div');
|
|
40
|
+
const control = new Control({
|
|
41
|
+
element,
|
|
42
|
+
});
|
|
43
|
+
const spy1 = jest.spyOn(control, 'activate');
|
|
44
|
+
const spy2 = jest.spyOn(control, 'deactivate');
|
|
45
|
+
const spy3 = jest.spyOn(control, 'render');
|
|
46
|
+
control.map = {
|
|
47
|
+
getContainer() {
|
|
48
|
+
return target;
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
expect(target.childNodes[0]).toBe(element);
|
|
52
|
+
expect(spy1).toBeCalledTimes(1);
|
|
53
|
+
expect(spy2).toBeCalledTimes(2);
|
|
54
|
+
expect(spy3).toBeCalledTimes(1);
|
|
55
|
+
|
|
56
|
+
control.detachFromMap();
|
|
57
|
+
expect(target.childNodes[0]).toBe();
|
|
58
|
+
expect(spy1).toBeCalledTimes(1);
|
|
59
|
+
expect(spy2).toBeCalledTimes(3);
|
|
60
|
+
expect(spy3).toBeCalledTimes(2);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test('should append/remove the element to the target property when map is set', () => {
|
|
64
|
+
const element = document.createElement('div');
|
|
65
|
+
const target = document.createElement('div');
|
|
66
|
+
const control = new Control({
|
|
67
|
+
target,
|
|
68
|
+
element,
|
|
69
|
+
});
|
|
70
|
+
const spy1 = jest.spyOn(control, 'activate');
|
|
71
|
+
const spy2 = jest.spyOn(control, 'deactivate');
|
|
72
|
+
const spy3 = jest.spyOn(control, 'render');
|
|
73
|
+
control.map = {};
|
|
74
|
+
expect(target.childNodes[0]).toBe(element);
|
|
75
|
+
expect(spy1).toBeCalledTimes(1);
|
|
76
|
+
expect(spy2).toBeCalledTimes(2);
|
|
77
|
+
expect(spy3).toBeCalledTimes(1);
|
|
78
|
+
|
|
79
|
+
control.detachFromMap();
|
|
80
|
+
expect(target.childNodes[0]).toBe();
|
|
81
|
+
expect(spy1).toBeCalledTimes(1);
|
|
82
|
+
expect(spy2).toBeCalledTimes(3);
|
|
83
|
+
expect(spy3).toBeCalledTimes(2);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test('set a custom render method', () => {
|
|
87
|
+
const spy = jest.fn();
|
|
88
|
+
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
|
|
89
|
+
const control = new Control({
|
|
90
|
+
render: spy,
|
|
91
|
+
});
|
|
92
|
+
expect(spy).toBeCalledTimes(1);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test('pass function params to custom render method', () => {
|
|
96
|
+
const spy = jest.fn();
|
|
97
|
+
// eslint-disable-next-line no-unused-vars
|
|
98
|
+
const control = new Control({
|
|
99
|
+
render: spy,
|
|
100
|
+
});
|
|
101
|
+
control.render('foo', 'bar');
|
|
102
|
+
expect(spy).toBeCalledTimes(2);
|
|
103
|
+
expect(spy.mock.calls[1][0]).toBe('foo');
|
|
104
|
+
expect(spy.mock.calls[1][1]).toBe('bar');
|
|
105
|
+
});
|
|
106
|
+
});
|