Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
minor #2406 [Map] Fix and improve TypeScript types, refactor same log…
…ic into dedicated methods (to reduce file size) (Kocal) This PR was squashed before being merged into the 2.x branch. Discussion ---------- [Map] Fix and improve TypeScript types, refactor same logic into dedicated methods (to reduce file size) | Q | A | ------------- | --- | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Issues | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT <!-- Replace this notice by a description of your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - For new features, provide some code snippets to help understand usage. - Features and deprecations must be submitted against branch main. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> This PRs is purely internal, and aims to: - Making TypeScript happy by fixing types definitions and usages, but also simplify them - Refactoring methods containing the same logic (ex: `this.create*` or `this....ValueChanged`) methods) to dedicated methods: ```js createMarker(definition) { this.dispatchEvent('marker:before-create', { definition }); const marker = this.doCreateMarker(definition); this.dispatchEvent('marker:after-create', { marker }); marker['`@id`'] = definition['`@id`']; this.markers.set(definition['`@id`'], marker); return marker; } createPolygon(definition) { this.dispatchEvent('polygon:before-create', { definition }); const polygon = this.doCreatePolygon(definition); this.dispatchEvent('polygon:after-create', { polygon }); polygon['`@id`'] = definition['`@id`']; this.polygons.set(definition['`@id`'], polygon); return polygon; } createPolyline(definition) { this.dispatchEvent('polyline:before-create', { definition }); const polyline = this.doCreatePolyline(definition); this.dispatchEvent('polyline:after-create', { polyline }); polyline['`@id`'] = definition['`@id`']; this.polylines.set(definition['`@id`'], polyline); return polyline; } ``` becomes ```js this.createMarker = this.createDrawingFactory('marker', this.markers, this.doCreateMarker.bind(this)); this.createPolygon = this.createDrawingFactory('polygon', this.polygons, this.doCreatePolygon.bind(this)); this.createPolyline = this.createDrawingFactory('polyline', this.polylines, this.doCreatePolyline.bind(this)); ``` and ```js markersValueChanged() { if (!this.map) { return; } this.markers.forEach((marker) => { if (!this.markersValue.find((m) => m['`@id`'] === marker['`@id`'])) { this.removeMarker(marker); this.markers.delete(marker['`@id`']); } }); this.markersValue.forEach((marker) => { if (!this.markers.has(marker['`@id`'])) { this.createMarker(marker); } }); if (this.fitBoundsToMarkersValue) { this.doFitBoundsToMarkers(); } } polygonsValueChanged() { if (!this.map) { return; } this.polygons.forEach((polygon) => { if (!this.polygonsValue.find((p) => p['`@id`'] === polygon['`@id`'])) { this.removePolygon(polygon); this.polygons.delete(polygon['`@id`']); } }); this.polygonsValue.forEach((polygon) => { if (!this.polygons.has(polygon['`@id`'])) { this.createPolygon(polygon); } }); } polylinesValueChanged() { if (!this.map) { return; } this.polylines.forEach((polyline) => { if (!this.polylinesValue.find((p) => p['`@id`'] === polyline['`@id`'])) { this.removePolyline(polyline); this.polylines.delete(polyline['`@id`']); } }); this.polylinesValue.forEach((polyline) => { if (!this.polylines.has(polyline['`@id`'])) { this.createPolyline(polyline); } }); } ``` becomes ```js markersValueChanged() { if (!this.isConnected) { return; } this.onDrawChanged(this.markers, this.markersValue, this.createMarker, this.doRemoveMarker); if (this.fitBoundsToMarkersValue) { this.doFitBoundsToMarkers(); } } polygonsValueChanged() { if (!this.isConnected) { return; } this.onDrawChanged(this.polygons, this.polygonsValue, this.createPolygon, this.doRemovePolygon); } polylinesValueChanged() { if (!this.isConnected) { return; } this.onDrawChanged(this.polylines, this.polylinesValue, this.createPolyline, this.doRemovePolyline); } ``` Commits ------- 1d33a5f [Map] Create "onDrawChanged" to refactor methods "markersValueChanged"/"polygonsValueChanged"/"polylinesValueChanged" (not identical but follow the same pattern) d9d9ff9 [Map] Create "createDrawingFactory" to refactor methods "createMarker"/"createPolyline"/"createPolygon" (not identical but follow the same pattern) a443ad8 [Map] Re-organize methods order, simplify types, don't dirty-append ``@id`` anymore
- Loading branch information