Revert changes to add source condition (#2526)

* Revert "Fix addSource condition (#2524)"

This reverts commit 19e778bc7c67b3b2cd2c780fc4449a7f9c6fbaf6.

* Revert "Fix source loading condition (#2513)"

This reverts commit 3673af7ec24b3f69378d3447a9ab5c02ccaea951.

* Revert "Fix style loading check on Source layers (#2377) (#2493)"

This reverts commit f9cd7b5aa894267dc8fdb41359c798505ebf215e.
This commit is contained in:
Xiaoji Chen 2025-04-15 17:42:03 -07:00 committed by Xiaoji Chen
parent 078bcbd4aa
commit a6a9d37b7d

View File

@ -1,5 +1,5 @@
import * as React from 'react';
import {useContext, useEffect, useMemo, useCallback, useState, useRef, cloneElement} from 'react';
import {useContext, useEffect, useMemo, useState, useRef, cloneElement} from 'react';
import {MapContext} from './map';
import assert from '../utils/assert';
import {deepEqual} from '../utils/deep-equal';
@ -25,7 +25,8 @@ export type SourceProps = (SourceSpecification | CanvasSourceSpecification) & {
let sourceCounter = 0;
function createSource(map: MapInstance, id: string, props: SourceProps) {
if (map.isStyleLoaded()) {
// @ts-ignore
if (map.style && map.style._loaded) {
const options = {...props};
delete options.id;
delete options.children;
@ -83,12 +84,11 @@ export function Source(props: SourceProps) {
const [, setStyleLoaded] = useState(0);
const id = useMemo(() => props.id || `jsx-source-${sourceCounter++}`, []);
const forceUpdate = useCallback(() => setStyleLoaded(version => version + 1), []);
useEffect(() => {
if (map) {
// Fired on initial load signaling the map is ready to add custom sources
// Subsequently fired on style changes
/* global setTimeout */
const forceUpdate = () => setTimeout(() => setStyleLoaded(version => version + 1), 0);
map.on('styledata', forceUpdate);
forceUpdate();
@ -124,20 +124,6 @@ export function Source(props: SourceProps) {
}
propsRef.current = props;
useEffect(() => {
if (!source) {
// on `styledata` event, `map.isStyleLoaded()` still returns false.
// `load` and `style.load` only fire once and not when `isStyleLoaded` changes from true to false to true.
// `sourcedata` potentially suggests that `isStyleLoaded` has changed. But it fires on every tile load.
// Unsubscribe once source is added.
map.on('sourcedata', forceUpdate);
return () => {
map.off('sourcedata', forceUpdate);
};
}
return undefined;
}, [map, source]);
return (
(source &&
React.Children.map(