2
2
3
3
import { PureComponent , createElement } from 'react' ;
4
4
import type MapboxMap from 'mapbox-gl/src/ui/map' ;
5
+ import type { ChildrenArray , Element } from 'react' ;
5
6
import type {
6
7
SourceSpecification ,
7
8
VectorSourceSpecification ,
8
9
GeoJSONSourceSpecification
9
10
} from 'mapbox-gl/src/style-spec/types' ;
10
11
11
12
import MapContext from '../MapContext' ;
13
+ import Layer from '../Layer' ;
12
14
import validateSource from '../../utils/validateSource' ;
13
15
14
16
export type Props = {
15
17
/** Mapbox GL Source */
16
18
...SourceSpecification ,
17
19
18
20
/** Mapbox GL Source id */
19
- id : string
21
+ id : string ,
22
+
23
+ /** Layers */
24
+ children ?: ChildrenArray < Element < typeof Layer >>
25
+ } ;
26
+
27
+ type State = {
28
+ loaded : boolean
20
29
} ;
21
30
22
- class Source extends PureComponent < Props > {
31
+ class Source extends PureComponent < Props , State > {
23
32
_map : MapboxMap ;
24
33
25
34
static displayName = 'Source' ;
26
35
36
+ state = {
37
+ loaded : false
38
+ } ;
39
+
27
40
componentDidMount ( ) {
28
- const { id, ...source } = validateSource ( this . props ) ;
41
+ const { id, children , ...source } = validateSource ( this . props ) ;
29
42
this . _map . addSource ( id , source ) ;
43
+ this . _map . on ( 'sourcedata' , this . _onSourceData ) ;
30
44
}
31
45
32
46
componentDidUpdate ( prevProps : Props ) {
33
- const { id : prevId , ...prevSource } = validateSource ( prevProps ) ;
34
- const { id, ...source } = validateSource ( this . props ) ;
47
+ const {
48
+ id : prevId ,
49
+ children : prevChildren ,
50
+ ...prevSource
51
+ } = validateSource ( prevProps ) ;
52
+ const { id, children, ...source } = validateSource ( this . props ) ;
35
53
36
54
if ( id !== prevId || source . type !== prevSource . type ) {
37
55
this . _map . removeSource ( prevId ) ;
@@ -57,6 +75,15 @@ class Source extends PureComponent<Props> {
57
75
this . _removeSource ( ) ;
58
76
}
59
77
78
+ _onSourceData = ( ) => {
79
+ if ( ! this . _map . isSourceLoaded ( this . props . id ) ) {
80
+ return ;
81
+ }
82
+
83
+ this . _map . off ( 'sourcedata' , this . _onSourceData ) ;
84
+ this . setState ( { loaded : true } ) ;
85
+ } ;
86
+
60
87
_updateGeoJSONSource = (
61
88
id : string ,
62
89
prevSource : GeoJSONSourceSpecification ,
@@ -110,12 +137,16 @@ class Source extends PureComponent<Props> {
110
137
} ;
111
138
112
139
render ( ) {
140
+ const { loaded } = this . state ;
141
+ const { children } = this . props ;
142
+
143
+ // $FlowFixMe
113
144
return createElement ( MapContext . Consumer , { } , ( map : ?MapboxMap ) => {
114
145
if ( map ) {
115
146
this . _map = map ;
116
147
}
117
148
118
- return null ;
149
+ return loaded && children ;
119
150
} ) ;
120
151
}
121
152
}
0 commit comments