Skip to content

Commit

Permalink
Context refactoring
Browse files Browse the repository at this point in the history
- Removes all await tick()
- Prevents children from being rendered before initializing component leaflet instance, except for components that only have html as children
- fixes Popup content setup, DivIcon should be a direct children of Marker, not Popup
  • Loading branch information
romaindurand committed Nov 5, 2024
1 parent 9f4b086 commit 1324e78
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 26 deletions.
7 changes: 2 additions & 5 deletions src/lib/DivIcon.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { getContext, onDestroy, onMount, tick, type Snippet } from 'svelte';
import { getContext, onDestroy, onMount, type Snippet } from 'svelte';
import type { Marker as LeafletMarker, DivIcon as LeafletDivIcon, DivIconOptions } from 'leaflet';
type Props = { children: Snippet; instance?: LeafletDivIcon; options?: DivIconOptions };
Expand All @@ -9,10 +9,7 @@
const getMarker = getContext<() => LeafletMarker>('marker');
const L = globalThis.window.L;
onMount(async () => {
await tick(); // wait for parent context to be defined (loop tick ?)
await tick();
await tick();
onMount(() => {
const marker = getMarker?.();
if (marker) {
instance = L.divIcon({
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Map.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import type { MapOptions, Marker, Map as LeafletMap, LatLngTuple } from 'leaflet';
import type Leaflet from 'leaflet';
import { type MapEvents, type LocateControlOptions, bindEvents, mapEvents } from './index.js';
import { setContext, tick, type Snippet } from 'svelte';
import { setContext, type Snippet } from 'svelte';
import GeolocationButton from '../components/GeolocationButton.svelte';
import { createLocateOnAdd, updateMapProps } from './map.svelte.js';
Expand Down
8 changes: 3 additions & 5 deletions src/lib/Marker.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { getContext, onDestroy, onMount, setContext, tick, type Snippet } from 'svelte';
import { getContext, onDestroy, onMount, setContext, type Snippet } from 'svelte';
import type {
Map as LeafletMap,
Marker as LeafletMarker,
Expand Down Expand Up @@ -43,9 +43,7 @@
}
});
onMount(async () => {
await tick(); // Attendre que le contexte parent soit défini
await tick();
onMount(() => {
const markerOptions = { ...options };
instance = L.marker(latlng, markerOptions);
bindEvents(instance, restProps, markerEvents);
Expand All @@ -72,6 +70,6 @@
});
</script>

{#if children}
{#if instance && children}
{@render children()}
{/if}
7 changes: 3 additions & 4 deletions src/lib/MarkerClusterGroup.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { getContext, onDestroy, onMount, setContext, tick, type Snippet } from 'svelte';
import { getContext, onDestroy, onMount, setContext, type Snippet } from 'svelte';
import type { MarkerClusterGroup, LayerGroup } from 'leaflet';
const L = globalThis.window.L;
Expand All @@ -16,8 +16,7 @@
setContext('layerGroup', () => instance);
onMount(async () => {
await tick(); // Attendre que le contexte soit défini
onMount(() => {
const map = getMap?.();
const layerGroup = getLayerGroup?.();
const context = layerGroup || map;
Expand All @@ -31,6 +30,6 @@
});
</script>

{#if children}
{#if instance && children}
{@render children()}
{/if}
14 changes: 3 additions & 11 deletions src/lib/Popup.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Map as LeafletMap,
Marker as LeafletMarker,
} from 'leaflet';
import { getContext, onDestroy, onMount, tick, type Snippet } from 'svelte';
import { getContext, onDestroy, onMount, type Snippet } from 'svelte';
import { bindEvents, popupEvents, type PopupEvents } from './index.js';
import { updatePopupProps } from './popup.svelte.js';
import { getFirstNonCommentChild } from './utils.js';
Expand Down Expand Up @@ -35,11 +35,7 @@
const getLayerGroup = getContext<() => LayerGroup>('layerGroup');
const getMarker = getContext<() => LeafletMarker>('marker');
onMount(async () => {
// wait for parent contexts to have been set
await tick();
await tick();
await tick();
onMount(() => {
const map = getMap?.();
const layerGroup = getLayerGroup?.();
const marker = getMarker?.();
Expand All @@ -50,11 +46,7 @@
if (marker) marker.bindPopup(instance);
else if (map) instance.openOn(map);
else instance.addTo(context);
if (popupContent) {
// popupContent can be bound to an empty div if passed a custom icon
const hasActualContent = getFirstNonCommentChild(popupContent);
if (hasActualContent) instance.setContent(popupContent);
}
instance.setContent(popupContent);
});
onDestroy(() => {
Expand Down

0 comments on commit 1324e78

Please sign in to comment.