billboard.js 4.0.3 → 4.1.0-next.2

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.
Files changed (271) hide show
  1. package/README.md +82 -7
  2. package/dist/billboard.css +1 -1
  3. package/dist/billboard.js +1848 -494
  4. package/dist/billboard.min.css +1 -1
  5. package/dist/billboard.min.js +19 -24
  6. package/dist/billboard.pkgd.js +1844 -679
  7. package/dist/billboard.pkgd.min.js +24 -29
  8. package/dist/billboard.react.js +205 -0
  9. package/dist/billboard.react.min.js +9 -0
  10. package/dist/billboard.worker.js +10 -0
  11. package/dist/plugin/billboardjs-plugin-bubblecompare.js +5 -3
  12. package/dist/plugin/billboardjs-plugin-bubblecompare.min.js +2 -2
  13. package/dist/plugin/billboardjs-plugin-sparkline.js +6 -3
  14. package/dist/plugin/billboardjs-plugin-sparkline.min.js +3 -3
  15. package/dist/plugin/billboardjs-plugin-stanford.js +6 -3
  16. package/dist/plugin/billboardjs-plugin-stanford.min.js +3 -3
  17. package/dist/plugin/billboardjs-plugin-tableview.js +59 -27
  18. package/dist/plugin/billboardjs-plugin-tableview.min.js +5 -5
  19. package/dist/plugin/billboardjs-plugin-textoverlap.js +75 -55
  20. package/dist/plugin/billboardjs-plugin-textoverlap.min.js +2 -2
  21. package/dist/plugin/pkgd/billboardjs-plugin-bubblecompare.pkgd.js +5 -3
  22. package/dist/plugin/pkgd/billboardjs-plugin-bubblecompare.pkgd.min.js +2 -2
  23. package/dist/plugin/pkgd/billboardjs-plugin-sparkline.pkgd.js +6 -3
  24. package/dist/plugin/pkgd/billboardjs-plugin-sparkline.pkgd.min.js +3 -3
  25. package/dist/plugin/pkgd/billboardjs-plugin-stanford.pkgd.js +6 -3
  26. package/dist/plugin/pkgd/billboardjs-plugin-stanford.pkgd.min.js +2 -2
  27. package/dist/plugin/pkgd/billboardjs-plugin-tableview.pkgd.js +59 -27
  28. package/dist/plugin/pkgd/billboardjs-plugin-tableview.pkgd.min.js +11 -11
  29. package/dist/plugin/pkgd/billboardjs-plugin-textoverlap.pkgd.js +68 -3557
  30. package/dist/plugin/pkgd/billboardjs-plugin-textoverlap.pkgd.min.js +8 -8
  31. package/dist/theme/dark.css +1 -1
  32. package/dist/theme/dark.min.css +1 -1
  33. package/dist/theme/datalab.css +1 -1
  34. package/dist/theme/datalab.min.css +1 -1
  35. package/dist/theme/graph.css +1 -1
  36. package/dist/theme/graph.min.css +1 -1
  37. package/dist/theme/insight.css +1 -1
  38. package/dist/theme/insight.min.css +1 -1
  39. package/dist/theme/modern.css +1 -1
  40. package/dist/theme/modern.min.css +1 -1
  41. package/dist-esm/Chart/Chart.js +134 -152
  42. package/dist-esm/Chart/api/axis.js +217 -237
  43. package/dist-esm/Chart/api/category.js +54 -54
  44. package/dist-esm/Chart/api/chart.js +172 -215
  45. package/dist-esm/Chart/api/color.js +21 -21
  46. package/dist-esm/Chart/api/data.js +172 -173
  47. package/dist-esm/Chart/api/export.js +242 -262
  48. package/dist-esm/Chart/api/flow.js +198 -254
  49. package/dist-esm/Chart/api/focus.js +136 -143
  50. package/dist-esm/Chart/api/grid.js +191 -190
  51. package/dist-esm/Chart/api/group.js +31 -34
  52. package/dist-esm/Chart/api/legend.js +54 -63
  53. package/dist-esm/Chart/api/load.js +237 -257
  54. package/dist-esm/Chart/api/regions.js +148 -168
  55. package/dist-esm/Chart/api/selection.js +181 -139
  56. package/dist-esm/Chart/api/show.js +133 -147
  57. package/dist-esm/Chart/api/stubs.js +43 -42
  58. package/dist-esm/Chart/api/subchart.js +155 -172
  59. package/dist-esm/Chart/api/tooltip.js +132 -177
  60. package/dist-esm/Chart/api/x.js +67 -71
  61. package/dist-esm/Chart/api/zoom.js +183 -220
  62. package/dist-esm/ChartInternal/Axis/Axis.js +1059 -1266
  63. package/dist-esm/ChartInternal/Axis/AxisRenderer.js +408 -540
  64. package/dist-esm/ChartInternal/Axis/AxisRendererHelper.js +113 -149
  65. package/dist-esm/ChartInternal/ChartInternal.js +573 -749
  66. package/dist-esm/ChartInternal/data/convert.helper.js +133 -159
  67. package/dist-esm/ChartInternal/data/convert.js +200 -229
  68. package/dist-esm/ChartInternal/data/data.js +967 -1153
  69. package/dist-esm/ChartInternal/data/load.js +145 -197
  70. package/dist-esm/ChartInternal/interactions/drag.js +86 -122
  71. package/dist-esm/ChartInternal/interactions/eventrect.js +455 -621
  72. package/dist-esm/ChartInternal/interactions/flow.js +151 -215
  73. package/dist-esm/ChartInternal/interactions/interaction.js +214 -252
  74. package/dist-esm/ChartInternal/interactions/subchart.js +425 -300
  75. package/dist-esm/ChartInternal/interactions/zoom.js +321 -440
  76. package/dist-esm/ChartInternal/internals/category.js +18 -18
  77. package/dist-esm/ChartInternal/internals/class.js +89 -99
  78. package/dist-esm/ChartInternal/internals/clip.js +88 -119
  79. package/dist-esm/ChartInternal/internals/color.js +233 -288
  80. package/dist-esm/ChartInternal/internals/domain.js +452 -588
  81. package/dist-esm/ChartInternal/internals/format.js +64 -77
  82. package/dist-esm/ChartInternal/internals/grid.js +333 -457
  83. package/dist-esm/ChartInternal/internals/legend.js +675 -927
  84. package/dist-esm/ChartInternal/internals/redraw.js +200 -289
  85. package/dist-esm/ChartInternal/internals/region.js +106 -159
  86. package/dist-esm/ChartInternal/internals/scale.js +186 -215
  87. package/dist-esm/ChartInternal/internals/selection.js +132 -159
  88. package/dist-esm/ChartInternal/internals/size.axis.js +92 -139
  89. package/dist-esm/ChartInternal/internals/size.js +286 -394
  90. package/dist-esm/ChartInternal/internals/style.js +39 -41
  91. package/dist-esm/ChartInternal/internals/subchart.util.js +49 -0
  92. package/dist-esm/ChartInternal/internals/text.arc.js +155 -219
  93. package/dist-esm/ChartInternal/internals/text.js +368 -528
  94. package/dist-esm/ChartInternal/internals/text.util.js +179 -239
  95. package/dist-esm/ChartInternal/internals/title.js +96 -121
  96. package/dist-esm/ChartInternal/internals/tooltip.js +450 -586
  97. package/dist-esm/ChartInternal/internals/transform.js +79 -97
  98. package/dist-esm/ChartInternal/internals/type.js +285 -236
  99. package/dist-esm/ChartInternal/shape/arc.js +825 -1112
  100. package/dist-esm/ChartInternal/shape/area.js +73 -94
  101. package/dist-esm/ChartInternal/shape/bar.js +227 -288
  102. package/dist-esm/ChartInternal/shape/bubble.js +9 -8
  103. package/dist-esm/ChartInternal/shape/candlestick.js +110 -138
  104. package/dist-esm/ChartInternal/shape/core/barRadius.js +197 -133
  105. package/dist-esm/ChartInternal/shape/core/bubble.js +64 -73
  106. package/dist-esm/ChartInternal/shape/core/candlestick.js +93 -86
  107. package/dist-esm/ChartInternal/shape/core/dataRegion.js +68 -75
  108. package/dist-esm/ChartInternal/shape/core/geometry.js +112 -112
  109. package/dist-esm/ChartInternal/shape/core/path.js +142 -115
  110. package/dist-esm/ChartInternal/shape/core/point.js +105 -125
  111. package/dist-esm/ChartInternal/shape/core/treemap.js +83 -88
  112. package/dist-esm/ChartInternal/shape/funnel.js +310 -338
  113. package/dist-esm/ChartInternal/shape/gauge.js +71 -103
  114. package/dist-esm/ChartInternal/shape/line.js +208 -278
  115. package/dist-esm/ChartInternal/shape/point.js +310 -425
  116. package/dist-esm/ChartInternal/shape/polar.js +84 -109
  117. package/dist-esm/ChartInternal/shape/radar.js +228 -318
  118. package/dist-esm/ChartInternal/shape/shape.js +782 -843
  119. package/dist-esm/ChartInternal/shape/treemap.js +152 -190
  120. package/dist-esm/canvas/CanvasAxisRenderer.js +1180 -1328
  121. package/dist-esm/canvas/CanvasEngine.js +148 -166
  122. package/dist-esm/canvas/CanvasPainter.js +395 -411
  123. package/dist-esm/canvas/CanvasRenderer.js +1584 -1834
  124. package/dist-esm/canvas/CanvasTheme.js +899 -802
  125. package/dist-esm/canvas/HitDetector.js +394 -463
  126. package/dist-esm/canvas/axisTicks.js +672 -786
  127. package/dist-esm/canvas/classes.js +19 -17
  128. package/dist-esm/canvas/color.js +40 -59
  129. package/dist-esm/canvas/geometry.js +38 -38
  130. package/dist-esm/canvas/labels.js +318 -371
  131. package/dist-esm/canvas/pointPattern.js +578 -580
  132. package/dist-esm/canvas/util.js +242 -262
  133. package/dist-esm/config/Options/Options.js +29 -32
  134. package/dist-esm/config/Options/axis/axis.js +101 -101
  135. package/dist-esm/config/Options/axis/x.js +759 -755
  136. package/dist-esm/config/Options/axis/y.js +525 -522
  137. package/dist-esm/config/Options/axis/y2.js +501 -485
  138. package/dist-esm/config/Options/common/boost.js +93 -32
  139. package/dist-esm/config/Options/common/color.js +88 -87
  140. package/dist-esm/config/Options/common/grid.js +74 -73
  141. package/dist-esm/config/Options/common/legend.js +180 -179
  142. package/dist-esm/config/Options/common/main.js +474 -473
  143. package/dist-esm/config/Options/common/point.js +160 -159
  144. package/dist-esm/config/Options/common/title.js +47 -46
  145. package/dist-esm/config/Options/common/tooltip.js +253 -252
  146. package/dist-esm/config/Options/data/axis.js +173 -172
  147. package/dist-esm/config/Options/data/data.js +958 -957
  148. package/dist-esm/config/Options/data/selection.js +131 -130
  149. package/dist-esm/config/Options/interaction/interaction.js +53 -52
  150. package/dist-esm/config/Options/interaction/subchart.js +164 -73
  151. package/dist-esm/config/Options/interaction/zoom.js +86 -85
  152. package/dist-esm/config/Options/shape/arc.js +150 -149
  153. package/dist-esm/config/Options/shape/area.js +69 -68
  154. package/dist-esm/config/Options/shape/bar.js +147 -146
  155. package/dist-esm/config/Options/shape/bubble.js +35 -34
  156. package/dist-esm/config/Options/shape/candlestick.js +61 -60
  157. package/dist-esm/config/Options/shape/donut.js +181 -180
  158. package/dist-esm/config/Options/shape/funnel.js +49 -48
  159. package/dist-esm/config/Options/shape/gauge.js +237 -236
  160. package/dist-esm/config/Options/shape/line.js +58 -57
  161. package/dist-esm/config/Options/shape/pie.js +185 -184
  162. package/dist-esm/config/Options/shape/polar.js +167 -166
  163. package/dist-esm/config/Options/shape/radar.js +76 -75
  164. package/dist-esm/config/Options/shape/scatter.js +36 -36
  165. package/dist-esm/config/Options/shape/spline.js +46 -46
  166. package/dist-esm/config/Options/shape/treemap.js +80 -79
  167. package/dist-esm/config/Store/Element.js +64 -62
  168. package/dist-esm/config/Store/State.js +189 -200
  169. package/dist-esm/config/Store/Store.js +25 -26
  170. package/dist-esm/config/classes.js +157 -155
  171. package/dist-esm/config/config.js +26 -35
  172. package/dist-esm/config/const.js +120 -128
  173. package/dist-esm/config/resolver/axis.core.js +25 -25
  174. package/dist-esm/config/resolver/axis.js +26 -27
  175. package/dist-esm/config/resolver/canvas.js +2071 -2472
  176. package/dist-esm/config/resolver/canvasShape.js +201 -177
  177. package/dist-esm/config/resolver/category.js +26 -27
  178. package/dist-esm/config/resolver/export.js +24 -26
  179. package/dist-esm/config/resolver/flow.js +29 -31
  180. package/dist-esm/config/resolver/grid.js +34 -35
  181. package/dist-esm/config/resolver/interaction/selection.js +17 -17
  182. package/dist-esm/config/resolver/interaction/subchart.js +17 -17
  183. package/dist-esm/config/resolver/interaction/zoom.js +17 -17
  184. package/dist-esm/config/resolver/regions.js +30 -31
  185. package/dist-esm/config/resolver/shape/arc.helpers.js +19 -19
  186. package/dist-esm/config/resolver/shape/area.js +17 -17
  187. package/dist-esm/config/resolver/shape/axis.helpers.js +28 -25
  188. package/dist-esm/config/resolver/shape/bar.js +13 -13
  189. package/dist-esm/config/resolver/shape/bubble.js +18 -14
  190. package/dist-esm/config/resolver/shape/candlestick.js +13 -13
  191. package/dist-esm/config/resolver/shape/donut.js +11 -11
  192. package/dist-esm/config/resolver/shape/funnel.js +11 -11
  193. package/dist-esm/config/resolver/shape/gauge.js +12 -12
  194. package/dist-esm/config/resolver/shape/line.js +10 -10
  195. package/dist-esm/config/resolver/shape/pie.js +11 -11
  196. package/dist-esm/config/resolver/shape/polar.js +12 -12
  197. package/dist-esm/config/resolver/shape/radar.js +22 -14
  198. package/dist-esm/config/resolver/shape/scatter.js +13 -13
  199. package/dist-esm/config/resolver/shape/treemap.js +11 -11
  200. package/dist-esm/core.js +208 -211
  201. package/dist-esm/index.canvas.js +14 -13
  202. package/dist-esm/index.esm.js +25 -24
  203. package/dist-esm/module/Cache.js +117 -129
  204. package/dist-esm/module/browser.js +30 -35
  205. package/dist-esm/module/dsv.js +107 -127
  206. package/dist-esm/module/error.js +45 -52
  207. package/dist-esm/module/generator.js +75 -83
  208. package/dist-esm/module/sanitize.js +302 -399
  209. package/dist-esm/module/util/brush.js +27 -38
  210. package/dist-esm/module/util/dom.js +288 -348
  211. package/dist-esm/module/util/object.js +277 -341
  212. package/dist-esm/module/util/type-checks.js +21 -25
  213. package/dist-esm/module/worker.js +312 -130
  214. package/dist-esm/plugin/billboardjs-plugin-bubblecompare.js +185 -198
  215. package/dist-esm/plugin/billboardjs-plugin-sparkline.js +442 -450
  216. package/dist-esm/plugin/billboardjs-plugin-stanford.js +813 -950
  217. package/dist-esm/plugin/billboardjs-plugin-tableview.js +708 -788
  218. package/dist-esm/plugin/billboardjs-plugin-textoverlap.js +344 -301
  219. package/dist-esm/react/index.js +157 -0
  220. package/package.json +28 -8
  221. package/src/Chart/api/selection.ts +130 -5
  222. package/src/ChartInternal/Axis/Axis.ts +164 -65
  223. package/src/ChartInternal/Axis/AxisRenderer.ts +41 -8
  224. package/src/ChartInternal/Axis/AxisRendererHelper.ts +1 -1
  225. package/src/ChartInternal/ChartInternal.ts +1 -1
  226. package/src/ChartInternal/data/convert.ts +54 -4
  227. package/src/ChartInternal/data/data.ts +4 -1
  228. package/src/ChartInternal/interactions/eventrect.ts +8 -2
  229. package/src/ChartInternal/interactions/subchart.ts +437 -66
  230. package/src/ChartInternal/internals/color.ts +2 -2
  231. package/src/ChartInternal/internals/domain.ts +15 -3
  232. package/src/ChartInternal/internals/grid.ts +21 -1
  233. package/src/ChartInternal/internals/scale.ts +9 -2
  234. package/src/ChartInternal/internals/selection.ts +1 -1
  235. package/src/ChartInternal/internals/subchart.util.ts +55 -0
  236. package/src/ChartInternal/internals/text.arc.ts +3 -1
  237. package/src/ChartInternal/internals/text.ts +18 -5
  238. package/src/ChartInternal/internals/transform.ts +8 -2
  239. package/src/ChartInternal/internals/type.ts +105 -1
  240. package/src/ChartInternal/shape/IShape.ts +6 -3
  241. package/src/ChartInternal/shape/bar.ts +62 -31
  242. package/src/ChartInternal/shape/core/barRadius.ts +129 -0
  243. package/src/ChartInternal/shape/core/candlestick.ts +1 -1
  244. package/src/ChartInternal/shape/core/path.ts +76 -9
  245. package/src/ChartInternal/shape/funnel.ts +2 -3
  246. package/src/ChartInternal/shape/point.ts +2 -2
  247. package/src/ChartInternal/shape/shape.ts +149 -17
  248. package/src/Plugin/stanford/index.ts +0 -4
  249. package/src/Plugin/tableview/index.ts +85 -3
  250. package/src/Plugin/textoverlap/index.ts +9 -15
  251. package/src/canvas/CanvasAxisRenderer.ts +184 -36
  252. package/src/canvas/CanvasRenderer.ts +247 -160
  253. package/src/canvas/CanvasTheme.ts +212 -0
  254. package/src/canvas/axisTicks.ts +122 -40
  255. package/src/config/Options/axis/y2.ts +13 -0
  256. package/src/config/Options/common/boost.ts +64 -4
  257. package/src/config/Options/common/color.ts +2 -2
  258. package/src/config/Options/interaction/subchart.ts +98 -1
  259. package/src/config/Options/shape/gauge.ts +1 -1
  260. package/src/config/Options/shape/treemap.ts +2 -2
  261. package/src/config/Store/Element.ts +3 -1
  262. package/src/config/Store/State.ts +1 -0
  263. package/src/config/classes.ts +1 -0
  264. package/src/config/resolver/canvas.ts +92 -10
  265. package/src/module/util/type-checks.ts +3 -1
  266. package/src/module/voronoi.ts +119 -0
  267. package/src/module/worker.entry.ts +61 -0
  268. package/src/module/worker.ts +346 -63
  269. package/src/react/index.ts +193 -0
  270. package/types/options.d.ts +206 -3
  271. package/types/react/index.d.ts +27 -0
@@ -5,13 +5,14 @@
5
5
  * billboard.js, JavaScript chart library
6
6
  * https://naver.github.io/billboard.js/
7
7
  *
8
- * @version 4.0.3
8
+ * @version 4.1.0-next.2
9
9
  */
10
+ //#region src/module/util/type-checks.ts
10
11
  /**
11
- * Copyright (c) 2017 ~ present NAVER Corp.
12
- * billboard.js project is licensed under the MIT license
13
- * @ignore
14
- */
12
+ * Copyright (c) 2017 ~ present NAVER Corp.
13
+ * billboard.js project is licensed under the MIT license
14
+ * @ignore
15
+ */
15
16
  const isValue = (v) => v || v === 0;
16
17
  const isFunction = (v) => typeof v === "function";
17
18
  const isString = (v) => typeof v === "string";
@@ -20,33 +21,28 @@ const isUndefined = (v) => typeof v === "undefined";
20
21
  const isDefined = (v) => typeof v !== "undefined";
21
22
  const isBoolean = (v) => typeof v === "boolean";
22
23
  const ceil10 = (v) => Math.ceil(v / 10) * 10;
23
- const asHalfPixel = (n) => Math.ceil(n) + 0.5;
24
+ const asHalfPixel = (n) => Math.ceil(n) + .5;
24
25
  const diffDomain = (d) => d[1] - d[0];
25
26
  const isObjectType = (v) => typeof v === "object";
26
27
  const isEmptyObject = (obj) => {
27
- for (const x in obj) {
28
- return false;
29
- }
30
- return true;
28
+ for (const x in obj) if (Object.prototype.hasOwnProperty.call(obj, x)) return false;
29
+ return true;
31
30
  };
32
- const isEmpty = (o) => (isUndefined(o) || o === null ||
33
- (isString(o) && o.length === 0) ||
34
- (isObjectType(o) && !(o instanceof Date) && isEmptyObject(o)) ||
35
- (isNumber(o) && isNaN(o)));
31
+ const isEmpty = (o) => isUndefined(o) || o === null || isString(o) && o.length === 0 || isObjectType(o) && !(o instanceof Date) && isEmptyObject(o) || isNumber(o) && isNaN(o);
36
32
  const notEmpty = (o) => !isEmpty(o);
37
33
  /**
38
- * Check if is array
39
- * @param {Array} arr Data to be checked
40
- * @returns {boolean}
41
- * @private
42
- */
34
+ * Check if is array
35
+ * @param {Array} arr Data to be checked
36
+ * @returns {boolean}
37
+ * @private
38
+ */
43
39
  const isArray = (arr) => Array.isArray(arr);
44
40
  /**
45
- * Check if is object
46
- * @param {object} obj Data to be checked
47
- * @returns {boolean}
48
- * @private
49
- */
41
+ * Check if is object
42
+ * @param {object} obj Data to be checked
43
+ * @returns {boolean}
44
+ * @private
45
+ */
50
46
  const isObject = (obj) => obj && !obj?.nodeType && isObjectType(obj) && !isArray(obj);
51
-
47
+ //#endregion
52
48
  export { asHalfPixel, ceil10, diffDomain, isArray, isBoolean, isDefined, isEmpty, isEmptyObject, isFunction, isNumber, isObject, isObjectType, isString, isUndefined, isValue, notEmpty };
@@ -5,148 +5,330 @@
5
5
  * billboard.js, JavaScript chart library
6
6
  * https://naver.github.io/billboard.js/
7
7
  *
8
- * @version 4.0.3
8
+ * @version 4.1.0-next.2
9
9
  */
10
- import { window as win } from './browser.js';
11
-
10
+ import { window as win } from "./browser.js";
11
+ //#region src/module/worker.ts
12
12
  /**
13
- * Copyright (c) 2017 ~ present NAVER Corp.
14
- * billboard.js project is licensed under the MIT license
15
- */
16
- // Store worker cache in memory
13
+ * Copyright (c) 2017 ~ present NAVER Corp.
14
+ * billboard.js project is licensed under the MIT license
15
+ */
16
+ /**
17
+ * The worker source constant (declared in config/globals.d.ts) is pre-bundled from
18
+ * `src/module/worker.entry.ts` and injected at build time by config/worker-src.js.
19
+ * Never name that constant outside the single reference in getWorkerSrc(): the
20
+ * rolldown injection is textual, so a mention in a comment would inline the whole
21
+ * source string a second time.
22
+ *
23
+ * Offloaded work is addressed by op name and the worker code is built
24
+ * independently of the application bundle, so no application function is ever
25
+ * stringified. Host toolchains that rewrite function bodies - coverage
26
+ * instrumentation, babel helpers, bundler-hoisted module scope - cannot break
27
+ * the worker anymore.
28
+ */
17
29
  const cache = {};
18
- // Correlation id for worker request/response matching
30
+ const disabledKeys = /* @__PURE__ */ new Set();
31
+ /**
32
+ * Parity self-test state, keyed as `${key}:${op}`: the in-flight check while it runs,
33
+ * `true` once the worker agreed with the main thread.
34
+ * @private
35
+ */
36
+ const verifiedOps = /* @__PURE__ */ new Map();
37
+ const DEFAULT_WORKER_TIMEOUT = 5e3;
38
+ /**
39
+ * Number of leading entries kept when sampling arguments for the parity self-test.
40
+ * @private
41
+ */
42
+ const VERIFY_SAMPLE_SIZE = 3;
19
43
  let messageId = 0;
20
44
  /**
21
- * Get or create cached worker resources (Object URL, Worker)
22
- * @param {function} fn Function to be executed in worker
23
- * @param {Array} depsFn Dependency functions to run given function(fn).
24
- * @returns {{key: string, src: string}} Cache key and Object URL
25
- * @private
26
- */
27
- function getOrCreateWorkerResources(fn, depsFn) {
28
- const fnString = fn.toString();
29
- // Include depsFn in cache key to handle different dependencies
30
- const depsString = depsFn?.map(String).join(";") ?? "";
31
- const key = (fnString + depsString).replace(/(function|[\s\W\n])/g, "").substring(0, 30);
32
- if (!(key in cache)) {
33
- try {
34
- // Create Blob and Object URL for Web Worker
35
- const blob = new win.Blob([
36
- `${depsString}
37
-
38
- self.onmessage=function({data}) {
39
- const result = (${fnString}).apply(null, data.args);
40
- self.postMessage({id: data.id, result});
41
- };`
42
- ], {
43
- type: "text/javascript"
44
- });
45
- cache[key] = {
46
- src: win.URL.createObjectURL(blob),
47
- worker: null
48
- };
49
- }
50
- catch {
51
- return null;
52
- }
53
- }
54
- return { key, src: cache[key].src };
45
+ * Get the build-injected worker source.
46
+ * @returns {string} Worker source, or empty string when it wasn't injected
47
+ * @private
48
+ */
49
+ function getWorkerSrc() {
50
+ try {
51
+ return "var bbWorker=(function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});function t(e){let t=[];return e.forEach(function(e,n){let r=e[0];e.forEach(function(e,i){if(i>0){if(t[i-1]===void 0&&(t[i-1]={}),e===void 0)throw Error(`Source data is missing a component at (${n}, ${i})!`);t[i-1][r]=e}})}),t}function n(e){let t=e[0],n=[];return e.forEach(function(e,r){if(r>0){let i={};e.forEach(function(e,n){if(e===void 0)throw Error(`Source data is missing a component at (${r}, ${n})!`);i[t[n]]=e}),n.push(i)}}),n}function r(e,r){let i=[],a,o;if(Array.isArray(e)){let t=function(e,t){if(e[t]!==void 0)return e[t];let n=t.replace(/\\[(\\w+)\\]/g,`.$1`).replace(/^\\./,``).split(`.`),r=e;return n.some(function(e){return!(r=r&&typeof r==`object`&&e in r?r[e]:void 0)}),r};a=r.x?r.value.concat(r.x):r.value,i.push(a),e.forEach(function(e){let n=a.map(function(n){let r=t(e,n);return r===void 0&&(r=null),r});i.push(n)}),o=n(i)}else Object.keys(e).forEach(function(t){let n=[].concat(e[t]);n.unshift?.(t),i.push(n)}),o=t(i);return o}let i={columns:t,json:r,rows:n},a=self;function o({data:e}){let{args:t,id:n,op:r}=e;try{let e=i[r];if(!e)throw Error(`Unknown worker op: ${r}`);a.postMessage({id:n,result:e(...t)})}catch(e){a.postMessage({id:n,error:e&&(e.message||e.name)||String(e)})}}return typeof window>`u`&&(a.onmessage=o),e.handleMessage=o,e.ops=i,e})({});";
52
+ } catch {
53
+ return "";
54
+ }
55
+ }
56
+ /**
57
+ * Get Web Worker related browser APIs when all required primitives are available.
58
+ * @returns {object|null} Worker API handles
59
+ * @private
60
+ */
61
+ function getWorkerAPI() {
62
+ const { Blob, Worker, URL } = win;
63
+ return Worker && Blob && URL?.createObjectURL && URL?.revokeObjectURL ? {
64
+ Blob,
65
+ Worker,
66
+ URL
67
+ } : null;
68
+ }
69
+ /**
70
+ * Get Worker constructor when available.
71
+ * @returns {function|null} Worker constructor
72
+ * @private
73
+ */
74
+ function getWorkerConstructor() {
75
+ return win.Worker || null;
76
+ }
77
+ /**
78
+ * Normalize worker options while preserving the legacy numeric timeout argument.
79
+ * @param {number|object} options Worker timeout or options
80
+ * @returns {object} Normalized worker options
81
+ * @private
82
+ */
83
+ function normalizeWorkerOptions(options) {
84
+ return typeof options === "number" ? {
85
+ timeout: options,
86
+ workerUrl: ""
87
+ } : {
88
+ timeout: options?.timeout ?? DEFAULT_WORKER_TIMEOUT,
89
+ workerUrl: options?.workerUrl ?? ""
90
+ };
91
+ }
92
+ /**
93
+ * Release cached worker resources and optionally disable this worker for the session.
94
+ * @param {string} key Cache key
95
+ * @param {boolean} disable Whether to disable future worker attempts
96
+ * @private
97
+ */
98
+ function releaseWorker(key, disable = false) {
99
+ const cached = cache[key];
100
+ if (disable) disabledKeys.add(key);
101
+ if (cached) {
102
+ cached.worker?.terminate();
103
+ cached.revoke && getWorkerAPI()?.URL.revokeObjectURL(cached.src);
104
+ delete cache[key];
105
+ }
106
+ for (const verified of verifiedOps.keys()) if (verified.startsWith(`${key}:`)) verifiedOps.delete(verified);
107
+ }
108
+ /**
109
+ * Compare worker and main-thread results for the parity self-test.
110
+ * @param {unknown} actual Worker result
111
+ * @param {unknown} expected Main-thread result
112
+ * @returns {boolean} Whether results match
113
+ * @private
114
+ */
115
+ function isSameResult(actual, expected) {
116
+ if (actual === expected) return true;
117
+ try {
118
+ return JSON.stringify(actual) === JSON.stringify(expected);
119
+ } catch {
120
+ return true;
121
+ }
122
+ }
123
+ /**
124
+ * Truncate conversion arguments to a small payload of the same shape.
125
+ *
126
+ * `args[0]` is the data array for every op: an array of series for `columns`, of rows
127
+ * for `rows`, of records for `json`. Keeping a few leading entries - and, where those
128
+ * entries are themselves arrays, a few leading cells of each - stays a valid payload
129
+ * for all three while costing a fixed amount regardless of the real data size.
130
+ * Anything that isn't an array is passed through untouched.
131
+ * @param {Array} args Conversion arguments
132
+ * @returns {Array} Sampled arguments
133
+ * @private
134
+ */
135
+ function getVerifySample(args) {
136
+ const [data, ...rest] = args;
137
+ if (!Array.isArray(data)) return args;
138
+ return [data.slice(0, VERIFY_SAMPLE_SIZE).map((entry) => Array.isArray(entry) ? entry.slice(0, 4) : entry), ...rest];
139
+ }
140
+ /**
141
+ * Post a single request to the worker and resolve with its result.
142
+ * @param {Worker} worker Worker instance
143
+ * @param {string} op Worker op name
144
+ * @param {Array} args Arguments to hand over
145
+ * @param {number} timeout Response timeout in milliseconds
146
+ * @returns {Promise} Worker result
147
+ * @private
148
+ */
149
+ function request(worker, op, args, timeout) {
150
+ const id = ++messageId;
151
+ let settled = false;
152
+ let cleanup;
153
+ const promise = new Promise((resolve, reject) => {
154
+ const settle = (fn, value) => {
155
+ if (!settled) {
156
+ settled = true;
157
+ cleanup();
158
+ fn(value);
159
+ }
160
+ };
161
+ const handler = function(e) {
162
+ if (e.data?.id === id) e.data.error ? settle(reject, new Error(e.data.error)) : settle(resolve, e.data.result);
163
+ };
164
+ const errorHandler = function() {
165
+ settle(reject, /* @__PURE__ */ new Error("worker error"));
166
+ };
167
+ const timer = setTimeout(() => settle(reject, /* @__PURE__ */ new Error("worker timeout")), timeout);
168
+ cleanup = () => {
169
+ clearTimeout(timer);
170
+ worker.removeEventListener("message", handler);
171
+ worker.removeEventListener("error", errorHandler);
172
+ };
173
+ worker.addEventListener("message", handler);
174
+ worker.addEventListener("error", errorHandler);
175
+ });
176
+ try {
177
+ worker.postMessage({
178
+ args,
179
+ id,
180
+ op
181
+ });
182
+ } catch (error) {
183
+ settled = true;
184
+ cleanup();
185
+ throw error;
186
+ }
187
+ return promise;
55
188
  }
56
189
  /**
57
- * Get or create cached WebWorker instance
58
- * @param {string} key Cache key
59
- * @param {string} src URL object as string
60
- * @returns {Worker} WebWorker instance
61
- * @private
62
- */
190
+ * Start the parity self-test for an op, on a sampled payload rather than the real one.
191
+ *
192
+ * The check answers "does this worker implement this op the way the main thread does",
193
+ * which the sample settles just as well as the full dataset - and at a cost that does
194
+ * not grow with it. Re-parsing the real payload here would hand back the very
195
+ * main-thread work the offload was meant to avoid.
196
+ * @param {Worker} worker Worker instance
197
+ * @param {string} verifyKey Cache key as `${key}:${op}`
198
+ * @param {string} op Worker op name
199
+ * @param {function} fn Equivalent main-thread function
200
+ * @param {Array} args Conversion arguments to sample from
201
+ * @param {number} timeout Response timeout in milliseconds
202
+ * @returns {Promise} Whether the worker matched the main thread
203
+ * @private
204
+ */
205
+ function startVerify(worker, verifyKey, op, fn, args, timeout) {
206
+ const sample = getVerifySample(args);
207
+ const check = request(worker, op, sample, timeout).then((result) => isSameResult(result, fn(...sample)), () => false).then((matched) => {
208
+ matched ? verifiedOps.set(verifyKey, true) : verifiedOps.delete(verifyKey);
209
+ return matched;
210
+ });
211
+ verifiedOps.set(verifyKey, check);
212
+ return check;
213
+ }
214
+ /**
215
+ * Get or create cached worker resources (Object URL, Worker)
216
+ * @param {string} op Worker op name
217
+ * @param {string} workerUrl Custom worker script URL.
218
+ * @returns {{key: string, src: string}} Cache key and Object URL
219
+ * @private
220
+ */
221
+ function getOrCreateWorkerResources(op, workerUrl = "") {
222
+ const hasWorker = !!getWorkerConstructor();
223
+ const api = workerUrl ? null : getWorkerAPI();
224
+ const src = workerUrl ? "" : getWorkerSrc();
225
+ const key = workerUrl || "blob";
226
+ if (!hasWorker || !workerUrl && (!api || !src) || disabledKeys.has(key)) return null;
227
+ if (!(key in cache)) try {
228
+ if (workerUrl) cache[key] = {
229
+ revoke: false,
230
+ src: workerUrl,
231
+ worker: null
232
+ };
233
+ else if (api) {
234
+ const blob = new api.Blob([src], { type: "text/javascript" });
235
+ cache[key] = {
236
+ revoke: true,
237
+ src: api.URL.createObjectURL(blob),
238
+ worker: null
239
+ };
240
+ }
241
+ } catch {
242
+ return null;
243
+ }
244
+ return {
245
+ key,
246
+ src: cache[key].src
247
+ };
248
+ }
249
+ /**
250
+ * Get or create cached WebWorker instance
251
+ * @param {string} key Cache key
252
+ * @param {string} src URL object as string
253
+ * @returns {Worker} WebWorker instance
254
+ * @private
255
+ */
63
256
  function getWorker(key, src) {
64
- const cached = cache[key];
65
- // Return null if cache entry doesn't exist
66
- if (!cached) {
67
- return null;
68
- }
69
- if (!cached.worker) {
70
- try {
71
- cached.worker = new win.Worker(src);
72
- }
73
- catch {
74
- return null;
75
- }
76
- // handle error
77
- if (cached.worker) {
78
- cached.worker.onerror = function (e) {
79
- // eslint-disable-next-line no-console
80
- console.error ? console.error(e) : console.log(e);
81
- };
82
- }
83
- }
84
- return cached.worker;
257
+ const cached = cache[key];
258
+ const Worker = getWorkerConstructor();
259
+ if (!cached || !Worker || disabledKeys.has(key)) return null;
260
+ if (!cached.worker) try {
261
+ cached.worker = new Worker(src);
262
+ } catch {
263
+ releaseWorker(key, true);
264
+ return null;
265
+ }
266
+ return cached.worker;
85
267
  }
86
268
  /**
87
- * Create and run on Web Worker
88
- * @param {boolean} useWorker Use Web Worker
89
- * @param {function} fn Function to be executed in worker
90
- * @param {function} callback Callback function to receive result from worker
91
- * @param {Array} depsFn Dependency functions to run given function(fn).
92
- * @returns {function}
93
- * @example
94
- * const worker = runWorker(function(arg) {
95
- * // do some tasks...
96
- * console.log("param:", A(arg));
97
- *
98
- * return 1234;
99
- * }, function(data) {
100
- * // callback after worker is done
101
- * console.log("result:", data);
102
- * },
103
- * [function A(){}]
104
- * );
105
- *
106
- * worker(11111);
107
- * @private
108
- */
109
- function runWorker(useWorker = true, fn, callback, depsFn) {
110
- let runFn = function (...args) {
111
- const res = fn(...args);
112
- callback(res);
113
- };
114
- if (win.Worker && useWorker) {
115
- const workerResources = getOrCreateWorkerResources(fn, depsFn);
116
- const worker = workerResources && getWorker(workerResources.key, workerResources.src);
117
- if (worker) {
118
- runFn = function (...args) {
119
- // workers are cached and shared: match the response by id so concurrent
120
- // callers don't steal each other's result
121
- const id = ++messageId;
122
- const handler = function (e) {
123
- if (e.data?.id === id) {
124
- worker.removeEventListener("message", handler);
125
- callback(e.data.result);
126
- }
127
- };
128
- worker.addEventListener("message", handler);
129
- worker.postMessage({ id, args });
130
- };
131
- }
132
- }
133
- return runFn;
269
+ * Create and run on Web Worker
270
+ * @param {boolean} useWorker Use Web Worker
271
+ * @param {string} op Op name registered in src/module/worker.entry.ts
272
+ * @param {function} fn Equivalent main-thread function, used for fallback and for the
273
+ * sampled parity self-test
274
+ * @param {function} callback Callback function to receive result from worker
275
+ * @param {number|object} options Worker response timeout or options.
276
+ * @returns {function}
277
+ * @example
278
+ * const worker = runWorker(true, "rows", rows, function(data) {
279
+ * // callback after worker is done
280
+ * console.log("result:", data);
281
+ * });
282
+ *
283
+ * worker(11111);
284
+ * @private
285
+ */
286
+ function runWorker(useWorker = true, op, fn, callback, options) {
287
+ const { timeout, workerUrl } = normalizeWorkerOptions(options);
288
+ const runSync = function(...args) {
289
+ callback(fn(...args));
290
+ };
291
+ let runFn = runSync;
292
+ if (useWorker) {
293
+ const workerResources = getOrCreateWorkerResources(op, workerUrl);
294
+ const worker = workerResources ? getWorker(workerResources.key, workerResources.src) : null;
295
+ if (worker && workerResources) {
296
+ const { key } = workerResources;
297
+ runFn = function(...args) {
298
+ const fallback = () => {
299
+ releaseWorker(key, true);
300
+ runFn = runSync;
301
+ runSync(...args);
302
+ };
303
+ try {
304
+ const verifyKey = `${key}:${op}`;
305
+ const verified = verifiedOps.get(verifyKey) ?? startVerify(worker, verifyKey, op, fn, args, timeout);
306
+ request(worker, op, args, timeout).then((result) => {
307
+ if (verified === true) callback(result);
308
+ else verified.then((matched) => matched ? callback(result) : fallback());
309
+ }, fallback);
310
+ } catch {
311
+ fallback();
312
+ }
313
+ };
314
+ }
315
+ }
316
+ return runFn;
134
317
  }
135
318
  /**
136
- * Clean-up all cached workers and release resources
137
- * @private
138
- */
319
+ * Clean-up all cached workers and release resources
320
+ * @private
321
+ */
139
322
  function cleanupWorkers() {
140
- for (const key in cache) {
141
- const cached = cache[key];
142
- if (cached.worker) {
143
- cached.worker.terminate();
144
- }
145
- if (cached.src) {
146
- win.URL.revokeObjectURL(cached.src);
147
- }
148
- delete cache[key];
149
- }
323
+ const api = getWorkerAPI();
324
+ for (const key in cache) {
325
+ const cached = cache[key];
326
+ if (cached.worker) cached.worker.terminate();
327
+ if (cached.src) cached.revoke && api?.URL.revokeObjectURL(cached.src);
328
+ delete cache[key];
329
+ }
330
+ disabledKeys.clear();
331
+ verifiedOps.clear();
150
332
  }
151
-
333
+ //#endregion
152
334
  export { cleanupWorkers, getWorker, runWorker };