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

andreashuber69 / kiss-worker / #61

03 May 2024 09:20PM UTC coverage: 97.521% (-2.5%) from 100.0%
#61

push

andreashuber69
Merge branch 'release/v2.0.0-alpha.0'

26 of 26 branches covered (100.0%)

Branch coverage included in aggregate %.

53 of 56 new or added lines in 10 files covered. (94.64%)

92 of 95 relevant lines covered (96.84%)

17.18 hits per line

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

75.0
/src/implementObjectWorkerExternal.ts
1
// https://github.com/andreashuber69/kiss-worker/blob/develop/README.md
2
import type { DedicatedWorker } from "./DedicatedWorker.js";
3
import type { implementObjectWorker } from "./implementObjectWorker.js";
4
import type { MethodsOnlyObject } from "./MethodsOnlyObject.js";
5
import type { ObjectInfo } from "./ObjectInfo.js";
6
import type { ObjectWorker } from "./ObjectWorker.js";
7
import { ObjectWorkerImpl } from "./ObjectWorkerImpl.js";
8
import type { serveObject } from "./serveObject.js";
9

10
/**
11
 * Provides a function returning an object implementing the {@linkcode ObjectWorker} interface.
12
 * @description Compared to {@linkcode implementObjectWorker}, {@linkcode implementObjectWorkerExternal} covers the
13
 * following additional requirements:
14
 * - The function returned by {@linkcode implementObjectWorkerExternal} can be executed on **any** thread.
15
 * - The code of the served object is only ever loaded on the worker thread. This can become important when the amount
16
 * of code of the served object is significant, such that you'd rather not load it on the thread calling
17
 * {@linkcode ObjectWorker.obj}. Build tools like [vite](vitejs.dev) support this use case by detecting
18
 * `new Worker(...)` calls and putting the worker script as well as all directly and indirectly called code into a
19
 * separate chunk. Please see [this example](https://github.com/andreashuber69/kiss-worker-demo2) for more information.
20
 * @param createWorker A function that creates a new [`Worker`](https://developer.mozilla.org/en-US/docs/Web/API/Worker)
21
 * with every call. This function **must** create a worker running a script different from the one it is created in.
22
 * The script must call {@linkcode serveObject} passing a constructor function and export the type of the object.
23
 * @param info An instance of {@linkcode ObjectInfo} instantiated with the type exported by the script running on the
24
 * worker thread.
25
 * @returns The function returning an object implementing the {@linkcode ObjectWorker} interface.
26
 */
27
export const implementObjectWorkerExternal = <C extends new (...args: never[]) => T, T extends MethodsOnlyObject<T>>(
4✔
28
    createWorker: () => DedicatedWorker,
29
    info: ObjectInfo<C, T>,
30
): (...args2: ConstructorParameters<C>) => Promise<ObjectWorker<T>> => async (...args2: ConstructorParameters<C>) => {
4✔
31
    const result = new ObjectWorkerImpl<C, T>(createWorker, info);
4✔
32

33
    try {
4✔
34
        await result.construct(...args2);
4✔
35
        return result;
4✔
36
    } catch (error: unknown) {
NEW
37
        result.terminate();
×
NEW
38
        throw error;
×
39
    }
40
};
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