• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

andreashuber69 / kiss-worker / #117

02 Jun 2024 07:24AM UTC coverage: 99.315%. Remained the same
#117

push

andreashuber69
docs: minor changes

32 of 32 branches covered (100.0%)

Branch coverage included in aggregate %.

113 of 114 relevant lines covered (99.12%)

26.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/ObjectWorkerImpl.ts
1
// https://github.com/andreashuber69/kiss-worker/blob/develop/README.md
2
import type { DedicatedWorker } from "./DedicatedWorker.ts";
3
import { FunctionInfo } from "./FunctionInfo.ts";
4
import type { FunctionWorker } from "./FunctionWorker.ts";
5
import { implementFunctionWorkerExternal } from "./implementFunctionWorkerExternal.ts";
6
import type { MethodsOnlyObject } from "./MethodsOnlyObject.ts";
7
import type { Proxy } from "./Proxy.ts";
8
import type { CallSignature, WorkerSignature } from "./Signature.ts";
9

10
class ProxyImpl<C extends new (..._: never[]) => T, T extends MethodsOnlyObject<T>> {
11
    [key: string]: Proxy<T>[keyof Proxy<T>];
12

13
    public constructor(
14
        worker: FunctionWorker<WorkerSignature<C, T>>,
15
        propertyNames: ReadonlyArray<Extract<keyof T, string>>,
16
    ) {
17
        this.#worker = worker;
6✔
18

19
        for (const key of propertyNames) {
6✔
20
            // The cast-fest below is necessary because there seems to be no way to transform the type of args to the
21
            // parameter types of #worker.execute. The same goes for the type of the function.
22
            this[key as string] = (async (...args: Parameters<Proxy<T>[keyof Proxy<T>]>) =>
10✔
23
                await this.#worker.execute(...(["call", key, ...args] as Parameters<CallSignature<T>>))
10✔
24
            ) as Proxy<T>[keyof Proxy<T>];
25
        }
26
    }
27

28
    readonly #worker: FunctionWorker<WorkerSignature<C, T>>;
29
}
30

31
export class ObjectWorkerImpl<C extends new (..._: never[]) => T, T extends MethodsOnlyObject<T>> {
32
    public static async create<C extends new (..._: never[]) => T, T extends MethodsOnlyObject<T>>(
33
        createWorker: () => DedicatedWorker,
34
        ...args: ConstructorParameters<C>
35
    ) {
36
        const result = new ObjectWorkerImpl<C, T>(createWorker);
10✔
37

38
        try {
10✔
39
            await result.construct(...args);
10✔
40
        } catch (error: unknown) {
41
            result.terminate();
4✔
42
            throw error;
4✔
43
        }
44

45
        return result;
6✔
46
    }
47

48
    public get obj() {
49
        return this.#obj;
10✔
50
    }
51

52
    public terminate() {
53
        this.#worker.terminate();
6✔
54
    }
55

56

57
    private constructor(createWorker: () => DedicatedWorker) {
58
        const createFunctionWorker =
59
            implementFunctionWorkerExternal(createWorker, new FunctionInfo<WorkerSignature<C, T>>());
10✔
60

61
        this.#worker = createFunctionWorker();
10✔
62
        // Admittedly, this isn't pretty, but seems to be the only way how we can convince the compiler that obj will
63
        // never be undefined without additional runtime checks.
64
        this.#obj = undefined as unknown as Proxy<T>;
10✔
65
    }
66

67
    readonly #worker: FunctionWorker<WorkerSignature<C, T>>;
68
    #obj: Proxy<T>;
69

70
    private async construct(...args: ConstructorParameters<C>) {
71
        const propertyNames =
72
            await this.#worker.execute("construct", ...args) as ReadonlyArray<Extract<keyof T, string>>;
10✔
73

74
        this.#obj = new ProxyImpl<C, T>(this.#worker, propertyNames) as unknown as Proxy<T>;
6✔
75
    }
76
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc