Skip to content

Commit e9bf46e

Browse files
committed
Fix RAF re-entry
1 parent 2b37d67 commit e9bf46e

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

core/src/TeaCup/Animation.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,51 @@ import { Sub } from './Sub';
2727

2828
let subs: Array<RafSub<any>> = [];
2929

30+
let ticking = false;
31+
3032
function tick() {
31-
if (subs.length > 0) {
33+
console.log("tick()");
34+
if (!ticking) {
35+
ticking = true;
3236
requestAnimationFrame((t: number) => {
37+
console.log("got RAF", t, "subs", subs.length);
3338
subs.forEach((s) => s.trigger(t));
39+
ticking = false;
3440
});
3541
}
3642
}
3743

44+
let rafSubId = 0;
45+
3846
class RafSub<M> extends Sub<M> {
3947
readonly mapper: (t: number) => M;
4048

49+
private readonly uuid: string;
50+
4151
constructor(mapper: (t: number) => M) {
4252
super();
4353
this.mapper = mapper;
54+
this.uuid = "rafsub-" + rafSubId;
55+
rafSubId++;
4456
}
4557

4658
protected onInit() {
59+
console.log("RafSub", this.uuid ,"onInit()");
4760
super.onInit();
4861
subs.push(this);
62+
console.log("RafSub", this.uuid ,"onInit()", subs.length);
4963
tick();
5064
}
5165

5266
protected onRelease() {
67+
console.log("RafSub", this.uuid ,"onRelease()");
5368
super.onRelease();
5469
subs = subs.filter((s) => s !== this);
70+
console.log("RafSub", this.uuid ,"onRelease()", subs.length);
5571
}
5672

5773
trigger(t: number) {
74+
console.log("RafSub", this.uuid ,"trigger(", t, ")");
5875
this.dispatch(this.mapper(t));
5976
}
6077
}

0 commit comments

Comments
 (0)