summaryrefslogtreecommitdiff
path: root/viewer/loader.ts
blob: 487102fc9d0c610026e780059f88e35b8c13d75d (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { Resource, RespackEntry } from "./resources.ts";

export abstract class Loader {
    num_loading: number = 0
    abstract download(res: Resource<undefined>): Promise<Uint8Array>
    abstract get_entry(): Promise<Resource<RespackEntry>>;
    abstract wait_ready(): Promise<void>
}

export class HTTPLoader extends Loader {
    constructor() {
        super();
    }
    // deno-lint-ignore require-await
    async wait_ready(): Promise<void> {
        return
    }
    async download<T>(res: Resource<T>): Promise<Uint8Array> {
        this.num_loading += 1
        const resp = await fetch(`http://127.0.0.1:28556/${res.toString()}`)
        if (!resp.ok) throw new Error("aaaa");
        const buf = await resp.bytes()
        this.num_loading -= 1
        return buf
    }
    async get_entry(): Promise<Resource<RespackEntry>> {
        const resp = await fetch("http://127.0.0.1:28556/entry")
        if (!resp.ok) throw new Error("aaaa");
        return new Resource(await resp.bytes())
    }
}
export class WSLoader extends Loader {
    ws: WebSocket
    queue: ((r: Uint8Array) => void)[] = []
    constructor() {
        super()
        this.ws = new WebSocket(`http://127.0.0.1:28557/`)
        this.ws.onopen = () => console.log("open");
        this.ws.onclose = () => console.log("close");
        this.ws.onmessage = m => {
            if (typeof m.data == "string") throw new Error(m.data);
            if (m.data instanceof Blob) {
                const h = this.queue.shift()!
                m.data.arrayBuffer()
                    .then(b => new Uint8Array(b))
                    .then(h)
                    .then(() => this.num_loading -= 1)
            }
        }
    }
    wait_ready(): Promise<void> {
        return new Promise(resolve => {
            if (this.ws.readyState == this.ws.OPEN) return resolve()
            else this.ws.addEventListener("open", () => resolve())
        })
    }
    download<T>(res: Resource<T>): Promise<Uint8Array> {
        return new Promise(resolve => {
            this.num_loading += 1
            this.queue.push(resolve)
            this.ws.send(res.hash)
        })
    }
    get_entry(): Promise<Resource<RespackEntry>> {
        return new Promise(resolve => {
            this.num_loading += 1
            this.queue.push(buf => resolve(new Resource(buf)))
            this.ws.send("entry")
        })
    }
}