-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcycle.js
81 lines (68 loc) · 2.22 KB
/
cycle.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
import React from "react";
import RAudioNode from "./../base/audio-node.js";
import RComponent from "./../base/component.js";
import { isConnectable } from "./utils.js";
/**
* A RComponent which connects each child to itself as well as the destination
*
* @class RCycle (name)
*/
export default class RCycle 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 > 0) 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) => {
const type = childTuple.component.type;
if (
RComponent.isPrototypeOf(childTuple.component.type) &&
isConnectable(childTuple.component)
) {
this.inputs.push(childTuple.identifier);
}
const pipelineProps = {
destination: () => {
let destination = this.props.destination();
const ownNode = this.context.nodes.get(childTuple.identifier);
if (!(destination instanceof Array)) destination = [destination];
return destination.concat([ownNode]);
},
identifier: childTuple.identifier,
};
return React.cloneElement(childTuple.component, pipelineProps);
});
if (this.inputs.length === 0) {
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>RCycle</strong>
<ul>{children}</ul>
</li>
);
}
return children;
}
}