agglomerative-clustering 3.0.0-beta.1 → 3.0.0-beta.3
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 +192 -49
- package/dist/clustering.js +1 -1
- package/dist/clustering.wasm +0 -0
- package/dist/index.cjs +649 -830
- package/dist/index.d.cts +16 -15
- package/dist/index.d.ts +16 -15
- package/dist/index.mjs +633 -828
- package/dist/shared.cjs +202 -132
- package/dist/shared.mjs +184 -130
- package/dist/worker-thread.mjs +115 -349
- package/dist/worker.d.ts +2 -2
- package/dist/worker.mjs +480 -646
- package/package.json +4 -1
package/dist/worker-thread.mjs
CHANGED
|
@@ -1,32 +1,12 @@
|
|
|
1
|
-
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
|
|
2
|
-
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."); }
|
|
3
|
-
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
|
|
4
|
-
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
|
|
5
|
-
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); }
|
|
6
|
-
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
|
|
7
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
8
|
-
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; } }
|
|
9
|
-
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; }
|
|
10
|
-
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
11
|
-
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
|
|
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 ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
15
|
-
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
16
|
-
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
17
|
-
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
18
|
-
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
19
|
-
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); }
|
|
20
|
-
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); }); }; }
|
|
21
1
|
// Runs inside the worker started by worker.js: receives calls, runs them with the main entry, and posts results
|
|
22
2
|
// back, transferring every result buffer. Imports `./index.mjs`, which exists only in dist/ (like clustering.js).
|
|
23
3
|
import * as ac from './index.mjs';
|
|
24
4
|
import { MARK, mark, revive, isPalette } from "./shared.mjs";
|
|
25
5
|
|
|
26
6
|
// Images kept in the worker by `cluster`, with their clusterings, by handle id.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
7
|
+
const handles = new Map();
|
|
8
|
+
const handle = id => {
|
|
9
|
+
const entry = handles.get(id);
|
|
30
10
|
if (!entry) {
|
|
31
11
|
throw Object.assign(new Error("Invalid handle: it was released, or belongs to another worker."), {
|
|
32
12
|
code: 'invalid-handle'
|
|
@@ -38,242 +18,64 @@ var handle = function handle(id) {
|
|
|
38
18
|
// A palette doesn't say which colour space it was made in. A handle's palette almost always came from the handle itself,
|
|
39
19
|
// so mapping with it should match in the space the handle was clustered in, as handle.quantize does, unless the caller
|
|
40
20
|
// names one. Options that aren't an ordinary object are passed on as they are for the main entry to reject.
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
break;
|
|
56
|
-
}
|
|
57
|
-
return _context.a(2, options);
|
|
58
|
-
case 2:
|
|
59
|
-
_context.n = 3;
|
|
60
|
-
return ac.getClusteringInfo(handle(id).clustering);
|
|
61
|
-
case 3:
|
|
62
|
-
_yield$ac$getClusteri = _context.v;
|
|
63
|
-
colorSpace = _yield$ac$getClusteri.colorSpace;
|
|
64
|
-
return _context.a(2, _objectSpread(_objectSpread({}, options), {}, {
|
|
65
|
-
colorSpace: options && options.colorSpace !== undefined ? options.colorSpace : colorSpace
|
|
66
|
-
}));
|
|
67
|
-
}
|
|
68
|
-
}, _callee);
|
|
69
|
-
}));
|
|
70
|
-
return function inClusteringSpace(_x, _x2, _x3) {
|
|
71
|
-
return _ref.apply(this, arguments);
|
|
21
|
+
const inClusteringSpace = async (id, options, palette) => {
|
|
22
|
+
if (options !== undefined && Object.prototype.toString.call(options) !== '[object Object]') {
|
|
23
|
+
return options;
|
|
24
|
+
}
|
|
25
|
+
// A Palette knows the space it was made in; the main entry uses that unless one is named.
|
|
26
|
+
if (isPalette(palette) && !(options && options.colorSpace !== undefined)) {
|
|
27
|
+
return options;
|
|
28
|
+
}
|
|
29
|
+
const {
|
|
30
|
+
colorSpace
|
|
31
|
+
} = await ac.getClusteringInfo(handle(id).clustering);
|
|
32
|
+
return {
|
|
33
|
+
...options,
|
|
34
|
+
colorSpace: options && options.colorSpace !== undefined ? options.colorSpace : colorSpace
|
|
72
35
|
};
|
|
73
|
-
}
|
|
36
|
+
};
|
|
74
37
|
|
|
75
38
|
// Palettes and clusterings cross the boundary as markers (shared.js): arguments are revived here, and results are marked
|
|
76
39
|
// before they are posted.
|
|
77
40
|
// The functions of the main entry, by name (not its classes and constants).
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
image: _t3,
|
|
100
|
-
clustering: _t4
|
|
101
|
-
};
|
|
102
|
-
_t.set.call(_t, _t2, _t5);
|
|
103
|
-
case 2:
|
|
104
|
-
return _context2.a(2);
|
|
105
|
-
}
|
|
106
|
-
}, _callee2);
|
|
107
|
-
}));
|
|
108
|
-
function cluster(_x4, _x5, _x6) {
|
|
109
|
-
return _cluster.apply(this, arguments);
|
|
110
|
-
}
|
|
111
|
-
return cluster;
|
|
112
|
-
}(),
|
|
113
|
-
release: function () {
|
|
114
|
-
var _release = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(id) {
|
|
115
|
-
return _regenerator().w(function (_context3) {
|
|
116
|
-
while (1) switch (_context3.n) {
|
|
117
|
-
case 0:
|
|
118
|
-
handles["delete"](id);
|
|
119
|
-
case 1:
|
|
120
|
-
return _context3.a(2);
|
|
121
|
-
}
|
|
122
|
-
}, _callee3);
|
|
123
|
-
}));
|
|
124
|
-
function release(_x7) {
|
|
125
|
-
return _release.apply(this, arguments);
|
|
126
|
-
}
|
|
127
|
-
return release;
|
|
128
|
-
}(),
|
|
129
|
-
handleGetClustering: function () {
|
|
130
|
-
var _handleGetClustering = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(id) {
|
|
131
|
-
return _regenerator().w(function (_context4) {
|
|
132
|
-
while (1) switch (_context4.n) {
|
|
133
|
-
case 0:
|
|
134
|
-
return _context4.a(2, new ac.Clustering(handle(id).clustering));
|
|
135
|
-
}
|
|
136
|
-
}, _callee4);
|
|
137
|
-
}));
|
|
138
|
-
function handleGetClustering(_x8) {
|
|
139
|
-
return _handleGetClustering.apply(this, arguments);
|
|
140
|
-
}
|
|
141
|
-
return handleGetClustering;
|
|
142
|
-
}(),
|
|
143
|
-
handleGetPalette: function () {
|
|
144
|
-
var _handleGetPalette = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(id, k) {
|
|
145
|
-
return _regenerator().w(function (_context5) {
|
|
146
|
-
while (1) switch (_context5.n) {
|
|
147
|
-
case 0:
|
|
148
|
-
return _context5.a(2, ac.getPaletteFromClustering(handle(id).clustering, k));
|
|
149
|
-
}
|
|
150
|
-
}, _callee5);
|
|
151
|
-
}));
|
|
152
|
-
function handleGetPalette(_x9, _x0) {
|
|
153
|
-
return _handleGetPalette.apply(this, arguments);
|
|
154
|
-
}
|
|
155
|
-
return handleGetPalette;
|
|
156
|
-
}(),
|
|
157
|
-
handleGetSwatches: function () {
|
|
158
|
-
var _handleGetSwatches = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(id, k) {
|
|
159
|
-
return _regenerator().w(function (_context6) {
|
|
160
|
-
while (1) switch (_context6.n) {
|
|
161
|
-
case 0:
|
|
162
|
-
return _context6.a(2, ac.getSwatchesFromClustering(handle(id).clustering, k));
|
|
163
|
-
}
|
|
164
|
-
}, _callee6);
|
|
165
|
-
}));
|
|
166
|
-
function handleGetSwatches(_x1, _x10) {
|
|
167
|
-
return _handleGetSwatches.apply(this, arguments);
|
|
168
|
-
}
|
|
169
|
-
return handleGetSwatches;
|
|
170
|
-
}(),
|
|
171
|
-
handleGetClusteringInfo: function () {
|
|
172
|
-
var _handleGetClusteringInfo = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(id) {
|
|
173
|
-
return _regenerator().w(function (_context7) {
|
|
174
|
-
while (1) switch (_context7.n) {
|
|
175
|
-
case 0:
|
|
176
|
-
return _context7.a(2, ac.getClusteringInfo(handle(id).clustering));
|
|
177
|
-
}
|
|
178
|
-
}, _callee7);
|
|
179
|
-
}));
|
|
180
|
-
function handleGetClusteringInfo(_x11) {
|
|
181
|
-
return _handleGetClusteringInfo.apply(this, arguments);
|
|
182
|
-
}
|
|
183
|
-
return handleGetClusteringInfo;
|
|
184
|
-
}(),
|
|
185
|
-
handleQuantize: function () {
|
|
186
|
-
var _handleQuantize = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8(id, k, options) {
|
|
187
|
-
return _regenerator().w(function (_context8) {
|
|
188
|
-
while (1) switch (_context8.n) {
|
|
189
|
-
case 0:
|
|
190
|
-
return _context8.a(2, ac.quantizeFromClustering(handle(id).image, handle(id).clustering, k, options));
|
|
191
|
-
}
|
|
192
|
-
}, _callee8);
|
|
193
|
-
}));
|
|
194
|
-
function handleQuantize(_x12, _x13, _x14) {
|
|
195
|
-
return _handleQuantize.apply(this, arguments);
|
|
196
|
-
}
|
|
197
|
-
return handleQuantize;
|
|
198
|
-
}(),
|
|
199
|
-
handleQuantizeToIndices: function () {
|
|
200
|
-
var _handleQuantizeToIndices = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee9(id, k, options) {
|
|
201
|
-
return _regenerator().w(function (_context9) {
|
|
202
|
-
while (1) switch (_context9.n) {
|
|
203
|
-
case 0:
|
|
204
|
-
return _context9.a(2, ac.quantizeToIndicesFromClustering(handle(id).image, handle(id).clustering, k, options));
|
|
205
|
-
}
|
|
206
|
-
}, _callee9);
|
|
207
|
-
}));
|
|
208
|
-
function handleQuantizeToIndices(_x15, _x16, _x17) {
|
|
209
|
-
return _handleQuantizeToIndices.apply(this, arguments);
|
|
210
|
-
}
|
|
211
|
-
return handleQuantizeToIndices;
|
|
212
|
-
}(),
|
|
213
|
-
handleQuantizeWithPalette: function () {
|
|
214
|
-
var _handleQuantizeWithPalette = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee0(id, palette, options) {
|
|
215
|
-
var _t6, _t7, _t8, _t9;
|
|
216
|
-
return _regenerator().w(function (_context0) {
|
|
217
|
-
while (1) switch (_context0.n) {
|
|
218
|
-
case 0:
|
|
219
|
-
_t6 = ac;
|
|
220
|
-
_t7 = handle(id).image;
|
|
221
|
-
_t8 = palette;
|
|
222
|
-
_context0.n = 1;
|
|
223
|
-
return inClusteringSpace(id, options, palette);
|
|
224
|
-
case 1:
|
|
225
|
-
_t9 = _context0.v;
|
|
226
|
-
return _context0.a(2, _t6.quantizeWithPalette.call(_t6, _t7, _t8, _t9));
|
|
227
|
-
}
|
|
228
|
-
}, _callee0);
|
|
229
|
-
}));
|
|
230
|
-
function handleQuantizeWithPalette(_x18, _x19, _x20) {
|
|
231
|
-
return _handleQuantizeWithPalette.apply(this, arguments);
|
|
232
|
-
}
|
|
233
|
-
return handleQuantizeWithPalette;
|
|
234
|
-
}(),
|
|
235
|
-
handleQuantizeToIndicesWithPalette: function () {
|
|
236
|
-
var _handleQuantizeToIndicesWithPalette = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee1(id, palette, options) {
|
|
237
|
-
var _t0, _t1, _t10, _t11;
|
|
238
|
-
return _regenerator().w(function (_context1) {
|
|
239
|
-
while (1) switch (_context1.n) {
|
|
240
|
-
case 0:
|
|
241
|
-
_t0 = ac;
|
|
242
|
-
_t1 = handle(id).image;
|
|
243
|
-
_t10 = palette;
|
|
244
|
-
_context1.n = 1;
|
|
245
|
-
return inClusteringSpace(id, options, palette);
|
|
246
|
-
case 1:
|
|
247
|
-
_t11 = _context1.v;
|
|
248
|
-
return _context1.a(2, _t0.quantizeToIndicesWithPalette.call(_t0, _t1, _t10, _t11));
|
|
249
|
-
}
|
|
250
|
-
}, _callee1);
|
|
251
|
-
}));
|
|
252
|
-
function handleQuantizeToIndicesWithPalette(_x21, _x22, _x23) {
|
|
253
|
-
return _handleQuantizeToIndicesWithPalette.apply(this, arguments);
|
|
254
|
-
}
|
|
255
|
-
return handleQuantizeToIndicesWithPalette;
|
|
256
|
-
}()
|
|
257
|
-
});
|
|
41
|
+
const functions = Object.fromEntries(Object.entries(ac).filter(([name, value]) => typeof value === 'function' && !/^[A-Z]/.test(name)));
|
|
42
|
+
const methods = {
|
|
43
|
+
...functions,
|
|
44
|
+
cluster: async (id, image, options) => {
|
|
45
|
+
handles.set(id, {
|
|
46
|
+
image,
|
|
47
|
+
clustering: await ac.getClustering(image, options)
|
|
48
|
+
});
|
|
49
|
+
},
|
|
50
|
+
release: async id => {
|
|
51
|
+
handles.delete(id);
|
|
52
|
+
},
|
|
53
|
+
handleGetClustering: async id => new ac.Clustering(handle(id).clustering),
|
|
54
|
+
handleGetPalette: async (id, k) => ac.getPaletteFromClustering(handle(id).clustering, k),
|
|
55
|
+
handleGetSwatches: async (id, k) => ac.getSwatchesFromClustering(handle(id).clustering, k),
|
|
56
|
+
handleGetClusteringInfo: async id => ac.getClusteringInfo(handle(id).clustering),
|
|
57
|
+
handleQuantize: async (id, k, options) => ac.quantizeFromClustering(handle(id).image, handle(id).clustering, k, options),
|
|
58
|
+
handleQuantizeToIndices: async (id, k, options) => ac.quantizeToIndicesFromClustering(handle(id).image, handle(id).clustering, k, options),
|
|
59
|
+
handleQuantizeWithPalette: async (id, palette, options) => ac.quantizeWithPalette(handle(id).image, palette, await inClusteringSpace(id, options, palette)),
|
|
60
|
+
handleQuantizeToIndicesWithPalette: async (id, palette, options) => ac.quantizeToIndicesWithPalette(handle(id).image, palette, await inClusteringSpace(id, options, palette))
|
|
61
|
+
};
|
|
258
62
|
|
|
259
63
|
// The buffers in a result, to transfer rather than copy. Results are freshly allocated, so nothing here uses them.
|
|
260
|
-
|
|
64
|
+
const buffers = value => {
|
|
261
65
|
if (ArrayBuffer.isView(value)) {
|
|
262
66
|
return [value.buffer];
|
|
263
67
|
}
|
|
264
|
-
if (value &&
|
|
68
|
+
if (value && typeof value === 'object') {
|
|
265
69
|
if (value[MARK]) {
|
|
266
70
|
return [value.bytes.buffer];
|
|
267
71
|
}
|
|
268
|
-
return Object.values(value).flatMap(
|
|
269
|
-
return ArrayBuffer.isView(part) ? [part.buffer] : part && _typeof(part) === 'object' && part[MARK] ? [part.bytes.buffer] : [];
|
|
270
|
-
});
|
|
72
|
+
return Object.values(value).flatMap(part => ArrayBuffer.isView(part) ? [part.buffer] : part && typeof part === 'object' && part[MARK] ? [part.bytes.buffer] : []);
|
|
271
73
|
}
|
|
272
74
|
return [];
|
|
273
75
|
};
|
|
274
76
|
|
|
275
77
|
// Where the options sit among each method's arguments, to give them the call's signal and progress callback.
|
|
276
|
-
|
|
78
|
+
const optionsIndex = {
|
|
277
79
|
getClustering: 1,
|
|
278
80
|
getPalette: 2,
|
|
279
81
|
getSwatches: 2,
|
|
@@ -292,104 +94,77 @@ var optionsIndex = {
|
|
|
292
94
|
|
|
293
95
|
// One AbortController per call that can be cancelled, by call id. It exists from the moment the call arrives, so a call
|
|
294
96
|
// still waiting its turn can be cancelled and never starts.
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
args = args.slice();
|
|
321
|
-
index = optionsIndex[method];
|
|
322
|
-
options = _objectSpread({}, args[index] || {});
|
|
323
|
-
if (controller) {
|
|
324
|
-
options.signal = controller.signal;
|
|
325
|
-
}
|
|
326
|
-
if (progress) {
|
|
327
|
-
options.onProgress = function (fraction) {
|
|
328
|
-
return post({
|
|
329
|
-
id: id,
|
|
330
|
-
progress: fraction
|
|
331
|
-
});
|
|
332
|
-
};
|
|
333
|
-
}
|
|
334
|
-
args[index] = options;
|
|
335
|
-
}
|
|
336
|
-
_t12 = mark;
|
|
337
|
-
_context10.n = 4;
|
|
338
|
-
return methods[method].apply(methods, _toConsumableArray(args.map(revive)));
|
|
339
|
-
case 4:
|
|
340
|
-
_t13 = _context10.v;
|
|
341
|
-
result = _t12(_t13);
|
|
342
|
-
post({
|
|
343
|
-
id: id,
|
|
344
|
-
result: result
|
|
345
|
-
}, _toConsumableArray(new Set(buffers(result))));
|
|
346
|
-
_context10.n = 7;
|
|
347
|
-
break;
|
|
348
|
-
case 5:
|
|
349
|
-
_context10.p = 5;
|
|
350
|
-
_t14 = _context10.v;
|
|
351
|
-
if (!(controller && controller.signal.aborted)) {
|
|
352
|
-
_context10.n = 6;
|
|
353
|
-
break;
|
|
354
|
-
}
|
|
355
|
-
return _context10.a(2);
|
|
356
|
-
case 6:
|
|
357
|
-
// Errors cross as plain data: the caller's side rebuilds them with the same name, message and code.
|
|
358
|
-
post({
|
|
359
|
-
id: id,
|
|
360
|
-
error: _t14 instanceof Error ? {
|
|
361
|
-
name: _t14.name,
|
|
362
|
-
message: _t14.message,
|
|
363
|
-
code: _t14.code,
|
|
364
|
-
argument: _t14.argument,
|
|
365
|
-
stack: _t14.stack
|
|
366
|
-
} : {
|
|
367
|
-
name: 'Error',
|
|
368
|
-
message: String(_t14),
|
|
369
|
-
code: 'worker-failed'
|
|
370
|
-
}
|
|
371
|
-
});
|
|
372
|
-
case 7:
|
|
373
|
-
_context10.p = 7;
|
|
374
|
-
controllers["delete"](id);
|
|
375
|
-
return _context10.f(7);
|
|
376
|
-
case 8:
|
|
377
|
-
return _context10.a(2);
|
|
97
|
+
const controllers = new Map();
|
|
98
|
+
const respond = async ({
|
|
99
|
+
id,
|
|
100
|
+
method,
|
|
101
|
+
args,
|
|
102
|
+
progress
|
|
103
|
+
}, post) => {
|
|
104
|
+
const controller = controllers.get(id);
|
|
105
|
+
try {
|
|
106
|
+
if (controller && controller.signal.aborted) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (!Object.hasOwn(methods, method)) {
|
|
110
|
+
throw Object.assign(new Error(`Unknown method '${method}'.`), {
|
|
111
|
+
code: 'invalid-argument'
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
if (controller || progress) {
|
|
115
|
+
args = args.slice();
|
|
116
|
+
const index = optionsIndex[method];
|
|
117
|
+
const options = {
|
|
118
|
+
...(args[index] || {})
|
|
119
|
+
};
|
|
120
|
+
if (controller) {
|
|
121
|
+
options.signal = controller.signal;
|
|
378
122
|
}
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
}
|
|
123
|
+
if (progress) {
|
|
124
|
+
options.onProgress = fraction => post({
|
|
125
|
+
id,
|
|
126
|
+
progress: fraction
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
args[index] = options;
|
|
130
|
+
}
|
|
131
|
+
const result = mark(await methods[method](...args.map(revive)));
|
|
132
|
+
post({
|
|
133
|
+
id,
|
|
134
|
+
result
|
|
135
|
+
}, [...new Set(buffers(result))]);
|
|
136
|
+
} catch (error) {
|
|
137
|
+
// A cancelled call has already been rejected on the other side.
|
|
138
|
+
if (controller && controller.signal.aborted) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
// Errors cross as plain data: the caller's side rebuilds them with the same name, message and code.
|
|
142
|
+
post({
|
|
143
|
+
id,
|
|
144
|
+
error: error instanceof Error ? {
|
|
145
|
+
name: error.name,
|
|
146
|
+
message: error.message,
|
|
147
|
+
code: error.code,
|
|
148
|
+
argument: error.argument,
|
|
149
|
+
stack: error.stack
|
|
150
|
+
} : {
|
|
151
|
+
name: 'Error',
|
|
152
|
+
message: String(error),
|
|
153
|
+
code: 'worker-failed'
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
} finally {
|
|
157
|
+
controllers.delete(id);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
385
160
|
|
|
386
161
|
// Calls run one at a time, in the order they arrived. A call that takes several turns of the event loop (a clustering run
|
|
387
162
|
// in slices) lets messages in between, which is how a cancel reaches it; without the queue, later calls would run
|
|
388
163
|
// in between its slices instead of after it.
|
|
389
|
-
|
|
390
|
-
|
|
164
|
+
let queue = Promise.resolve();
|
|
165
|
+
const onMessage = (data, post) => {
|
|
391
166
|
if (data.cancel !== undefined) {
|
|
392
|
-
|
|
167
|
+
const controller = controllers.get(data.cancel);
|
|
393
168
|
if (controller) {
|
|
394
169
|
controller.abort();
|
|
395
170
|
}
|
|
@@ -400,30 +175,21 @@ var onMessage = function onMessage(data, post) {
|
|
|
400
175
|
}
|
|
401
176
|
// respond() answers every failure itself; this catch only keeps one unforeseen rejection (an error that can't be posted)
|
|
402
177
|
// from leaving the queue rejected, which would silently stop the thread from running any later call.
|
|
403
|
-
queue = queue.then(
|
|
404
|
-
return respond(data, post);
|
|
405
|
-
})["catch"](function () {});
|
|
178
|
+
queue = queue.then(() => respond(data, post)).catch(() => {});
|
|
406
179
|
};
|
|
407
180
|
|
|
408
181
|
// No top-level await: bundlers build workers as classic scripts by default (Vite's 'iife'), which can't have it.
|
|
409
182
|
// Decided the way worker.js decides how it started this thread: where Node's APIs exist (Node, Deno, Bun), the thread was
|
|
410
183
|
// started through worker_threads and talks through parentPort. Bun also defines the browser's `self.postMessage`
|
|
411
184
|
// inside such a thread, so checking `self` first would listen on a channel the parent never writes to.
|
|
412
|
-
|
|
185
|
+
const isNode = typeof process !== 'undefined' && !!(process.versions && process.versions.node);
|
|
413
186
|
if (!isNode && typeof self !== 'undefined' && typeof self.postMessage === 'function' && typeof self.addEventListener === 'function') {
|
|
414
|
-
self.addEventListener('message',
|
|
415
|
-
return onMessage(event.data, function (message, transfer) {
|
|
416
|
-
return self.postMessage(message, transfer);
|
|
417
|
-
});
|
|
418
|
-
});
|
|
187
|
+
self.addEventListener('message', event => onMessage(event.data, (message, transfer) => self.postMessage(message, transfer)));
|
|
419
188
|
} else {
|
|
420
189
|
// Built at run time so no bundler tries to resolve it (see worker.js).
|
|
421
|
-
import(/* webpackIgnore: true */ /* @vite-ignore */['node', 'worker_threads'].join(':')).then(
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
return parentPort.postMessage(message, transfer);
|
|
426
|
-
});
|
|
427
|
-
});
|
|
190
|
+
import(/* webpackIgnore: true */ /* @vite-ignore */['node', 'worker_threads'].join(':')).then(({
|
|
191
|
+
parentPort
|
|
192
|
+
}) => {
|
|
193
|
+
parentPort.on('message', data => onMessage(data, (message, transfer) => parentPort.postMessage(message, transfer)));
|
|
428
194
|
});
|
|
429
195
|
}
|
package/dist/worker.d.ts
CHANGED
|
@@ -33,9 +33,9 @@ export interface WorkerOptions {
|
|
|
33
33
|
* are a whole buffer of their own are transferred: a view into part of a larger buffer (a subarray, a small Node
|
|
34
34
|
* Buffer) is copied, so the caller's other data in that buffer is never detached.
|
|
35
35
|
*/
|
|
36
|
-
transfer?: boolean;
|
|
36
|
+
transfer?: boolean | undefined;
|
|
37
37
|
/** Where the worker gets its WebAssembly module; see `InitOptions`. It is sent as the worker's first call. */
|
|
38
|
-
wasm?: InitOptions['wasm'];
|
|
38
|
+
wasm?: InitOptions['wasm'] | undefined;
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
/** An image and its clustering, kept in the worker so re-cutting and re-mapping them copies nothing across. */
|