@trebco/treb 32.13.2 → 36.1.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.
- package/api-generator/api-generator-types.ts +3 -0
- package/api-generator/api-generator.ts +15 -1
- package/bun.lock +145 -99
- package/dist/chunk-43DLP2OX.mjs +11 -0
- package/dist/chunk-4CKS56PE.mjs +11 -0
- package/dist/chunk-75PARUQE.mjs +11 -0
- package/dist/chunk-7QD63AZS.mjs +24601 -0
- package/dist/chunk-A55ARVRD.mjs +11 -0
- package/dist/chunk-DESAKYW4.mjs +11 -0
- package/dist/chunk-EQ2R5W6P.mjs +24565 -0
- package/dist/chunk-IYJU2J6D.mjs +24601 -0
- package/dist/chunk-KSJFPGXT.mjs +11 -0
- package/dist/chunk-ORQFKLXM.mjs +24601 -0
- package/dist/chunk-SFDNNDHY.mjs +11 -0
- package/dist/chunk-T47DX5MI.mjs +11 -0
- package/dist/chunk-T6ILBVEX.mjs +11 -0
- package/dist/chunk-TPRCDYYG.mjs +11 -0
- package/dist/chunk-YAHNOOHO.mjs +11 -0
- package/dist/treb-export-worker.mjs +9 -2
- package/dist/treb-spreadsheet.mjs +7 -19
- package/dist/treb.d.ts +20 -4
- package/esbuild-composite.mjs +18 -6
- package/esbuild-utils.mjs +62 -3
- package/i18n/languages/treb-i18n-da.mjs +1 -1
- package/i18n/languages/treb-i18n-de.mjs +1 -1
- package/i18n/languages/treb-i18n-es.mjs +1 -1
- package/i18n/languages/treb-i18n-fr.mjs +1 -1
- package/i18n/languages/treb-i18n-it.mjs +1 -1
- package/i18n/languages/treb-i18n-nl.mjs +1 -1
- package/i18n/languages/treb-i18n-no.mjs +1 -1
- package/i18n/languages/treb-i18n-pl.mjs +1 -1
- package/i18n/languages/treb-i18n-pt.mjs +1 -1
- package/i18n/languages/treb-i18n-sv.mjs +1 -1
- package/ooxml-types/README.md +141 -0
- package/ooxml-types/package.json +5 -0
- package/ooxml-types/src/types/drawingml/chart.ts +327 -0
- package/ooxml-types/src/types/drawingml/index.ts +63 -0
- package/ooxml-types/src/types/drawingml/spreadsheetDrawing.ts +105 -0
- package/ooxml-types/src/types/drawingml/theme.ts +104 -0
- package/ooxml-types/src/types/index.ts +3 -0
- package/ooxml-types/src/types/package/contentTypes.ts +49 -0
- package/ooxml-types/src/types/package/docProps.ts +46 -0
- package/ooxml-types/src/types/package/index.ts +17 -0
- package/ooxml-types/src/types/package/relationships.ts +37 -0
- package/ooxml-types/src/types/spreadsheetml/columns.ts +20 -0
- package/ooxml-types/src/types/spreadsheetml/comments.ts +30 -0
- package/ooxml-types/src/types/spreadsheetml/dataFeatures.ts +261 -0
- package/ooxml-types/src/types/spreadsheetml/enums.ts +175 -0
- package/ooxml-types/src/types/spreadsheetml/index.ts +186 -0
- package/ooxml-types/src/types/spreadsheetml/metadata.ts +90 -0
- package/ooxml-types/src/types/spreadsheetml/misc.ts +35 -0
- package/ooxml-types/src/types/spreadsheetml/pageLayout.ts +83 -0
- package/ooxml-types/src/types/spreadsheetml/sharedStrings.ts +33 -0
- package/ooxml-types/src/types/spreadsheetml/sheetData.ts +70 -0
- package/ooxml-types/src/types/spreadsheetml/sheetProperties.ts +86 -0
- package/ooxml-types/src/types/spreadsheetml/sheetViews.ts +51 -0
- package/ooxml-types/src/types/spreadsheetml/sparkline.ts +46 -0
- package/ooxml-types/src/types/spreadsheetml/styles.ts +274 -0
- package/ooxml-types/src/types/spreadsheetml/table.ts +106 -0
- package/ooxml-types/src/types/spreadsheetml/util.ts +15 -0
- package/ooxml-types/src/types/spreadsheetml/workbook.ts +165 -0
- package/ooxml-types/src/types/spreadsheetml/worksheet.ts +60 -0
- package/package.json +13 -11
- package/treb-base-types/src/api_types.ts +1 -1
- package/treb-base-types/src/area-utils.ts +1 -1
- package/treb-base-types/src/area.ts +1 -1
- package/treb-base-types/src/basic_types.ts +1 -1
- package/treb-base-types/src/cell.ts +1 -1
- package/treb-base-types/src/cells.ts +1 -1
- package/treb-base-types/src/color.ts +1 -1
- package/treb-base-types/src/dom-utilities.ts +1 -1
- package/treb-base-types/src/evaluate-options.ts +1 -1
- package/treb-base-types/src/font-stack.ts +1 -1
- package/treb-base-types/src/gradient.ts +1 -1
- package/treb-base-types/src/import.ts +1 -1
- package/treb-base-types/src/index-standalone.ts +1 -1
- package/treb-base-types/src/index.ts +2 -1
- package/treb-base-types/src/layout.ts +1 -1
- package/treb-base-types/src/localization.ts +1 -1
- package/treb-base-types/src/rectangle.ts +1 -1
- package/treb-base-types/src/render_text.ts +7 -1
- package/treb-base-types/src/style.ts +1 -1
- package/treb-base-types/src/table.ts +1 -1
- package/treb-base-types/src/text_part.ts +1 -1
- package/treb-base-types/src/theme.ts +1 -1
- package/treb-base-types/src/union.ts +4 -1
- package/treb-base-types/src/value-type.ts +1 -1
- package/treb-base-types/src/worker-proxy.ts +294 -0
- package/treb-base-types/style/resizable.css +1 -1
- package/treb-calculator/src/calculator.ts +133 -14
- package/treb-calculator/src/complex-math.ts +1 -1
- package/treb-calculator/src/dag/array-vertex.ts +1 -1
- package/treb-calculator/src/dag/calculation_leaf_vertex.ts +1 -1
- package/treb-calculator/src/dag/graph.ts +1 -1
- package/treb-calculator/src/dag/spreadsheet_vertex.ts +1 -1
- package/treb-calculator/src/dag/spreadsheet_vertex_base.ts +1 -1
- package/treb-calculator/src/dag/state_leaf_vertex.ts +1 -1
- package/treb-calculator/src/dag/vertex.ts +1 -1
- package/treb-calculator/src/descriptors.ts +9 -1
- package/treb-calculator/src/expression-calculator.ts +1 -1
- package/treb-calculator/src/function-error.ts +1 -1
- package/treb-calculator/src/function-library.ts +1 -1
- package/treb-calculator/src/functions/base-functions.ts +10 -4
- package/treb-calculator/src/functions/beta.ts +1 -1
- package/treb-calculator/src/functions/checkbox.ts +1 -1
- package/treb-calculator/src/functions/complex-functions.ts +1 -1
- package/treb-calculator/src/functions/date-utils.ts +1 -1
- package/treb-calculator/src/functions/finance-functions.ts +2 -4
- package/treb-calculator/src/functions/fp.ts +1 -1
- package/treb-calculator/src/functions/function-utilities.ts +1 -1
- package/treb-calculator/src/functions/gamma.ts +1 -1
- package/treb-calculator/src/functions/information-functions.ts +1 -1
- package/treb-calculator/src/functions/lambda-functions.ts +4 -1
- package/treb-calculator/src/functions/matrix-functions.ts +1 -1
- package/treb-calculator/src/functions/normal.ts +1 -1
- package/treb-calculator/src/functions/regex-functions.ts +13 -4
- package/treb-calculator/src/functions/sparkline.ts +1 -1
- package/treb-calculator/src/functions/statistics-functions.ts +1 -1
- package/treb-calculator/src/functions/students-t.ts +1 -1
- package/treb-calculator/src/functions/text-functions.ts +5 -1
- package/treb-calculator/src/index.ts +1 -1
- package/treb-calculator/src/notifier-types.ts +1 -1
- package/treb-calculator/src/primitives.ts +1 -1
- package/treb-calculator/src/utilities.ts +1 -1
- package/treb-charts/src/chart-functions.ts +1 -1
- package/treb-charts/src/chart-types.ts +1 -1
- package/treb-charts/src/chart-utils.ts +1 -1
- package/treb-charts/src/chart.ts +1 -1
- package/treb-charts/src/default-chart-renderer.ts +1 -1
- package/treb-charts/src/index.ts +1 -1
- package/treb-charts/src/main.ts +1 -1
- package/treb-charts/src/quicksort.ts +1 -1
- package/treb-charts/src/rectangle.ts +1 -1
- package/treb-charts/src/renderer-type.ts +1 -1
- package/treb-charts/src/renderer.ts +1 -1
- package/treb-charts/src/util.ts +1 -1
- package/treb-charts/style/charts.scss +1 -1
- package/treb-data-model/src/annotation.ts +1 -1
- package/treb-data-model/src/conditional_format.ts +1 -1
- package/treb-data-model/src/data-validation.ts +1 -1
- package/treb-data-model/src/data_model.ts +32 -5
- package/treb-data-model/src/index.ts +1 -1
- package/treb-data-model/src/language-model.ts +1 -1
- package/treb-data-model/src/named.ts +1 -1
- package/treb-data-model/src/serialize_options.ts +1 -1
- package/treb-data-model/src/sheet.ts +1 -1
- package/treb-data-model/src/sheet_collection.ts +1 -1
- package/treb-data-model/src/sheet_selection.ts +1 -1
- package/treb-data-model/src/sheet_types.ts +1 -1
- package/treb-data-model/src/types.ts +1 -1
- package/treb-embed/src/content-types.d.ts +1 -1
- package/treb-embed/src/custom-element/global.d.ts +1 -1
- package/treb-embed/src/custom-element/spreadsheet-constructor.ts +1 -1
- package/treb-embed/src/custom-element/treb-global.ts +1 -1
- package/treb-embed/src/custom-element/treb-spreadsheet-element.ts +1 -1
- package/treb-embed/src/embedded-spreadsheet.ts +342 -149
- package/treb-embed/src/index.ts +1 -1
- package/treb-embed/src/options.ts +4 -2
- package/treb-embed/src/plugin.ts +1 -1
- package/treb-embed/src/progress-dialog.ts +1 -1
- package/treb-embed/src/selection-state.ts +1 -1
- package/treb-embed/src/spinner.ts +1 -1
- package/treb-embed/src/toolbar-message.ts +6 -1
- package/treb-embed/src/types.ts +13 -1
- package/treb-embed/style/autocomplete.scss +1 -1
- package/treb-embed/style/dark-theme.scss +1 -1
- package/treb-embed/style/defaults.scss +1 -1
- package/treb-embed/style/dialog.scss +1 -1
- package/treb-embed/style/dropdown-select.scss +1 -1
- package/treb-embed/style/font-stacks.scss +1 -1
- package/treb-embed/style/formula-bar.scss +1 -1
- package/treb-embed/style/grid.scss +1 -1
- package/treb-embed/style/layout.scss +1 -1
- package/treb-embed/style/mouse-mask.scss +1 -1
- package/treb-embed/style/note.scss +1 -1
- package/treb-embed/style/overlay-editor.scss +1 -1
- package/treb-embed/style/spinner.scss +1 -1
- package/treb-embed/style/tab-bar.scss +1 -1
- package/treb-embed/style/table.scss +1 -1
- package/treb-embed/style/theme-defaults.scss +1 -1
- package/treb-embed/style/toolbar.scss +1 -1
- package/treb-embed/style/tooltip.scss +1 -1
- package/treb-embed/style/treb-icons.scss +1 -1
- package/treb-embed/style/treb-spreadsheet-element.scss +1 -1
- package/treb-embed/style/z-index.scss +1 -1
- package/treb-export/src/address-type.ts +1 -1
- package/treb-export/src/base-template.ts +1 -1
- package/treb-export/src/column-width.ts +1 -1
- package/treb-export/src/drawing/bubble-chart-template.ts +1 -1
- package/treb-export/src/drawing/chart-template-components2.ts +1 -1
- package/treb-export/src/drawing/chart.ts +1 -1
- package/treb-export/src/drawing/column-chart-template2.ts +1 -1
- package/treb-export/src/drawing/donut-chart-template2.ts +1 -1
- package/treb-export/src/drawing/drawing.ts +1 -1
- package/treb-export/src/drawing/embedded-image.ts +1 -1
- package/treb-export/src/drawing/scatter-chart-template2.ts +1 -1
- package/treb-export/src/export.ts +121 -8
- package/treb-export/src/import-export-messages.ts +61 -0
- package/treb-export/src/import.ts +318 -301
- package/treb-export/src/index.worker.ts +85 -53
- package/treb-export/src/metadata.ts +71 -3
- package/treb-export/src/ooxml.ts +47 -0
- package/treb-export/src/relationship.ts +1 -1
- package/treb-export/src/shared-strings.ts +19 -15
- package/treb-export/src/template-2.ts +1 -1
- package/treb-export/src/unescape_xml.ts +1 -1
- package/treb-export/src/workbook-sheet.ts +11 -6
- package/treb-export/src/workbook-style.ts +137 -25
- package/treb-export/src/workbook-theme.ts +20 -4
- package/treb-export/src/workbook.ts +85 -88
- package/treb-export/src/xml-test.ts +1 -1
- package/treb-export/src/xml-utils.ts +1 -1
- package/treb-export/src/zip-wrapper.ts +1 -1
- package/treb-export/tsconfig.json +2 -1
- package/treb-format/src/format.test.ts +1 -1
- package/treb-format/src/format.ts +12 -5
- package/treb-format/src/format_cache.ts +3 -3
- package/treb-format/src/format_parser.ts +1 -1
- package/treb-format/src/index.ts +1 -1
- package/treb-format/src/number_format_section.ts +1 -1
- package/treb-format/src/value_parser.ts +1 -1
- package/treb-grid/src/editors/autocomplete.ts +1 -1
- package/treb-grid/src/editors/autocomplete_matcher.ts +1 -1
- package/treb-grid/src/editors/editor.ts +15 -6
- package/treb-grid/src/editors/external_editor.ts +33 -8
- package/treb-grid/src/editors/formula_bar.ts +12 -1
- package/treb-grid/src/editors/overlay_editor.ts +4 -1
- package/treb-grid/src/index.ts +1 -1
- package/treb-grid/src/layout/base_layout.ts +1 -1
- package/treb-grid/src/layout/grid_layout.ts +1 -1
- package/treb-grid/src/layout/mock-layout.ts +1 -1
- package/treb-grid/src/render/selection-renderer.ts +1 -1
- package/treb-grid/src/render/svg_header_overlay.ts +1 -1
- package/treb-grid/src/render/svg_selection_block.ts +1 -1
- package/treb-grid/src/render/tile_renderer.ts +36 -7
- package/treb-grid/src/types/border_constants.ts +1 -1
- package/treb-grid/src/types/clipboard_data.ts +1 -1
- package/treb-grid/src/types/clipboard_data2.ts +1 -1
- package/treb-grid/src/types/drag_mask.ts +1 -1
- package/treb-grid/src/types/external_editor_config.ts +1 -1
- package/treb-grid/src/types/grid.ts +181 -40
- package/treb-grid/src/types/grid_base.ts +7 -4
- package/treb-grid/src/types/grid_command.ts +7 -1
- package/treb-grid/src/types/grid_events.ts +2 -1
- package/treb-grid/src/types/grid_options.ts +1 -1
- package/treb-grid/src/types/scale-control.ts +1 -1
- package/treb-grid/src/types/set_range_options.ts +1 -1
- package/treb-grid/src/types/tab_bar.ts +1 -1
- package/treb-grid/src/types/tile.ts +1 -1
- package/treb-grid/src/types/update_flags.ts +1 -1
- package/treb-grid/src/util/fontmetrics.ts +1 -1
- package/treb-grid/src/util/ua.ts +1 -1
- package/treb-parser/src/csv-parser.ts +1 -1
- package/treb-parser/src/index.ts +1 -1
- package/treb-parser/src/md-parser.ts +1 -1
- package/treb-parser/src/parser-types.ts +1 -1
- package/treb-parser/src/parser.ts +1 -1
- package/treb-utils/src/event_source.ts +1 -1
- package/treb-utils/src/ievent_source.ts +1 -1
- package/treb-utils/src/index.ts +1 -1
- package/treb-utils/src/measurement.ts +1 -1
- package/treb-utils/src/scale.ts +1 -1
- package/treb-utils/src/serialize_html.ts +1 -1
- package/treb-utils/src/validate_uri.ts +1 -1
- package/tsproject.json +2 -3
- package/treb-embed/src/export-worker.ts +0 -44
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
* You should have received a copy of the GNU General Public License along
|
|
15
15
|
* with TREB. If not, see <https://www.gnu.org/licenses/>.
|
|
16
16
|
*
|
|
17
|
-
* Copyright 2022-
|
|
17
|
+
* Copyright 2022-2026 trebco, llc.
|
|
18
18
|
* info@treb.app
|
|
19
19
|
*
|
|
20
20
|
*/
|
|
@@ -39,6 +39,7 @@ export * from './table';
|
|
|
39
39
|
export * from './gradient';
|
|
40
40
|
export * from './evaluate-options';
|
|
41
41
|
export * from './dom-utilities';
|
|
42
|
+
export * from './worker-proxy';
|
|
42
43
|
|
|
43
44
|
export * as AreaUtils from './area-utils';
|
|
44
45
|
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
* You should have received a copy of the GNU General Public License along
|
|
15
15
|
* with TREB. If not, see <https://www.gnu.org/licenses/>.
|
|
16
16
|
*
|
|
17
|
-
* Copyright 2022-
|
|
17
|
+
* Copyright 2022-2026 trebco, llc.
|
|
18
18
|
* info@treb.app
|
|
19
19
|
*
|
|
20
20
|
*/
|
|
@@ -51,6 +51,12 @@ export interface RenderTextPart {
|
|
|
51
51
|
emphasis?: boolean;
|
|
52
52
|
strike?: boolean;
|
|
53
53
|
|
|
54
|
+
// indent is just text here but we need to omit
|
|
55
|
+
// underline and strikethrough on this part, so
|
|
56
|
+
// we need to track it
|
|
57
|
+
|
|
58
|
+
indent?: boolean;
|
|
59
|
+
|
|
54
60
|
}
|
|
55
61
|
|
|
56
62
|
export interface PreparedText {
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
* You should have received a copy of the GNU General Public License along
|
|
15
15
|
* with TREB. If not, see <https://www.gnu.org/licenses/>.
|
|
16
16
|
*
|
|
17
|
-
* Copyright 2022-
|
|
17
|
+
* Copyright 2022-2026 trebco, llc.
|
|
18
18
|
* info@treb.app
|
|
19
19
|
*
|
|
20
20
|
*/
|
|
@@ -83,6 +83,9 @@ export interface ExtendedUnion {
|
|
|
83
83
|
|
|
84
84
|
value: unknown; // any;
|
|
85
85
|
key?: string;
|
|
86
|
+
|
|
87
|
+
/** adding a source field so we can track */
|
|
88
|
+
source?: string;
|
|
86
89
|
}
|
|
87
90
|
|
|
88
91
|
export interface FunctionUnion {
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This file is part of TREB.
|
|
3
|
+
*
|
|
4
|
+
* TREB is free software: you can redistribute it and/or modify it under the
|
|
5
|
+
* terms of the GNU General Public License as published by the Free Software
|
|
6
|
+
* Foundation, either version 3 of the License, or (at your option) any
|
|
7
|
+
* later version.
|
|
8
|
+
*
|
|
9
|
+
* TREB is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
10
|
+
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
11
|
+
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
12
|
+
* details.
|
|
13
|
+
*
|
|
14
|
+
* You should have received a copy of the GNU General Public License along
|
|
15
|
+
* with TREB. If not, see <https://www.gnu.org/licenses/>.
|
|
16
|
+
*
|
|
17
|
+
* Copyright 2022-2026 trebco, llc.
|
|
18
|
+
* info@treb.app
|
|
19
|
+
*
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* this is a wrapper for workers. we want to support node in addition to the
|
|
24
|
+
* browser. bun seems to support web APIs with no modification, but node needs
|
|
25
|
+
* special attention. not sure about deno.
|
|
26
|
+
*
|
|
27
|
+
* switching to specific classes for different environments. also the plan is
|
|
28
|
+
* to add a main-thread (i.e. no-worker, inline calc) version.
|
|
29
|
+
*
|
|
30
|
+
*/
|
|
31
|
+
export interface WorkerProxy<TX, RX = TX> {
|
|
32
|
+
|
|
33
|
+
/** terminate worker */
|
|
34
|
+
Terminate: () => void;
|
|
35
|
+
|
|
36
|
+
/** initialize, possibly loading code */
|
|
37
|
+
Init: (url: string, browser_init_method: () => Worker) => Promise<void>;
|
|
38
|
+
|
|
39
|
+
/** wrapper for postMessage */
|
|
40
|
+
PostMessage: (message: TX) => void;
|
|
41
|
+
|
|
42
|
+
/** wrapper for addEventListener */
|
|
43
|
+
OnMessage: (fn: (message: MessageEvent<RX>) => void | Promise<void>) => void;
|
|
44
|
+
|
|
45
|
+
/** wrapper for addEventListener */
|
|
46
|
+
OnError: (fn: (message: ErrorEvent) => void | Promise<void>) => void;
|
|
47
|
+
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function CheckNodeSemantics() {
|
|
51
|
+
return typeof Worker === 'undefined';
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export class WorkerProxyBrowser<TX, RX=TX> implements WorkerProxy<TX, RX> {
|
|
55
|
+
|
|
56
|
+
public worker?: Worker;
|
|
57
|
+
|
|
58
|
+
public Terminate() {
|
|
59
|
+
this.worker?.terminate();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* OK this changed to support svelte/vite/I guess it's all rollup under
|
|
64
|
+
* the hood. whatever the tool is, it does static analysis and it has
|
|
65
|
+
* to see the pattern loaded in order to bundle the worker module. so
|
|
66
|
+
* we play ball. this is not a good situation.
|
|
67
|
+
*
|
|
68
|
+
* the browser init method should look something like this:
|
|
69
|
+
*
|
|
70
|
+
* () => {
|
|
71
|
+
* return new Worker(new URL(url, import.meta.url), {
|
|
72
|
+
* type: 'module'
|
|
73
|
+
* }) as Worker;
|
|
74
|
+
* }
|
|
75
|
+
*
|
|
76
|
+
* so that the code analyzer will see it.
|
|
77
|
+
*
|
|
78
|
+
* @param url
|
|
79
|
+
* @param browser_init_method
|
|
80
|
+
*/
|
|
81
|
+
public async Init(url: string, browser_init_method: () => Worker) {
|
|
82
|
+
this.worker = browser_init_method();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public PostMessage(message: TX) {
|
|
86
|
+
this.worker?.postMessage(message);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
public OnMessage(fn: (message: MessageEvent<RX>) => (void|Promise<void>)) {
|
|
90
|
+
if (this.worker) {
|
|
91
|
+
this.worker.onmessage = fn;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
public OnError(fn: (message: ErrorEvent) => (void|Promise<void>)) {
|
|
96
|
+
if (this.worker) {
|
|
97
|
+
this.worker.onerror = fn;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export class WorkerProxyNode<TX, RX=TX> implements WorkerProxy<TX, RX> {
|
|
104
|
+
|
|
105
|
+
public worker?: {
|
|
106
|
+
postMessage: (message: TX) => void|Promise<void>;
|
|
107
|
+
terminate: () => void;
|
|
108
|
+
on: (type: 'message'|'error', handler: (data: RX) => void|Promise<void>) => void;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
public Terminate() {
|
|
112
|
+
this.worker?.terminate();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public async Init(url: string) {
|
|
116
|
+
const { Worker: NodeWorker } = await import('node:worker_threads');
|
|
117
|
+
this.worker = new NodeWorker(new URL(url, import.meta.url));
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
public PostMessage(message: TX) {
|
|
121
|
+
// apparently postMessage works on node workers as well?
|
|
122
|
+
this.worker?.postMessage(message);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
public OnMessage(fn: (message: MessageEvent<RX>) => (void|Promise<void>)) {
|
|
126
|
+
if (this.worker) {
|
|
127
|
+
this.worker.on('message', (data: RX) => fn({data} as MessageEvent<RX>));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
public OnError(fn: (message: ErrorEvent) => (void|Promise<void>)) {
|
|
132
|
+
if (this.worker) {
|
|
133
|
+
this.worker.on('error', (err) => fn({error: err} as ErrorEvent));
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
interface InProcessContext<TX, RX> {
|
|
140
|
+
addEventListener: (type: 'message', handler: (event: MessageEvent<TX>) => void|Promise<void>) => void;
|
|
141
|
+
postMessage: (data: RX) => void;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
let in_process_worker_context: InProcessContext<unknown, unknown>|undefined = undefined;
|
|
145
|
+
|
|
146
|
+
type composite<TX, RX> = WorkerProxy<TX, RX> & InProcessContext<TX, RX>;
|
|
147
|
+
|
|
148
|
+
export class WorkerProxyInProcess<TX, RX = TX> implements composite<TX, RX> {
|
|
149
|
+
|
|
150
|
+
protected tx_cache: TX[] = [];
|
|
151
|
+
protected main_thread_to_worker_message_handler?: (event: MessageEvent<TX>) => void|Promise<void>;
|
|
152
|
+
|
|
153
|
+
protected rx_cache: RX[] = [];
|
|
154
|
+
protected worker_to_main_thread_message_handler?: (message: MessageEvent<RX>) => void|Promise<void>;
|
|
155
|
+
|
|
156
|
+
public Terminate() {
|
|
157
|
+
this.tx_cache = [];
|
|
158
|
+
this.rx_cache = [];
|
|
159
|
+
this.main_thread_to_worker_message_handler = undefined;
|
|
160
|
+
this.worker_to_main_thread_message_handler = undefined;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
public async Init(url: string) {
|
|
164
|
+
in_process_worker_context = this as InProcessContext<unknown, unknown>;
|
|
165
|
+
/* @vite-ignore */
|
|
166
|
+
await import(new URL(url, import.meta.url).toString());
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** post message from "worker" to "main thread" */
|
|
170
|
+
public PostMessage(message: TX) {
|
|
171
|
+
if (this.main_thread_to_worker_message_handler) {
|
|
172
|
+
// console.info('calling handler', message);
|
|
173
|
+
const handler = this.main_thread_to_worker_message_handler;
|
|
174
|
+
Promise.resolve().then(() => handler({data: message} as MessageEvent<TX>));
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
// console.info("no handler, caching message");
|
|
178
|
+
this.tx_cache.push(message);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** callback when "main thread" sends message to "worker" */
|
|
183
|
+
public OnMessage(fn: (message: MessageEvent<RX>) => (void|Promise<void>)) {
|
|
184
|
+
this.worker_to_main_thread_message_handler = fn;
|
|
185
|
+
if (this.rx_cache.length) {
|
|
186
|
+
const copy = [...this.rx_cache];
|
|
187
|
+
this.rx_cache = [];
|
|
188
|
+
this.FlushCache(copy, fn);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
193
|
+
public OnError(fn: (message: ErrorEvent) => (void|Promise<void>)) {
|
|
194
|
+
// ??
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
public FlushCache<T>(cache: T[], fn: (message: MessageEvent<T>) => void|Promise<void>) {
|
|
198
|
+
// console.info(`flush cache ${cache.length}`);
|
|
199
|
+
Promise.resolve().then(async () => {
|
|
200
|
+
for (const message of cache) {
|
|
201
|
+
const result = fn({data: message} as MessageEvent);
|
|
202
|
+
if (result instanceof Promise) {
|
|
203
|
+
await result; // serial?
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
public addEventListener(type: 'message', handler: (event: MessageEvent<TX>) => void|Promise<void>): void {
|
|
210
|
+
this.main_thread_to_worker_message_handler = handler;
|
|
211
|
+
if (this.tx_cache.length) {
|
|
212
|
+
const copy = [...this.tx_cache];
|
|
213
|
+
this.tx_cache = [];
|
|
214
|
+
this.FlushCache(copy, handler);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
public postMessage(data: RX): void {
|
|
219
|
+
if (this.worker_to_main_thread_message_handler) {
|
|
220
|
+
this.worker_to_main_thread_message_handler({data} as MessageEvent);
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
this.rx_cache.push(data);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* factory method for workers. supports web workers (also bun), node workers
|
|
231
|
+
* and (via parameter) in-process workers. we're still figuring out how to
|
|
232
|
+
* signal that we're in process, currently somewhat clumsy
|
|
233
|
+
*
|
|
234
|
+
* @param in_process
|
|
235
|
+
* @returns
|
|
236
|
+
*/
|
|
237
|
+
export function CreateWorker<TX, RX = TX>(in_process = false): WorkerProxy<TX, RX> {
|
|
238
|
+
if (in_process) {
|
|
239
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
240
|
+
if (!(globalThis as {worker_console_message?: boolean}).worker_console_message) {
|
|
241
|
+
console.info('using in-process worker');
|
|
242
|
+
(globalThis as {worker_console_message?: boolean}).worker_console_message = true;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return new WorkerProxyInProcess<TX, RX>();
|
|
246
|
+
}
|
|
247
|
+
if (CheckNodeSemantics()) {
|
|
248
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
249
|
+
if (!(globalThis as {worker_console_message?: boolean}).worker_console_message) {
|
|
250
|
+
console.info('using node worker');
|
|
251
|
+
(globalThis as {worker_console_message?: boolean}).worker_console_message = true;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return new WorkerProxyNode<TX, RX>();
|
|
255
|
+
}
|
|
256
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
257
|
+
if (!(globalThis as {worker_console_message?: boolean}).worker_console_message) {
|
|
258
|
+
console.info('using standard web worker');
|
|
259
|
+
(globalThis as {worker_console_message?: boolean}).worker_console_message = true;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return new WorkerProxyBrowser<TX, RX>();
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* this is for the worker side, to normalize postMessage/onmessage
|
|
267
|
+
*/
|
|
268
|
+
export const GetWorkerContext = async (): Promise<Worker> => {
|
|
269
|
+
|
|
270
|
+
let ctx: Worker;
|
|
271
|
+
|
|
272
|
+
if (in_process_worker_context) {
|
|
273
|
+
ctx = in_process_worker_context as unknown as Worker;
|
|
274
|
+
}
|
|
275
|
+
else if (typeof self === 'undefined') {
|
|
276
|
+
const { parentPort } = await import('node:worker_threads');
|
|
277
|
+
ctx = {
|
|
278
|
+
postMessage: (data: unknown) => {
|
|
279
|
+
parentPort?.postMessage(data);
|
|
280
|
+
},
|
|
281
|
+
addEventListener: (type: 'message', handler: (event: MessageEvent) => void) => {
|
|
282
|
+
parentPort?.on('message', (data) => {
|
|
283
|
+
handler({ data } as MessageEvent);
|
|
284
|
+
});
|
|
285
|
+
},
|
|
286
|
+
} as unknown as Worker;
|
|
287
|
+
}
|
|
288
|
+
else {
|
|
289
|
+
ctx = self as unknown as Worker;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
return ctx;
|
|
293
|
+
|
|
294
|
+
};
|