const invariant = require("invariant"); const React = require("react-native"); const { NativeModules: { GLShadersRegistry }, requireNativeComponent, Component, PropTypes, View, } = React; let _uid = 1; const Shaders = { create: function (obj) { invariant(typeof obj === "object", "config must be an object"); const result = {}; for (let key in obj) { // TODO : validate first const id = _uid ++; GLShadersRegistry.register(id, obj[key]); result[key] = id; } return result; }, exists: function (id) { return typeof id === "number" && id >= 1 && id < _uid; } }; class Target extends Component { render () { invariant( false, "GL.Target elements are for GL.View configuration only and should not be rendered" ); } } Target.displayName = "GL.Target"; Target.propTypes = { children: PropTypes.any.isRequired, uniform: PropTypes.string.isRequired }; const GLViewNative = requireNativeComponent("GLView", GLView); class GLView extends Component { setNativeProps (props) { this.refs.native.setNativeProps(props); } render() { const props = this.props; const { style, width, height, children, shader } = props; invariant(Shaders.exists(shader), "Shader #%s does not exists", shader); const nativeStyle = { width: width, height: height, ...style }; if (children) { const parentStyle = { position: "relative", width: nativeStyle.width, height: nativeStyle.height, overflow: "hidden" }; const childrenStyle = { position: "absolute", top: 0, left: 0, width: nativeStyle.width, height: nativeStyle.height }; const targetUniforms = []; const targets = React.Children.map(children, child => { invariant(child.type === Target, "GL.View can only contains children of type GL.Target. Got '%s'", child.type && child.type.displayName || child); const uniform = child.props.uniform; targetUniforms.push(uniform); return {child.props.children}; }); return {targets} ; } else { return ; } } } GLView.displayName = "GL.View"; GLView.propTypes = { shader: PropTypes.number.isRequired, width: PropTypes.number, height: PropTypes.number, uniforms: PropTypes.object, childrenUniform: PropTypes.string, opaque: PropTypes.bool }; GLView.defaultProps = { opaque: true }; module.exports = { View: GLView, Target, Shaders };