Skip to content

Commit ce8bf76

Browse files
authored
Merge pull request #67 from thefrontside/tm/add-raf
Added Request Animation Frame package
2 parents 09c0fd3 + fe877b2 commit ce8bf76

File tree

6 files changed

+84
-2
lines changed

6 files changed

+84
-2
lines changed

deno.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"imports": {
3-
"bdd": "jsr:@std/testing/bdd",
4-
"expect": "jsr:@std/expect"
3+
"bdd": "jsr:@std/testing@1/bdd",
4+
"expect": "jsr:@std/expect@1"
55
},
66
"compilerOptions": {
77
"lib": [
@@ -24,6 +24,7 @@
2424
"./deno-deploy",
2525
"./jsonl-store",
2626
"./fx",
27+
"./raf",
2728
"./task-buffer",
2829
"./test-adapter",
2930
"./tinyexec",

raf/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# RAF: Request Animation Frame
2+
3+
Subscribe to a stream of Request Animation Frame updates with an Effection
4+
resource.
5+
6+
---
7+
8+
```ts
9+
import { each, main, suspend } from "effection";
10+
import { raf } from "@effectionx/raf";
11+
12+
await main(function* () {
13+
for (const frame of yield* each(raf)) {
14+
// add your handler code here
15+
console.log(frame);
16+
yield* each.next();
17+
}
18+
});
19+
```

raf/deno.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "@effectionx/raf",
3+
"version": "1.0.0-alpha.0",
4+
"exports": "./mod.ts",
5+
"license": "MIT",
6+
"imports": {
7+
"effection": "npm:effection@^3"
8+
}
9+
}

raf/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./raf.ts";

raf/raf.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {
2+
caf as cancelAnimationFrame,
3+
raf as requestAnimationFrame,
4+
} from "npm:@essentials/raf@^1.2.0";
5+
6+
import { each, run, sleep, spawn } from "effection";
7+
import { describe, it } from "bdd";
8+
import { expect } from "expect";
9+
import { raf } from "./raf.ts";
10+
11+
Object.assign(globalThis, {
12+
requestAnimationFrame,
13+
cancelAnimationFrame,
14+
});
15+
16+
describe("raf", () => {
17+
it("subscription", async () => {
18+
expect.assertions(1);
19+
let count = 0;
20+
await run(function* () {
21+
yield* spawn(function* () {
22+
for (const _ of yield* each(raf)) {
23+
count++;
24+
yield* each.next();
25+
}
26+
});
27+
yield* sleep(100);
28+
});
29+
30+
expect(count > 5).toBe(true);
31+
});
32+
});

raf/raf.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { createSignal, resource, type Stream } from "effection";
2+
/**
3+
* Consume RAF's as a Stream.
4+
*/
5+
export const raf: Stream<number, never> = resource(
6+
function* (provide) {
7+
let signal = createSignal<number, never>();
8+
let id = 0;
9+
let callback: FrameRequestCallback = (timestamp) => {
10+
signal.send(timestamp);
11+
id = requestAnimationFrame(callback);
12+
};
13+
id = requestAnimationFrame(callback);
14+
try {
15+
yield* provide(yield* signal);
16+
} finally {
17+
cancelAnimationFrame(id);
18+
}
19+
},
20+
);

0 commit comments

Comments
 (0)