This repository has been archived by the owner on Jul 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathModuleDescriptor.js
71 lines (55 loc) · 2.08 KB
/
ModuleDescriptor.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
import ReactPropTypeLocationNames from 'react/lib/ReactPropTypeLocationNames';
import THREEElementDescriptor from './THREEElementDescriptor';
import Module from '../Module';
import PropTypeError from '../utils/PropTypeError';
// Returns class name of the object, if any.
// Used for the subclass proptype checker
function getClassName(propValue) {
if (!propValue.constructor || !propValue.constructor.name) {
return '<<anonymous>>';
}
return propValue.constructor.name;
}
class ModuleDescriptor extends THREEElementDescriptor {
constructor(react3RendererInstance) {
super(react3RendererInstance);
const moduleSubclassValidator = (props, propName, componentName, location, propFullName) => {
const locationName = ReactPropTypeLocationNames[location];
if (!props[propName]) {
return new PropTypeError(
`The ${locationName} \`${propFullName}\` is marked as required in ` +
`\`${componentName}\`, but its value is \`undefined\`.`
);
}
if (!(props[propName].prototype instanceof Module)) {
const actualClassName = getClassName(props[propName]);
return new PropTypeError(`Invalid ${locationName} \`${propFullName}\` ` +
`of type \`${actualClassName}\` supplied to \`${componentName}\`, ` +
'expected subclass of `Module`.');
}
// success returns undefined
return undefined;
};
moduleSubclassValidator.toString = () =>
`${'```'} subclass of ReactThreeRenderer.Module ${'```'} *${'```'} required ${'```'}*`;
this.hasProp('descriptor', {
type: moduleSubclassValidator,
update: this.triggerRemount,
default: undefined,
});
}
construct(props) {
// going insane here but... let's... just do this.
const ModuleClass = props.descriptor;
return new ModuleClass();
}
applyInitialProps(threeObject, props) {
super.applyInitialProps(threeObject, props);
threeObject.setup(this.react3RendererInstance);
}
unmount(threeObject) {
threeObject.dispose();
super.unmount(threeObject);
}
}
module.exports = ModuleDescriptor;