1
+ class Event {
2
+ constructor ( ) {
3
+ this . stacks = { } ;
4
+ }
5
+
6
+ on ( type , cb ) {
7
+ this . stacks [ type ] = this . stacks [ type ] || [ ] ;
8
+ this . stacks [ type ] . push ( cb ) ;
9
+ }
10
+ emit ( type ) {
11
+ this . stacks [ type ] ?. forEach ( ( cb ) => {
12
+ cb ( ) ;
13
+ } ) ;
14
+ }
15
+ }
16
+ const event = new Event ( ) ;
1
17
class Scene {
2
18
constructor ( ) {
3
19
this . layerService = {
@@ -6,12 +22,16 @@ class Scene {
6
22
startAnimate : ( ) => true
7
23
} ;
8
24
this . callbacks = { } ;
25
+ event . stacks = { } ;
9
26
}
10
27
removeAllLayer ( ) { }
11
28
getLayer ( ) {
12
29
return true ;
13
30
}
14
31
addLayer ( ) {
32
+ setTimeout ( ( ) => {
33
+ event . emit ( 'add' ) ;
34
+ } , 100 ) ;
15
35
return true ;
16
36
}
17
37
removeLayer ( ) {
@@ -49,10 +69,9 @@ class Layer {
49
69
this . layerModel = {
50
70
spriteAnimate : false
51
71
} ;
52
- this . stacks = { } ;
53
72
this . rawConfig = options ;
54
73
this . layerSource = {
55
- originData : { type : 'FeatureCollection' , features : [ ] } ,
74
+ originData : { type : 'FeatureCollection' , features : [ ] } ,
56
75
data : {
57
76
dataArray : [ ]
58
77
}
@@ -69,7 +88,7 @@ class Layer {
69
88
}
70
89
const newFeature = {
71
90
properties : { } ,
72
- geometry : { type : '' , coordinates : [ ] } ,
91
+ geometry : { type : '' , coordinates : [ ] }
73
92
} ;
74
93
const coordinates = item . coordinates ;
75
94
delete item . coordinates ;
@@ -81,12 +100,12 @@ class Layer {
81
100
} ) ;
82
101
return res ;
83
102
}
84
- }
103
+ } ;
85
104
}
86
105
source ( data , options = { } ) {
87
- const parser = options . parser || { type : " geojson" } ;
106
+ const parser = options . parser || { type : ' geojson' } ;
88
107
let dataArray = [ ] ;
89
- if ( parser . type === " geojson" ) {
108
+ if ( parser . type === ' geojson' ) {
90
109
dataArray = data ;
91
110
}
92
111
if ( parser . type === 'json' ) {
@@ -105,18 +124,18 @@ class Layer {
105
124
this . layerSource = {
106
125
...options ,
107
126
parser,
108
- originData : data . features ? data : { type : 'FeatureCollection' , features : data } ,
127
+ originData : data . features ? data : { type : 'FeatureCollection' , features : data } ,
109
128
data : {
110
- dataArray : parser . type === " geojson" ? data : [ ]
129
+ dataArray : parser . type === ' geojson' ? data : [ ]
111
130
}
112
131
} ;
113
- if ( parser . type === " mvt" ) {
132
+ if ( parser . type === ' mvt' ) {
114
133
this . layerSource . tileset = {
115
- cacheTiles : this . rawConfig . name . includes ( " empty" )
134
+ cacheTiles : this . rawConfig . name . includes ( ' empty' )
116
135
? new Map ( )
117
136
: new Map ( [
118
137
[
119
- "0" ,
138
+ '0' ,
120
139
{
121
140
data : {
122
141
vectorLayerCache : {
@@ -155,7 +174,7 @@ class Layer {
155
174
}
156
175
shape ( type ) {
157
176
this . shape = type ;
158
- if ( this . shape === " sprite" ) {
177
+ if ( this . shape === ' sprite' ) {
159
178
this . layerModel = {
160
179
spriteAnimate : true
161
180
} ;
@@ -174,27 +193,20 @@ class Layer {
174
193
return this ;
175
194
}
176
195
getSource ( ) {
177
- const sourceInfo = {
178
- on : ( type , cb ) => {
179
- this . stacks [ type ] = [ cb ] ;
180
- } ,
181
- emit : type => {
182
- this . stacks [ type ] . forEach ( cb => {
183
- cb ( ) ;
184
- } ) ;
185
- }
186
- } ;
196
+ const sourceInfo = event ;
187
197
sourceInfo . _data = this . layerSource . originData ;
188
198
sourceInfo . getData = ( ) => this . layerSource . originData ;
189
199
sourceInfo . setData = this . setData ;
190
200
return sourceInfo ;
191
201
}
192
202
setData ( data ) {
193
203
this . layerSource . originData = data ;
194
- this . getSource ( ) . emit ( " update" ) ;
204
+ this . getSource ( ) . emit ( ' update' ) ;
195
205
}
196
206
197
- on ( ) { }
207
+ on ( type , cb ) {
208
+ event . on ( type , cb ) ;
209
+ }
198
210
199
211
once ( ) { }
200
212
0 commit comments