-
Notifications
You must be signed in to change notification settings - Fork 663
/
composite.js
63 lines (52 loc) · 1.28 KB
/
composite.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
import Action from './';
import { onFrameUpdate } from 'framesync';
class CompositeAction extends Action {
static defaultProps = {
passive: true
};
constructor(props) {
const { actions, ...remainingProps } = props;
super(remainingProps);
this.current = {};
this.actionKeys = [];
this.addActions(actions);
}
addActions(actions) {
for (const key in actions) {
if (this.actionKeys.indexOf(key) === -1) {
this.actionKeys.push(key);
}
this[key] = actions[key];
const onUpdate = (v) => {
this.current[key] = v;
onFrameUpdate(this.scheduledUpdate);
};
this[key]
.setProps({
_onStop: () => this.numActiveActions--
})
.addListener(onUpdate);
}
}
onStart() {
this.numActiveActions = this.actionKeys.length;
this.actionKeys.forEach((key) => this[key].start());
}
onStop() {
this.actionKeys.forEach((key) => this[key].stop());
}
getVelocity() {
const velocity = {};
this.actionKeys.forEach((key) => velocity[key] = this[key].getVelocity());
return velocity;
}
isActionComplete() {
return (this.numActiveActions === 0);
}
}
export default (actions, props) => {
return new CompositeAction({
actions,
...props
});
};