-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
36 lines (28 loc) · 816 Bytes
/
index.ts
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
export function* sinergia(work: GeneratorFunction) {
let result: any;
let animToken: number;
let _resolve;
try {
const workIterator: Generator = work();
yield new Promise(resolve => {
_resolve = resolve;
const step = () => {
const iteration = workIterator.next();
if (iteration.done) {
resolve();
return;
}
result = iteration.value;
animToken = window.requestAnimationFrame(step);
};
animToken = window.requestAnimationFrame(step);
});
return { value: result };
} finally {
// This block is called when sinergia is interrupted with `.return()`
if (animToken) window.cancelAnimationFrame(animToken);
_resolve();
// Return the latest yielded result
yield { value: result };
}
}