Skip to content

Commit

Permalink
fix: types for slotted children
Browse files Browse the repository at this point in the history
  • Loading branch information
bennypowers committed Aug 4, 2020
1 parent ccbc228 commit 0dbde4e
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions leaflet-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ interface FeatureElement extends LeafletBase {
layer: L.LayerGroup | L.Layer;
}

const isFeatureElement = (
function isFeatureElement(
x: Node & Partial<FeatureElement>
): x is Node & FeatureElement => x && 'feature' in x;
): x is FeatureElement {
return x && (x.tagName.startsWith('LEAFLET-') || 'feature' in x);
}

function isSlot(node: ChildNode): node is HTMLSLotElement {
function isSlot(node: ChildNode): node is HTMLSlotElement {
return node instanceof HTMLSlotElement;
}

Expand Down Expand Up @@ -365,13 +367,15 @@ export class LeafletMap extends LeafletBase {
}

private get elements(): ChildNode[] {
return [...this.children].reduce(
(acc, child) => [
...acc,
...(isSlot(child) ? child.assignedElements() : [child]),
],
[]
);
return [...this.children]
.reduce(
(acc, child) => [
...acc,
...(isSlot(child) ? child.assignedElements() : [child]),
],
[]
)
.filter(isFeatureElement);
}

_ignoreViewChange: boolean;
Expand Down Expand Up @@ -400,7 +404,7 @@ export class LeafletMap extends LeafletBase {

async fitToMarkersChanged() {
if (this.map && this.fitToMarkers) {
const elements = this.elements.filter(isFeatureElement);
const { elements } = this;
if (!elements.length) return;
await Promise.race([
new Promise(r => setTimeout(r, 100)),
Expand Down

0 comments on commit 0dbde4e

Please sign in to comment.