-
Notifications
You must be signed in to change notification settings - Fork 0
/
split.js
88 lines (75 loc) · 2.56 KB
/
split.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
import React from "react";
import RAudioNode from "./../base/audio-node.js";
import RComponent from "./../base/component.js";
import { isConnectable, propertyFromChildOrParent } from "./utils.js";
/**
* A RComponent which connects its children in parallel, creating inbound branches if necessary.
*
* @class RSplit (name)
*/
export default class RSplit extends RComponent {
constructor(props) {
super(props);
this.inputs = [];
}
componentWillMount() {
super.componentWillMount();
this.context.nodes.set(this.props.identifier, this.inputs);
}
componentWillUpdate(nextProps, nextState) {
// update the node's record in the node registry
if (this.props.identifier !== nextProps.identifier) {
this.context.nodes.delete(this.props.identifier);
this.context.nodes.set(nextProps.identifier, this.inputs);
}
}
render() {
while (this.inputs.length) this.inputs.pop();
const children = React.Children.toArray(this.props.children)
.filter((c) => c !== null && c !== [])
.map((c) => ({
component: c,
identifier: Symbol(c.type.name + Date.now()),
}))
.map((childTuple, childIndex, childrenArray) => {
if (!RComponent.isPrototypeOf(childTuple.component.type))
return childTuple.component;
const type = childTuple.component.type;
if (
RComponent.isPrototypeOf(type) &&
isConnectable(childTuple.component)
) {
this.inputs.push(childTuple.identifier);
}
// this rather strange and terse piece of code
// figures out the channel connections of the RSplit child
// where children can override the parent (RSplit) settings
// this is useful for RSplitChannels
const [connectFromChannel, connectToChannel] = [
"connectFromChannel",
"connectToChannel",
].map(propertyFromChildOrParent(childTuple.component, this));
const splitProps = {
destination: this.props.destination,
identifier: childTuple.identifier,
connectFromChannel,
connectToChannel,
};
return React.cloneElement(childTuple.component, splitProps);
});
if (!this.inputs.length) {
const destination = this.props.destination();
if (destination instanceof Array) this.inputs.push(...destination);
else this.inputs.push(destination);
}
if (this.context.debug) {
return (
<li>
<strong>RSplit</strong>
<ul>{children}</ul>
</li>
);
}
return children;
}
}