@uwdata/mosaic-core 0.16.2 → 0.18.0
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 +1 -1
- package/README.md +0 -1
- package/dist/src/Coordinator.d.ts +147 -0
- package/dist/src/Coordinator.d.ts.map +1 -0
- package/dist/src/Coordinator.js +269 -0
- package/dist/src/Coordinator.js.map +1 -0
- package/dist/{types → src}/MosaicClient.d.ts +37 -42
- package/dist/src/MosaicClient.d.ts.map +1 -0
- package/dist/src/MosaicClient.js +213 -0
- package/dist/src/MosaicClient.js.map +1 -0
- package/dist/src/Param.d.ts +56 -0
- package/dist/src/Param.d.ts.map +1 -0
- package/dist/src/Param.js +89 -0
- package/dist/src/Param.js.map +1 -0
- package/dist/src/QueryConsolidator.d.ts +11 -0
- package/dist/src/QueryConsolidator.d.ts.map +1 -0
- package/dist/src/QueryConsolidator.js +249 -0
- package/dist/src/QueryConsolidator.js.map +1 -0
- package/dist/src/QueryManager.d.ts +77 -0
- package/dist/src/QueryManager.d.ts.map +1 -0
- package/dist/src/QueryManager.js +174 -0
- package/dist/src/QueryManager.js.map +1 -0
- package/dist/src/Selection.d.ts +222 -0
- package/dist/src/Selection.d.ts.map +1 -0
- package/dist/src/Selection.js +319 -0
- package/dist/src/Selection.js.map +1 -0
- package/dist/src/SelectionClause.d.ts +192 -0
- package/dist/src/SelectionClause.d.ts.map +1 -0
- package/dist/src/SelectionClause.js +126 -0
- package/dist/src/SelectionClause.js.map +1 -0
- package/dist/{types → src}/connectors/Connector.d.ts +6 -5
- package/dist/src/connectors/Connector.d.ts.map +1 -0
- package/dist/src/connectors/Connector.js +2 -0
- package/dist/src/connectors/Connector.js.map +1 -0
- package/dist/src/connectors/rest.d.ts +24 -0
- package/dist/src/connectors/rest.d.ts.map +1 -0
- package/dist/src/connectors/rest.js +37 -0
- package/dist/src/connectors/rest.js.map +1 -0
- package/dist/src/connectors/socket.d.ts +40 -0
- package/dist/src/connectors/socket.d.ts.map +1 -0
- package/dist/src/connectors/socket.js +115 -0
- package/dist/src/connectors/socket.js.map +1 -0
- package/dist/src/connectors/wasm.d.ts +53 -0
- package/dist/src/connectors/wasm.d.ts.map +1 -0
- package/dist/src/connectors/wasm.js +113 -0
- package/dist/src/connectors/wasm.js.map +1 -0
- package/dist/src/index.d.ts +28 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +25 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/make-client.d.ts +35 -0
- package/dist/src/make-client.d.ts.map +1 -0
- package/dist/src/make-client.js +52 -0
- package/dist/src/make-client.js.map +1 -0
- package/dist/{types → src}/preagg/PreAggregator.d.ts +64 -94
- package/dist/src/preagg/PreAggregator.d.ts.map +1 -0
- package/dist/src/preagg/PreAggregator.js +382 -0
- package/dist/src/preagg/PreAggregator.js.map +1 -0
- package/dist/{types → src}/preagg/preagg-columns.d.ts +10 -8
- package/dist/src/preagg/preagg-columns.d.ts.map +1 -0
- package/dist/src/preagg/preagg-columns.js +95 -0
- package/dist/src/preagg/preagg-columns.js.map +1 -0
- package/dist/src/preagg/sufficient-statistics.d.ts +14 -0
- package/dist/src/preagg/sufficient-statistics.d.ts.map +1 -0
- package/dist/src/preagg/sufficient-statistics.js +446 -0
- package/dist/src/preagg/sufficient-statistics.js.map +1 -0
- package/dist/{types → src}/types.d.ts +23 -9
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +2 -0
- package/dist/src/types.js.map +1 -0
- package/dist/{types → src}/util/AsyncDispatch.d.ts +53 -32
- package/dist/src/util/AsyncDispatch.d.ts.map +1 -0
- package/dist/src/util/AsyncDispatch.js +188 -0
- package/dist/src/util/AsyncDispatch.js.map +1 -0
- package/dist/src/util/cache.d.ts +19 -0
- package/dist/src/util/cache.d.ts.map +1 -0
- package/dist/src/util/cache.js +66 -0
- package/dist/src/util/cache.js.map +1 -0
- package/dist/src/util/decode-ipc.d.ts +12 -0
- package/dist/src/util/decode-ipc.d.ts.map +1 -0
- package/{src → dist/src}/util/decode-ipc.js +5 -6
- package/dist/src/util/decode-ipc.js.map +1 -0
- package/dist/src/util/distinct.d.ts +3 -0
- package/dist/src/util/distinct.d.ts.map +1 -0
- package/dist/src/util/distinct.js +16 -0
- package/dist/src/util/distinct.js.map +1 -0
- package/dist/src/util/field-info.d.ts +26 -0
- package/dist/src/util/field-info.d.ts.map +1 -0
- package/dist/src/util/field-info.js +91 -0
- package/dist/src/util/field-info.js.map +1 -0
- package/dist/src/util/hash.d.ts +2 -0
- package/dist/src/util/hash.d.ts.map +1 -0
- package/dist/src/util/hash.js +26 -0
- package/dist/src/util/hash.js.map +1 -0
- package/dist/src/util/is-activatable.d.ts +8 -0
- package/dist/src/util/is-activatable.d.ts.map +1 -0
- package/dist/src/util/is-activatable.js +10 -0
- package/dist/src/util/is-activatable.js.map +1 -0
- package/dist/src/util/is-arrow-table.d.ts +9 -0
- package/dist/src/util/is-arrow-table.d.ts.map +1 -0
- package/dist/src/util/is-arrow-table.js +11 -0
- package/dist/src/util/is-arrow-table.js.map +1 -0
- package/dist/src/util/js-type.d.ts +9 -0
- package/dist/src/util/js-type.d.ts.map +1 -0
- package/dist/src/util/js-type.js +59 -0
- package/dist/src/util/js-type.js.map +1 -0
- package/dist/{types → src}/util/priority-queue.d.ts +12 -14
- package/dist/src/util/priority-queue.d.ts.map +1 -0
- package/dist/src/util/priority-queue.js +81 -0
- package/dist/src/util/priority-queue.js.map +1 -0
- package/dist/src/util/query-result.d.ts +47 -0
- package/dist/src/util/query-result.d.ts.map +1 -0
- package/dist/src/util/query-result.js +83 -0
- package/dist/src/util/query-result.js.map +1 -0
- package/dist/src/util/synchronizer.d.ts +36 -0
- package/dist/src/util/synchronizer.d.ts.map +1 -0
- package/dist/src/util/synchronizer.js +52 -0
- package/dist/src/util/synchronizer.js.map +1 -0
- package/dist/src/util/throttle.d.ts +12 -0
- package/dist/src/util/throttle.d.ts.map +1 -0
- package/dist/src/util/throttle.js +51 -0
- package/dist/src/util/throttle.js.map +1 -0
- package/dist/src/util/to-data-columns.d.ts +22 -0
- package/dist/src/util/to-data-columns.d.ts.map +1 -0
- package/dist/src/util/to-data-columns.js +51 -0
- package/dist/src/util/to-data-columns.js.map +1 -0
- package/dist/src/util/void-logger.d.ts +13 -0
- package/dist/src/util/void-logger.d.ts.map +1 -0
- package/dist/src/util/void-logger.js +13 -0
- package/dist/src/util/void-logger.js.map +1 -0
- package/package.json +15 -13
- package/src/Coordinator.ts +367 -0
- package/src/{MosaicClient.js → MosaicClient.ts} +49 -43
- package/src/{Param.js → Param.ts} +29 -28
- package/src/{QueryConsolidator.js → QueryConsolidator.ts} +81 -58
- package/src/{QueryManager.js → QueryManager.ts} +61 -54
- package/src/Selection.ts +388 -0
- package/src/SelectionClause.ts +275 -0
- package/src/connectors/Connector.ts +6 -6
- package/src/connectors/rest.ts +56 -0
- package/src/connectors/{socket.js → socket.ts} +53 -42
- package/src/connectors/{wasm.js → wasm.ts} +46 -62
- package/src/{index.js → index.ts} +13 -1
- package/src/make-client.ts +93 -0
- package/src/preagg/{PreAggregator.js → PreAggregator.ts} +164 -145
- package/src/preagg/{preagg-columns.js → preagg-columns.ts} +27 -24
- package/src/preagg/{sufficient-statistics.js → sufficient-statistics.ts} +161 -110
- package/src/types.ts +24 -9
- package/src/util/{AsyncDispatch.js → AsyncDispatch.ts} +62 -43
- package/src/util/{cache.js → cache.ts} +25 -15
- package/src/util/decode-ipc.ts +15 -0
- package/src/util/{distinct.js → distinct.ts} +3 -3
- package/src/util/{field-info.js → field-info.ts} +31 -32
- package/src/util/{hash.js → hash.ts} +4 -4
- package/src/util/is-activatable.ts +11 -0
- package/src/util/is-arrow-table.ts +12 -0
- package/src/util/{js-type.js → js-type.ts} +7 -5
- package/src/util/{priority-queue.js → priority-queue.ts} +32 -20
- package/src/util/{query-result.js → query-result.ts} +24 -17
- package/src/util/synchronizer.ts +56 -0
- package/src/util/throttle.ts +59 -0
- package/src/util/to-data-columns.ts +65 -0
- package/src/util/void-logger.ts +23 -0
- package/dist/types/Coordinator.d.ts +0 -164
- package/dist/types/Param.d.ts +0 -47
- package/dist/types/QueryConsolidator.d.ts +0 -9
- package/dist/types/QueryManager.d.ts +0 -91
- package/dist/types/Selection.d.ts +0 -235
- package/dist/types/SelectionClause.d.ts +0 -105
- package/dist/types/connectors/rest.d.ts +0 -13
- package/dist/types/connectors/socket.d.ts +0 -100
- package/dist/types/connectors/wasm.d.ts +0 -135
- package/dist/types/index-types.d.ts +0 -4
- package/dist/types/index.d.ts +0 -19
- package/dist/types/make-client.d.ts +0 -78
- package/dist/types/preagg/sufficient-statistics.d.ts +0 -13
- package/dist/types/util/cache.d.ts +0 -17
- package/dist/types/util/decode-ipc.d.ts +0 -12
- package/dist/types/util/distinct.d.ts +0 -2
- package/dist/types/util/field-info.d.ts +0 -23
- package/dist/types/util/hash.d.ts +0 -1
- package/dist/types/util/is-activatable.d.ts +0 -6
- package/dist/types/util/is-arrow-table.d.ts +0 -8
- package/dist/types/util/js-type.d.ts +0 -7
- package/dist/types/util/query-result.d.ts +0 -44
- package/dist/types/util/selection-types.d.ts +0 -114
- package/dist/types/util/synchronizer.d.ts +0 -29
- package/dist/types/util/throttle.d.ts +0 -13
- package/dist/types/util/to-data-columns.d.ts +0 -29
- package/dist/types/util/void-logger.d.ts +0 -10
- package/jsconfig.json +0 -11
- package/src/Coordinator.js +0 -337
- package/src/Selection.js +0 -380
- package/src/SelectionClause.js +0 -159
- package/src/connectors/rest.js +0 -38
- package/src/index-types.ts +0 -4
- package/src/make-client.js +0 -101
- package/src/util/is-activatable.js +0 -8
- package/src/util/is-arrow-table.js +0 -10
- package/src/util/selection-types.ts +0 -137
- package/src/util/synchronizer.js +0 -47
- package/src/util/throttle.js +0 -54
- package/src/util/to-data-columns.js +0 -60
- package/src/util/void-logger.js +0 -13
- package/tsconfig.json +0 -11
- package/vitest.config.ts +0 -3
|
@@ -1,109 +1,93 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import type { ExtractionOptions, Table } from '@uwdata/flechette';
|
|
2
|
+
import type { ArrowQueryRequest, Connector, ExecQueryRequest, JSONQueryRequest, ConnectorQueryRequest } from './Connector.js';
|
|
3
3
|
import * as duckdb from '@duckdb/duckdb-wasm';
|
|
4
4
|
import { decodeIPC } from '../util/decode-ipc.js';
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
interface DuckDBWASMOptions {
|
|
7
|
+
/** Flag to enable logging. */
|
|
8
|
+
log?: boolean;
|
|
9
|
+
}
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
* Optional pre-existing DuckDB-WASM connection.
|
|
20
|
-
*/
|
|
11
|
+
interface DuckDBWASMConnectorOptions extends DuckDBWASMOptions {
|
|
12
|
+
/** Arrow IPC extraction options. */
|
|
13
|
+
ipc?: ExtractionOptions;
|
|
14
|
+
/** Optional pre-existing DuckDB-WASM instance. */
|
|
15
|
+
duckdb?: duckdb.AsyncDuckDB;
|
|
16
|
+
/** Optional pre-existing DuckDB-WASM connection. */
|
|
17
|
+
connection?: duckdb.AsyncDuckDBConnection;
|
|
18
|
+
}
|
|
21
19
|
|
|
22
20
|
/**
|
|
23
21
|
* Connect to a DuckDB-WASM instance.
|
|
24
|
-
* @param
|
|
25
|
-
* @returns
|
|
22
|
+
* @param options Connector options.
|
|
23
|
+
* @returns A connector instance.
|
|
26
24
|
*/
|
|
27
|
-
export function wasmConnector(options = {}) {
|
|
25
|
+
export function wasmConnector(options: DuckDBWASMConnectorOptions = {}): DuckDBWASMConnector {
|
|
28
26
|
return new DuckDBWASMConnector(options);
|
|
29
27
|
}
|
|
30
28
|
|
|
31
29
|
/**
|
|
32
30
|
* DuckDB-WASM connector.
|
|
33
|
-
* @implements {Connector}
|
|
34
31
|
*/
|
|
35
|
-
export class DuckDBWASMConnector {
|
|
32
|
+
export class DuckDBWASMConnector implements Connector {
|
|
33
|
+
private _ipc?: ExtractionOptions;
|
|
34
|
+
public _options: DuckDBWASMOptions;
|
|
35
|
+
public _db?: duckdb.AsyncDuckDB;
|
|
36
|
+
public _con?: duckdb.AsyncDuckDBConnection;
|
|
37
|
+
public _loadPromise?: Promise<unknown>;
|
|
38
|
+
|
|
36
39
|
/**
|
|
37
40
|
* Create a new DuckDB-WASM connector instance.
|
|
38
|
-
* @param
|
|
41
|
+
* @param options Connector options.
|
|
39
42
|
*/
|
|
40
|
-
constructor(options = {}) {
|
|
43
|
+
constructor(options: DuckDBWASMConnectorOptions = {}) {
|
|
41
44
|
const { ipc, duckdb, connection, ...opts } = options;
|
|
42
|
-
/** @type {ExtractionOptions} */
|
|
43
45
|
this._ipc = ipc;
|
|
44
|
-
/** @type {DuckDBWASMOptions} */
|
|
45
46
|
this._options = opts;
|
|
46
|
-
/** @type {duckdb.AsyncDuckDB} */
|
|
47
47
|
this._db = duckdb;
|
|
48
|
-
/** @type {duckdb.AsyncDuckDBConnection} */
|
|
49
48
|
this._con = connection;
|
|
50
|
-
/** @type {Promise<unknown>} */
|
|
51
|
-
this._loadPromise;
|
|
52
49
|
}
|
|
53
50
|
|
|
54
51
|
/**
|
|
55
52
|
* Get the backing DuckDB-WASM instance.
|
|
56
53
|
* Lazily initializes DuckDB-WASM if not already loaded.
|
|
57
|
-
* @returns
|
|
54
|
+
* @returns The DuckDB-WASM instance.
|
|
58
55
|
*/
|
|
59
|
-
async getDuckDB() {
|
|
56
|
+
async getDuckDB(): Promise<duckdb.AsyncDuckDB> {
|
|
60
57
|
if (!this._db) await connect(this);
|
|
61
|
-
return this._db
|
|
58
|
+
return this._db!;
|
|
62
59
|
}
|
|
63
60
|
|
|
64
61
|
/**
|
|
65
62
|
* Get the backing DuckDB-WASM connection.
|
|
66
63
|
* Lazily initializes DuckDB-WASM if not already loaded.
|
|
67
|
-
* @returns
|
|
64
|
+
* @returns The DuckDB-WASM connection.
|
|
68
65
|
*/
|
|
69
|
-
async getConnection() {
|
|
66
|
+
async getConnection(): Promise<duckdb.AsyncDuckDBConnection> {
|
|
70
67
|
if (!this._con) await connect(this);
|
|
71
|
-
return this._con
|
|
68
|
+
return this._con!;
|
|
72
69
|
}
|
|
73
70
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
*
|
|
79
|
-
* @overload
|
|
80
|
-
* @param {ExecQueryRequest} query
|
|
81
|
-
* @returns {Promise<void>}
|
|
82
|
-
*
|
|
83
|
-
* @overload
|
|
84
|
-
* @param {JSONQueryRequest} query
|
|
85
|
-
* @returns {Promise<Record<string, any>[]>}
|
|
86
|
-
*
|
|
87
|
-
* @param {ArrowQueryRequest | ExecQueryRequest | JSONQueryRequest} query
|
|
88
|
-
* @returns {Promise<Table | void | Record<string, any>[]>}}
|
|
89
|
-
*/
|
|
90
|
-
async query(query) {
|
|
71
|
+
async query(query: ArrowQueryRequest): Promise<Table>;
|
|
72
|
+
async query(query: ExecQueryRequest): Promise<void>;
|
|
73
|
+
async query(query: JSONQueryRequest): Promise<Record<string, unknown>[]>;
|
|
74
|
+
async query(query: ConnectorQueryRequest): Promise<unknown> {
|
|
91
75
|
const { type, sql } = query;
|
|
92
76
|
const con = await this.getConnection();
|
|
93
77
|
const result = await getArrowIPC(con, sql);
|
|
94
78
|
return type === 'exec' ? undefined
|
|
95
79
|
: type === 'arrow' ? decodeIPC(result, this._ipc)
|
|
96
80
|
: decodeIPC(result).toArray();
|
|
97
|
-
}
|
|
81
|
+
}
|
|
98
82
|
}
|
|
99
83
|
|
|
100
84
|
/**
|
|
101
85
|
* Bypass duckdb-wasm query method to get Arrow IPC bytes directly.
|
|
102
86
|
* https://github.com/duckdb/duckdb-wasm/issues/267#issuecomment-2252749509
|
|
103
|
-
* @param
|
|
104
|
-
* @param
|
|
87
|
+
* @param con The DuckDB-WASM connection.
|
|
88
|
+
* @param query The SQL query to run.
|
|
105
89
|
*/
|
|
106
|
-
function getArrowIPC(con, query) {
|
|
90
|
+
function getArrowIPC(con: duckdb.AsyncDuckDBConnection, query: string): Promise<ArrayBuffer> {
|
|
107
91
|
return new Promise((resolve, reject) => {
|
|
108
92
|
con.useUnsafe(async (bindings, conn) => {
|
|
109
93
|
try {
|
|
@@ -118,10 +102,10 @@ function getArrowIPC(con, query) {
|
|
|
118
102
|
|
|
119
103
|
/**
|
|
120
104
|
* Establish a new database connection for the given connector.
|
|
121
|
-
* @param
|
|
122
|
-
* @returns
|
|
105
|
+
* @param c The connector.
|
|
106
|
+
* @returns Connection promise.
|
|
123
107
|
*/
|
|
124
|
-
function connect(c) {
|
|
108
|
+
function connect(c: DuckDBWASMConnector): Promise<unknown> {
|
|
125
109
|
if (!c._loadPromise) {
|
|
126
110
|
// use a loading promise to avoid race conditions
|
|
127
111
|
// synchronizes multiple callees on the same load
|
|
@@ -137,11 +121,11 @@ function connect(c) {
|
|
|
137
121
|
|
|
138
122
|
/**
|
|
139
123
|
* Initialize a new DuckDB-WASM instance.
|
|
140
|
-
* @param
|
|
124
|
+
* @param options Database initialization options.
|
|
141
125
|
*/
|
|
142
126
|
async function initDatabase({
|
|
143
127
|
log = false
|
|
144
|
-
} = {}) {
|
|
128
|
+
}: DuckDBWASMOptions = {}): Promise<duckdb.AsyncDuckDB> {
|
|
145
129
|
const JSDELIVR_BUNDLES = duckdb.getJsDelivrBundles();
|
|
146
130
|
|
|
147
131
|
// Select a bundle based on browser checks
|
|
@@ -159,4 +143,4 @@ async function initDatabase({
|
|
|
159
143
|
URL.revokeObjectURL(worker_url);
|
|
160
144
|
|
|
161
145
|
return db;
|
|
162
|
-
}
|
|
146
|
+
}
|
|
@@ -5,10 +5,15 @@ export { Selection, isSelection } from './Selection.js';
|
|
|
5
5
|
export { Param, isParam } from './Param.js';
|
|
6
6
|
export { Priority } from './QueryManager.js';
|
|
7
7
|
|
|
8
|
+
// deprecated in favor of constructors
|
|
8
9
|
export { restConnector } from './connectors/rest.js';
|
|
9
10
|
export { socketConnector } from './connectors/socket.js';
|
|
10
11
|
export { wasmConnector } from './connectors/wasm.js';
|
|
11
12
|
|
|
13
|
+
export { RestConnector } from './connectors/rest.js';
|
|
14
|
+
export { SocketConnector } from './connectors/socket.js';
|
|
15
|
+
export { DuckDBWASMConnector } from './connectors/wasm.js';
|
|
16
|
+
|
|
12
17
|
export {
|
|
13
18
|
clauseInterval,
|
|
14
19
|
clauseIntervals,
|
|
@@ -20,9 +25,16 @@ export {
|
|
|
20
25
|
export { decodeIPC } from './util/decode-ipc.js';
|
|
21
26
|
export { distinct } from './util/distinct.js';
|
|
22
27
|
export { isArrowTable } from './util/is-arrow-table.js';
|
|
23
|
-
export {
|
|
28
|
+
export { Synchronizer } from './util/synchronizer.js';
|
|
24
29
|
export { throttle } from './util/throttle.js';
|
|
25
30
|
export { toDataColumns } from './util/to-data-columns.js';
|
|
26
31
|
export { queryFieldInfo } from './util/field-info.js';
|
|
27
32
|
export { jsType } from './util/js-type.js';
|
|
28
33
|
export { isActivatable } from './util/is-activatable.js';
|
|
34
|
+
export type { QueryResult } from './util/query-result.js';
|
|
35
|
+
|
|
36
|
+
export * from './types.js';
|
|
37
|
+
|
|
38
|
+
export type * from './connectors/Connector.js';
|
|
39
|
+
export type * from './Selection.js';
|
|
40
|
+
export type * from './SelectionClause.js';
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { Coordinator } from './Coordinator.js';
|
|
2
|
+
import type { Selection } from './Selection.js';
|
|
3
|
+
import type { FilterExpr } from '@uwdata/mosaic-sql';
|
|
4
|
+
import { type ClientQuery, MosaicClient } from './MosaicClient.js';
|
|
5
|
+
import { coordinator as defaultCoordinator } from './Coordinator.js';
|
|
6
|
+
|
|
7
|
+
export interface MakeClientOptions {
|
|
8
|
+
/** Mosaic coordinator. Defaults to the global coordinator. */
|
|
9
|
+
coordinator?: Coordinator;
|
|
10
|
+
/** A selection whose predicates are fed into the query function to produce the SQL query. */
|
|
11
|
+
selection?: Selection;
|
|
12
|
+
/** A flag (default `true`) indicating if the client should initially be enabled or not. */
|
|
13
|
+
enabled?: boolean;
|
|
14
|
+
/** A flag (default `true`) indicating if client queries can be sped up using pre-aggregated data.
|
|
15
|
+
* Should be set to `false` if filtering changes the groupby domain of the query. */
|
|
16
|
+
filterStable?: boolean;
|
|
17
|
+
/** An async function to prepare the client before running queries. */
|
|
18
|
+
prepare?: () => Promise<void>;
|
|
19
|
+
/** A function that returns a query from a list of selection predicates. */
|
|
20
|
+
query?: (filter: FilterExpr) => ClientQuery;
|
|
21
|
+
/** Called by the coordinator to return a query result. */
|
|
22
|
+
queryResult?: (data: unknown) => void;
|
|
23
|
+
/** Called by the coordinator to inform the client that a query is pending. */
|
|
24
|
+
queryPending?: () => void;
|
|
25
|
+
/** Called by the coordinator to report a query execution error. */
|
|
26
|
+
queryError?: (error: Error) => void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Make a new client with the given options, and connect the client to the
|
|
31
|
+
* provided coordinator.
|
|
32
|
+
* @param options The options for making the client.
|
|
33
|
+
* @returns The resulting client, along with a method to destroy the client when no longer needed.
|
|
34
|
+
*/
|
|
35
|
+
export function makeClient(options: MakeClientOptions): MosaicClient & { destroy: () => void } {
|
|
36
|
+
const {
|
|
37
|
+
coordinator = defaultCoordinator(),
|
|
38
|
+
...clientOptions
|
|
39
|
+
} = options;
|
|
40
|
+
const client = new ProxyClient(clientOptions);
|
|
41
|
+
coordinator.connect(client);
|
|
42
|
+
return client;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* An internal class used to implement the makeClient API.
|
|
47
|
+
*/
|
|
48
|
+
class ProxyClient extends MosaicClient {
|
|
49
|
+
private readonly _methods: Omit<MakeClientOptions, 'coordinator' | 'selection' | 'enabled' | 'filterStable'>;
|
|
50
|
+
private readonly _filterStable: boolean;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @param options The options for making the client.
|
|
54
|
+
*/
|
|
55
|
+
constructor({
|
|
56
|
+
selection = undefined,
|
|
57
|
+
enabled = true,
|
|
58
|
+
filterStable = true,
|
|
59
|
+
...methods
|
|
60
|
+
}: Omit<MakeClientOptions, 'coordinator'>) {
|
|
61
|
+
super(selection);
|
|
62
|
+
this.enabled = enabled;
|
|
63
|
+
this._methods = methods;
|
|
64
|
+
this._filterStable = filterStable;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
get filterStable(): boolean {
|
|
68
|
+
return this._filterStable;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async prepare(): Promise<void> {
|
|
72
|
+
await this._methods.prepare?.();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
query(filter: FilterExpr): ClientQuery {
|
|
76
|
+
return this._methods.query?.(filter) ?? null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
queryResult(data: unknown): this {
|
|
80
|
+
this._methods.queryResult?.(data);
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
queryPending(): this {
|
|
85
|
+
this._methods.queryPending?.();
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
queryError(error: Error): this {
|
|
90
|
+
this._methods.queryError?.(error);
|
|
91
|
+
return this;
|
|
92
|
+
}
|
|
93
|
+
}
|