@mapwhit/tilerenderer 0.47.1
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/LICENSE.txt +87 -0
- package/README.md +25 -0
- package/build/min/.dir +0 -0
- package/build/min/package.json +3 -0
- package/build/min/src/shaders/_prelude.fragment.glsl.txt +13 -0
- package/build/min/src/shaders/_prelude.vertex.glsl.txt +14 -0
- package/build/min/src/shaders/background.fragment.glsl.txt +5 -0
- package/build/min/src/shaders/background.vertex.glsl.txt +1 -0
- package/build/min/src/shaders/background_pattern.fragment.glsl.txt +5 -0
- package/build/min/src/shaders/background_pattern.vertex.glsl.txt +1 -0
- package/build/min/src/shaders/circle.fragment.glsl.txt +20 -0
- package/build/min/src/shaders/circle.vertex.glsl.txt +17 -0
- package/build/min/src/shaders/clipping_mask.fragment.glsl.txt +1 -0
- package/build/min/src/shaders/clipping_mask.vertex.glsl.txt +1 -0
- package/build/min/src/shaders/collision_box.fragment.glsl.txt +1 -0
- package/build/min/src/shaders/collision_box.vertex.glsl.txt +1 -0
- package/build/min/src/shaders/collision_circle.fragment.glsl.txt +1 -0
- package/build/min/src/shaders/collision_circle.vertex.glsl.txt +1 -0
- package/build/min/src/shaders/debug.fragment.glsl.txt +1 -0
- package/build/min/src/shaders/debug.vertex.glsl.txt +1 -0
- package/build/min/src/shaders/fill.fragment.glsl.txt +10 -0
- package/build/min/src/shaders/fill.vertex.glsl.txt +7 -0
- package/build/min/src/shaders/fill_extrusion.fragment.glsl.txt +13 -0
- package/build/min/src/shaders/fill_extrusion.vertex.glsl.txt +9 -0
- package/build/min/src/shaders/fill_extrusion_pattern.fragment.glsl.txt +15 -0
- package/build/min/src/shaders/fill_extrusion_pattern.vertex.glsl.txt +11 -0
- package/build/min/src/shaders/fill_outline.fragment.glsl.txt +10 -0
- package/build/min/src/shaders/fill_outline.vertex.glsl.txt +7 -0
- package/build/min/src/shaders/fill_outline_pattern.fragment.glsl.txt +13 -0
- package/build/min/src/shaders/fill_outline_pattern.vertex.glsl.txt +9 -0
- package/build/min/src/shaders/fill_pattern.fragment.glsl.txt +13 -0
- package/build/min/src/shaders/fill_pattern.vertex.glsl.txt +9 -0
- package/build/min/src/shaders/heatmap.fragment.glsl.txt +10 -0
- package/build/min/src/shaders/heatmap.vertex.glsl.txt +8 -0
- package/build/min/src/shaders/heatmap_texture.fragment.glsl.txt +5 -0
- package/build/min/src/shaders/heatmap_texture.vertex.glsl.txt +1 -0
- package/build/min/src/shaders/hillshade.fragment.glsl.txt +7 -0
- package/build/min/src/shaders/hillshade.vertex.glsl.txt +1 -0
- package/build/min/src/shaders/hillshade_prepare.fragment.glsl.txt +8 -0
- package/build/min/src/shaders/hillshade_prepare.vertex.glsl.txt +1 -0
- package/build/min/src/shaders/line.fragment.glsl.txt +12 -0
- package/build/min/src/shaders/line.vertex.glsl.txt +17 -0
- package/build/min/src/shaders/line_gradient.fragment.glsl.txt +10 -0
- package/build/min/src/shaders/line_gradient.vertex.glsl.txt +16 -0
- package/build/min/src/shaders/line_pattern.fragment.glsl.txt +15 -0
- package/build/min/src/shaders/line_pattern.vertex.glsl.txt +20 -0
- package/build/min/src/shaders/line_sdf.fragment.glsl.txt +17 -0
- package/build/min/src/shaders/line_sdf.vertex.glsl.txt +20 -0
- package/build/min/src/shaders/raster.fragment.glsl.txt +5 -0
- package/build/min/src/shaders/raster.vertex.glsl.txt +1 -0
- package/build/min/src/shaders/symbol_icon.fragment.glsl.txt +9 -0
- package/build/min/src/shaders/symbol_icon.vertex.glsl.txt +5 -0
- package/build/min/src/shaders/symbol_sdf.fragment.glsl.txt +19 -0
- package/build/min/src/shaders/symbol_sdf.vertex.glsl.txt +13 -0
- package/package.json +44 -0
- package/src/css/mapbox-gl.css +506 -0
- package/src/css/svg/mapboxgl-ctrl-attrib.svg +3 -0
- package/src/css/svg/mapboxgl-ctrl-compass.svg +4 -0
- package/src/css/svg/mapboxgl-ctrl-fullscreen.svg +3 -0
- package/src/css/svg/mapboxgl-ctrl-geolocate-background.svg +3 -0
- package/src/css/svg/mapboxgl-ctrl-geolocate.svg +3 -0
- package/src/css/svg/mapboxgl-ctrl-logo-compact.svg +2 -0
- package/src/css/svg/mapboxgl-ctrl-logo.svg +1 -0
- package/src/css/svg/mapboxgl-ctrl-shrink.svg +3 -0
- package/src/css/svg/mapboxgl-ctrl-zoom-in.svg +3 -0
- package/src/css/svg/mapboxgl-ctrl-zoom-out.svg +3 -0
- package/src/data/array_types.js +1138 -0
- package/src/data/bucket/circle_attributes.js +5 -0
- package/src/data/bucket/circle_bucket.js +118 -0
- package/src/data/bucket/fill_attributes.js +5 -0
- package/src/data/bucket/fill_bucket.js +166 -0
- package/src/data/bucket/fill_extrusion_attributes.js +11 -0
- package/src/data/bucket/fill_extrusion_bucket.js +247 -0
- package/src/data/bucket/heatmap_bucket.js +12 -0
- package/src/data/bucket/line_attributes.js +11 -0
- package/src/data/bucket/line_bucket.js +625 -0
- package/src/data/bucket/pattern_attributes.js +9 -0
- package/src/data/bucket/pattern_bucket_features.js +44 -0
- package/src/data/bucket/symbol_attributes.js +95 -0
- package/src/data/bucket/symbol_bucket.js +697 -0
- package/src/data/bucket.js +53 -0
- package/src/data/dem_data.js +126 -0
- package/src/data/extent.js +17 -0
- package/src/data/feature_index.js +254 -0
- package/src/data/index_array_type.js +14 -0
- package/src/data/load_geometry.js +42 -0
- package/src/data/pos_attributes.js +3 -0
- package/src/data/program_configuration.js +782 -0
- package/src/data/raster_bounds_attributes.js +6 -0
- package/src/data/segment.js +63 -0
- package/src/geo/coordinate.js +78 -0
- package/src/geo/lng_lat.js +129 -0
- package/src/geo/lng_lat_bounds.js +253 -0
- package/src/geo/transform.js +605 -0
- package/src/gl/color_mode.js +21 -0
- package/src/gl/context.js +193 -0
- package/src/gl/cull_face_mode.js +22 -0
- package/src/gl/depth_mode.js +18 -0
- package/src/gl/framebuffer.js +28 -0
- package/src/gl/index_buffer.js +52 -0
- package/src/gl/stencil_mode.js +17 -0
- package/src/gl/types.js +0 -0
- package/src/gl/value.js +676 -0
- package/src/gl/vertex_buffer.js +101 -0
- package/src/index.js +50 -0
- package/src/render/draw_background.js +60 -0
- package/src/render/draw_circle.js +55 -0
- package/src/render/draw_collision_debug.js +45 -0
- package/src/render/draw_debug.js +429 -0
- package/src/render/draw_fill.js +143 -0
- package/src/render/draw_fill_extrusion.js +101 -0
- package/src/render/draw_heatmap.js +159 -0
- package/src/render/draw_hillshade.js +144 -0
- package/src/render/draw_line.js +99 -0
- package/src/render/draw_raster.js +151 -0
- package/src/render/draw_symbol.js +231 -0
- package/src/render/glyph_atlas.js +55 -0
- package/src/render/glyph_manager.js +145 -0
- package/src/render/image_atlas.js +97 -0
- package/src/render/image_manager.js +183 -0
- package/src/render/line_atlas.js +139 -0
- package/src/render/painter.js +483 -0
- package/src/render/program/background_program.js +46 -0
- package/src/render/program/circle_program.js +40 -0
- package/src/render/program/clipping_mask_program.js +11 -0
- package/src/render/program/collision_program.js +28 -0
- package/src/render/program/debug_program.js +13 -0
- package/src/render/program/fill_extrusion_program.js +76 -0
- package/src/render/program/fill_program.js +60 -0
- package/src/render/program/heatmap_program.js +46 -0
- package/src/render/program/hillshade_program.js +77 -0
- package/src/render/program/line_program.js +119 -0
- package/src/render/program/pattern.js +57 -0
- package/src/render/program/program_uniforms.js +46 -0
- package/src/render/program/raster_program.js +50 -0
- package/src/render/program/symbol_program.js +112 -0
- package/src/render/program.js +133 -0
- package/src/render/texture.js +88 -0
- package/src/render/tile_mask.js +108 -0
- package/src/render/uniform_binding.js +129 -0
- package/src/render/vertex_array_object.js +155 -0
- package/src/shaders/README.md +42 -0
- package/src/shaders/_prelude.fragment.glsl +17 -0
- package/src/shaders/_prelude.vertex.glsl +73 -0
- package/src/shaders/background.fragment.glsl +10 -0
- package/src/shaders/background.vertex.glsl +7 -0
- package/src/shaders/background_pattern.fragment.glsl +28 -0
- package/src/shaders/background_pattern.vertex.glsl +20 -0
- package/src/shaders/circle.fragment.glsl +39 -0
- package/src/shaders/circle.vertex.glsl +63 -0
- package/src/shaders/clipping_mask.fragment.glsl +3 -0
- package/src/shaders/clipping_mask.vertex.glsl +7 -0
- package/src/shaders/collision_box.fragment.glsl +21 -0
- package/src/shaders/collision_box.vertex.glsl +26 -0
- package/src/shaders/collision_circle.fragment.glsl +34 -0
- package/src/shaders/collision_circle.vertex.glsl +36 -0
- package/src/shaders/debug.fragment.glsl +5 -0
- package/src/shaders/debug.vertex.glsl +7 -0
- package/src/shaders/encode_attribute.js +19 -0
- package/src/shaders/fill.fragment.glsl +13 -0
- package/src/shaders/fill.vertex.glsl +13 -0
- package/src/shaders/fill_extrusion.fragment.glsl +16 -0
- package/src/shaders/fill_extrusion.vertex.glsl +66 -0
- package/src/shaders/fill_extrusion_pattern.fragment.glsl +41 -0
- package/src/shaders/fill_extrusion_pattern.vertex.glsl +76 -0
- package/src/shaders/fill_outline.fragment.glsl +17 -0
- package/src/shaders/fill_outline.vertex.glsl +17 -0
- package/src/shaders/fill_outline_pattern.fragment.glsl +43 -0
- package/src/shaders/fill_outline_pattern.vertex.glsl +41 -0
- package/src/shaders/fill_pattern.fragment.glsl +36 -0
- package/src/shaders/fill_pattern.vertex.glsl +36 -0
- package/src/shaders/heatmap.fragment.glsl +21 -0
- package/src/shaders/heatmap.vertex.glsl +53 -0
- package/src/shaders/heatmap_texture.fragment.glsl +14 -0
- package/src/shaders/heatmap_texture.vertex.glsl +11 -0
- package/src/shaders/hillshade.fragment.glsl +52 -0
- package/src/shaders/hillshade.vertex.glsl +11 -0
- package/src/shaders/hillshade_prepare.fragment.glsl +72 -0
- package/src/shaders/hillshade_prepare.vertex.glsl +15 -0
- package/src/shaders/index.js +194 -0
- package/src/shaders/line.fragment.glsl +28 -0
- package/src/shaders/line.vertex.glsl +84 -0
- package/src/shaders/line_gradient.fragment.glsl +34 -0
- package/src/shaders/line_gradient.vertex.glsl +84 -0
- package/src/shaders/line_pattern.fragment.glsl +69 -0
- package/src/shaders/line_pattern.vertex.glsl +88 -0
- package/src/shaders/line_sdf.fragment.glsl +44 -0
- package/src/shaders/line_sdf.vertex.glsl +95 -0
- package/src/shaders/raster.fragment.glsl +52 -0
- package/src/shaders/raster.vertex.glsl +21 -0
- package/src/shaders/symbol_icon.fragment.glsl +17 -0
- package/src/shaders/symbol_icon.vertex.glsl +91 -0
- package/src/shaders/symbol_sdf.fragment.glsl +50 -0
- package/src/shaders/symbol_sdf.vertex.glsl +117 -0
- package/src/source/geojson_source.js +267 -0
- package/src/source/geojson_worker_source.js +210 -0
- package/src/source/geojson_wrapper.js +67 -0
- package/src/source/image_source.js +213 -0
- package/src/source/load_tilejson.js +40 -0
- package/src/source/pixels_to_tile_units.js +17 -0
- package/src/source/query_features.js +198 -0
- package/src/source/raster_dem_tile_source.js +140 -0
- package/src/source/raster_dem_tile_worker_source.js +26 -0
- package/src/source/raster_tile_source.js +126 -0
- package/src/source/rtl_text_plugin.js +63 -0
- package/src/source/source.js +75 -0
- package/src/source/source_cache.js +794 -0
- package/src/source/source_state.js +55 -0
- package/src/source/tile.js +332 -0
- package/src/source/tile_bounds.js +40 -0
- package/src/source/tile_cache.js +122 -0
- package/src/source/tile_id.js +150 -0
- package/src/source/vector_tile_source.js +144 -0
- package/src/source/vector_tile_worker_source.js +126 -0
- package/src/source/worker.js +175 -0
- package/src/source/worker_source.js +14 -0
- package/src/source/worker_tile.js +199 -0
- package/src/style/create_style_layer.js +25 -0
- package/src/style/evaluation_parameters.js +45 -0
- package/src/style/light.js +112 -0
- package/src/style/load_glyph_range.js +17 -0
- package/src/style/load_sprite.js +26 -0
- package/src/style/parse_glyph_pbf.js +45 -0
- package/src/style/pauseable_placement.js +88 -0
- package/src/style/properties.js +691 -0
- package/src/style/query_utils.js +39 -0
- package/src/style/style.js +955 -0
- package/src/style/style_layer/background_style_layer.js +11 -0
- package/src/style/style_layer/background_style_layer_properties.js +25 -0
- package/src/style/style_layer/circle_style_layer.js +93 -0
- package/src/style/style_layer/circle_style_layer_properties.js +76 -0
- package/src/style/style_layer/fill_extrusion_style_layer.js +194 -0
- package/src/style/style_layer/fill_extrusion_style_layer_properties.js +56 -0
- package/src/style/style_layer/fill_style_layer.js +46 -0
- package/src/style/style_layer/fill_style_layer_properties.js +45 -0
- package/src/style/style_layer/heatmap_style_layer.js +51 -0
- package/src/style/style_layer/heatmap_style_layer_properties.js +52 -0
- package/src/style/style_layer/hillshade_style_layer.js +15 -0
- package/src/style/style_layer/hillshade_style_layer_properties.js +43 -0
- package/src/style/style_layer/line_style_layer.js +129 -0
- package/src/style/style_layer/line_style_layer_properties.js +104 -0
- package/src/style/style_layer/raster_style_layer.js +11 -0
- package/src/style/style_layer/raster_style_layer_properties.js +55 -0
- package/src/style/style_layer/symbol_style_layer.js +66 -0
- package/src/style/style_layer/symbol_style_layer_properties.js +288 -0
- package/src/style/style_layer.js +183 -0
- package/src/style/style_layer_index.js +61 -0
- package/src/style/zoom_history.js +36 -0
- package/src/style-spec/deref.js +51 -0
- package/src/style-spec/error/parsing_error.js +8 -0
- package/src/style-spec/error/validation_error.js +10 -0
- package/src/style-spec/expression/compound_expression.js +118 -0
- package/src/style-spec/expression/definitions/array.js +82 -0
- package/src/style-spec/expression/definitions/assertion.js +69 -0
- package/src/style-spec/expression/definitions/at.js +57 -0
- package/src/style-spec/expression/definitions/case.js +73 -0
- package/src/style-spec/expression/definitions/coalesce.js +68 -0
- package/src/style-spec/expression/definitions/coercion.js +96 -0
- package/src/style-spec/expression/definitions/collator.js +102 -0
- package/src/style-spec/expression/definitions/equals.js +93 -0
- package/src/style-spec/expression/definitions/index.js +407 -0
- package/src/style-spec/expression/definitions/interpolate.js +235 -0
- package/src/style-spec/expression/definitions/length.js +54 -0
- package/src/style-spec/expression/definitions/let.js +60 -0
- package/src/style-spec/expression/definitions/literal.js +64 -0
- package/src/style-spec/expression/definitions/match.js +142 -0
- package/src/style-spec/expression/definitions/step.js +116 -0
- package/src/style-spec/expression/definitions/var.js +38 -0
- package/src/style-spec/expression/evaluation_context.js +35 -0
- package/src/style-spec/expression/index.js +329 -0
- package/src/style-spec/expression/is_constant.js +63 -0
- package/src/style-spec/expression/parsing_context.js +213 -0
- package/src/style-spec/expression/parsing_error.js +9 -0
- package/src/style-spec/expression/runtime_error.js +12 -0
- package/src/style-spec/expression/scope.js +34 -0
- package/src/style-spec/expression/stops.js +37 -0
- package/src/style-spec/expression/types.js +77 -0
- package/src/style-spec/expression/values.js +126 -0
- package/src/style-spec/feature_filter/README.md +55 -0
- package/src/style-spec/feature_filter/index.js +158 -0
- package/src/style-spec/function/convert.js +256 -0
- package/src/style-spec/function/index.js +299 -0
- package/src/style-spec/group_by_layout.js +68 -0
- package/src/style-spec/reference/v8.json +5356 -0
- package/src/style-spec/util/color.js +73 -0
- package/src/style-spec/util/color_spaces.js +128 -0
- package/src/style-spec/util/eval_support.js +8 -0
- package/src/style-spec/util/get_type.js +18 -0
- package/src/style-spec/util/interpolate.js +21 -0
- package/src/style-spec/util/properties.js +17 -0
- package/src/style-spec/util/ref_properties.js +1 -0
- package/src/style-spec/util/result.js +19 -0
- package/src/symbol/anchor.js +21 -0
- package/src/symbol/check_max_angle.js +75 -0
- package/src/symbol/clip_line.js +73 -0
- package/src/symbol/collision_feature.js +230 -0
- package/src/symbol/collision_index.js +379 -0
- package/src/symbol/cross_tile_symbol_index.js +270 -0
- package/src/symbol/get_anchors.js +177 -0
- package/src/symbol/grid_index.js +318 -0
- package/src/symbol/mergelines.js +75 -0
- package/src/symbol/opacity_state.js +21 -0
- package/src/symbol/placement.js +563 -0
- package/src/symbol/projection.js +601 -0
- package/src/symbol/quads.js +173 -0
- package/src/symbol/shaping.js +347 -0
- package/src/symbol/symbol_layout.js +519 -0
- package/src/symbol/symbol_size.js +110 -0
- package/src/symbol/transform_text.js +16 -0
- package/src/ui/anchor.js +24 -0
- package/src/ui/bind_handlers.js +199 -0
- package/src/ui/camera.js +954 -0
- package/src/ui/events.js +210 -0
- package/src/ui/handler/box_zoom.js +151 -0
- package/src/ui/handler/dblclick_zoom.js +91 -0
- package/src/ui/handler/drag_pan.js +285 -0
- package/src/ui/handler/drag_rotate.js +290 -0
- package/src/ui/handler/frame.js +28 -0
- package/src/ui/handler/inertia.js +45 -0
- package/src/ui/handler/keyboard.js +148 -0
- package/src/ui/handler/scroll_zoom.js +284 -0
- package/src/ui/handler/touch_zoom_rotate.js +263 -0
- package/src/ui/map.js +1645 -0
- package/src/util/actor.js +104 -0
- package/src/util/async.js +23 -0
- package/src/util/browser.js +61 -0
- package/src/util/callback.js +26 -0
- package/src/util/classify_rings.js +43 -0
- package/src/util/color_ramp.js +24 -0
- package/src/util/config.js +24 -0
- package/src/util/dictionary_coder.js +25 -0
- package/src/util/dispatcher.js +68 -0
- package/src/util/dom.js +102 -0
- package/src/util/evented.js +182 -0
- package/src/util/find_pole_of_inaccessibility.js +129 -0
- package/src/util/global_worker_pool.js +15 -0
- package/src/util/image.js +124 -0
- package/src/util/interpolate.js +5 -0
- package/src/util/intersection_tests.js +207 -0
- package/src/util/is_char_in_unicode_block.js +287 -0
- package/src/util/loader/image.js +32 -0
- package/src/util/object.js +178 -0
- package/src/util/script_detection.js +337 -0
- package/src/util/struct_array.js +197 -0
- package/src/util/task_queue.js +57 -0
- package/src/util/throttle.js +26 -0
- package/src/util/tile_cover.js +114 -0
- package/src/util/token.js +13 -0
- package/src/util/unique_id.js +12 -0
- package/src/util/util.js +192 -0
- package/src/util/vectortile_to_geojson.js +44 -0
- package/src/util/verticalize_punctuation.js +112 -0
- package/src/util/warn.js +21 -0
- package/src/util/web_worker.js +5 -0
- package/src/util/web_worker_transfer.js +228 -0
- package/src/util/worker_pool.js +41 -0
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
const { serialize, deserialize } = require('./web_worker_transfer');
|
|
2
|
+
|
|
3
|
+
module.exports = actor;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* An implementation of the [Actor design pattern](http://en.wikipedia.org/wiki/Actor_model)
|
|
7
|
+
* that maintains the relationship between asynchronous tasks and the objects
|
|
8
|
+
* that spin them off - in this case, tasks like parsing parts of styles,
|
|
9
|
+
* owned by the styles
|
|
10
|
+
*
|
|
11
|
+
* @param {WebWorker} target
|
|
12
|
+
* @param {WebWorker} parent
|
|
13
|
+
* @param {string|number} mapId A unique identifier for the Map instance using this Actor.
|
|
14
|
+
* @private
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
function actor(target, parent, mapId, name) {
|
|
18
|
+
const callbacks = {};
|
|
19
|
+
let callbackID = 0;
|
|
20
|
+
target.addEventListener('message', receive, false);
|
|
21
|
+
|
|
22
|
+
function postMessage(targetMapId, id, type, data, err) {
|
|
23
|
+
const buffers = [];
|
|
24
|
+
const payload = {
|
|
25
|
+
targetMapId,
|
|
26
|
+
sourceMapId: mapId,
|
|
27
|
+
type,
|
|
28
|
+
id,
|
|
29
|
+
data: serialize(data, buffers)
|
|
30
|
+
};
|
|
31
|
+
if (err) {
|
|
32
|
+
payload.error = serialize(err);
|
|
33
|
+
}
|
|
34
|
+
target.postMessage(payload, buffers);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Sends a message from a main-thread map to a Worker or from a Worker back to
|
|
39
|
+
* a main-thread map instance.
|
|
40
|
+
*
|
|
41
|
+
* @param type The name of the target method to invoke or '[source-type].[source-name].name' for a method on a WorkerSource.
|
|
42
|
+
* @param targetMapId A particular mapId to which to send this message.
|
|
43
|
+
* @private
|
|
44
|
+
*/
|
|
45
|
+
function send(type, data, callback, targetMapId) {
|
|
46
|
+
let id = 'null';
|
|
47
|
+
if (callback) {
|
|
48
|
+
id = `${mapId}:${callbackID++}`;
|
|
49
|
+
callbacks[id] = callback;
|
|
50
|
+
}
|
|
51
|
+
postMessage(targetMapId, id, type, data);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function receive(message) {
|
|
55
|
+
const { data } = message;
|
|
56
|
+
const { id, type, targetMapId } = data;
|
|
57
|
+
|
|
58
|
+
if (targetMapId && mapId !== targetMapId) return;
|
|
59
|
+
|
|
60
|
+
const done = (err, data) => postMessage(undefined, id, '<response>', data, err);
|
|
61
|
+
|
|
62
|
+
if (type === '<response>') {
|
|
63
|
+
const callback = callbacks[id];
|
|
64
|
+
delete callbacks[id];
|
|
65
|
+
if (callback) {
|
|
66
|
+
if (data.error) {
|
|
67
|
+
callback(deserialize(data.error));
|
|
68
|
+
} else {
|
|
69
|
+
callback(null, deserialize(data.data));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (typeof id !== 'undefined') {
|
|
76
|
+
if (parent[type]) {
|
|
77
|
+
// data.type == 'loadTile', 'removeTile', etc.
|
|
78
|
+
parent[type](data.sourceMapId, deserialize(data.data), done);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
if (parent.getWorkerSource) {
|
|
82
|
+
// data.type == sourcetype.method
|
|
83
|
+
const [sourcetype, method] = type.split('.');
|
|
84
|
+
const params = deserialize(data.data);
|
|
85
|
+
const workerSource = parent.getWorkerSource(data.sourceMapId, sourcetype, params.source);
|
|
86
|
+
workerSource[method](params, done);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
parent[type](deserialize(data.data));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function remove() {
|
|
95
|
+
target.removeEventListener('message', receive, false);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
send,
|
|
100
|
+
receive,
|
|
101
|
+
remove,
|
|
102
|
+
name
|
|
103
|
+
};
|
|
104
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Call an asynchronous function on an array of arguments,
|
|
3
|
+
* calling `callback` with the completed results of all calls.
|
|
4
|
+
*
|
|
5
|
+
* @param array input to each call of the async function.
|
|
6
|
+
* @param fn an async function with signature (data, callback)
|
|
7
|
+
* @param callback a callback run after all async work is done.
|
|
8
|
+
* called with an array, containing the results of each async call.
|
|
9
|
+
* @private
|
|
10
|
+
*/
|
|
11
|
+
exports.all = function (array, fn, callback) {
|
|
12
|
+
if (!array.length) return callback(null, []);
|
|
13
|
+
let remaining = array.length;
|
|
14
|
+
const results = new Array(array.length);
|
|
15
|
+
let error = null;
|
|
16
|
+
array.forEach((item, i) => {
|
|
17
|
+
fn(item, (err, result) => {
|
|
18
|
+
if (err) error = err;
|
|
19
|
+
results[i] = result;
|
|
20
|
+
if (--remaining === 0) callback(error, results);
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// TODO move code that assumes that `window` may not be set to relevant tests
|
|
2
|
+
|
|
3
|
+
const now =
|
|
4
|
+
typeof window === 'object' && window.performance?.now
|
|
5
|
+
? window.performance.now.bind(window.performance)
|
|
6
|
+
: Date.now.bind(Date);
|
|
7
|
+
|
|
8
|
+
const raf =
|
|
9
|
+
typeof window === 'object'
|
|
10
|
+
? window.requestAnimationFrame ||
|
|
11
|
+
window.mozRequestAnimationFrame ||
|
|
12
|
+
window.webkitRequestAnimationFrame ||
|
|
13
|
+
window.msRequestAnimationFrame
|
|
14
|
+
: fn => setTimeout(fn, 0);
|
|
15
|
+
|
|
16
|
+
const cancel =
|
|
17
|
+
typeof window === 'object'
|
|
18
|
+
? window.cancelAnimationFrame ||
|
|
19
|
+
window.mozCancelAnimationFrame ||
|
|
20
|
+
window.webkitCancelAnimationFrame ||
|
|
21
|
+
window.msCancelAnimationFrame
|
|
22
|
+
: id => clearTimeout(id);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @private
|
|
26
|
+
*/
|
|
27
|
+
const exported = {
|
|
28
|
+
/**
|
|
29
|
+
* Provides a function that outputs milliseconds: either performance.now()
|
|
30
|
+
* or a fallback to Date.now()
|
|
31
|
+
*/
|
|
32
|
+
now,
|
|
33
|
+
|
|
34
|
+
frame(fn) {
|
|
35
|
+
return raf(fn);
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
cancelFrame(id) {
|
|
39
|
+
return cancel(id);
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
getImageData(img) {
|
|
43
|
+
const canvas = window.document.createElement('canvas');
|
|
44
|
+
const context = canvas.getContext('2d');
|
|
45
|
+
if (!context) {
|
|
46
|
+
throw new Error('failed to create canvas 2d context');
|
|
47
|
+
}
|
|
48
|
+
canvas.width = img.width;
|
|
49
|
+
canvas.height = img.height;
|
|
50
|
+
context.drawImage(img, 0, 0, img.width, img.height);
|
|
51
|
+
return context.getImageData(0, 0, img.width, img.height);
|
|
52
|
+
},
|
|
53
|
+
|
|
54
|
+
hardwareConcurrency: typeof window === 'object' ? window.navigator.hardwareConcurrency : 4,
|
|
55
|
+
|
|
56
|
+
get devicePixelRatio() {
|
|
57
|
+
return typeof window === 'object' ? window.devicePixelRatio : 1;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
module.exports = exported;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
callback,
|
|
3
|
+
callbackWithSpread,
|
|
4
|
+
callbackWithNoResult
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
function callback(fn, promise) {
|
|
8
|
+
if (fn) {
|
|
9
|
+
promise.then(result => fn(null, result), fn);
|
|
10
|
+
}
|
|
11
|
+
return promise;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function callbackWithSpread(fn, promise) {
|
|
15
|
+
if (fn) {
|
|
16
|
+
promise.then(result => fn(null, ...result), fn);
|
|
17
|
+
}
|
|
18
|
+
return promise;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function callbackWithNoResult(fn, promise) {
|
|
22
|
+
if (fn) {
|
|
23
|
+
promise.then(() => fn(null), fn);
|
|
24
|
+
}
|
|
25
|
+
return promise;
|
|
26
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
const quickselect = require('quickselect');
|
|
2
|
+
|
|
3
|
+
const { calculateSignedArea } = require('./util');
|
|
4
|
+
|
|
5
|
+
// classifies an array of rings into polygons with outer rings and holes
|
|
6
|
+
module.exports = function classifyRings(rings, maxRings) {
|
|
7
|
+
if (rings.length <= 1) return [rings];
|
|
8
|
+
|
|
9
|
+
const polygons = [];
|
|
10
|
+
let polygon;
|
|
11
|
+
let ccw;
|
|
12
|
+
|
|
13
|
+
for (const ring of rings) {
|
|
14
|
+
const area = calculateSignedArea(ring);
|
|
15
|
+
if (area === 0) continue;
|
|
16
|
+
|
|
17
|
+
ring.area = Math.abs(area);
|
|
18
|
+
|
|
19
|
+
if (ccw === undefined) ccw = area < 0;
|
|
20
|
+
|
|
21
|
+
if (ccw === area < 0) {
|
|
22
|
+
append(polygon);
|
|
23
|
+
polygon = [ring];
|
|
24
|
+
} else {
|
|
25
|
+
polygon.push(ring);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
append(polygon);
|
|
29
|
+
return polygons;
|
|
30
|
+
|
|
31
|
+
function append(polygon) {
|
|
32
|
+
if (!polygon) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
// Earcut performance degrades with the # of rings in a polygon. For this
|
|
36
|
+
// reason, we limit strip out all but the `maxRings` largest rings.
|
|
37
|
+
if (maxRings > 1 && maxRings < polygon.length) {
|
|
38
|
+
quickselect(polygon, maxRings, 1, polygon.length - 1, (a, b) => b.area - a.area);
|
|
39
|
+
polygon.length = maxRings;
|
|
40
|
+
}
|
|
41
|
+
polygons.push(polygon);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const { RGBAImage } = require('./image');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Given an expression that should evaluate to a color ramp, return
|
|
5
|
+
* a 256x1 px RGBA image representing that ramp expression.
|
|
6
|
+
*
|
|
7
|
+
* @private
|
|
8
|
+
*/
|
|
9
|
+
module.exports = function renderColorRamp(expression, colorRampEvaluationParameter) {
|
|
10
|
+
const colorRampData = new Uint8Array(256 * 4);
|
|
11
|
+
const evaluationGlobals = {};
|
|
12
|
+
for (let i = 0, j = 0; i < 256; i++, j += 4) {
|
|
13
|
+
evaluationGlobals[colorRampEvaluationParameter] = i / 255;
|
|
14
|
+
const pxColor = expression.evaluate(evaluationGlobals);
|
|
15
|
+
// the colors are being unpremultiplied because Color uses
|
|
16
|
+
// premultiplied values, and the Texture class expects unpremultiplied ones
|
|
17
|
+
colorRampData[j + 0] = Math.floor((pxColor.r * 255) / pxColor.a);
|
|
18
|
+
colorRampData[j + 1] = Math.floor((pxColor.g * 255) / pxColor.a);
|
|
19
|
+
colorRampData[j + 2] = Math.floor((pxColor.b * 255) / pxColor.a);
|
|
20
|
+
colorRampData[j + 3] = Math.floor(pxColor.a * 255);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return new RGBAImage({ width: 256, height: 1 }, colorRampData);
|
|
24
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const { Evented } = require('./evented');
|
|
2
|
+
|
|
3
|
+
function getDefaultWorkerCount() {
|
|
4
|
+
const browser = require('./browser');
|
|
5
|
+
return Math.max(Math.floor(browser.hardwareConcurrency / 2), 1);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const config = new Evented();
|
|
9
|
+
|
|
10
|
+
config.set = function set(c) {
|
|
11
|
+
Object.assign(config, c);
|
|
12
|
+
config.notify();
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
config.notify = function () {
|
|
16
|
+
config.fire('change', config);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
config.set({
|
|
20
|
+
WORKER_COUNT: getDefaultWorkerCount(),
|
|
21
|
+
WORKER_URL: ''
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
module.exports = config;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
|
|
3
|
+
class DictionaryCoder {
|
|
4
|
+
constructor(strings) {
|
|
5
|
+
this._stringToNumber = {};
|
|
6
|
+
this._numberToString = [];
|
|
7
|
+
for (let i = 0; i < strings.length; i++) {
|
|
8
|
+
const string = strings[i];
|
|
9
|
+
this._stringToNumber[string] = i;
|
|
10
|
+
this._numberToString[i] = string;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
encode(string) {
|
|
15
|
+
assert(string in this._stringToNumber);
|
|
16
|
+
return this._stringToNumber[string];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
decode(n) {
|
|
20
|
+
assert(n < this._numberToString.length);
|
|
21
|
+
return this._numberToString[n];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
module.exports = DictionaryCoder;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
const async = require('./async');
|
|
2
|
+
const uniqueId = require('./unique_id');
|
|
3
|
+
const actor = require('./actor');
|
|
4
|
+
|
|
5
|
+
module.exports = dispatcher;
|
|
6
|
+
|
|
7
|
+
const noop = () => {};
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Responsible for sending messages from a {@link Source} to an associated
|
|
11
|
+
* {@link WorkerSource}.
|
|
12
|
+
*
|
|
13
|
+
* @private
|
|
14
|
+
*/
|
|
15
|
+
function dispatcher(workerPool, parent, makeActor = actor) {
|
|
16
|
+
// exposed to allow stubbing in unit tests
|
|
17
|
+
|
|
18
|
+
let currentActor = 0;
|
|
19
|
+
const id = uniqueId();
|
|
20
|
+
const workers = workerPool.acquire(id);
|
|
21
|
+
const actors = workers.map((worker, i) => makeActor(worker, parent, id, `Worker ${i}`));
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Broadcast a message to all Workers.
|
|
25
|
+
*/
|
|
26
|
+
function broadcast(type, data, cb = noop) {
|
|
27
|
+
async.all(actors, (actor, done) => actor.send(type, data, done), cb);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Use round robin to send requests to web workers.
|
|
31
|
+
function nextActorId() {
|
|
32
|
+
currentActor += 1;
|
|
33
|
+
if (currentActor === actors.length) {
|
|
34
|
+
currentActor = 0;
|
|
35
|
+
}
|
|
36
|
+
return currentActor;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Send a message to a Worker.
|
|
41
|
+
* @param targetID The ID of the Worker to which to send this message. Omit to allow the dispatcher to choose.
|
|
42
|
+
* @returns The ID of the worker to which the message was sent.
|
|
43
|
+
*/
|
|
44
|
+
function send(type, data, callback, targetID) {
|
|
45
|
+
if (typeof targetID !== 'number' || isNaN(targetID)) {
|
|
46
|
+
targetID = nextActorId();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const actor = actors[targetID];
|
|
50
|
+
if (actor) {
|
|
51
|
+
actor.send(type, data, callback);
|
|
52
|
+
}
|
|
53
|
+
return targetID;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function remove() {
|
|
57
|
+
actors.forEach(actor => actor.remove());
|
|
58
|
+
actors.length = 0;
|
|
59
|
+
workerPool.release(id);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
id,
|
|
64
|
+
broadcast,
|
|
65
|
+
send,
|
|
66
|
+
remove
|
|
67
|
+
};
|
|
68
|
+
}
|
package/src/util/dom.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
const Point = require('@mapbox/point-geometry');
|
|
2
|
+
|
|
3
|
+
const assert = require('assert');
|
|
4
|
+
|
|
5
|
+
const DOM = {};
|
|
6
|
+
module.exports = DOM;
|
|
7
|
+
|
|
8
|
+
DOM.create = function (tagName, className, container) {
|
|
9
|
+
const el = window.document.createElement(tagName);
|
|
10
|
+
if (className) el.className = className;
|
|
11
|
+
if (container) container.appendChild(el);
|
|
12
|
+
return el;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
DOM.createNS = function (namespaceURI, tagName) {
|
|
16
|
+
const el = window.document.createElementNS(namespaceURI, tagName);
|
|
17
|
+
return el;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
let docStyle;
|
|
21
|
+
let selectProp;
|
|
22
|
+
|
|
23
|
+
DOM.initEnableDisableDrag = function () {
|
|
24
|
+
docStyle = window.document ? window.document.documentElement.style : null;
|
|
25
|
+
|
|
26
|
+
function testProp(props) {
|
|
27
|
+
if (!docStyle) return null;
|
|
28
|
+
for (let i = 0; i < props.length; i++) {
|
|
29
|
+
if (props[i] in docStyle) {
|
|
30
|
+
return props[i];
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return props[0];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
selectProp = testProp(['userSelect', 'MozUserSelect', 'WebkitUserSelect', 'msUserSelect']);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
let userSelect;
|
|
40
|
+
|
|
41
|
+
DOM.disableDrag = function () {
|
|
42
|
+
if (docStyle && selectProp) {
|
|
43
|
+
userSelect = docStyle[selectProp];
|
|
44
|
+
docStyle[selectProp] = 'none';
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
DOM.enableDrag = function () {
|
|
49
|
+
if (docStyle && selectProp) {
|
|
50
|
+
docStyle[selectProp] = userSelect;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// Suppress the next click, but only if it's immediate.
|
|
55
|
+
const suppressClick = function (e) {
|
|
56
|
+
e.preventDefault();
|
|
57
|
+
e.stopPropagation();
|
|
58
|
+
window.removeEventListener('click', suppressClick, true);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
DOM.suppressClick = function () {
|
|
62
|
+
window.addEventListener('click', suppressClick, true);
|
|
63
|
+
window.setTimeout(() => {
|
|
64
|
+
if (typeof window === 'object' && window) {
|
|
65
|
+
window.removeEventListener('click', suppressClick, true);
|
|
66
|
+
}
|
|
67
|
+
}, 0);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
DOM.mousePos = function (el, e) {
|
|
71
|
+
const rect = el.getBoundingClientRect();
|
|
72
|
+
e = e.touches ? e.touches[0] : e;
|
|
73
|
+
return new Point(e.clientX - rect.left - el.clientLeft, e.clientY - rect.top - el.clientTop);
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
DOM.touchPos = function (el, e) {
|
|
77
|
+
const rect = el.getBoundingClientRect();
|
|
78
|
+
const points = [];
|
|
79
|
+
const touches = e.type === 'touchend' ? e.changedTouches : e.touches;
|
|
80
|
+
for (let i = 0; i < touches.length; i++) {
|
|
81
|
+
points.push(
|
|
82
|
+
new Point(touches[i].clientX - rect.left - el.clientLeft, touches[i].clientY - rect.top - el.clientTop)
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
return points;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
DOM.mouseButton = function (e) {
|
|
89
|
+
assert(e.type === 'mousedown' || e.type === 'mouseup');
|
|
90
|
+
if (
|
|
91
|
+
typeof window.InstallTrigger !== 'undefined' &&
|
|
92
|
+
e.button === 2 &&
|
|
93
|
+
e.ctrlKey &&
|
|
94
|
+
window.navigator.platform.toUpperCase().indexOf('MAC') >= 0
|
|
95
|
+
) {
|
|
96
|
+
// Fix for https://github.com/mapbox/mapbox-gl-js/issues/3131:
|
|
97
|
+
// Firefox (detected by InstallTrigger) on Mac determines e.button = 2 when
|
|
98
|
+
// using Control + left click
|
|
99
|
+
return 0;
|
|
100
|
+
}
|
|
101
|
+
return e.button;
|
|
102
|
+
};
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
class Event {
|
|
2
|
+
constructor(type, data) {
|
|
3
|
+
Object.assign(this, data);
|
|
4
|
+
this.type = type;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
class ErrorEvent extends Event {
|
|
9
|
+
constructor(error, data) {
|
|
10
|
+
super('error', Object.assign({ error }, data));
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Methods mixed in to other classes for event capabilities.
|
|
16
|
+
*
|
|
17
|
+
* @mixin Evented
|
|
18
|
+
*/
|
|
19
|
+
class Evented {
|
|
20
|
+
#listeners;
|
|
21
|
+
#oneTimeListeners;
|
|
22
|
+
#parent;
|
|
23
|
+
#parentData;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Adds a listener to a specified event type.
|
|
27
|
+
*
|
|
28
|
+
* @param {string} type The event type to add a listen for.
|
|
29
|
+
* @param {Function} listener The function to be called when the event is fired.
|
|
30
|
+
* The listener function is called with the data object passed to `fire`,
|
|
31
|
+
* extended with `target` and `type` properties.
|
|
32
|
+
* @returns {Object} `this`
|
|
33
|
+
*/
|
|
34
|
+
on(type, listener) {
|
|
35
|
+
this.#listeners ??= listeners();
|
|
36
|
+
this.#listeners.add(type, listener);
|
|
37
|
+
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Removes a previously registered event listener.
|
|
43
|
+
*
|
|
44
|
+
* @param {string} type The event type to remove listeners for.
|
|
45
|
+
* @param {Function} listener The listener function to remove.
|
|
46
|
+
* @returns {Object} `this`
|
|
47
|
+
*/
|
|
48
|
+
off(type, listener) {
|
|
49
|
+
this.#listeners?.remove(type, listener);
|
|
50
|
+
this.#oneTimeListeners?.remove(type, listener);
|
|
51
|
+
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Adds a listener that will be called only once to a specified event type.
|
|
57
|
+
*
|
|
58
|
+
* The listener will be called first time the event fires after the listener is registered.
|
|
59
|
+
*
|
|
60
|
+
* @param {string} type The event type to listen for.
|
|
61
|
+
* @param {Function} listener The function to be called when the event is fired the first time.
|
|
62
|
+
* @returns {Object} `this`
|
|
63
|
+
*/
|
|
64
|
+
once(type, listener) {
|
|
65
|
+
this.#oneTimeListeners ??= listeners({ once: true });
|
|
66
|
+
this.#oneTimeListeners.add(type, listener);
|
|
67
|
+
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
fire(event) {
|
|
72
|
+
// Compatibility with (type: string, properties: Object) signature from previous versions.
|
|
73
|
+
// See https://github.com/mapbox/mapbox-gl-js/issues/6522,
|
|
74
|
+
// https://github.com/mapbox/mapbox-gl-draw/issues/766
|
|
75
|
+
if (typeof event === 'string') {
|
|
76
|
+
event = new Event(event, arguments[1]);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const { type } = event;
|
|
80
|
+
|
|
81
|
+
if (this.listens(type)) {
|
|
82
|
+
event.target = this;
|
|
83
|
+
this.#listeners?.fire(type, this, event);
|
|
84
|
+
this.#oneTimeListeners?.fire(type, this, event);
|
|
85
|
+
|
|
86
|
+
const parent = this.#parent;
|
|
87
|
+
if (parent) {
|
|
88
|
+
const data = typeof this.#parentData === 'function' ? this.#parentData() : this.#parentData;
|
|
89
|
+
Object.assign(event, data);
|
|
90
|
+
parent.fire(event);
|
|
91
|
+
}
|
|
92
|
+
} else if (event instanceof ErrorEvent) {
|
|
93
|
+
// To ensure that no error events are dropped, print them to the
|
|
94
|
+
// console if they have no listeners.
|
|
95
|
+
console.error(event.error);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return this;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Returns a true if this instance of Evented or any forwardeed instances of Evented have a listener for the specified type.
|
|
103
|
+
*
|
|
104
|
+
* @param {string} type The event type
|
|
105
|
+
* @returns {boolean} `true` if there is at least one registered listener for specified event type, `false` otherwise
|
|
106
|
+
*/
|
|
107
|
+
listens(type) {
|
|
108
|
+
return this.#listeners?.listens(type) || this.#oneTimeListeners?.listens(type) || this.#parent?.listens(type);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Bubble all events fired by this instance of Evented to this parent instance of Evented.
|
|
113
|
+
*
|
|
114
|
+
* @returns {Object} `this`
|
|
115
|
+
*/
|
|
116
|
+
setEventedParent(parent, data) {
|
|
117
|
+
this.#parent = parent;
|
|
118
|
+
this.#parentData = data;
|
|
119
|
+
|
|
120
|
+
return this;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
module.exports = {
|
|
125
|
+
Event,
|
|
126
|
+
ErrorEvent,
|
|
127
|
+
Evented
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
function listeners({ once } = {}) {
|
|
131
|
+
const bag = new Map();
|
|
132
|
+
|
|
133
|
+
return {
|
|
134
|
+
add,
|
|
135
|
+
remove,
|
|
136
|
+
fire,
|
|
137
|
+
listens
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
function add(type, listener) {
|
|
141
|
+
const list = bag.get(type);
|
|
142
|
+
if (!list) {
|
|
143
|
+
bag.set(type, [listener]);
|
|
144
|
+
} else if (!list.includes(listener)) {
|
|
145
|
+
list.push(listener);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function remove(type, listener) {
|
|
150
|
+
const list = bag.get(type);
|
|
151
|
+
if (!list) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const index = list.indexOf(listener);
|
|
155
|
+
if (index !== -1) {
|
|
156
|
+
list.splice(index, 1);
|
|
157
|
+
}
|
|
158
|
+
if (list.length === 0) {
|
|
159
|
+
bag.delete(type);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function fire(type, thisArg, data) {
|
|
164
|
+
let list = bag.get(type);
|
|
165
|
+
if (!list || list.length === 0) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
if (once) {
|
|
169
|
+
bag.delete(type);
|
|
170
|
+
} else {
|
|
171
|
+
// make sure adding or removing listeners inside other listeners won't cause an infinite loop
|
|
172
|
+
list = list.slice();
|
|
173
|
+
}
|
|
174
|
+
for (const listener of list) {
|
|
175
|
+
listener.call(thisArg, data);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function listens(type) {
|
|
180
|
+
return bag.get(type)?.length > 0;
|
|
181
|
+
}
|
|
182
|
+
}
|