-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrender.js
490 lines (435 loc) · 11.4 KB
/
render.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
import OLMap from 'ol/Map.js';
import {unByKey} from 'ol/Observable.js';
import Overlay from 'ol/Overlay.js';
import View from 'ol/View.js';
import Control from 'ol/control/Control.js';
import Interaction from 'ol/interaction/Interaction.js';
import BaseLayer from 'ol/layer/Base.js';
import GroupLayer from 'ol/layer/Group.js';
import Layer from 'ol/layer/Layer.js';
import Source from 'ol/source/Source.js';
import Vector from 'ol/source/Vector.js';
import ReactReconciler from 'react-reconciler';
import {
ConcurrentRoot,
DefaultEventPriority,
//@ts-ignore (TODO: remove after https://github.com/DefinitelyTyped/DefinitelyTyped/pull/72046 is released)
NoEventPriority,
} from 'react-reconciler/constants.js';
import {CONTROL, INTERACTION, LAYER, OVERLAY, SOURCE, VIEW} from './config.js';
import {
arrayEquals,
prepareControlUpdate,
prepareInteractionUpdate,
prepareLayerUpdate,
prepareOverlayUpdate,
prepareSourceUpdate,
prepareViewUpdate,
reservedProps,
} from './update.js';
const listenerRegex = /^on([A-Z].*)/;
const listenerColonRegex = /^onChange-/;
/**
* @param {string} str A string.
*/
function upperFirst(str) {
return str[0].toUpperCase() + str.slice(1);
}
/**
* @param {string} str A string.
*/
function setterName(str) {
return 'set' + upperFirst(str);
}
/**
* @type {Object<string, boolean>}
*/
const knownTypes = {
[VIEW]: true,
[OVERLAY]: true,
[CONTROL]: true,
[INTERACTION]: true,
[LAYER]: true,
[SOURCE]: true,
};
const customViewChangeEventType = 'custom-change';
/**
* @type {import('./update.js').Updater}
*/
export function updateInstanceFromProps(instance, type, oldProps, newProps) {
for (const key in newProps) {
if (reservedProps[key]) {
continue;
}
const newValue = newProps[key];
const oldValue = oldProps[key];
if (oldValue === newValue) {
continue;
}
if (listenerRegex.test(key)) {
const listener = newProps[key];
let eventType = key
.replace(listenerColonRegex, 'onChange:')
.replace(listenerRegex, '$1')
.toLowerCase();
// special handling for view change
if (instance instanceof View && eventType === 'change') {
eventType = customViewChangeEventType;
}
instance.on(eventType, listener);
const oldListener = oldProps[key];
if (oldListener) {
instance.un(eventType, oldListener);
if (instance.changed) {
instance.changed();
}
}
continue;
}
if (key === 'center' && arrayEquals(newValue, oldValue)) {
continue;
}
const setter = setterName(key);
if (typeof instance[setter] === 'function') {
instance[setter](newValue);
continue;
}
if (instance instanceof Vector) {
if (key === 'features') {
// TODO: there is likely a smarter way to diff features
instance.clear(true);
instance.addFeatures(newValue);
continue;
}
}
if (instance instanceof OLMap) {
if (key === 'interactions') {
const interactions = /** @type {Array<Interaction>} */ (newValue);
instance.getInteractions().clear();
interactions.forEach(interaction =>
instance.addInteraction(interaction),
);
continue;
}
if (key === 'controls') {
const controls = /** @type {Array<Control>} */ (newValue);
instance.getControls().clear();
controls.forEach(control => instance.addControl(control));
continue;
}
}
throw new Error(`Cannot update '${key}' property`);
}
}
/**
* @typedef {Object} InstanceProps
* @property {new(options: any) => any} cls A class.
* @property {any} [options] The options.
*/
/**
* @param {string} type The string type.
* @param {InstanceProps} The instance props.
*/
function createInstance(type, {cls: Constructor, ...props}) {
if (!knownTypes[type]) {
throw new Error(`Unsupported element type: ${type}`);
}
if (!Constructor) {
throw new Error(`No constructor for type: ${type}`);
}
const instance = new Constructor(props.options || {});
updateInstanceFromProps(instance, type, {}, props);
return instance;
}
function createTextInstance() {
throw new Error('Cannot add text to the map');
}
/**
* @param {OLMap} map The map.
* @param {any} child The child.
*/
function appendChildToContainer(map, child) {
if (child instanceof View) {
const key = map.on('moveend', () => {
child.dispatchEvent(customViewChangeEventType);
});
map.setView(child);
map.on('change:view', () => {
unByKey(key);
});
return;
}
if (child instanceof Overlay) {
map.addOverlay(child);
return;
}
if (child instanceof Control) {
map.addControl(child);
return;
}
if (child instanceof Interaction) {
map.addInteraction(child);
return;
}
if (child instanceof BaseLayer) {
map.addLayer(child);
return;
}
throw new Error(`Cannot add child to the map: ${child}`);
}
/**
* @param {any} parent The parent.
* @param {any} child The child.
*/
function appendChild(parent, child) {
if (child instanceof Source) {
if (!(parent instanceof Layer)) {
throw new Error(`Cannot add source to ${parent}`);
}
parent.setSource(child);
return;
}
// Layer groups are an instance of a layer
if (child instanceof BaseLayer && parent instanceof GroupLayer) {
parent.getLayers().push(child);
return;
}
throw new Error(`Cannot add ${child} to ${parent}`);
}
/**
* @type {Object<string, import('./update.js').Updater>}
*/
const updaters = {
[VIEW]: prepareViewUpdate,
[OVERLAY]: prepareOverlayUpdate,
[CONTROL]: prepareControlUpdate,
[INTERACTION]: prepareInteractionUpdate,
[LAYER]: prepareLayerUpdate,
[SOURCE]: prepareSourceUpdate,
};
/**
* @param {any} instance The instance to update.
* @param {string} type The string type.
* @param {Object<string, any>} oldProps The old props.
* @param {Object<string, any>} newProps The new props.
*/
function prepareUpdate(instance, type, oldProps, newProps) {
const updater = updaters[type];
if (!updater) {
throw new Error(`Unsupported element type: ${type}`);
}
return updater(instance, type, oldProps, newProps);
}
/**
* @type {import('./update.js').Updater}
*/
function commitUpdate(instance, type, oldProps, newProps) {
updateInstanceFromProps(instance, type, oldProps, newProps);
}
/**
* @param {OLMap} map The map.
* @param {any} child The object to remove from the map.
*/
function removeChildFromContainer(map, child) {
if (child instanceof View) {
// @ts-ignore (remove when https://github.com/openlayers/openlayers/pull/16691 is released)
map.setView(null);
return;
}
if (child instanceof Overlay) {
map.removeOverlay(child);
return;
}
if (child instanceof Control) {
map.removeControl(child);
return;
}
if (child instanceof Interaction) {
map.removeInteraction(child);
return;
}
if (child instanceof BaseLayer) {
map.removeLayer(child);
return;
}
throw new Error(`Cannot remove child from the map: ${child}`);
}
/**
* @param {any} parent The parent object.
* @param {any} child The child object.
*/
function removeChild(parent, child) {
// this happens with group layers
if (child instanceof BaseLayer && parent.getLayers) {
parent.getLayers().remove(child);
return;
}
throw new Error(`TODO: implement removeChild for ${parent} and ${child}`);
}
/**
* @param {OLMap} map The map.
*/
function clearContainer(map) {
map.getLayers().clear();
// Need to leave the default controls and interactions.
// TODO: determine when this gets called.
}
/**
* @template {any} T
* @param {import("ol/Collection.js").default<T>} collection A collection.
* @param {T} child The child to insert.
* @param {T} beforeChild The insertion point.
*/
function insertInCollection(collection, child, beforeChild) {
const index = collection.getArray().indexOf(beforeChild);
if (index < 0) {
collection.push(child);
} else {
collection.insertAt(index, child);
}
}
/**
* @param {OLMap} map The map.
* @param {any} child The child to insert.
* @param {any} beforeChild The insertion point.
*/
function insertInContainerBefore(map, child, beforeChild) {
if (child instanceof View) {
const key = map.on('moveend', () => {
child.dispatchEvent(customViewChangeEventType);
});
map.setView(child);
map.on('change:view', () => {
unByKey(key);
});
return;
}
let collection;
if (child instanceof Overlay) {
collection = map.getOverlays();
} else if (child instanceof Control) {
collection = map.getControls();
} else if (child instanceof Interaction) {
collection = map.getInteractions();
} else if (child instanceof BaseLayer) {
collection = map.getLayers();
}
if (collection) {
insertInCollection(collection, child, beforeChild);
}
}
/**
* @param {any} parent The parent.
* @param {any} child The child to insert.
* @param {any} beforeChild The insertion point.
*/
function insertBefore(parent, child, beforeChild) {
if (child instanceof BaseLayer && parent instanceof GroupLayer) {
insertInCollection(parent.getLayers(), child, beforeChild);
return;
}
throw new Error(`Cannot insert child ${child} into parent ${parent}`);
}
const noContext = {};
let currentUpdatePriority = NoEventPriority;
const reconciler = ReactReconciler({
supportsMutation: true,
isPrimaryRenderer: false,
createInstance,
createTextInstance,
appendChildToContainer,
appendChild,
appendInitialChild: appendChild,
prepareUpdate,
commitUpdate,
clearContainer,
removeChildFromContainer,
removeChild,
insertInContainerBefore,
insertBefore,
noTimeout: -1,
getInstanceFromNode() {
return null;
},
//@ts-ignore
shouldAttemptEagerTransition() {
return false;
},
requestPostPaintCallback() {},
maySuspendCommit() {
return false;
},
preloadInstance() {
return true;
},
startSuspendingCommit() {},
suspendInstance() {},
waitForCommitToBeReady() {
return null;
},
/**
* @param {number} newPriority The new priority.
*/
setCurrentUpdatePriority(newPriority) {
currentUpdatePriority = newPriority;
},
getCurrentUpdatePriority() {
return currentUpdatePriority;
},
resolveUpdatePriority() {
if (currentUpdatePriority) {
return currentUpdatePriority;
}
return DefaultEventPriority;
},
finalizeInitialChildren() {
return false;
},
getChildHostContext() {
return noContext;
},
getPublicInstance(instance) {
return instance;
},
getRootHostContext() {
return noContext;
},
getCurrentEventPriority() {
return DefaultEventPriority;
},
prepareForCommit() {
return null;
},
resetAfterCommit() {},
shouldSetTextContent() {
return false;
},
detachDeletedInstance() {},
});
/**
* @type {Map<any, ReactReconciler.OpaqueRoot>}
*/
const roots = new Map();
/**
* @param {React.ReactNode} element The element to render.
* @param {any} container The container.
*/
export function render(element, container) {
let root = roots.get(container);
if (!root) {
const logRecoverableError =
typeof reportError === 'function' ? reportError : console.error; // eslint-disable-line no-console
root = reconciler.createContainer(
container,
ConcurrentRoot,
null,
false,
null,
'',
logRecoverableError,
null,
);
roots.set(container, root);
}
reconciler.updateContainer(element, root, null, null);
}