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

naver / billboard.js / 6010599079

29 Aug 2023 09:43AM UTC coverage: 94.095%. Remained the same
6010599079

push

github

web-flow
test(module): update test for worker (#3386)

- Add conditional for onerror logging
- Update test condition

Ref #3333

5956 of 6624 branches covered (0.0%)

Branch coverage included in aggregate %.

0 of 1 new or added line in 1 file covered. (0.0%)

7717 of 7907 relevant lines covered (97.6%)

21341.71 hits per line

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

83.33
/src/module/worker.ts
1
/**
2
 * Copyright (c) 2017 ~ present NAVER Corp.
3
 * billboard.js project is licensed under the MIT license
4
 */
5
import {window} from "./browser";
6

7
// Store blob in memory
8
const blob = {};
3✔
9

10
/**
11
 * Get Object URL
12
 * @param {Function} fn Function to be executed in worker
13
 * @param {Array} depsFn Dependency functions to run given function(fn).
14
 * @returns {string}
15
 * @private
16
 */
17
function getObjectURL(fn: Function, depsFn?: Function[]): string {
18
        const fnString = fn.toString();
3✔
19
        const key = fnString.replace(/(function|[\s\W\n])/g, "").substring(0, 15);
3✔
20

21
        if (!(key in blob)) {
3!
22
                // Web Worker body
23
                blob[key] = new window.Blob([
3✔
24
                        `${depsFn?.map(String).join(";") ?? ""}
6!
25

26
                        self.onmessage=function({data}) {
27
                                const result = (${fnString}).apply(null, data);
28
                                self.postMessage(result);
29
                        };`
30
                ], {
31
                        type: "text/javascript"
32
                });
33
        }
34

35
        return window.URL.createObjectURL(blob[key]);
3✔
36
}
37

38
/**
39
 * Create and run on Web Worker
40
 * @param {boolean} useWorker Use Web Worker
41
 * @param {Function} fn Function to be executed in worker
42
 * @param {Function} callback Callback function to receive result from worker
43
 * @param {Array} depsFn Dependency functions to run given function(fn).
44
 * @returns {object}
45
 * @example
46
 *         const worker = runWorker(function(arg) {
47
 *                  // do some tasks...
48
 *                  console.log("param:", A(arg));
49
 *
50
 *                  return 1234;
51
 *           }, function(data) {
52
 *                  // callback after worker is done
53
 *                   console.log("result:", data);
54
 *           },
55
 *           [function A(){}]
56
 *        );
57
 *
58
 *        worker(11111);
59
 * @private
60
 */
61
export function runWorker(
62
        useWorker = true, fn: Function, callback: Function, depsFn?: Function[]
3✔
63
): Function {
5,115✔
64
        let runFn;
65

66
        if (window.Worker && useWorker) {
5,115✔
67
                const src = getObjectURL(fn, depsFn);
3✔
68
                const worker = new window.Worker(src);
3✔
69

70
                runFn = function(...args) {
3✔
71
                        // trigger worker
72
                        worker.postMessage(args);
3✔
73

74
                        // listen worker
75
                        worker.onmessage = function(e) {
3✔
76
                                // release object URL from memory
77
                                window.URL.revokeObjectURL(src);
3✔
78

79
                                return callback(e.data);
3✔
80
                        };
81

82
                        // handle error
83
                        worker.onerror = function(e) {
3✔
84
                                // eslint-disable-next-line no-console
NEW
85
                                console.error ? console.error(e) : console.log(e);
×
86
                        };
87

88
                        // return new Promise((resolve, reject) => {
89
                        //         worker.onmessage = ({data}) => resolve(data);
90
                        //         worker.onerror = reject;
91
                        // });
92
                };
93
        } else {
94
                runFn = function(...args) {
5,112✔
95
                        const res = fn(...args);
5,112✔
96

97
                        callback(res);
5,106✔
98
                };
99
        }
100

101
        return runFn;
5,115✔
102
}
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