-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
worker.js
114 lines (95 loc) · 2.99 KB
/
worker.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
/* global fetch */
import TEST_CASES from './test-cases';
import {LayerManager} from '@deck.gl/core';
import * as Layers from '@deck.gl/layers';
export default self => {
self.onmessage = evt => {
const {id} = evt.data;
const testCase = TEST_CASES[id];
fetchJSON(testCase.data).then(data => {
// eslint-disable-next-line import/namespace
const LayerType = Layers[id];
const {props, transferList} = getLayerSnapshot(new LayerType({...testCase, data}));
self.postMessage(props, transferList);
});
};
};
function fetchJSON(url) {
return fetch(url).then(resp => resp.json());
}
function getLayerSnapshot(layer) {
const layerManager = new LayerManager();
layerManager.setProps({layers: [layer]});
layerManager.updateLayers();
const props = {};
let transferList = [];
layerManager.layers.forEach(l => {
const ids = [];
let parentLayer = l.parent;
let parentProps = props;
while (parentLayer) {
ids.push(getSublayerId(parentLayer));
parentLayer = parentLayer.parent;
}
while (ids.length) {
parentProps = parentProps[ids.pop()]._subLayerProps;
}
if (l.isComposite) {
parentProps[getSublayerId(l)] = getCompositeLayerSnapshot(l).props;
} else {
const snapshot = getPrimitiveLayerSnapshot(l);
parentProps[getSublayerId(l)] = snapshot.props;
transferList = transferList.concat(snapshot.transferList);
}
});
// Release resources
layerManager.setProps({layers: []});
layerManager.updateLayers();
layerManager.finalize();
return {props: props[layer.id], transferList};
}
function getSublayerId(layer) {
const id = layer.id;
if (layer.parent) {
const parentId = layer.parent && layer.parent.id;
return id.slice(parentId.length + 1);
}
return id;
}
function getCompositeLayerSnapshot(layer) {
return {
props: {
id: layer.id,
type: layer.constructor.name,
_subLayerProps: {}
}
};
}
// Props used for attribute generation, can be safely discarded
const propBlackList = new Set(['data', 'updateTriggers']);
function getPrimitiveLayerSnapshot(layer) {
// Extract generated attributes - should move to AttributeManager?
const props = {};
const transferList = [];
const attributes = layer.getAttributeManager().getAttributes();
for (const attributeName in attributes) {
const attribute = attributes[attributeName];
if (!attribute.constant && ArrayBuffer.isView(attribute.value)) {
props[attributeName] = attribute.value;
transferList.push(attribute.value.buffer);
}
}
for (const propName in layer.props) {
if (
Object.hasOwnProperty.call(layer.props, propName) &&
!propBlackList.has(propName) &&
(!layer.parent || layer.props[propName] !== layer.parent.props[propName]) &&
typeof layer.props[propName] !== 'function'
) {
props[propName] = layer.props[propName];
}
}
props.type = layer.constructor.name;
props.numInstances = layer.getNumInstances();
return {props, transferList};
}