forked from code-matt/react-native-arkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateArComponent.js
More file actions
260 lines (232 loc) · 6.97 KB
/
createArComponent.js
File metadata and controls
260 lines (232 loc) · 6.97 KB
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
import { NativeModules, processColor } from 'react-native';
import PropTypes from 'prop-types';
import React, { PureComponent, Fragment } from 'react';
import filter from 'lodash/filter';
import isDeepEqual from 'fast-deep-equal';
import isEmpty from 'lodash/isEmpty';
import keys from 'lodash/keys';
import pick from 'lodash/pick';
import some from 'lodash/some';
import {
castsShadow,
categoryBitMask,
eulerAngles,
opacity,
orientation,
position,
renderingOrder,
rotation,
scale,
constraint,
transition,
} from './propTypes';
import addAnimatedSupport from './addAnimatedSupport';
import generateId from './generateId';
import processMaterial from './processMaterial';
const DEBUG = false;
const { ARGeosManager } = NativeModules;
const PROP_TYPES_IMMUTABLE = {
id: PropTypes.string,
frame: PropTypes.string,
};
const MOUNT_UNMOUNT_ANIMATION_PROPS = {
propsOnMount: PropTypes.any,
propsOnUnMount: PropTypes.any,
};
const PROP_TYPES_NODE = {
position,
transition,
orientation,
eulerAngles,
rotation,
scale,
categoryBitMask,
castsShadow,
renderingOrder,
opacity,
constraint,
};
const NODE_PROPS = keys(PROP_TYPES_NODE);
const IMMUTABLE_PROPS = keys(PROP_TYPES_IMMUTABLE);
const TIMERS = {};
/**
mountConfig,
propTypes,
nonUpdateablePropKeys: if a prop key is in this list,
the property will be updated on scenekit, instead of beeing remounted.
this excludes at the moment: model, font, text, (???)
* */
export default (mountConfig, propTypes = {}, nonUpdateablePropKeys = []) => {
const allPropTypes = {
...MOUNT_UNMOUNT_ANIMATION_PROPS,
...PROP_TYPES_IMMUTABLE,
...PROP_TYPES_NODE,
...propTypes,
};
// any custom props (material, shape, ...)
const nonNodePropKeys = keys(propTypes);
const parseMaterials = props => ({
...props,
...(props.shadowColor
? { shadowColor: processColor(props.shadowColor) }
: {}),
...(props.color ? { color: processColor(props.color) } : {}),
...(props.material ? { material: processMaterial(props.material) } : {}),
});
const getNonNodeProps = props => parseMaterials(pick(props, nonNodePropKeys));
const mountFunc =
typeof mountConfig === 'string'
? ARGeosManager[mountConfig]
: mountConfig.mount;
const mount = (id, props, parentId) => {
if (DEBUG) console.log(`[${id}] [${new Date().getTime()}] mount`, props);
return mountFunc(
getNonNodeProps(props),
{
id,
...pick(props, NODE_PROPS),
},
props.frame,
parentId,
);
};
const update = (id, props) => {
if (DEBUG) console.log(`[${id}] [${new Date().getTime()}] update`, props);
return ARGeosManager.updateNode(id, props);
};
const unmount = id => {
if (DEBUG) console.log(`[${id}] [${new Date().getTime()}] unmount`);
return ARGeosManager.unmount(id);
};
const ARComponent = class extends PureComponent {
constructor(props) {
super(props);
this.identifier = props.id || generateId();
}
identifier = null;
componentDidMount() {
const { propsOnMount, ...props } = this.props;
if (propsOnMount) {
const fullPropsOnMount = { ...props, ...propsOnMount };
const {
transition: transitionOnMount = { duration: 0 },
} = fullPropsOnMount;
console.log("fullpropsmount", fullPropsOnMount)
this.doPendingTimers();
this.mountWithProps(fullPropsOnMount).then(() => {
this.props = propsOnMount;
this.componentWillUpdate({ ...props, transition: transitionOnMount });
});
} else {
console.log("halfpropsmount", props)
this.mountWithProps(props);
}
}
async mountWithProps(props) {
console.log("parentId", this.context.arkitParentId)
console.log("props", props)
return mount(this.identifier, props, this.context.arkitParentId);
}
componentWillUpdate(props) {
const changedKeys = filter(
keys(this.props),
key => key !== 'children' && !isDeepEqual(props[key], this.props[key]),
);
if (isEmpty(changedKeys)) {
return;
}
if (__DEV__) {
const nonAllowedUpdates = filter(changedKeys, k =>
IMMUTABLE_PROPS.includes(k),
);
if (nonAllowedUpdates.length > 0) {
throw new Error(
`[${this
.identifier}] prop can't be updated: '${nonAllowedUpdates.join(
', ',
)}'`,
);
}
}
if (some(changedKeys, k => nonUpdateablePropKeys.includes(k))) {
if (DEBUG)
console.log(
`[${this.identifier}] need to remount node because of `,
changedKeys,
);
this.mountWithProps({ ...this.props, ...props });
} else {
// every property is updateable
// send only these changed property to the native part
const propsToupdate = {
// always inclue transition
transition: {
...this.props.transition,
...props.transition,
},
...parseMaterials(pick(props, changedKeys)),
};
update(this.identifier, propsToupdate).catch(() => {
// sometimes calls are out of order, so this node has been unmounted
// we therefore mount again
this.mountWithProps({ ...this.props, ...props });
});
}
}
componentWillUnmount() {
const { propsOnUnmount, ...props } = this.props;
if (propsOnUnmount) {
const fullProps = { ...props, ...propsOnUnmount };
const { transition: { duration = 0 } = {} } = fullProps;
this.componentWillUpdate(fullProps);
this.delayed(() => {
unmount(this.identifier);
}, duration * 1000);
} else {
this.doPendingTimers();
unmount(this.identifier);
}
}
/**
do something delayed, but keep order of events per id
* */
delayed(callback, duration) {
this.doPendingTimers();
TIMERS[this.identifier] = {
handle: global.setTimeout(() => {
callback.call(this);
delete TIMERS[this.identifier];
}, duration),
callback,
};
}
doPendingTimers() {
if (TIMERS[this.identifier]) {
// timer is running, do it now, otherwise we might change order
// e.g. it could be that an unmount happens after a remount
global.clearTimeout(TIMERS[this.identifier].handle);
TIMERS[this.identifier].callback.call(this);
delete TIMERS[this.identifier];
}
}
getChildContext() {
return {
arkitParentId: this.identifier,
};
}
render() {
return this.props.children ? (
<Fragment>{this.props.children}</Fragment>
) : null;
}
};
ARComponent.childContextTypes = {
arkitParentId: PropTypes.string,
};
ARComponent.contextTypes = {
arkitParentId: PropTypes.string,
};
const ARComponentAnimated = addAnimatedSupport(ARComponent);
ARComponentAnimated.propTypes = allPropTypes;
return ARComponentAnimated;
};