@affino/datagrid-worker 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 affino
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,7 @@
1
+ export { DATAGRID_WORKER_PROTOCOL_CHANNEL, DATAGRID_WORKER_PROTOCOL_VERSION, DATAGRID_WORKER_COMPUTE_PAYLOAD_SCHEMA_VERSION, createDataGridWorkerComputeRequestMessage, createDataGridWorkerComputeAckMessage, createDataGridWorkerComputeErrorAckMessage, isDataGridWorkerComputeRequestMessage, isDataGridWorkerComputeAckMessage, resolveWorkerComputeRequestPayload, type DataGridWorkerComputeRequest, type DataGridWorkerComputeRequestPayload, type DataGridWorkerComputeRequestPayloadEnvelope, type DataGridWorkerComputeBatchPlan, type DataGridWorkerComputeResult, type DataGridWorkerProtocolHeader, type DataGridWorkerComputeRequestMessage, type DataGridWorkerComputeAckMessage, type DataGridWorkerProtocolMessage, } from "./protocol.js";
2
+ export { createDataGridWorkerPostMessageTransport, type DataGridWorkerMessageEvent, type DataGridWorkerMessageSource, type DataGridWorkerMessageTarget, type DataGridWorkerDispatchStrategy, type DataGridWorkerPostMessageTransportStats, type DataGridWorkerPostMessageTransport, type CreateDataGridWorkerPostMessageTransportOptions, } from "./postMessageTransport.js";
3
+ export { createDataGridWorkerMessageHost, type CreateDataGridWorkerMessageHostOptions, type DataGridWorkerMessageHost, } from "./workerHost.js";
4
+ export { DATAGRID_WORKER_ROW_MODEL_PROTOCOL_CHANNEL, DATAGRID_WORKER_ROW_MODEL_PROTOCOL_VERSION, DATAGRID_WORKER_ROW_MODEL_PAYLOAD_SCHEMA_VERSION, createDataGridWorkerRowModelCommandMessage, createDataGridWorkerRowModelUpdateMessage, isDataGridWorkerRowModelCommandMessage, isDataGridWorkerRowModelUpdateMessage, type DataGridWorkerRowModelProtocolHeader, type DataGridWorkerViewportCoalesceScope, type DataGridWorkerRowModelCommand, type DataGridWorkerRowModelCommandMessage, type DataGridWorkerRowModelUpdatePayload, type DataGridWorkerRowModelUpdateMessage, type DataGridWorkerRowModelProtocolMessage, } from "./workerOwnedRowModelProtocol.js";
5
+ export { createDataGridWorkerOwnedRowModelHost, type CreateDataGridWorkerOwnedRowModelHostOptions, type DataGridWorkerOwnedRowModelHost, } from "./workerOwnedRowModelHost.js";
6
+ export { createDataGridWorkerOwnedRowModel, type DataGridWorkerOwnedRowModel, type CreateDataGridWorkerOwnedRowModelOptions, } from "./workerOwnedRowModel.js";
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gCAAgC,EAChC,gCAAgC,EAChC,8CAA8C,EAC9C,yCAAyC,EACzC,qCAAqC,EACrC,0CAA0C,EAC1C,qCAAqC,EACrC,iCAAiC,EACjC,kCAAkC,EAClC,KAAK,4BAA4B,EACjC,KAAK,mCAAmC,EACxC,KAAK,2CAA2C,EAChD,KAAK,8BAA8B,EACnC,KAAK,2BAA2B,EAChC,KAAK,4BAA4B,EACjC,KAAK,mCAAmC,EACxC,KAAK,+BAA+B,EACpC,KAAK,6BAA6B,GACnC,MAAM,eAAe,CAAA;AAEtB,OAAO,EACL,wCAAwC,EACxC,KAAK,0BAA0B,EAC/B,KAAK,2BAA2B,EAChC,KAAK,2BAA2B,EAChC,KAAK,8BAA8B,EACnC,KAAK,uCAAuC,EAC5C,KAAK,kCAAkC,EACvC,KAAK,+CAA+C,GACrD,MAAM,2BAA2B,CAAA;AAElC,OAAO,EACL,+BAA+B,EAC/B,KAAK,sCAAsC,EAC3C,KAAK,yBAAyB,GAC/B,MAAM,iBAAiB,CAAA;AAExB,OAAO,EACL,0CAA0C,EAC1C,0CAA0C,EAC1C,gDAAgD,EAChD,0CAA0C,EAC1C,yCAAyC,EACzC,sCAAsC,EACtC,qCAAqC,EACrC,KAAK,oCAAoC,EACzC,KAAK,mCAAmC,EACxC,KAAK,6BAA6B,EAClC,KAAK,oCAAoC,EACzC,KAAK,mCAAmC,EACxC,KAAK,mCAAmC,EACxC,KAAK,qCAAqC,GAC3C,MAAM,kCAAkC,CAAA;AAEzC,OAAO,EACL,qCAAqC,EACrC,KAAK,4CAA4C,EACjD,KAAK,+BAA+B,GACrC,MAAM,8BAA8B,CAAA;AAErC,OAAO,EACL,iCAAiC,EACjC,KAAK,2BAA2B,EAChC,KAAK,wCAAwC,GAC9C,MAAM,0BAA0B,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ export { DATAGRID_WORKER_PROTOCOL_CHANNEL, DATAGRID_WORKER_PROTOCOL_VERSION, DATAGRID_WORKER_COMPUTE_PAYLOAD_SCHEMA_VERSION, createDataGridWorkerComputeRequestMessage, createDataGridWorkerComputeAckMessage, createDataGridWorkerComputeErrorAckMessage, isDataGridWorkerComputeRequestMessage, isDataGridWorkerComputeAckMessage, resolveWorkerComputeRequestPayload, } from "./protocol.js";
2
+ export { createDataGridWorkerPostMessageTransport, } from "./postMessageTransport.js";
3
+ export { createDataGridWorkerMessageHost, } from "./workerHost.js";
4
+ export { DATAGRID_WORKER_ROW_MODEL_PROTOCOL_CHANNEL, DATAGRID_WORKER_ROW_MODEL_PROTOCOL_VERSION, DATAGRID_WORKER_ROW_MODEL_PAYLOAD_SCHEMA_VERSION, createDataGridWorkerRowModelCommandMessage, createDataGridWorkerRowModelUpdateMessage, isDataGridWorkerRowModelCommandMessage, isDataGridWorkerRowModelUpdateMessage, } from "./workerOwnedRowModelProtocol.js";
5
+ export { createDataGridWorkerOwnedRowModelHost, } from "./workerOwnedRowModelHost.js";
6
+ export { createDataGridWorkerOwnedRowModel, } from "./workerOwnedRowModel.js";
@@ -0,0 +1,31 @@
1
+ import type { DataGridClientComputeTransport } from "@affino/datagrid-core";
2
+ export interface DataGridWorkerMessageEvent<T = unknown> {
3
+ data: T;
4
+ }
5
+ export interface DataGridWorkerMessageTarget {
6
+ postMessage: (message: unknown) => void;
7
+ }
8
+ export interface DataGridWorkerMessageSource {
9
+ addEventListener: (type: "message", listener: (event: DataGridWorkerMessageEvent) => void) => void;
10
+ removeEventListener: (type: "message", listener: (event: DataGridWorkerMessageEvent) => void) => void;
11
+ }
12
+ export type DataGridWorkerDispatchStrategy = "sync-fallback" | "fire-and-forget";
13
+ export interface DataGridWorkerPostMessageTransportStats {
14
+ dispatched: number;
15
+ acked: number;
16
+ errored: number;
17
+ timedOut: number;
18
+ inflight: number;
19
+ }
20
+ export interface DataGridWorkerPostMessageTransport extends DataGridClientComputeTransport {
21
+ getStats: () => DataGridWorkerPostMessageTransportStats;
22
+ }
23
+ export interface CreateDataGridWorkerPostMessageTransportOptions {
24
+ target: DataGridWorkerMessageTarget;
25
+ source?: DataGridWorkerMessageSource | null;
26
+ channel?: string | null;
27
+ dispatchStrategy?: DataGridWorkerDispatchStrategy;
28
+ requestTimeoutMs?: number;
29
+ }
30
+ export declare function createDataGridWorkerPostMessageTransport(options: CreateDataGridWorkerPostMessageTransportOptions): DataGridWorkerPostMessageTransport;
31
+ //# sourceMappingURL=postMessageTransport.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"postMessageTransport.d.ts","sourceRoot":"","sources":["../src/postMessageTransport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,8BAA8B,EAAE,MAAM,uBAAuB,CAAA;AAO3E,MAAM,WAAW,0BAA0B,CAAC,CAAC,GAAG,OAAO;IACrD,IAAI,EAAE,CAAC,CAAA;CACR;AAED,MAAM,WAAW,2BAA2B;IAC1C,WAAW,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,CAAA;CACxC;AAED,MAAM,WAAW,2BAA2B;IAC1C,gBAAgB,EAAE,CAChB,IAAI,EAAE,SAAS,EACf,QAAQ,EAAE,CAAC,KAAK,EAAE,0BAA0B,KAAK,IAAI,KAClD,IAAI,CAAA;IACT,mBAAmB,EAAE,CACnB,IAAI,EAAE,SAAS,EACf,QAAQ,EAAE,CAAC,KAAK,EAAE,0BAA0B,KAAK,IAAI,KAClD,IAAI,CAAA;CACV;AAED,MAAM,MAAM,8BAA8B,GAAG,eAAe,GAAG,iBAAiB,CAAA;AAEhF,MAAM,WAAW,uCAAuC;IACtD,UAAU,EAAE,MAAM,CAAA;IAClB,KAAK,EAAE,MAAM,CAAA;IACb,OAAO,EAAE,MAAM,CAAA;IACf,QAAQ,EAAE,MAAM,CAAA;IAChB,QAAQ,EAAE,MAAM,CAAA;CACjB;AAED,MAAM,WAAW,kCAAmC,SAAQ,8BAA8B;IACxF,QAAQ,EAAE,MAAM,uCAAuC,CAAA;CACxD;AAED,MAAM,WAAW,+CAA+C;IAC9D,MAAM,EAAE,2BAA2B,CAAA;IACnC,MAAM,CAAC,EAAE,2BAA2B,GAAG,IAAI,CAAA;IAC3C,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IACvB,gBAAgB,CAAC,EAAE,8BAA8B,CAAA;IACjD,gBAAgB,CAAC,EAAE,MAAM,CAAA;CAC1B;AAMD,wBAAgB,wCAAwC,CACtD,OAAO,EAAE,+CAA+C,GACvD,kCAAkC,CAuFpC"}
@@ -0,0 +1,81 @@
1
+ import { createDataGridWorkerComputeRequestMessage, isDataGridWorkerComputeAckMessage, } from "./protocol.js";
2
+ export function createDataGridWorkerPostMessageTransport(options) {
3
+ var _a, _b, _c;
4
+ const channel = options.channel;
5
+ const strategy = (_a = options.dispatchStrategy) !== null && _a !== void 0 ? _a : "sync-fallback";
6
+ const requestTimeoutMs = Math.max(0, Math.trunc((_b = options.requestTimeoutMs) !== null && _b !== void 0 ? _b : 15000));
7
+ let nextRequestId = 1;
8
+ let dispatched = 0;
9
+ let acked = 0;
10
+ let errored = 0;
11
+ let timedOut = 0;
12
+ const pendingById = new Map();
13
+ const clearPending = (requestId) => {
14
+ const pending = pendingById.get(requestId);
15
+ if (!pending) {
16
+ return;
17
+ }
18
+ if (pending.timeoutHandle) {
19
+ clearTimeout(pending.timeoutHandle);
20
+ }
21
+ pendingById.delete(requestId);
22
+ };
23
+ const onMessage = (event) => {
24
+ if (!isDataGridWorkerComputeAckMessage(event.data, channel)) {
25
+ return;
26
+ }
27
+ const ackMessage = event.data;
28
+ if (!pendingById.has(ackMessage.requestId)) {
29
+ return;
30
+ }
31
+ clearPending(ackMessage.requestId);
32
+ acked += 1;
33
+ if (ackMessage.payload.error) {
34
+ errored += 1;
35
+ }
36
+ };
37
+ (_c = options.source) === null || _c === void 0 ? void 0 : _c.addEventListener("message", onMessage);
38
+ const dispatchRequest = (request) => {
39
+ const requestId = nextRequestId;
40
+ nextRequestId += 1;
41
+ dispatched += 1;
42
+ const requestMessage = createDataGridWorkerComputeRequestMessage(requestId, request, channel);
43
+ options.target.postMessage(requestMessage);
44
+ if (requestTimeoutMs > 0) {
45
+ const timeoutHandle = setTimeout(() => {
46
+ if (!pendingById.has(requestId)) {
47
+ return;
48
+ }
49
+ pendingById.delete(requestId);
50
+ timedOut += 1;
51
+ }, requestTimeoutMs);
52
+ pendingById.set(requestId, { timeoutHandle });
53
+ return;
54
+ }
55
+ pendingById.set(requestId, { timeoutHandle: null });
56
+ };
57
+ return {
58
+ dispatch(request) {
59
+ dispatchRequest(request);
60
+ return {
61
+ handled: strategy === "fire-and-forget",
62
+ };
63
+ },
64
+ getStats() {
65
+ return {
66
+ dispatched,
67
+ acked,
68
+ errored,
69
+ timedOut,
70
+ inflight: pendingById.size,
71
+ };
72
+ },
73
+ dispose() {
74
+ var _a;
75
+ (_a = options.source) === null || _a === void 0 ? void 0 : _a.removeEventListener("message", onMessage);
76
+ for (const requestId of pendingById.keys()) {
77
+ clearPending(requestId);
78
+ }
79
+ },
80
+ };
81
+ }
@@ -0,0 +1,43 @@
1
+ import type { DataGridClientComputeTransport, DataGridClientComputeTransportResult } from "@affino/datagrid-core";
2
+ export declare const DATAGRID_WORKER_PROTOCOL_VERSION: 1;
3
+ export declare const DATAGRID_WORKER_COMPUTE_PAYLOAD_SCHEMA_VERSION: 2;
4
+ export declare const DATAGRID_WORKER_PROTOCOL_CHANNEL = "affino.datagrid.compute";
5
+ export type DataGridWorkerComputeRequest = Parameters<DataGridClientComputeTransport["dispatch"]>[0];
6
+ export type DataGridWorkerComputeResult = DataGridClientComputeTransportResult;
7
+ export interface DataGridWorkerComputeBatchPlan {
8
+ kind: "stage-plan" | "execution-plan";
9
+ requestedStages: readonly string[];
10
+ blockedStages: readonly string[];
11
+ patchChangedRowCount?: number;
12
+ }
13
+ export interface DataGridWorkerComputeRequestPayloadEnvelope {
14
+ schemaVersion: number;
15
+ request: DataGridWorkerComputeRequest;
16
+ batchPlan?: DataGridWorkerComputeBatchPlan | null;
17
+ }
18
+ export type DataGridWorkerComputeRequestPayload = DataGridWorkerComputeRequest | DataGridWorkerComputeRequestPayloadEnvelope;
19
+ export interface DataGridWorkerProtocolHeader {
20
+ version: typeof DATAGRID_WORKER_PROTOCOL_VERSION;
21
+ channel: string;
22
+ requestId: number;
23
+ timestamp: number;
24
+ }
25
+ export interface DataGridWorkerComputeRequestMessage extends DataGridWorkerProtocolHeader {
26
+ kind: "compute-request";
27
+ payload: DataGridWorkerComputeRequestPayload;
28
+ }
29
+ export interface DataGridWorkerComputeAckMessage extends DataGridWorkerProtocolHeader {
30
+ kind: "compute-ack";
31
+ payload: {
32
+ handled: boolean;
33
+ error?: string | null;
34
+ };
35
+ }
36
+ export type DataGridWorkerProtocolMessage = DataGridWorkerComputeRequestMessage | DataGridWorkerComputeAckMessage;
37
+ export declare function createDataGridWorkerComputeRequestMessage(requestId: number, payload: DataGridWorkerComputeRequest, channel?: string | null): DataGridWorkerComputeRequestMessage;
38
+ export declare function createDataGridWorkerComputeAckMessage(requestId: number, result: DataGridWorkerComputeResult, channel?: string | null): DataGridWorkerComputeAckMessage;
39
+ export declare function createDataGridWorkerComputeErrorAckMessage(requestId: number, error: unknown, channel?: string | null): DataGridWorkerComputeAckMessage;
40
+ export declare function isDataGridWorkerComputeRequestMessage(value: unknown, channel?: string | null): value is DataGridWorkerComputeRequestMessage;
41
+ export declare function isDataGridWorkerComputeAckMessage(value: unknown, channel?: string | null): value is DataGridWorkerComputeAckMessage;
42
+ export declare function resolveWorkerComputeRequestPayload(payload: unknown): DataGridWorkerComputeRequestPayloadEnvelope | null;
43
+ //# sourceMappingURL=protocol.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protocol.d.ts","sourceRoot":"","sources":["../src/protocol.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,8BAA8B,EAAE,oCAAoC,EAAE,MAAM,uBAAuB,CAAA;AAEjH,eAAO,MAAM,gCAAgC,EAAG,CAAU,CAAA;AAC1D,eAAO,MAAM,8CAA8C,EAAG,CAAU,CAAA;AACxE,eAAO,MAAM,gCAAgC,4BAA4B,CAAA;AAEzE,MAAM,MAAM,4BAA4B,GAAG,U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jB,MAAM,EAAE,2BAA2B,EACnC,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,GACtB,+BAA+B,CAYjC;AAED,wBAAgB,0CAA0C,CACxD,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,OAAO,EACd,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,GACtB,+BAA+B,CAejC;AAED,wBAAgB,qCAAqC,CACnD,KAAK,EAAE,OAAO,EACd,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,GACtB,KAAK,IAAI,mCAAmC,CAW9C;AAED,wBAAgB,iCAAiC,CAC/C,KAAK,EAAE,OAAO,EACd,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,GACtB,KAAK,IAAI,+BAA+B,CAO1C;AAED,wBAAgB,kCAAkC,CAChD,OAAO,EAAE,OAAO,GACf,2CAA2C,GAAG,IAAI,CA0BpD"}
@@ -0,0 +1,134 @@
1
+ export const DATAGRID_WORKER_PROTOCOL_VERSION = 1;
2
+ export const DATAGRID_WORKER_COMPUTE_PAYLOAD_SCHEMA_VERSION = 2;
3
+ export const DATAGRID_WORKER_PROTOCOL_CHANNEL = "affino.datagrid.compute";
4
+ function isWorkerComputeRequestKind(value) {
5
+ return (value === "recompute-from-stage"
6
+ || value === "recompute-with-stage-plan"
7
+ || value === "recompute-with-execution-plan"
8
+ || value === "refresh");
9
+ }
10
+ function serializeComputeBatchPlan(request) {
11
+ var _a, _b, _c;
12
+ if (request.kind === "recompute-with-stage-plan") {
13
+ return {
14
+ kind: "stage-plan",
15
+ requestedStages: [...request.plan.requestedStages],
16
+ blockedStages: [...((_a = request.plan.blockedStages) !== null && _a !== void 0 ? _a : [])],
17
+ };
18
+ }
19
+ if (request.kind === "recompute-with-execution-plan") {
20
+ return {
21
+ kind: "execution-plan",
22
+ requestedStages: [...request.plan.requestedStages],
23
+ blockedStages: [...((_b = request.plan.blockedStages) !== null && _b !== void 0 ? _b : [])],
24
+ ...(typeof ((_c = request.options) === null || _c === void 0 ? void 0 : _c.patchChangedRowCount) === "number"
25
+ ? { patchChangedRowCount: request.options.patchChangedRowCount }
26
+ : {}),
27
+ };
28
+ }
29
+ return null;
30
+ }
31
+ function normalizeChannel(channel) {
32
+ const normalized = channel === null || channel === void 0 ? void 0 : channel.trim();
33
+ return normalized && normalized.length > 0
34
+ ? normalized
35
+ : DATAGRID_WORKER_PROTOCOL_CHANNEL;
36
+ }
37
+ function isWorkerProtocolHeader(value) {
38
+ if (!value || typeof value !== "object") {
39
+ return false;
40
+ }
41
+ const candidate = value;
42
+ return (candidate.version === DATAGRID_WORKER_PROTOCOL_VERSION &&
43
+ typeof candidate.channel === "string" &&
44
+ Number.isFinite(candidate.requestId) &&
45
+ Number.isFinite(candidate.timestamp));
46
+ }
47
+ export function createDataGridWorkerComputeRequestMessage(requestId, payload, channel) {
48
+ const batchPlan = serializeComputeBatchPlan(payload);
49
+ return {
50
+ kind: "compute-request",
51
+ version: DATAGRID_WORKER_PROTOCOL_VERSION,
52
+ channel: normalizeChannel(channel),
53
+ requestId,
54
+ timestamp: Date.now(),
55
+ payload: {
56
+ schemaVersion: DATAGRID_WORKER_COMPUTE_PAYLOAD_SCHEMA_VERSION,
57
+ request: payload,
58
+ batchPlan,
59
+ },
60
+ };
61
+ }
62
+ export function createDataGridWorkerComputeAckMessage(requestId, result, channel) {
63
+ return {
64
+ kind: "compute-ack",
65
+ version: DATAGRID_WORKER_PROTOCOL_VERSION,
66
+ channel: normalizeChannel(channel),
67
+ requestId,
68
+ timestamp: Date.now(),
69
+ payload: {
70
+ handled: (result === null || result === void 0 ? void 0 : result.handled) === true,
71
+ error: null,
72
+ },
73
+ };
74
+ }
75
+ export function createDataGridWorkerComputeErrorAckMessage(requestId, error, channel) {
76
+ const message = error instanceof Error
77
+ ? error.message
78
+ : String(error !== null && error !== void 0 ? error : "unknown worker error");
79
+ return {
80
+ kind: "compute-ack",
81
+ version: DATAGRID_WORKER_PROTOCOL_VERSION,
82
+ channel: normalizeChannel(channel),
83
+ requestId,
84
+ timestamp: Date.now(),
85
+ payload: {
86
+ handled: false,
87
+ error: message,
88
+ },
89
+ };
90
+ }
91
+ export function isDataGridWorkerComputeRequestMessage(value, channel) {
92
+ if (!isWorkerProtocolHeader(value)) {
93
+ return false;
94
+ }
95
+ const candidate = value;
96
+ const channelMatches = candidate.channel === normalizeChannel(channel);
97
+ return (candidate.kind === "compute-request"
98
+ && channelMatches
99
+ && resolveWorkerComputeRequestPayload(candidate.payload) !== null);
100
+ }
101
+ export function isDataGridWorkerComputeAckMessage(value, channel) {
102
+ if (!isWorkerProtocolHeader(value)) {
103
+ return false;
104
+ }
105
+ const candidate = value;
106
+ const channelMatches = candidate.channel === normalizeChannel(channel);
107
+ return candidate.kind === "compute-ack" && channelMatches && candidate.payload != null;
108
+ }
109
+ export function resolveWorkerComputeRequestPayload(payload) {
110
+ var _a;
111
+ if (!payload || typeof payload !== "object") {
112
+ return null;
113
+ }
114
+ const directCandidate = payload;
115
+ if (isWorkerComputeRequestKind(directCandidate.kind)) {
116
+ return {
117
+ schemaVersion: 1,
118
+ request: directCandidate,
119
+ batchPlan: serializeComputeBatchPlan(directCandidate),
120
+ };
121
+ }
122
+ const envelope = payload;
123
+ if (typeof envelope.schemaVersion !== "number"
124
+ || !Number.isFinite(envelope.schemaVersion)
125
+ || !envelope.request
126
+ || !isWorkerComputeRequestKind(envelope.request.kind)) {
127
+ return null;
128
+ }
129
+ return {
130
+ schemaVersion: envelope.schemaVersion,
131
+ request: envelope.request,
132
+ batchPlan: (_a = envelope.batchPlan) !== null && _a !== void 0 ? _a : null,
133
+ };
134
+ }
@@ -0,0 +1,13 @@
1
+ import { type DataGridWorkerComputeRequest, type DataGridWorkerComputeResult } from "./protocol.js";
2
+ import type { DataGridWorkerMessageSource, DataGridWorkerMessageTarget } from "./postMessageTransport.js";
3
+ export interface CreateDataGridWorkerMessageHostOptions {
4
+ source: DataGridWorkerMessageSource;
5
+ target: DataGridWorkerMessageTarget;
6
+ channel?: string | null;
7
+ handleRequest: (request: DataGridWorkerComputeRequest) => DataGridWorkerComputeResult | Promise<DataGridWorkerComputeResult>;
8
+ }
9
+ export interface DataGridWorkerMessageHost {
10
+ dispose: () => void;
11
+ }
12
+ export declare function createDataGridWorkerMessageHost(options: CreateDataGridWorkerMessageHostOptions): DataGridWorkerMessageHost;
13
+ //# sourceMappingURL=workerHost.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"workerHost.d.ts","sourceRoot":"","sources":["../src/workerHost.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,KAAK,4BAA4B,EACjC,KAAK,2BAA2B,EACjC,MAAM,eAAe,CAAA;AACtB,OAAO,KAAK,EAEV,2BAA2B,EAC3B,2BAA2B,EAC5B,MAAM,2BAA2B,CAAA;AAElC,MAAM,WAAW,sCAAsC;IACrD,MAAM,EAAE,2BAA2B,CAAA;IACnC,MAAM,EAAE,2BAA2B,CAAA;IACnC,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IACvB,aAAa,EAAE,CAAC,OAAO,EAAE,4BAA4B,KAAK,2BAA2B,GAAG,OAAO,CAAC,2BAA2B,CAAC,CAAA;CAC7H;AAED,MAAM,WAAW,yBAAyB;IACxC,OAAO,EAAE,MAAM,IAAI,CAAA;CACpB;AAED,wBAAgB,+BAA+B,CAC7C,OAAO,EAAE,sCAAsC,GAC9C,yBAAyB,CAoE3B"}
@@ -0,0 +1,58 @@
1
+ import { createDataGridWorkerComputeAckMessage, createDataGridWorkerComputeErrorAckMessage, isDataGridWorkerComputeRequestMessage, resolveWorkerComputeRequestPayload, } from "./protocol.js";
2
+ export function createDataGridWorkerMessageHost(options) {
3
+ let disposed = false;
4
+ const postSuccessAck = (requestId, result) => {
5
+ if (disposed) {
6
+ return;
7
+ }
8
+ const ack = createDataGridWorkerComputeAckMessage(requestId, result, options.channel);
9
+ options.target.postMessage(ack);
10
+ };
11
+ const postErrorAck = (requestId, error) => {
12
+ if (disposed) {
13
+ return;
14
+ }
15
+ const ack = createDataGridWorkerComputeErrorAckMessage(requestId, error, options.channel);
16
+ options.target.postMessage(ack);
17
+ };
18
+ const onMessage = (event) => {
19
+ if (disposed) {
20
+ return;
21
+ }
22
+ if (!isDataGridWorkerComputeRequestMessage(event.data, options.channel)) {
23
+ return;
24
+ }
25
+ const requestMessage = event.data;
26
+ const payload = resolveWorkerComputeRequestPayload(requestMessage.payload);
27
+ if (!payload) {
28
+ return;
29
+ }
30
+ try {
31
+ const result = options.handleRequest(payload.request);
32
+ if (result instanceof Promise) {
33
+ result
34
+ .then((resolvedResult) => {
35
+ postSuccessAck(requestMessage.requestId, resolvedResult);
36
+ })
37
+ .catch((error) => {
38
+ postErrorAck(requestMessage.requestId, error);
39
+ });
40
+ return;
41
+ }
42
+ postSuccessAck(requestMessage.requestId, result);
43
+ }
44
+ catch (error) {
45
+ postErrorAck(requestMessage.requestId, error);
46
+ }
47
+ };
48
+ options.source.addEventListener("message", onMessage);
49
+ return {
50
+ dispose() {
51
+ if (disposed) {
52
+ return;
53
+ }
54
+ disposed = true;
55
+ options.source.removeEventListener("message", onMessage);
56
+ },
57
+ };
58
+ }
@@ -0,0 +1,41 @@
1
+ import type { DataGridClientComputeDiagnostics, DataGridClientRowPatch, DataGridClientRowPatchOptions, DataGridFormulaComputeStageDiagnostics, DataGridFormulaFieldSnapshot, DataGridRowModel, DataGridRowModelSnapshot, DataGridRowNodeInput } from "@affino/datagrid-core";
2
+ import type { DataGridFormulaExecutionPlanSnapshot, DataGridFormulaFieldDefinition } from "@affino/datagrid-formula-engine";
3
+ import type { DataGridWorkerMessageSource, DataGridWorkerMessageTarget } from "./postMessageTransport.js";
4
+ export interface DataGridWorkerOwnedRowModel<T = unknown> extends DataGridRowModel<T> {
5
+ setRows: (rows: readonly DataGridRowNodeInput<T>[]) => void;
6
+ patchRows: (updates: readonly DataGridClientRowPatch<T>[], options?: DataGridClientRowPatchOptions) => void;
7
+ registerFormulaField: (definition: DataGridFormulaFieldDefinition) => void;
8
+ getFormulaFields: () => readonly DataGridFormulaFieldSnapshot[];
9
+ getFormulaExecutionPlan: () => DataGridFormulaExecutionPlanSnapshot | null;
10
+ getFormulaComputeStageDiagnostics: () => DataGridFormulaComputeStageDiagnostics | null;
11
+ getComputeDiagnostics: () => DataGridClientComputeDiagnostics;
12
+ getWorkerProtocolDiagnostics: () => {
13
+ updatesReceived: number;
14
+ updatesApplied: number;
15
+ updatesDroppedStale: number;
16
+ updatesDroppedInitialAfterApplied: number;
17
+ commandsCoalesced: number;
18
+ patchCommandsCoalesced: number;
19
+ patchUpdatesReceived: number;
20
+ patchUpdatesDispatched: number;
21
+ patchUpdatesMergedAway: number;
22
+ immediateFlushCount: number;
23
+ queuePeak: number;
24
+ protocolVersion: number;
25
+ expectedPayloadSchemaVersion: number;
26
+ lastPayloadSchemaVersion: number | null;
27
+ payloadSchemaMismatches: number;
28
+ loadingSetCount: number;
29
+ loadingClearCount: number;
30
+ };
31
+ }
32
+ export interface CreateDataGridWorkerOwnedRowModelOptions<T = unknown> {
33
+ source: DataGridWorkerMessageSource;
34
+ target: DataGridWorkerMessageTarget;
35
+ channel?: string | null;
36
+ initialSnapshot?: DataGridRowModelSnapshot<T> | null;
37
+ requestInitialSync?: boolean;
38
+ viewportCoalescingStrategy?: "split" | "simple";
39
+ }
40
+ export declare function createDataGridWorkerOwnedRowModel<T = unknown>(options: CreateDataGridWorkerOwnedRowModelOptions<T>): DataGridWorkerOwnedRowModel<T>;
41
+ //# sourceMappingURL=workerOwnedRowModel.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"workerOwnedRowModel.d.ts","sourceRoot":"","sources":["../src/workerOwnedRowModel.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,gCAAgC,EAChC,sBAAsB,EACtB,6BAA6B,EAC7B,sCAAsC,EACtC,4BAA4B,EAO5B,gBAAgB,EAGhB,wBAAwB,EAExB,oBAAoB,EAIrB,MAAM,uBAAuB,CAAA;AAC9B,OAAO,KAAK,EACV,oCAAoC,EAC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