gl-react
Version:
Universal React library, write and compose WebGL shaders, implement complex effects using a descriptive paradigm
167 lines (164 loc) • 5.42 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _invariant = _interopRequireDefault(require("invariant"));
var _react = _interopRequireWildcard(require("react"));
var _Node = _interopRequireDefault(require("./Node"));
var _invariantNoDependentsLoop = _interopRequireDefault(require("./helpers/invariantNoDependentsLoop"));
var _genId = _interopRequireDefault(require("./genId"));
var _GLContext = _interopRequireDefault(require("./GLContext"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/**
* a **Bus is a container to "cache" and re-use content** (tree of Node, canvas, video,...) somewhere else in your GL graph.
*/
class Bus extends _react.Component {
id = (0, _genId.default)();
static contextType = _GLContext.default;
dependents = [];
static defaultProps = {
index: 0
};
componentDidMount() {
const {
uniform,
index
} = this.props;
if (uniform) {
const {
glParent
} = this.context;
(0, _invariant.default)(glParent instanceof _Node.default, 'a <Bus uniform=".." /> needs to be inside a Node');
glParent._addUniformBus(this, uniform, index);
}
this.redraw();
}
componentWillUnmount() {
const {
uniform,
index
} = this.props;
if (uniform) {
const {
glParent
} = this.context;
(0, _invariant.default)(glParent instanceof _Node.default, 'a <Bus uniform=".." /> needs to be inside a Node');
glParent._removeUniformBus(this, uniform, index);
}
}
componentDidUpdate(prevProps) {
const {
uniform: oldUniform,
index: oldIndex
} = prevProps;
const {
uniform,
index
} = this.props;
if (uniform && (uniform !== oldUniform || index !== oldIndex)) {
const {
glParent
} = this.context;
(0, _invariant.default)(glParent instanceof _Node.default, 'a <Bus uniform=".." /> needs to be inside a Node');
if (oldUniform) glParent._removeUniformBus(this, oldUniform, oldIndex);
glParent._addUniformBus(this, uniform, index);
}
this.redraw();
}
glNode = null;
_addGLNodeChild(node) {
this.glNode = node;
this.context.glParent.redraw();
}
_removeGLNodeChild(node) {
this.glNode = null;
}
_addDependent(node) {
const i = this.dependents.indexOf(node);
if (i === -1) {
(0, _invariantNoDependentsLoop.default)(this, node);
this.dependents.push(node);
}
}
_removeDependent(node) {
const i = this.dependents.indexOf(node);
if (i !== -1) this.dependents.splice(i, 1);
}
getGLRenderableNode() {
return this.glNode;
}
getGLRenderableContent() {
const {
mapRenderableContent
} = this.context.glSurface;
const {
glBusRootNode
} = this;
return glBusRootNode && mapRenderableContent ? mapRenderableContent(glBusRootNode) : null;
}
getGLName() {
return `Bus(${this.glNode ? this.glNode.getGLName() : String(this.getGLRenderableContent())})`;
}
getGLShortName() {
const content = this.getGLRenderableContent();
const shortContentName = String(content && content.constructor && content.constructor.name || content);
return `Bus(${this.glNode ? this.glNode.getGLShortName() : shortContentName})`;
}
/**
* Capture the underlying Node pixels.
* NB it only works for nodes, not for content like video/canvas.
*/
capture(x, y, w, h) {
(0, _invariant.default)(this.glNode, "Bus does not contain any Node");
return this.glNode.capture(x, y, w, h);
}
glBusRootNode = null;
onRef = ref => {
this.glBusRootNode = ref;
};
/**
* Schedule a redraw of all nodes that depends on this Bus.
*/
redraw = () => {
this.dependents.forEach(d => d.redraw());
};
_onContextLost() {
const {
glNode
} = this;
if (glNode) glNode._onContextLost();
}
_onContextRestored(gl) {
const {
glNode
} = this;
if (glNode) glNode._onContextRestored(gl);
}
_draw = () => {
// FIXME: _draw() on a Bus? (would a third party need this?)
};
render() {
const {
children
} = this.props;
const {
glSurface: {
RenderLessElement,
mapRenderableContent
}
} = this.context;
return /*#__PURE__*/_react.default.createElement(_GLContext.default.Provider, {
value: {
glParent: this,
glSurface: this.context.glSurface,
glSizable: this.context.glSizable
}
}, /*#__PURE__*/_react.default.createElement(RenderLessElement, {
ref: mapRenderableContent ? this.onRef : undefined
}, typeof children === "function" ? children(this.redraw) : children));
}
}
exports.default = Bus;
//# sourceMappingURL=Bus.js.map