agglomerative-clustering 2.0.0 → 3.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +455 -108
- package/dist/clustering.js +2 -14
- package/dist/clustering.wasm +0 -0
- package/dist/index.cjs +995 -150
- package/dist/index.d.cts +446 -0
- package/dist/index.d.ts +389 -21
- package/dist/index.mjs +977 -147
- package/dist/shared.cjs +349 -0
- package/dist/shared.mjs +340 -0
- package/dist/worker-thread.mjs +429 -0
- package/dist/worker.d.ts +117 -0
- package/dist/worker.mjs +953 -0
- package/package.json +64 -26
package/dist/worker.mjs
ADDED
|
@@ -0,0 +1,953 @@
|
|
|
1
|
+
var _excluded = ["signal", "onProgress"];
|
|
2
|
+
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
|
|
3
|
+
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
|
|
4
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
5
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
6
|
+
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
|
|
7
|
+
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
|
|
8
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
9
|
+
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
|
|
10
|
+
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
|
|
11
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
12
|
+
function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (c = i[4] || 3, u = i[5] === e ? i[3] : i[5], i[4] = 3, i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
|
|
13
|
+
function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { if (r) i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n;else { var o = function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); }; o("next", 0), o("throw", 1), o("return", 2); } }, _regeneratorDefine2(e, r, n, t); }
|
|
14
|
+
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
|
|
15
|
+
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
|
|
16
|
+
// The `agglomerative-clustering/worker` entry: the same API, run in a Web Worker (browsers) or a worker thread
|
|
17
|
+
// (Node), so clustering doesn't block the calling thread. `createWorker()` starts a worker with every function of
|
|
18
|
+
// the main entry as a method, plus `cluster`, which keeps an image and its clustering in the worker behind a
|
|
19
|
+
// handle, so re-cutting and re-mapping it copies nothing across. The functions exported directly use one shared
|
|
20
|
+
// worker, started on first use.
|
|
21
|
+
|
|
22
|
+
import { fail, isOptionsObject, snapshot, mark, revive, latestVersion } from "./shared.mjs";
|
|
23
|
+
|
|
24
|
+
// The same version, types and data-format version as the main entry (one implementation, in shared.js).
|
|
25
|
+
export { version, Palette, Clustering } from "./shared.mjs";
|
|
26
|
+
export var formatVersion = latestVersion;
|
|
27
|
+
var isNode = typeof process !== 'undefined' && !!(process.versions && process.versions.node) && typeof window === 'undefined';
|
|
28
|
+
|
|
29
|
+
// Starts the worker and returns {post, terminate, ref, unref}, with messages delivered to `receive`.
|
|
30
|
+
var start = /*#__PURE__*/function () {
|
|
31
|
+
var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(receive) {
|
|
32
|
+
var _worker, _yield$import, NodeWorker, worker;
|
|
33
|
+
return _regenerator().w(function (_context) {
|
|
34
|
+
while (1) switch (_context.n) {
|
|
35
|
+
case 0:
|
|
36
|
+
if (isNode) {
|
|
37
|
+
_context.n = 1;
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
// Written out in full so bundlers (Vite, webpack 5, Parcel) find and bundle the worker script.
|
|
41
|
+
_worker = new Worker(new URL('./worker-thread.mjs', import.meta.url), {
|
|
42
|
+
type: 'module'
|
|
43
|
+
});
|
|
44
|
+
_worker.addEventListener('message', function (event) {
|
|
45
|
+
return receive(event.data);
|
|
46
|
+
});
|
|
47
|
+
_worker.addEventListener('error', function (event) {
|
|
48
|
+
return receive({
|
|
49
|
+
failure: event.message || 'The worker failed to start.'
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
return _context.a(2, {
|
|
53
|
+
post: function post(message, transfer) {
|
|
54
|
+
return _worker.postMessage(message, transfer);
|
|
55
|
+
},
|
|
56
|
+
terminate: function terminate() {
|
|
57
|
+
return _worker.terminate();
|
|
58
|
+
},
|
|
59
|
+
ref: function ref() {},
|
|
60
|
+
unref: function unref() {}
|
|
61
|
+
});
|
|
62
|
+
case 1:
|
|
63
|
+
_context.n = 2;
|
|
64
|
+
return import(/* webpackIgnore: true */ /* @vite-ignore */['node', 'worker_threads'].join(':'));
|
|
65
|
+
case 2:
|
|
66
|
+
_yield$import = _context.v;
|
|
67
|
+
NodeWorker = _yield$import.Worker;
|
|
68
|
+
// The worker script needs none of the parent's flags, and some (like --input-type) are invalid in a worker.
|
|
69
|
+
worker = new NodeWorker(new URL('./worker-thread.mjs', import.meta.url), {
|
|
70
|
+
execArgv: []
|
|
71
|
+
});
|
|
72
|
+
worker.on('message', receive);
|
|
73
|
+
worker.on('error', function (error) {
|
|
74
|
+
return receive({
|
|
75
|
+
failure: error.message
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
worker.on('exit', function (code) {
|
|
79
|
+
return receive({
|
|
80
|
+
failure: "The worker exited unexpectedly (code ".concat(code, ").")
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
worker.unref();
|
|
84
|
+
return _context.a(2, {
|
|
85
|
+
post: function post(message, transfer) {
|
|
86
|
+
return worker.postMessage(message, transfer);
|
|
87
|
+
},
|
|
88
|
+
terminate: function terminate() {
|
|
89
|
+
return worker.terminate();
|
|
90
|
+
},
|
|
91
|
+
ref: function ref() {
|
|
92
|
+
return worker.ref();
|
|
93
|
+
},
|
|
94
|
+
unref: function unref() {
|
|
95
|
+
return worker.unref();
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}, _callee);
|
|
100
|
+
}));
|
|
101
|
+
return function start(_x) {
|
|
102
|
+
return _ref.apply(this, arguments);
|
|
103
|
+
};
|
|
104
|
+
}();
|
|
105
|
+
|
|
106
|
+
// Images cross to the worker as plain {data, format, width?, height?} objects (ImageData can't cross into Node
|
|
107
|
+
// worker threads, and its clamped array needn't), and mapped images are rebuilt in the caller's type on return.
|
|
108
|
+
// Anything else is sent as it is, so the worker's validation gives the usual errors.
|
|
109
|
+
var send = function send(image) {
|
|
110
|
+
if (typeof ImageData !== 'undefined' && image instanceof ImageData) {
|
|
111
|
+
var data = image.data;
|
|
112
|
+
return {
|
|
113
|
+
data: new Uint8Array(data.buffer, data.byteOffset, data.byteLength),
|
|
114
|
+
format: 'rgba',
|
|
115
|
+
width: image.width,
|
|
116
|
+
height: image.height
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
if (image && _typeof(image) === 'object' && !ArrayBuffer.isView(image) && ArrayBuffer.isView(image.data)) {
|
|
120
|
+
var sent = {
|
|
121
|
+
data: image.data,
|
|
122
|
+
format: image.format
|
|
123
|
+
};
|
|
124
|
+
if (image.width !== undefined) {
|
|
125
|
+
sent.width = image.width;
|
|
126
|
+
}
|
|
127
|
+
if (image.height !== undefined) {
|
|
128
|
+
sent.height = image.height;
|
|
129
|
+
}
|
|
130
|
+
return sent;
|
|
131
|
+
}
|
|
132
|
+
return image;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
// The worker returns what the main entry gives for the image it was sent: the same type for a Uint8Array or a
|
|
136
|
+
// RawImage, and a RawImage for ImageData, which is turned back into ImageData here.
|
|
137
|
+
var rebuild = function rebuild(image) {
|
|
138
|
+
if (typeof ImageData !== 'undefined' && image instanceof ImageData) {
|
|
139
|
+
var width = image.width,
|
|
140
|
+
height = image.height,
|
|
141
|
+
colorSpace = image.colorSpace;
|
|
142
|
+
return function (result) {
|
|
143
|
+
return new ImageData(new Uint8ClampedArray(result.data.buffer, result.data.byteOffset, result.data.byteLength), width, height, colorSpace ? {
|
|
144
|
+
colorSpace: colorSpace
|
|
145
|
+
} : undefined);
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
return function (result) {
|
|
149
|
+
return result;
|
|
150
|
+
};
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
// The buffer behind an image's pixels, to transfer when the worker was created with `transfer: true`: only when the
|
|
154
|
+
// pixels are the whole of an ordinary ArrayBuffer. Transferring empties every view of the buffer, so a view into part
|
|
155
|
+
// of a larger one (a subarray, a small Node Buffer from Node's shared pool) is copied instead, as is shared memory.
|
|
156
|
+
var pixelBuffers = function pixelBuffers(sent) {
|
|
157
|
+
var data = ArrayBuffer.isView(sent) ? sent : sent && ArrayBuffer.isView(sent.data) ? sent.data : null;
|
|
158
|
+
if (!data || !(data.buffer instanceof ArrayBuffer) || data.byteOffset !== 0 || data.byteLength !== data.buffer.byteLength) {
|
|
159
|
+
return [];
|
|
160
|
+
}
|
|
161
|
+
return [data.buffer];
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
// Rebuilds an error that happened in the worker: same name, message and code as the main entry gives for the same
|
|
165
|
+
// input, the stack of the call that failed (`origin`, made when the call was sent), and the worker's own stack as `cause`.
|
|
166
|
+
var remoteError = function remoteError(info, origin) {
|
|
167
|
+
var error = new Error(info.message);
|
|
168
|
+
if (info.name && info.name !== 'Error') {
|
|
169
|
+
error.name = info.name;
|
|
170
|
+
}
|
|
171
|
+
if (info.code !== undefined) {
|
|
172
|
+
error.code = info.code;
|
|
173
|
+
}
|
|
174
|
+
if (info.argument !== undefined) {
|
|
175
|
+
error.argument = info.argument;
|
|
176
|
+
}
|
|
177
|
+
if (origin.stack) {
|
|
178
|
+
error.stack = "".concat(error.name, ": ").concat(error.message).concat(origin.stack.slice(origin.stack.indexOf('\n')));
|
|
179
|
+
}
|
|
180
|
+
if (info.stack) {
|
|
181
|
+
var cause = new Error(info.message);
|
|
182
|
+
cause.name = error.name;
|
|
183
|
+
cause.stack = info.stack;
|
|
184
|
+
error.cause = cause;
|
|
185
|
+
}
|
|
186
|
+
return error;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
// Checks the `wasm` option (see init in the main entry) and makes it fit for posting to the worker: a URL becomes its
|
|
190
|
+
// string, and bytes and compiled modules are cloned across.
|
|
191
|
+
var wasmOption = function wasmOption(wasm) {
|
|
192
|
+
if (typeof wasm === 'string') {
|
|
193
|
+
return wasm;
|
|
194
|
+
}
|
|
195
|
+
if (typeof URL !== 'undefined' && wasm instanceof URL) {
|
|
196
|
+
return wasm.href;
|
|
197
|
+
}
|
|
198
|
+
if (wasm instanceof ArrayBuffer || ArrayBuffer.isView(wasm)) {
|
|
199
|
+
return wasm;
|
|
200
|
+
}
|
|
201
|
+
if (typeof WebAssembly !== 'undefined' && wasm instanceof WebAssembly.Module) {
|
|
202
|
+
return wasm;
|
|
203
|
+
}
|
|
204
|
+
throw fail('invalid-argument', "Invalid wasm: must be a URL or path string, a URL, an ArrayBuffer or typed array of the .wasm bytes, or a WebAssembly.Module.", 'options.wasm');
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
// Options for init: only `wasm`, or none.
|
|
208
|
+
var initOptions = function initOptions(options) {
|
|
209
|
+
if (options === undefined) {
|
|
210
|
+
return undefined;
|
|
211
|
+
}
|
|
212
|
+
if (!isOptionsObject(options)) {
|
|
213
|
+
throw fail('invalid-argument', "Invalid options: expected an object.", 'options');
|
|
214
|
+
}
|
|
215
|
+
options = snapshot(options);
|
|
216
|
+
for (var _i = 0, _Object$keys = Object.keys(options); _i < _Object$keys.length; _i++) {
|
|
217
|
+
var key = _Object$keys[_i];
|
|
218
|
+
if (key !== 'wasm') {
|
|
219
|
+
throw fail('invalid-argument', "Invalid option '".concat(key, "': expected wasm."), "options.".concat(key));
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return options.wasm === undefined ? {} : {
|
|
223
|
+
wasm: wasmOption(options.wasm)
|
|
224
|
+
};
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
// A signal and a progress callback can't cross to the worker, so they stay on this side: the options are sent without
|
|
228
|
+
// them, a cancel message is sent when the signal aborts, and the worker's progress messages call the callback.
|
|
229
|
+
var takeControls = function takeControls(options) {
|
|
230
|
+
var progressAllowed = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
|
|
231
|
+
// Anything but an ordinary object goes on as it is, for the worker's own validation to reject. An ordinary one is copied
|
|
232
|
+
// once first: structured cloning drops inherited properties, and the copy is what gets validated and sent.
|
|
233
|
+
if (!isOptionsObject(options)) {
|
|
234
|
+
return {
|
|
235
|
+
options: options,
|
|
236
|
+
signal: undefined,
|
|
237
|
+
onProgress: undefined
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
options = snapshot(options);
|
|
241
|
+
var _options = options,
|
|
242
|
+
signal = _options.signal,
|
|
243
|
+
onProgress = _options.onProgress;
|
|
244
|
+
if (!progressAllowed && onProgress !== undefined) {
|
|
245
|
+
throw fail('invalid-argument', "Invalid option 'onProgress': the functions that only map pixels take just `signal`.", 'options.onProgress');
|
|
246
|
+
}
|
|
247
|
+
if (signal === undefined && onProgress === undefined) {
|
|
248
|
+
return {
|
|
249
|
+
options: options,
|
|
250
|
+
signal: signal,
|
|
251
|
+
onProgress: onProgress
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
if (signal !== undefined && (signal === null || _typeof(signal) !== 'object' || typeof signal.aborted !== 'boolean' || typeof signal.addEventListener !== 'function')) {
|
|
255
|
+
throw fail('invalid-argument', "Invalid signal: must be an AbortSignal.", 'options.signal');
|
|
256
|
+
}
|
|
257
|
+
if (onProgress !== undefined && typeof onProgress !== 'function') {
|
|
258
|
+
throw fail('invalid-argument', "Invalid onProgress: must be a function.", 'options.onProgress');
|
|
259
|
+
}
|
|
260
|
+
var _options2 = options,
|
|
261
|
+
_signal = _options2.signal,
|
|
262
|
+
_onProgress = _options2.onProgress,
|
|
263
|
+
rest = _objectWithoutProperties(_options2, _excluded);
|
|
264
|
+
return {
|
|
265
|
+
options: rest,
|
|
266
|
+
signal: signal,
|
|
267
|
+
onProgress: onProgress
|
|
268
|
+
};
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
// Where the options sit among a function's arguments after the image (see takeControls).
|
|
272
|
+
var optionsPosition = {
|
|
273
|
+
getClustering: 0,
|
|
274
|
+
getPalette: 1,
|
|
275
|
+
getSwatches: 1,
|
|
276
|
+
quantize: 1,
|
|
277
|
+
quantizeFromClustering: 2,
|
|
278
|
+
quantizeWithPalette: 1,
|
|
279
|
+
quantizeToIndices: 1,
|
|
280
|
+
quantizeToIndicesFromClustering: 2,
|
|
281
|
+
quantizeToIndicesWithPalette: 1
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
// Like fetch: an aborted call rejects with the signal's reason, an AbortError unless the caller gave another.
|
|
285
|
+
var abortReason = function abortReason(signal) {
|
|
286
|
+
return signal.reason !== undefined ? signal.reason : new DOMException('This operation was aborted', 'AbortError');
|
|
287
|
+
};
|
|
288
|
+
var imageFunctions = ['getClustering', 'getPalette', 'getSwatches', 'quantize', 'quantizeFromClustering', 'quantizeWithPalette', 'quantizeToIndices', 'quantizeToIndicesFromClustering', 'quantizeToIndicesWithPalette'];
|
|
289
|
+
var mappedImageFunctions = ['quantize', 'quantizeFromClustering', 'quantizeWithPalette'];
|
|
290
|
+
// These only map pixels, so they take a signal but no progress callback (see mapOptions in index.js).
|
|
291
|
+
var mappingOnlyFunctions = ['quantizeFromClustering', 'quantizeWithPalette', 'quantizeToIndicesFromClustering', 'quantizeToIndicesWithPalette'];
|
|
292
|
+
var otherFunctions = ['getPaletteFromClustering', 'getSwatchesFromClustering', 'getClusteringInfo'];
|
|
293
|
+
export var createWorker = function createWorker(options) {
|
|
294
|
+
if (options === undefined) {
|
|
295
|
+
options = {};
|
|
296
|
+
}
|
|
297
|
+
if (!isOptionsObject(options)) {
|
|
298
|
+
throw fail('invalid-argument', "Invalid options: expected an object.", 'options');
|
|
299
|
+
}
|
|
300
|
+
options = snapshot(options);
|
|
301
|
+
for (var _i2 = 0, _Object$keys2 = Object.keys(options); _i2 < _Object$keys2.length; _i2++) {
|
|
302
|
+
var key = _Object$keys2[_i2];
|
|
303
|
+
if (key !== 'transfer' && key !== 'wasm') {
|
|
304
|
+
throw fail('invalid-argument', "Invalid option '".concat(key, "': expected transfer, wasm."), "options.".concat(key));
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
if (options.transfer !== undefined && typeof options.transfer !== 'boolean') {
|
|
308
|
+
throw fail('invalid-argument', "Invalid transfer: must be a boolean.", 'options.transfer');
|
|
309
|
+
}
|
|
310
|
+
var transfer = options.transfer === true;
|
|
311
|
+
// Where the worker finds its .wasm: sent as the worker's first call, and repeated until it has succeeded.
|
|
312
|
+
var initialWasm = options.wasm === undefined ? undefined : wasmOption(options.wasm);
|
|
313
|
+
var initial = initialWasm === undefined ? null : {
|
|
314
|
+
wasm: initialWasm
|
|
315
|
+
};
|
|
316
|
+
var initialising = null;
|
|
317
|
+
var connection = null;
|
|
318
|
+
var starting = null;
|
|
319
|
+
// Counts the workers this instance has started, so a late message or error from one it already dropped is ignored.
|
|
320
|
+
var generation = 0;
|
|
321
|
+
var startingGeneration = 0;
|
|
322
|
+
var terminated = false;
|
|
323
|
+
var nextId = 1;
|
|
324
|
+
var nextHandle = 1;
|
|
325
|
+
var pending = new Map();
|
|
326
|
+
|
|
327
|
+
// Safety net for handles dropped without release(): when a handle is garbage-collected, the worker frees what it
|
|
328
|
+
// kept for it. Best-effort, since collection time is up to the engine; release() or `await using` is the contract.
|
|
329
|
+
var reclaim = typeof FinalizationRegistry === 'function' ? new FinalizationRegistry(function (id) {
|
|
330
|
+
if (!terminated) {
|
|
331
|
+
invoke('release', [id], [], new Error())["catch"](function () {});
|
|
332
|
+
}
|
|
333
|
+
}) : null;
|
|
334
|
+
|
|
335
|
+
// A worker that failed (could not start, crashed or exited) is dropped: the calls waiting on it get the failure, and
|
|
336
|
+
// the next call starts a new one, which either works or fails the same clear way. Without this, later calls would be
|
|
337
|
+
// posted to a thread that will never answer and hang forever. Handles kept in the dropped worker are gone with it.
|
|
338
|
+
var lose = function lose(reason) {
|
|
339
|
+
var lost = connection;
|
|
340
|
+
generation++;
|
|
341
|
+
connection = null;
|
|
342
|
+
starting = null;
|
|
343
|
+
initialising = null;
|
|
344
|
+
if (initialWasm !== undefined) {
|
|
345
|
+
initial = {
|
|
346
|
+
wasm: initialWasm
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
var calls = _toConsumableArray(pending.values());
|
|
350
|
+
pending.clear();
|
|
351
|
+
var _iterator = _createForOfIteratorHelper(calls),
|
|
352
|
+
_step;
|
|
353
|
+
try {
|
|
354
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
355
|
+
var call = _step.value;
|
|
356
|
+
call.cleanup();
|
|
357
|
+
call.reject(fail('worker-failed', reason));
|
|
358
|
+
}
|
|
359
|
+
} catch (err) {
|
|
360
|
+
_iterator.e(err);
|
|
361
|
+
} finally {
|
|
362
|
+
_iterator.f();
|
|
363
|
+
}
|
|
364
|
+
if (lost) {
|
|
365
|
+
try {
|
|
366
|
+
Promise.resolve(lost.terminate())["catch"](function () {});
|
|
367
|
+
} catch (ignored) {/* already gone */}
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
var receive = function receive(message) {
|
|
371
|
+
if (message.failure !== undefined) {
|
|
372
|
+
if (!terminated) {
|
|
373
|
+
lose("Worker failed: ".concat(message.failure));
|
|
374
|
+
}
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
var call = pending.get(message.id);
|
|
378
|
+
if (!call) {
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
if (message.progress !== undefined) {
|
|
382
|
+
// As on the main thread, a callback that throws fails the call (with its error) and stops the work.
|
|
383
|
+
if (call.onProgress) {
|
|
384
|
+
try {
|
|
385
|
+
call.onProgress(message.progress);
|
|
386
|
+
} catch (error) {
|
|
387
|
+
pending["delete"](message.id);
|
|
388
|
+
call.cleanup();
|
|
389
|
+
if (pending.size === 0 && connection) {
|
|
390
|
+
connection.unref();
|
|
391
|
+
}
|
|
392
|
+
try {
|
|
393
|
+
connection.post({
|
|
394
|
+
cancel: message.id
|
|
395
|
+
}, []);
|
|
396
|
+
} catch (ignored) {/* the worker is gone anyway */}
|
|
397
|
+
call.reject(error);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
pending["delete"](message.id);
|
|
403
|
+
call.cleanup();
|
|
404
|
+
if (pending.size === 0 && connection) {
|
|
405
|
+
connection.unref();
|
|
406
|
+
}
|
|
407
|
+
if (message.error !== undefined) {
|
|
408
|
+
call.reject(remoteError(message.error, call.origin));
|
|
409
|
+
} else {
|
|
410
|
+
call.resolve(revive(message.result));
|
|
411
|
+
}
|
|
412
|
+
};
|
|
413
|
+
|
|
414
|
+
// `origin` is an Error made in the caller's own (non-async) function, for the stack of a failure: async helpers
|
|
415
|
+
// from transpiled code would otherwise fill the stack's frame limit before the caller shows up.
|
|
416
|
+
var invoke = /*#__PURE__*/function () {
|
|
417
|
+
var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(method, args, transferable, origin) {
|
|
418
|
+
var controls,
|
|
419
|
+
mine,
|
|
420
|
+
waitingFor,
|
|
421
|
+
started,
|
|
422
|
+
_args2 = arguments;
|
|
423
|
+
return _regenerator().w(function (_context2) {
|
|
424
|
+
while (1) switch (_context2.n) {
|
|
425
|
+
case 0:
|
|
426
|
+
controls = _args2.length > 4 && _args2[4] !== undefined ? _args2[4] : {};
|
|
427
|
+
if (!terminated) {
|
|
428
|
+
_context2.n = 1;
|
|
429
|
+
break;
|
|
430
|
+
}
|
|
431
|
+
throw fail('worker-terminated', "Worker terminated.");
|
|
432
|
+
case 1:
|
|
433
|
+
if (connection) {
|
|
434
|
+
_context2.n = 5;
|
|
435
|
+
break;
|
|
436
|
+
}
|
|
437
|
+
if (!starting) {
|
|
438
|
+
mine = ++generation;
|
|
439
|
+
startingGeneration = mine;
|
|
440
|
+
// A start that fails is not kept, so the next call tries again.
|
|
441
|
+
starting = start(function (message) {
|
|
442
|
+
if (mine === generation) {
|
|
443
|
+
receive(message);
|
|
444
|
+
}
|
|
445
|
+
})["catch"](function (error) {
|
|
446
|
+
if (mine === generation) {
|
|
447
|
+
starting = null;
|
|
448
|
+
}
|
|
449
|
+
throw Object.assign(fail('worker-failed', "Worker failed: ".concat(error && error.message)), {
|
|
450
|
+
cause: error
|
|
451
|
+
});
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
waitingFor = startingGeneration;
|
|
455
|
+
_context2.n = 2;
|
|
456
|
+
return starting;
|
|
457
|
+
case 2:
|
|
458
|
+
started = _context2.v;
|
|
459
|
+
if (!terminated) {
|
|
460
|
+
_context2.n = 3;
|
|
461
|
+
break;
|
|
462
|
+
}
|
|
463
|
+
started.terminate();
|
|
464
|
+
throw fail('worker-terminated', "Worker terminated.");
|
|
465
|
+
case 3:
|
|
466
|
+
if (!(waitingFor !== generation)) {
|
|
467
|
+
_context2.n = 4;
|
|
468
|
+
break;
|
|
469
|
+
}
|
|
470
|
+
throw fail('worker-failed', 'Worker failed while starting.');
|
|
471
|
+
case 4:
|
|
472
|
+
connection = connection || started;
|
|
473
|
+
case 5:
|
|
474
|
+
if (!(initial && !(method === 'init' && args.length > 0 && args[0].wasm !== undefined))) {
|
|
475
|
+
_context2.n = 6;
|
|
476
|
+
break;
|
|
477
|
+
}
|
|
478
|
+
initialising = initialising || post('init', [initial], [], origin).then(function () {
|
|
479
|
+
initial = null;
|
|
480
|
+
}, function (error) {
|
|
481
|
+
initialising = null;
|
|
482
|
+
throw error;
|
|
483
|
+
});
|
|
484
|
+
_context2.n = 6;
|
|
485
|
+
return initialising;
|
|
486
|
+
case 6:
|
|
487
|
+
return _context2.a(2, post(method, args, transferable, origin, controls));
|
|
488
|
+
}
|
|
489
|
+
}, _callee2);
|
|
490
|
+
}));
|
|
491
|
+
return function invoke(_x2, _x3, _x4, _x5) {
|
|
492
|
+
return _ref2.apply(this, arguments);
|
|
493
|
+
};
|
|
494
|
+
}();
|
|
495
|
+
var post = function post(method, args, transferable, origin) {
|
|
496
|
+
var _ref3 = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {},
|
|
497
|
+
signal = _ref3.signal,
|
|
498
|
+
onProgress = _ref3.onProgress;
|
|
499
|
+
if (terminated) {
|
|
500
|
+
return Promise.reject(fail('worker-terminated', "Worker terminated."));
|
|
501
|
+
}
|
|
502
|
+
if (signal && signal.aborted) {
|
|
503
|
+
return Promise.reject(abortReason(signal));
|
|
504
|
+
}
|
|
505
|
+
var id = nextId++;
|
|
506
|
+
return new Promise(function (resolve, reject) {
|
|
507
|
+
if (pending.size === 0) {
|
|
508
|
+
connection.ref();
|
|
509
|
+
}
|
|
510
|
+
var call = {
|
|
511
|
+
resolve: resolve,
|
|
512
|
+
reject: reject,
|
|
513
|
+
origin: origin,
|
|
514
|
+
onProgress: onProgress,
|
|
515
|
+
cleanup: function cleanup() {}
|
|
516
|
+
};
|
|
517
|
+
if (signal) {
|
|
518
|
+
// Rejects at once; the worker is told to stop (it may still be working on an earlier call or this one,
|
|
519
|
+
// and drops whatever it produces). The thread no longer keeps a Node process alive for this call.
|
|
520
|
+
var abort = function abort() {
|
|
521
|
+
if (!pending["delete"](id)) {
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
if (pending.size === 0) {
|
|
525
|
+
connection.unref();
|
|
526
|
+
}
|
|
527
|
+
try {
|
|
528
|
+
connection.post({
|
|
529
|
+
cancel: id
|
|
530
|
+
}, []);
|
|
531
|
+
} catch (error) {/* the worker is gone anyway */}
|
|
532
|
+
reject(abortReason(signal));
|
|
533
|
+
};
|
|
534
|
+
signal.addEventListener('abort', abort, {
|
|
535
|
+
once: true
|
|
536
|
+
});
|
|
537
|
+
call.cleanup = function () {
|
|
538
|
+
return signal.removeEventListener('abort', abort);
|
|
539
|
+
};
|
|
540
|
+
}
|
|
541
|
+
pending.set(id, call);
|
|
542
|
+
try {
|
|
543
|
+
var message = {
|
|
544
|
+
id: id,
|
|
545
|
+
method: method,
|
|
546
|
+
args: args.map(mark),
|
|
547
|
+
cancellable: signal !== undefined || onProgress !== undefined,
|
|
548
|
+
progress: onProgress !== undefined
|
|
549
|
+
};
|
|
550
|
+
try {
|
|
551
|
+
connection.post(message, transfer ? transferable : []);
|
|
552
|
+
} catch (error) {
|
|
553
|
+
// Some whole buffers still can't be transferred (Node marks some untransferable): copy them instead.
|
|
554
|
+
// A message that can't be copied either fails the same way again, and is reported below.
|
|
555
|
+
if (!(transfer && transferable.length > 0 && error && error.name === 'DataCloneError')) {
|
|
556
|
+
throw error;
|
|
557
|
+
}
|
|
558
|
+
connection.post(message, []);
|
|
559
|
+
}
|
|
560
|
+
} catch (error) {
|
|
561
|
+
pending["delete"](id);
|
|
562
|
+
call.cleanup();
|
|
563
|
+
if (pending.size === 0) {
|
|
564
|
+
connection.unref();
|
|
565
|
+
}
|
|
566
|
+
// A value that can't be structured-cloned (a function, symbol or promise) is an invalid argument, as it
|
|
567
|
+
// would be if the main entry were called with it.
|
|
568
|
+
reject(error && error.name === 'DataCloneError' ? Object.assign(fail('invalid-argument', "Invalid argument: it cannot be sent to the worker (".concat(error.message, ")")), {
|
|
569
|
+
cause: error
|
|
570
|
+
}) : error);
|
|
571
|
+
}
|
|
572
|
+
});
|
|
573
|
+
};
|
|
574
|
+
var instance = {};
|
|
575
|
+
var _loop = function _loop() {
|
|
576
|
+
var name = _imageFunctions[_i3];
|
|
577
|
+
var mapped = mappedImageFunctions.includes(name);
|
|
578
|
+
instance[name] = function (image) {
|
|
579
|
+
for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
580
|
+
rest[_key - 1] = arguments[_key];
|
|
581
|
+
}
|
|
582
|
+
var origin = new Error();
|
|
583
|
+
return _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3() {
|
|
584
|
+
var position, _takeControls, options, signal, onProgress, args, sent, result;
|
|
585
|
+
return _regenerator().w(function (_context3) {
|
|
586
|
+
while (1) switch (_context3.n) {
|
|
587
|
+
case 0:
|
|
588
|
+
position = optionsPosition[name];
|
|
589
|
+
_takeControls = takeControls(rest[position], !mappingOnlyFunctions.includes(name)), options = _takeControls.options, signal = _takeControls.signal, onProgress = _takeControls.onProgress; // Checked before the image is copied or transferred away, so an aborted call leaves it with the caller.
|
|
590
|
+
if (!(signal && signal.aborted)) {
|
|
591
|
+
_context3.n = 1;
|
|
592
|
+
break;
|
|
593
|
+
}
|
|
594
|
+
throw abortReason(signal);
|
|
595
|
+
case 1:
|
|
596
|
+
args = rest.slice();
|
|
597
|
+
if (position < args.length) {
|
|
598
|
+
args[position] = options;
|
|
599
|
+
}
|
|
600
|
+
sent = send(image);
|
|
601
|
+
_context3.n = 2;
|
|
602
|
+
return invoke(name, [sent].concat(_toConsumableArray(args)), pixelBuffers(sent), origin, {
|
|
603
|
+
signal: signal,
|
|
604
|
+
onProgress: onProgress
|
|
605
|
+
});
|
|
606
|
+
case 2:
|
|
607
|
+
result = _context3.v;
|
|
608
|
+
return _context3.a(2, mapped ? rebuild(image)(result) : result);
|
|
609
|
+
}
|
|
610
|
+
}, _callee3);
|
|
611
|
+
}))();
|
|
612
|
+
};
|
|
613
|
+
};
|
|
614
|
+
for (var _i3 = 0, _imageFunctions = imageFunctions; _i3 < _imageFunctions.length; _i3++) {
|
|
615
|
+
_loop();
|
|
616
|
+
}
|
|
617
|
+
var _loop2 = function _loop2() {
|
|
618
|
+
var name = _otherFunctions[_i4];
|
|
619
|
+
instance[name] = function () {
|
|
620
|
+
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
621
|
+
args[_key2] = arguments[_key2];
|
|
622
|
+
}
|
|
623
|
+
return invoke(name, args, [], new Error());
|
|
624
|
+
};
|
|
625
|
+
};
|
|
626
|
+
for (var _i4 = 0, _otherFunctions = otherFunctions; _i4 < _otherFunctions.length; _i4++) {
|
|
627
|
+
_loop2();
|
|
628
|
+
}
|
|
629
|
+
instance.init = function (initArgs) {
|
|
630
|
+
var origin = new Error();
|
|
631
|
+
var checked;
|
|
632
|
+
try {
|
|
633
|
+
checked = initOptions(initArgs);
|
|
634
|
+
} catch (error) {
|
|
635
|
+
return Promise.reject(error);
|
|
636
|
+
}
|
|
637
|
+
return invoke('init', checked === undefined ? [] : [checked], [], origin).then(function () {
|
|
638
|
+
return undefined;
|
|
639
|
+
});
|
|
640
|
+
};
|
|
641
|
+
|
|
642
|
+
// Clusters `image` and keeps it, with its clustering, in the worker.
|
|
643
|
+
instance.cluster = function (image, clusterOptions) {
|
|
644
|
+
var origin = new Error();
|
|
645
|
+
return _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7() {
|
|
646
|
+
var _takeControls2, options, signal, onProgress, id, sent, output, released, use, withControls, handle, _t6;
|
|
647
|
+
return _regenerator().w(function (_context7) {
|
|
648
|
+
while (1) switch (_context7.n) {
|
|
649
|
+
case 0:
|
|
650
|
+
_takeControls2 = takeControls(clusterOptions), options = _takeControls2.options, signal = _takeControls2.signal, onProgress = _takeControls2.onProgress;
|
|
651
|
+
if (!(signal && signal.aborted)) {
|
|
652
|
+
_context7.n = 1;
|
|
653
|
+
break;
|
|
654
|
+
}
|
|
655
|
+
throw abortReason(signal);
|
|
656
|
+
case 1:
|
|
657
|
+
id = nextHandle++;
|
|
658
|
+
sent = send(image);
|
|
659
|
+
_context7.p = 2;
|
|
660
|
+
_context7.n = 3;
|
|
661
|
+
return invoke('cluster', [id, sent, options], pixelBuffers(sent), origin, {
|
|
662
|
+
signal: signal,
|
|
663
|
+
onProgress: onProgress
|
|
664
|
+
});
|
|
665
|
+
case 3:
|
|
666
|
+
_context7.n = 5;
|
|
667
|
+
break;
|
|
668
|
+
case 4:
|
|
669
|
+
_context7.p = 4;
|
|
670
|
+
_t6 = _context7.v;
|
|
671
|
+
// A cancelled call is rejected at once, but the worker may have finished it just before it heard: it
|
|
672
|
+
// would then keep the image and clustering with nobody able to release them. Releasing an unknown handle
|
|
673
|
+
// is harmless, and this message follows the cluster call in the worker's queue.
|
|
674
|
+
if (!terminated) {
|
|
675
|
+
invoke('release', [id], [], new Error())["catch"](function () {});
|
|
676
|
+
}
|
|
677
|
+
throw _t6;
|
|
678
|
+
case 5:
|
|
679
|
+
output = rebuild(image);
|
|
680
|
+
released = false; // Every method goes through `use`, which refers to `handle`, so a method kept after the handle itself is dropped
|
|
681
|
+
// keeps it alive and it is not reclaimed while still in use.
|
|
682
|
+
use = function use(method, args, controls) {
|
|
683
|
+
if (released || !handle) {
|
|
684
|
+
return Promise.reject(fail('invalid-handle', "Invalid handle: it was released."));
|
|
685
|
+
}
|
|
686
|
+
return invoke(method, [id].concat(_toConsumableArray(args)), [], new Error(), controls);
|
|
687
|
+
}; // Calls with mapping options, whose signal and progress callback stay on this side (see takeControls).
|
|
688
|
+
withControls = function withControls(method, leading, mapOptions) {
|
|
689
|
+
var taken;
|
|
690
|
+
try {
|
|
691
|
+
taken = takeControls(mapOptions, false);
|
|
692
|
+
} catch (error) {
|
|
693
|
+
return Promise.reject(error);
|
|
694
|
+
}
|
|
695
|
+
return use(method, [].concat(_toConsumableArray(leading), [taken.options]), {
|
|
696
|
+
signal: taken.signal,
|
|
697
|
+
onProgress: taken.onProgress
|
|
698
|
+
});
|
|
699
|
+
};
|
|
700
|
+
handle = {
|
|
701
|
+
getClustering: function getClustering() {
|
|
702
|
+
return use('handleGetClustering', []);
|
|
703
|
+
},
|
|
704
|
+
getPalette: function getPalette(k) {
|
|
705
|
+
return use('handleGetPalette', [k]);
|
|
706
|
+
},
|
|
707
|
+
getSwatches: function getSwatches(k) {
|
|
708
|
+
return use('handleGetSwatches', [k]);
|
|
709
|
+
},
|
|
710
|
+
getClusteringInfo: function getClusteringInfo() {
|
|
711
|
+
return use('handleGetClusteringInfo', []);
|
|
712
|
+
},
|
|
713
|
+
quantize: function () {
|
|
714
|
+
var _quantize = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(k, mapOptions) {
|
|
715
|
+
var _t, _t2;
|
|
716
|
+
return _regenerator().w(function (_context4) {
|
|
717
|
+
while (1) switch (_context4.n) {
|
|
718
|
+
case 0:
|
|
719
|
+
_t = output;
|
|
720
|
+
_context4.n = 1;
|
|
721
|
+
return withControls('handleQuantize', [k], mapOptions);
|
|
722
|
+
case 1:
|
|
723
|
+
_t2 = _context4.v;
|
|
724
|
+
return _context4.a(2, _t(_t2));
|
|
725
|
+
}
|
|
726
|
+
}, _callee4);
|
|
727
|
+
}));
|
|
728
|
+
function quantize(_x6, _x7) {
|
|
729
|
+
return _quantize.apply(this, arguments);
|
|
730
|
+
}
|
|
731
|
+
return quantize;
|
|
732
|
+
}(),
|
|
733
|
+
quantizeToIndices: function quantizeToIndices(k, mapOptions) {
|
|
734
|
+
return withControls('handleQuantizeToIndices', [k], mapOptions);
|
|
735
|
+
},
|
|
736
|
+
quantizeWithPalette: function () {
|
|
737
|
+
var _quantizeWithPalette = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(palette, mapOptions) {
|
|
738
|
+
var _t3, _t4;
|
|
739
|
+
return _regenerator().w(function (_context5) {
|
|
740
|
+
while (1) switch (_context5.n) {
|
|
741
|
+
case 0:
|
|
742
|
+
_t3 = output;
|
|
743
|
+
_context5.n = 1;
|
|
744
|
+
return withControls('handleQuantizeWithPalette', [palette], mapOptions);
|
|
745
|
+
case 1:
|
|
746
|
+
_t4 = _context5.v;
|
|
747
|
+
return _context5.a(2, _t3(_t4));
|
|
748
|
+
}
|
|
749
|
+
}, _callee5);
|
|
750
|
+
}));
|
|
751
|
+
function quantizeWithPalette(_x8, _x9) {
|
|
752
|
+
return _quantizeWithPalette.apply(this, arguments);
|
|
753
|
+
}
|
|
754
|
+
return quantizeWithPalette;
|
|
755
|
+
}(),
|
|
756
|
+
quantizeToIndicesWithPalette: function quantizeToIndicesWithPalette(palette, mapOptions) {
|
|
757
|
+
return withControls('handleQuantizeToIndicesWithPalette', [palette], mapOptions);
|
|
758
|
+
},
|
|
759
|
+
release: function () {
|
|
760
|
+
var _release = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6() {
|
|
761
|
+
var _t5;
|
|
762
|
+
return _regenerator().w(function (_context6) {
|
|
763
|
+
while (1) switch (_context6.n) {
|
|
764
|
+
case 0:
|
|
765
|
+
if (!released) {
|
|
766
|
+
_context6.n = 1;
|
|
767
|
+
break;
|
|
768
|
+
}
|
|
769
|
+
return _context6.a(2);
|
|
770
|
+
case 1:
|
|
771
|
+
released = true;
|
|
772
|
+
if (reclaim) {
|
|
773
|
+
reclaim.unregister(handle);
|
|
774
|
+
}
|
|
775
|
+
// If the worker has been terminated there is nothing left to free (so `await using` and
|
|
776
|
+
// terminate() can come in either order); any other failure is reported.
|
|
777
|
+
_context6.p = 2;
|
|
778
|
+
_context6.n = 3;
|
|
779
|
+
return invoke('release', [id], [], new Error());
|
|
780
|
+
case 3:
|
|
781
|
+
_context6.n = 5;
|
|
782
|
+
break;
|
|
783
|
+
case 4:
|
|
784
|
+
_context6.p = 4;
|
|
785
|
+
_t5 = _context6.v;
|
|
786
|
+
if (!(_t5.code !== 'worker-terminated')) {
|
|
787
|
+
_context6.n = 5;
|
|
788
|
+
break;
|
|
789
|
+
}
|
|
790
|
+
throw _t5;
|
|
791
|
+
case 5:
|
|
792
|
+
return _context6.a(2);
|
|
793
|
+
}
|
|
794
|
+
}, _callee6, null, [[2, 4]]);
|
|
795
|
+
}));
|
|
796
|
+
function release() {
|
|
797
|
+
return _release.apply(this, arguments);
|
|
798
|
+
}
|
|
799
|
+
return release;
|
|
800
|
+
}()
|
|
801
|
+
}; // `await using handle = await worker.cluster(image)` releases it at the end of the block.
|
|
802
|
+
if (_typeof(Symbol.asyncDispose) === 'symbol') {
|
|
803
|
+
handle[Symbol.asyncDispose] = handle.release;
|
|
804
|
+
}
|
|
805
|
+
if (reclaim) {
|
|
806
|
+
reclaim.register(handle, id, handle);
|
|
807
|
+
}
|
|
808
|
+
return _context7.a(2, handle);
|
|
809
|
+
}
|
|
810
|
+
}, _callee7, null, [[2, 4]]);
|
|
811
|
+
}))();
|
|
812
|
+
};
|
|
813
|
+
|
|
814
|
+
// Stops the worker. Calls still waiting are rejected, and later calls fail.
|
|
815
|
+
instance.terminate = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8() {
|
|
816
|
+
var _iterator2, _step2, call, _t7;
|
|
817
|
+
return _regenerator().w(function (_context8) {
|
|
818
|
+
while (1) switch (_context8.n) {
|
|
819
|
+
case 0:
|
|
820
|
+
if (!terminated) {
|
|
821
|
+
_context8.n = 1;
|
|
822
|
+
break;
|
|
823
|
+
}
|
|
824
|
+
return _context8.a(2);
|
|
825
|
+
case 1:
|
|
826
|
+
terminated = true;
|
|
827
|
+
_iterator2 = _createForOfIteratorHelper(pending.values());
|
|
828
|
+
try {
|
|
829
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
830
|
+
call = _step2.value;
|
|
831
|
+
call.cleanup();
|
|
832
|
+
call.reject(fail('worker-terminated', "Worker terminated."));
|
|
833
|
+
}
|
|
834
|
+
} catch (err) {
|
|
835
|
+
_iterator2.e(err);
|
|
836
|
+
} finally {
|
|
837
|
+
_iterator2.f();
|
|
838
|
+
}
|
|
839
|
+
pending.clear();
|
|
840
|
+
if (!connection) {
|
|
841
|
+
_context8.n = 3;
|
|
842
|
+
break;
|
|
843
|
+
}
|
|
844
|
+
_context8.n = 2;
|
|
845
|
+
return connection.terminate();
|
|
846
|
+
case 2:
|
|
847
|
+
_context8.n = 7;
|
|
848
|
+
break;
|
|
849
|
+
case 3:
|
|
850
|
+
if (!starting) {
|
|
851
|
+
_context8.n = 7;
|
|
852
|
+
break;
|
|
853
|
+
}
|
|
854
|
+
_context8.p = 4;
|
|
855
|
+
_context8.n = 5;
|
|
856
|
+
return starting;
|
|
857
|
+
case 5:
|
|
858
|
+
_context8.v.terminate();
|
|
859
|
+
_context8.n = 7;
|
|
860
|
+
break;
|
|
861
|
+
case 6:
|
|
862
|
+
_context8.p = 6;
|
|
863
|
+
_t7 = _context8.v;
|
|
864
|
+
case 7:
|
|
865
|
+
return _context8.a(2);
|
|
866
|
+
}
|
|
867
|
+
}, _callee8, null, [[4, 6]]);
|
|
868
|
+
}));
|
|
869
|
+
return instance;
|
|
870
|
+
};
|
|
871
|
+
var shared = null;
|
|
872
|
+
var sharedWorker = function sharedWorker() {
|
|
873
|
+
return shared = shared || createWorker();
|
|
874
|
+
};
|
|
875
|
+
|
|
876
|
+
// Stops the shared worker, rejecting calls still waiting. The next call starts a fresh one.
|
|
877
|
+
export var terminate = /*#__PURE__*/function () {
|
|
878
|
+
var _ref7 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee9() {
|
|
879
|
+
var stopping;
|
|
880
|
+
return _regenerator().w(function (_context9) {
|
|
881
|
+
while (1) switch (_context9.n) {
|
|
882
|
+
case 0:
|
|
883
|
+
if (shared) {
|
|
884
|
+
_context9.n = 1;
|
|
885
|
+
break;
|
|
886
|
+
}
|
|
887
|
+
return _context9.a(2);
|
|
888
|
+
case 1:
|
|
889
|
+
stopping = shared;
|
|
890
|
+
shared = null;
|
|
891
|
+
_context9.n = 2;
|
|
892
|
+
return stopping.terminate();
|
|
893
|
+
case 2:
|
|
894
|
+
return _context9.a(2);
|
|
895
|
+
}
|
|
896
|
+
}, _callee9);
|
|
897
|
+
}));
|
|
898
|
+
return function terminate() {
|
|
899
|
+
return _ref7.apply(this, arguments);
|
|
900
|
+
};
|
|
901
|
+
}();
|
|
902
|
+
export var init = function init() {
|
|
903
|
+
var _sharedWorker;
|
|
904
|
+
return (_sharedWorker = sharedWorker()).init.apply(_sharedWorker, arguments);
|
|
905
|
+
};
|
|
906
|
+
export var getClustering = function getClustering() {
|
|
907
|
+
var _sharedWorker2;
|
|
908
|
+
return (_sharedWorker2 = sharedWorker()).getClustering.apply(_sharedWorker2, arguments);
|
|
909
|
+
};
|
|
910
|
+
export var getPalette = function getPalette() {
|
|
911
|
+
var _sharedWorker3;
|
|
912
|
+
return (_sharedWorker3 = sharedWorker()).getPalette.apply(_sharedWorker3, arguments);
|
|
913
|
+
};
|
|
914
|
+
export var getPaletteFromClustering = function getPaletteFromClustering() {
|
|
915
|
+
var _sharedWorker4;
|
|
916
|
+
return (_sharedWorker4 = sharedWorker()).getPaletteFromClustering.apply(_sharedWorker4, arguments);
|
|
917
|
+
};
|
|
918
|
+
export var getSwatches = function getSwatches() {
|
|
919
|
+
var _sharedWorker5;
|
|
920
|
+
return (_sharedWorker5 = sharedWorker()).getSwatches.apply(_sharedWorker5, arguments);
|
|
921
|
+
};
|
|
922
|
+
export var getSwatchesFromClustering = function getSwatchesFromClustering() {
|
|
923
|
+
var _sharedWorker6;
|
|
924
|
+
return (_sharedWorker6 = sharedWorker()).getSwatchesFromClustering.apply(_sharedWorker6, arguments);
|
|
925
|
+
};
|
|
926
|
+
export var getClusteringInfo = function getClusteringInfo() {
|
|
927
|
+
var _sharedWorker7;
|
|
928
|
+
return (_sharedWorker7 = sharedWorker()).getClusteringInfo.apply(_sharedWorker7, arguments);
|
|
929
|
+
};
|
|
930
|
+
export var quantize = function quantize() {
|
|
931
|
+
var _sharedWorker8;
|
|
932
|
+
return (_sharedWorker8 = sharedWorker()).quantize.apply(_sharedWorker8, arguments);
|
|
933
|
+
};
|
|
934
|
+
export var quantizeFromClustering = function quantizeFromClustering() {
|
|
935
|
+
var _sharedWorker9;
|
|
936
|
+
return (_sharedWorker9 = sharedWorker()).quantizeFromClustering.apply(_sharedWorker9, arguments);
|
|
937
|
+
};
|
|
938
|
+
export var quantizeWithPalette = function quantizeWithPalette() {
|
|
939
|
+
var _sharedWorker0;
|
|
940
|
+
return (_sharedWorker0 = sharedWorker()).quantizeWithPalette.apply(_sharedWorker0, arguments);
|
|
941
|
+
};
|
|
942
|
+
export var quantizeToIndices = function quantizeToIndices() {
|
|
943
|
+
var _sharedWorker1;
|
|
944
|
+
return (_sharedWorker1 = sharedWorker()).quantizeToIndices.apply(_sharedWorker1, arguments);
|
|
945
|
+
};
|
|
946
|
+
export var quantizeToIndicesFromClustering = function quantizeToIndicesFromClustering() {
|
|
947
|
+
var _sharedWorker10;
|
|
948
|
+
return (_sharedWorker10 = sharedWorker()).quantizeToIndicesFromClustering.apply(_sharedWorker10, arguments);
|
|
949
|
+
};
|
|
950
|
+
export var quantizeToIndicesWithPalette = function quantizeToIndicesWithPalette() {
|
|
951
|
+
var _sharedWorker11;
|
|
952
|
+
return (_sharedWorker11 = sharedWorker()).quantizeToIndicesWithPalette.apply(_sharedWorker11, arguments);
|
|
953
|
+
};
|