Skip to content

Commit 16be2cf

Browse files
committed
feat: ImgClip supports the tickInterceptor method
1 parent 6dcbcb3 commit 16be2cf

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

.changeset/good-seals-mix.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@webav/av-cliper': minor
3+
---
4+
5+
feat: ImgClip supports the tickInterceptor method

packages/av-cliper/src/clips/__tests__/img-clip.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, test } from 'vitest';
1+
import { expect, test, vi } from 'vitest';
22
import { ImgClip } from '..';
33

44
const animated_gif = `//${location.host}/img/animated.gif`;
@@ -32,3 +32,17 @@ test('clone ImgClip', async () => {
3232
const { state } = await cloneClip.tick(10e6);
3333
expect(state).toBe('success');
3434
});
35+
36+
test('ImgClip tickInterceptor', async () => {
37+
const clip = new ImgClip((await fetch(static_jpg)).body!);
38+
clip.tickInterceptor = vi.fn();
39+
await clip.ready;
40+
await clip.tick(10e6);
41+
expect(clip.tickInterceptor).toBeCalledWith(
42+
10e6,
43+
expect.objectContaining({
44+
state: 'success',
45+
video: expect.any(ImageBitmap),
46+
}),
47+
);
48+
});

packages/av-cliper/src/clips/img-clip.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,25 +119,30 @@ export class ImgClip implements IClip {
119119
Log.info('ImgClip ready:', this.#meta);
120120
}
121121

122+
tickInterceptor: <T extends Awaited<ReturnType<ImgClip['tick']>>>(
123+
time: number,
124+
tickRet: T,
125+
) => Promise<T> = async (_, tickRet) => tickRet;
126+
122127
async tick(time: number): Promise<{
123128
video: ImageBitmap | VideoFrame;
124129
state: 'success';
125130
}> {
126131
if (this.#img != null) {
127-
return {
132+
return await this.tickInterceptor(time, {
128133
video: await createImageBitmap(this.#img),
129134
state: 'success',
130-
};
135+
});
131136
}
132137
const tt = time % this.#meta.duration;
133-
return {
138+
return await this.tickInterceptor(time, {
134139
video: (
135140
this.#frames.find(
136141
(f) => tt >= f.timestamp && tt <= f.timestamp + (f.duration ?? 0),
137142
) ?? this.#frames[0]
138143
).clone(),
139144
state: 'success',
140-
};
145+
});
141146
}
142147

143148
async split(time: number) {

0 commit comments

Comments
 (0)