Line data Source code
1 : import type { ContentEntry, ContentStore } from '#/content'
2 : import { generateId } from '#/id'
3 :
4 2 : export class DefaultContentStore implements ContentStore {
5 : private entries = new Map<string, ContentEntry>()
6 :
7 : register(
8 : canvas: HTMLCanvasElement,
9 : opts?: { id?: string; uploadedUrl?: string }
10 : ): string {
11 4 : const id = opts?.id ?? generateId('content')
12 4 : this.entries.set(id, {
13 : id,
14 : canvas,
15 : width: canvas.width,
16 : height: canvas.height,
17 : uploadedUrl: opts?.uploadedUrl ?? null
18 : })
19 4 : return id
20 : }
21 :
22 : get(id: string): ContentEntry | undefined {
23 55 : return this.entries.get(id)
24 : }
25 :
26 : has(id: string): boolean {
27 0 : return this.entries.has(id)
28 : }
29 :
30 : dirtyIds(): string[] {
31 0 : const out: string[] = []
32 0 : for (const e of this.entries.values())
33 0 : if (e.uploadedUrl === null) out.push(e.id)
34 0 : return out
35 : }
36 :
37 : markUploaded(id: string, url: string): void {
38 0 : const e = this.entries.get(id)
39 0 : if (e) e.uploadedUrl = url
40 : }
41 :
42 : collectGarbage(liveIds: Set<string>): void {
43 13 : for (const id of Array.from(this.entries.keys())) {
44 16 : if (!liveIds.has(id)) this.entries.delete(id)
45 : }
46 : }
47 :
48 : totalBytes(): number {
49 0 : let n = 0
50 0 : for (const e of this.entries.values()) n += e.width * e.height * 4
51 0 : return n
52 : }
53 : }
|