PageSourceSearch

https://eveeye.com/js-lib/worker.js

js eveeye.com collected 2026-09-26 03:45:55 UTC 4,019 bytes, 138 lines download raw bytes

1// https://github.com/deebloo/worker
2
3(function () {
4    if (typeof module !== 'undefined' && module.exports) {
5        module.exports = $worker;
6    } else {
7        window.$worker = $worker;
8    }
9
10    function $worker() {
11        var workers = [];
12
13        // PUBLIC_API
14        return {
15            create: create,
16            runAll: runAll,
17            terminateAll: terminateAll,
18            list: list
19        }
20
21        /**
22         * create a new worker instance and saves it to the list
23         *
24         * @param {Function} fn - the function to run in the worker
25         * @param {[{name: string, method: Function}} - a list of other functions to be available inside the worker function
26         */
27        function create(fn, otherScripts) {
28            var newWorker = _createWorker.apply(null, arguments);
29
30            workers.push(newWorker);
31
32            return newWorker;
33        }
34
35        /**
36         * run all of the workers in the array and resolve all
37         */
38        function runAll(data) {
39            var promises = workers.map(function (worker) {
40                return worker.run(data);
41            });
42
43            return Promise.all(promises);
44        }
45
46        /**
47         * terminate all workers
48         */
49        function terminateAll() {
50            workers.forEach(function (worker) {
51                worker._shell.terminate();
52            });
53
54            workers.length = 0;
55        }
56
57        /**
58         * returns the list of current workers
59         */
60        function list() {
61            return workers;
62        }
63
64        /**
65         * Create an actual web worker from an object url from a blob
66         *
67         * @param {Function} fn - the function to be put into the blog array.
68         * @param {[{name: string, method: Function}} - a list of other functions to be available inside the worker function
69         */
70        function _createWorker(fn, otherScripts) {
71            otherScripts = otherScripts || [];
72
73            var blobArray = otherScripts.map(function (script) {
74                return 'self.' + script.name + '=' + script.value.toString() + ';';
75            });
76            blobArray = blobArray.concat(['self.onmessage=', fn.toString(), ';']);
77
78            var blob = new Blob(blobArray, { type: 'text/javascript' });
79            var url = URL.createObjectURL(blob);
80
81            return {
82                // the web worker instance
83                _shell: (function () {
84                    var worker = new Worker(url);
85
86                    URL.revokeObjectURL(url);
87
88                    return worker;
89                })(),
90
91                // run the web worker
92                run: function (data) {
93                    return __run(this._shell, data);
94                },
95
96                // terminate the web worker
97                terminate: function () {
98                    return __terminate(this);
99                },
100
101                // subscribe to the worker
102                subscribe: function (fn) {
103                    this._shell.addEventListener('message', fn);
104                },
105
106                // ubsubscribe from the worker
107                unsubscribe: function (fn) {
108                    this._shell.removeEventListener('message', fn);
109                }
110            };
111        }
112
113        /**
114         * run the passed in web worker
115         */
116        function __run(worker, data) {
117            data = data || {};
118
119            worker.postMessage(data);
120
121            var promise = new Promise(function (resolve, reject) {
122                worker.onmessage = resolve;
123                worker.onerror = reject;
124            });
125
126            return promise;
127        }
128
129        /**
130         * terminate the web worker;
131         */
132        function __terminate(ref) {
133            workers.splice(workers.indexOf(ref), 1);
134
135            return ref._shell.terminate();
136        }
137    }
138})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.