@uwdata/mosaic-core 0.17.0 → 0.19.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.
Files changed (178) hide show
  1. package/LICENSE +47 -0
  2. package/README.md +0 -1
  3. package/dist/src/Coordinator.d.ts +159 -0
  4. package/dist/src/Coordinator.d.ts.map +1 -0
  5. package/dist/src/Coordinator.js +250 -0
  6. package/dist/src/Coordinator.js.map +1 -0
  7. package/dist/src/MosaicClient.d.ts +138 -0
  8. package/dist/src/MosaicClient.d.ts.map +1 -0
  9. package/dist/src/MosaicClient.js +214 -0
  10. package/dist/src/MosaicClient.js.map +1 -0
  11. package/dist/src/Param.d.ts +56 -0
  12. package/dist/src/Param.d.ts.map +1 -0
  13. package/dist/src/Param.js +89 -0
  14. package/dist/src/Param.js.map +1 -0
  15. package/dist/src/QueryConsolidator.d.ts +11 -0
  16. package/dist/src/QueryConsolidator.d.ts.map +1 -0
  17. package/dist/src/QueryConsolidator.js +249 -0
  18. package/dist/src/QueryConsolidator.js.map +1 -0
  19. package/dist/src/QueryManager.d.ts +77 -0
  20. package/dist/src/QueryManager.d.ts.map +1 -0
  21. package/dist/src/QueryManager.js +174 -0
  22. package/dist/src/QueryManager.js.map +1 -0
  23. package/dist/src/Selection.d.ts +222 -0
  24. package/dist/src/Selection.d.ts.map +1 -0
  25. package/dist/src/Selection.js +322 -0
  26. package/dist/src/Selection.js.map +1 -0
  27. package/dist/src/SelectionClause.d.ts +222 -0
  28. package/dist/src/SelectionClause.d.ts.map +1 -0
  29. package/dist/src/SelectionClause.js +168 -0
  30. package/dist/src/SelectionClause.js.map +1 -0
  31. package/dist/src/connectors/Connector.d.ts +26 -0
  32. package/dist/src/connectors/Connector.d.ts.map +1 -0
  33. package/dist/src/connectors/Connector.js +2 -0
  34. package/dist/src/connectors/Connector.js.map +1 -0
  35. package/dist/src/connectors/rest.d.ts +24 -0
  36. package/dist/src/connectors/rest.d.ts.map +1 -0
  37. package/dist/src/connectors/rest.js +37 -0
  38. package/dist/src/connectors/rest.js.map +1 -0
  39. package/dist/src/connectors/socket.d.ts +40 -0
  40. package/dist/src/connectors/socket.d.ts.map +1 -0
  41. package/dist/src/connectors/socket.js +115 -0
  42. package/dist/src/connectors/socket.js.map +1 -0
  43. package/dist/src/connectors/wasm.d.ts +56 -0
  44. package/dist/src/connectors/wasm.d.ts.map +1 -0
  45. package/dist/src/connectors/wasm.js +116 -0
  46. package/dist/src/connectors/wasm.js.map +1 -0
  47. package/dist/src/index.d.ts +28 -0
  48. package/dist/src/index.d.ts.map +1 -0
  49. package/{src → dist/src}/index.js +8 -11
  50. package/dist/src/index.js.map +1 -0
  51. package/dist/src/make-client.d.ts +33 -0
  52. package/dist/src/make-client.d.ts.map +1 -0
  53. package/dist/src/make-client.js +52 -0
  54. package/dist/src/make-client.js.map +1 -0
  55. package/dist/src/preagg/PreAggregator.d.ts +150 -0
  56. package/dist/src/preagg/PreAggregator.d.ts.map +1 -0
  57. package/dist/src/preagg/PreAggregator.js +382 -0
  58. package/dist/src/preagg/PreAggregator.js.map +1 -0
  59. package/dist/src/preagg/preagg-columns.d.ts +16 -0
  60. package/dist/src/preagg/preagg-columns.d.ts.map +1 -0
  61. package/dist/src/preagg/preagg-columns.js +95 -0
  62. package/dist/src/preagg/preagg-columns.js.map +1 -0
  63. package/dist/src/preagg/sufficient-statistics.d.ts +14 -0
  64. package/dist/src/preagg/sufficient-statistics.d.ts.map +1 -0
  65. package/dist/src/preagg/sufficient-statistics.js +446 -0
  66. package/dist/src/preagg/sufficient-statistics.js.map +1 -0
  67. package/dist/src/types.d.ts +77 -0
  68. package/dist/src/types.d.ts.map +1 -0
  69. package/dist/src/types.js +2 -0
  70. package/dist/src/types.js.map +1 -0
  71. package/dist/src/util/AsyncDispatch.d.ts +121 -0
  72. package/dist/src/util/AsyncDispatch.d.ts.map +1 -0
  73. package/dist/src/util/AsyncDispatch.js +188 -0
  74. package/dist/src/util/AsyncDispatch.js.map +1 -0
  75. package/dist/src/util/cache.d.ts +19 -0
  76. package/dist/src/util/cache.d.ts.map +1 -0
  77. package/dist/src/util/cache.js +66 -0
  78. package/dist/src/util/cache.js.map +1 -0
  79. package/dist/src/util/decode-ipc.d.ts +12 -0
  80. package/dist/src/util/decode-ipc.d.ts.map +1 -0
  81. package/{src → dist/src}/util/decode-ipc.js +5 -6
  82. package/dist/src/util/decode-ipc.js.map +1 -0
  83. package/dist/src/util/distinct.d.ts +3 -0
  84. package/dist/src/util/distinct.d.ts.map +1 -0
  85. package/dist/src/util/distinct.js +16 -0
  86. package/dist/src/util/distinct.js.map +1 -0
  87. package/dist/src/util/field-info.d.ts +26 -0
  88. package/dist/src/util/field-info.d.ts.map +1 -0
  89. package/dist/src/util/field-info.js +91 -0
  90. package/dist/src/util/field-info.js.map +1 -0
  91. package/dist/src/util/hash.d.ts +2 -0
  92. package/dist/src/util/hash.d.ts.map +1 -0
  93. package/dist/src/util/hash.js +26 -0
  94. package/dist/src/util/hash.js.map +1 -0
  95. package/dist/src/util/is-activatable.d.ts +8 -0
  96. package/dist/src/util/is-activatable.d.ts.map +1 -0
  97. package/dist/src/util/is-activatable.js +10 -0
  98. package/dist/src/util/is-activatable.js.map +1 -0
  99. package/dist/src/util/is-arrow-table.d.ts +9 -0
  100. package/dist/src/util/is-arrow-table.d.ts.map +1 -0
  101. package/dist/src/util/is-arrow-table.js +11 -0
  102. package/dist/src/util/is-arrow-table.js.map +1 -0
  103. package/dist/src/util/js-type.d.ts +9 -0
  104. package/dist/src/util/js-type.d.ts.map +1 -0
  105. package/dist/src/util/js-type.js +59 -0
  106. package/dist/src/util/js-type.js.map +1 -0
  107. package/dist/src/util/priority-queue.d.ts +35 -0
  108. package/dist/src/util/priority-queue.d.ts.map +1 -0
  109. package/dist/src/util/priority-queue.js +81 -0
  110. package/dist/src/util/priority-queue.js.map +1 -0
  111. package/dist/src/util/query-result.d.ts +47 -0
  112. package/dist/src/util/query-result.d.ts.map +1 -0
  113. package/dist/src/util/query-result.js +83 -0
  114. package/dist/src/util/query-result.js.map +1 -0
  115. package/dist/src/util/synchronizer.d.ts +36 -0
  116. package/dist/src/util/synchronizer.d.ts.map +1 -0
  117. package/dist/src/util/synchronizer.js +52 -0
  118. package/dist/src/util/synchronizer.js.map +1 -0
  119. package/dist/src/util/throttle.d.ts +12 -0
  120. package/dist/src/util/throttle.d.ts.map +1 -0
  121. package/dist/src/util/throttle.js +51 -0
  122. package/dist/src/util/throttle.js.map +1 -0
  123. package/dist/src/util/to-data-columns.d.ts +22 -0
  124. package/dist/src/util/to-data-columns.d.ts.map +1 -0
  125. package/dist/src/util/to-data-columns.js +51 -0
  126. package/dist/src/util/to-data-columns.js.map +1 -0
  127. package/dist/src/util/void-logger.d.ts +13 -0
  128. package/dist/src/util/void-logger.d.ts.map +1 -0
  129. package/dist/src/util/void-logger.js +13 -0
  130. package/dist/src/util/void-logger.js.map +1 -0
  131. package/package.json +17 -11
  132. package/src/Coordinator.ts +396 -0
  133. package/src/{MosaicClient.js → MosaicClient.ts} +50 -43
  134. package/src/{Param.js → Param.ts} +29 -28
  135. package/src/{QueryConsolidator.js → QueryConsolidator.ts} +85 -62
  136. package/src/{QueryManager.js → QueryManager.ts} +61 -54
  137. package/src/Selection.ts +391 -0
  138. package/src/SelectionClause.ts +357 -0
  139. package/src/connectors/Connector.ts +6 -6
  140. package/src/connectors/rest.ts +56 -0
  141. package/src/connectors/{socket.js → socket.ts} +53 -42
  142. package/src/connectors/{wasm.js → wasm.ts} +52 -63
  143. package/src/index.ts +42 -0
  144. package/src/make-client.ts +93 -0
  145. package/src/preagg/{PreAggregator.js → PreAggregator.ts} +164 -145
  146. package/src/preagg/{preagg-columns.js → preagg-columns.ts} +27 -24
  147. package/src/preagg/{sufficient-statistics.js → sufficient-statistics.ts} +160 -110
  148. package/src/types.ts +24 -9
  149. package/src/util/{AsyncDispatch.js → AsyncDispatch.ts} +62 -43
  150. package/src/util/{cache.js → cache.ts} +25 -15
  151. package/src/util/decode-ipc.ts +15 -0
  152. package/src/util/{distinct.js → distinct.ts} +3 -3
  153. package/src/util/{field-info.js → field-info.ts} +30 -31
  154. package/src/util/{hash.js → hash.ts} +4 -4
  155. package/src/util/is-activatable.ts +11 -0
  156. package/src/util/is-arrow-table.ts +12 -0
  157. package/src/util/{js-type.js → js-type.ts} +7 -5
  158. package/src/util/{priority-queue.js → priority-queue.ts} +32 -20
  159. package/src/util/{query-result.js → query-result.ts} +24 -17
  160. package/src/util/synchronizer.ts +56 -0
  161. package/src/util/throttle.ts +59 -0
  162. package/src/util/to-data-columns.ts +65 -0
  163. package/src/util/void-logger.ts +23 -0
  164. package/src/Coordinator.js +0 -313
  165. package/src/Selection.js +0 -380
  166. package/src/SelectionClause.js +0 -159
  167. package/src/connectors/rest.js +0 -38
  168. package/src/index-types.ts +0 -5
  169. package/src/make-client.js +0 -101
  170. package/src/util/is-activatable.js +0 -8
  171. package/src/util/is-arrow-table.js +0 -10
  172. package/src/util/selection-types.ts +0 -137
  173. package/src/util/synchronizer.js +0 -47
  174. package/src/util/throttle.js +0 -54
  175. package/src/util/to-data-columns.js +0 -60
  176. package/src/util/void-logger.js +0 -13
  177. package/tsconfig.json +0 -9
  178. package/vitest.config.ts +0 -3
@@ -5,23 +5,30 @@ export const QueryState = Object.freeze({
5
5
  done: Symbol('done')
6
6
  });
7
7
 
8
+ type QueryStateType = typeof QueryState[keyof typeof QueryState];
9
+
8
10
  /**
9
11
  * A query result Promise that can allows external callers
10
12
  * to resolve or reject the Promise.
11
13
  */
12
- export class QueryResult extends Promise {
14
+ export class QueryResult<T = unknown> extends Promise<T> {
15
+ private _resolve!: (value: T | PromiseLike<T>) => void;
16
+ private _reject!: (reason?: unknown) => void;
17
+ private _state: QueryStateType;
18
+ private _value: T | undefined;
19
+
13
20
  /**
14
21
  * Create a new query result Promise.
15
22
  */
16
23
  constructor() {
17
- let resolve;
18
- let reject;
24
+ let resolve: (value: T | PromiseLike<T>) => void;
25
+ let reject: (reason?: unknown) => void;
19
26
  super((r, e) => {
20
27
  resolve = r;
21
28
  reject = e;
22
29
  });
23
- this._resolve = resolve;
24
- this._reject = reject;
30
+ this._resolve = resolve!;
31
+ this._reject = reject!;
25
32
  this._state = QueryState.pending;
26
33
  this._value = undefined;
27
34
  }
@@ -29,10 +36,10 @@ export class QueryResult extends Promise {
29
36
  /**
30
37
  * Resolve the result Promise with a prepared value or the provided value.
31
38
  * This method will only succeed if either a value is provided or the promise is ready.
32
- * @param {*} value The result value.
33
- * @returns {this}
39
+ * @param value The result value.
40
+ * @returns This QueryResult instance.
34
41
  */
35
- fulfill(value) {
42
+ fulfill(value?: T): this {
36
43
  if (this._value !== undefined) {
37
44
  if (value !== undefined) {
38
45
  throw Error('Promise is ready and fulfill has a provided value');
@@ -51,10 +58,10 @@ export class QueryResult extends Promise {
51
58
 
52
59
  /**
53
60
  * Prepare to resolve with the provided value.
54
- * @param {*} value The result value.
55
- * @returns {this}
61
+ * @param value The result value.
62
+ * @returns This QueryResult instance.
56
63
  */
57
- ready(value) {
64
+ ready(value: T): this {
58
65
  this._state = QueryState.ready;
59
66
  this._value = value;
60
67
  return this;
@@ -62,10 +69,10 @@ export class QueryResult extends Promise {
62
69
 
63
70
  /**
64
71
  * Rejects the result Promise with the provided error.
65
- * @param {*} error The error value.
66
- * @returns {this}
72
+ * @param error The error value.
73
+ * @returns This QueryResult instance.
67
74
  */
68
- reject(error) {
75
+ reject(error: unknown): this {
69
76
  this._state = QueryState.error;
70
77
  this._reject(error);
71
78
  return this;
@@ -73,12 +80,12 @@ export class QueryResult extends Promise {
73
80
 
74
81
  /**
75
82
  * Returns the state of this query result.
76
- * @returns {symbol}
83
+ * @returns The current state symbol.
77
84
  */
78
- get state() {
85
+ get state(): QueryStateType {
79
86
  return this._state;
80
87
  }
81
88
  }
82
89
 
83
90
  // necessary to make Promise subclass act like a Promise
84
- QueryResult.prototype.constructor = Promise;
91
+ QueryResult.prototype.constructor = Promise;
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Synchronizer class to aid synchronization of updates on multiple pending operations.
3
+ */
4
+ export class Synchronizer<T = unknown> {
5
+ private _set: Set<T>;
6
+ private _done: () => void;
7
+ private _promise: Promise<void>;
8
+
9
+ /**
10
+ * Create a new synchronizer instance.
11
+ */
12
+ constructor() {
13
+ this._set = new Set<T>();
14
+ this._done = () => {};
15
+ this._promise = new Promise<void>(resolve => this._done = resolve);
16
+ }
17
+
18
+ /**
19
+ * Mark an item as pending.
20
+ * @param item An item to synchronize on.
21
+ */
22
+ pending(item: T): void {
23
+ this._set.add(item);
24
+ }
25
+
26
+ /**
27
+ * Mark a pending item as ready, indicating it is
28
+ * ready for a synchronized update.
29
+ * @param item An item to synchronize on.
30
+ * @returns True if the synchronizer is ready to
31
+ * resolve, false otherwise.
32
+ */
33
+ ready(item: T): boolean {
34
+ this._set.delete(item);
35
+ return this._set.size === 0;
36
+ }
37
+
38
+ /**
39
+ * Resolve the current synchronization cycle, causing the synchronize
40
+ * promise to resolve and thereby trigger downstream updates.
41
+ */
42
+ resolve(): void {
43
+ this._promise = new Promise<void>(resolve => {
44
+ this._done();
45
+ this._done = resolve;
46
+ });
47
+ }
48
+
49
+ /**
50
+ * The promise for the current synchronization cycle.
51
+ * @returns The synchronization promise.
52
+ */
53
+ get promise(): Promise<void> {
54
+ return this._promise;
55
+ }
56
+ }
@@ -0,0 +1,59 @@
1
+ const NIL = {};
2
+
3
+ interface QueueItem<E> {
4
+ event: E;
5
+ }
6
+
7
+ /**
8
+ * Throttle invocations of a callback function. The callback must return
9
+ * a Promise. Upon repeated invocation, the callback will not be invoked
10
+ * until a prior Promise resolves. If multiple invocations occurs while
11
+ * waiting, only the most recent invocation will be pending.
12
+ * @param callback The callback function.
13
+ * @param debounce Flag indicating if invocations
14
+ * should also be debounced within the current animation frame.
15
+ * @returns A new function that throttles access to the callback.
16
+ */
17
+ export function throttle<E, T>(
18
+ callback: (event?: E) => Promise<T> | null,
19
+ debounce: boolean = false
20
+ ): (event?: E) => void {
21
+ let curr: Promise<unknown> | null;
22
+ let next: QueueItem<E> | undefined | null;
23
+ let pending: E | undefined | typeof NIL = NIL;
24
+
25
+ function invoke(event?: E): void {
26
+ curr = callback(event)
27
+ ?.catch(() => {})
28
+ .finally(() => {
29
+ if (next) {
30
+ const { event: value } = next;
31
+ next = null;
32
+ invoke(value);
33
+ } else {
34
+ curr = null;
35
+ }
36
+ }) || null;
37
+ }
38
+
39
+ function enqueue(event: E): void {
40
+ next = { event };
41
+ }
42
+
43
+ function process(event?: E): void {
44
+ if (curr) enqueue(event!); else invoke(event);
45
+ }
46
+
47
+ function delay(event?: E): void {
48
+ if (pending !== event) {
49
+ requestAnimationFrame(() => {
50
+ const e = pending as E;
51
+ pending = NIL;
52
+ process(e);
53
+ });
54
+ }
55
+ pending = event;
56
+ }
57
+
58
+ return debounce ? delay : process;
59
+ }
@@ -0,0 +1,65 @@
1
+ import { isArrowTable } from './is-arrow-table.js';
2
+ import type { Table } from '@uwdata/flechette';
3
+
4
+ /**
5
+ * An Array or TypedArray
6
+ */
7
+ type Arrayish = Array<unknown> | Int8Array | Uint8Array | Uint8ClampedArray
8
+ | Int16Array | Uint16Array | Int32Array | Uint32Array
9
+ | Float32Array | Float64Array;
10
+
11
+ /**
12
+ * Data columns structure with either named columns or values array
13
+ */
14
+ type DataColumns =
15
+ | { numRows: number; columns: Record<string, Arrayish> }
16
+ | { numRows: number; values: Arrayish };
17
+
18
+ /**
19
+ * Convert input data to a set of column arrays.
20
+ * @param data The input data.
21
+ * @returns An object with named column arrays.
22
+ */
23
+ export function toDataColumns(data: unknown): DataColumns {
24
+ if (isArrowTable(data)) {
25
+ return arrowToColumns(data);
26
+ } else if (Array.isArray(data)) {
27
+ return arrayToColumns(data);
28
+ } else {
29
+ throw new Error('Unrecognized data format.');
30
+ }
31
+ }
32
+
33
+ /**
34
+ * Convert an Arrow table to a set of column arrays.
35
+ * @param data An Arrow Table.
36
+ * @returns An object with named column arrays.
37
+ */
38
+ function arrowToColumns(data: Table): DataColumns {
39
+ const { numRows } = data;
40
+ return { numRows, columns: data.toColumns() as Record<string, Arrayish> };
41
+ }
42
+
43
+ /**
44
+ * Convert an array of values to a set of column arrays.
45
+ * If the array values are objects, build out named columns.
46
+ * We use the keys of the first object as the column names.
47
+ * Otherwise, use a special "values" array.
48
+ * @param data An array of data objects.
49
+ * @returns An object with named column arrays.
50
+ */
51
+ function arrayToColumns(data: Record<string,unknown>[]): DataColumns {
52
+ const numRows = data.length;
53
+ if (typeof data[0] === 'object') {
54
+ const names = numRows ? Object.keys(data[0]!) : [];
55
+ const columns: Record<string, unknown[]> = {};
56
+ if (names.length > 0) {
57
+ names.forEach(name => {
58
+ columns[name] = data.map(d => d[name]);
59
+ });
60
+ }
61
+ return { numRows, columns };
62
+ } else {
63
+ return { numRows, values: data };
64
+ }
65
+ }
@@ -0,0 +1,23 @@
1
+ interface Logger {
2
+ debug(...args: unknown[]): void;
3
+ info(...args: unknown[]): void;
4
+ log(...args: unknown[]): void;
5
+ warn(...args: unknown[]): void;
6
+ error(...args: unknown[]): void;
7
+ group(label?: string): void;
8
+ groupCollapsed(label?: string): void;
9
+ groupEnd(): void;
10
+ }
11
+
12
+ export function voidLogger(): Logger {
13
+ return {
14
+ debug(): void {},
15
+ info(): void {},
16
+ log(): void {},
17
+ warn(): void {},
18
+ error(): void {},
19
+ group(): void {},
20
+ groupCollapsed(): void {},
21
+ groupEnd(): void {}
22
+ };
23
+ }
@@ -1,313 +0,0 @@
1
- /** @import { Connector } from './connectors/Connector.js' */
2
- /** @import { PreAggregateOptions } from './preagg/PreAggregator.js' */
3
- /** @import { QueryResult } from './util/query-result.js' */
4
- /** @import { SelectionClause } from './util/selection-types.js' */
5
- /** @import { MosaicClient } from './MosaicClient.js' */
6
- /** @import { Selection } from './Selection.js' */
7
- /** @import { Logger, QueryType } from './types.js' */
8
- import { socketConnector } from './connectors/socket.js';
9
- import { PreAggregator } from './preagg/PreAggregator.js';
10
- import { voidLogger } from './util/void-logger.js';
11
- import { QueryManager, Priority } from './QueryManager.js';
12
-
13
- /**
14
- * The singleton Coordinator instance.
15
- * @type {Coordinator}
16
- */
17
- let _instance;
18
-
19
- /**
20
- * Set or retrieve the coordinator instance.
21
- * @param {Coordinator} [instance] the coordinator instance to set
22
- * @returns {Coordinator} the coordinator instance
23
- */
24
- export function coordinator(instance) {
25
- if (instance) {
26
- _instance = instance;
27
- } else if (_instance == null) {
28
- _instance = new Coordinator();
29
- }
30
- return _instance;
31
- }
32
-
33
- /**
34
- * A Mosaic Coordinator manages all database communication for clients and
35
- * handles selection updates. The Coordinator also performs optimizations
36
- * including query caching, consolidation, and pre-aggregation.
37
- */
38
- export class Coordinator {
39
- /**
40
- * @param {Connector} [db] Database connector. Defaults to a web socket connection.
41
- * @param {object} [options] Coordinator options.
42
- * @param {Logger} [options.logger=console] The logger to use, defaults to `console`.
43
- * @param {QueryManager} [options.manager] The query manager to use.
44
- * @param {boolean} [options.cache=true] Boolean flag to enable/disable query caching.
45
- * @param {boolean} [options.consolidate=true] Boolean flag to enable/disable query consolidation.
46
- * @param {PreAggregateOptions} [options.preagg] Options for the Pre-aggregator.
47
- */
48
- constructor(db = socketConnector(), {
49
- logger = console,
50
- manager = new QueryManager(),
51
- cache = true,
52
- consolidate = true,
53
- preagg = {}
54
- } = {}) {
55
- /** @type {QueryManager} */
56
- this.manager = manager;
57
- this.manager.cache(cache);
58
- this.manager.consolidate(consolidate);
59
- this.databaseConnector(db);
60
- this.logger(logger);
61
- this.clear();
62
- this.preaggregator = new PreAggregator(this, preagg);
63
- }
64
-
65
- /**
66
- * Clear the coordinator state.
67
- * @param {object} [options] Options object.
68
- * @param {boolean} [options.clients=true] If true, disconnect all clients.
69
- * @param {boolean} [options.cache=true] If true, clear the query cache.
70
- */
71
- clear({ clients = true, cache = true } = {}) {
72
- this.manager.clear();
73
- if (clients) {
74
- this.filterGroups?.forEach(group => group.disconnect());
75
- this.filterGroups = new Map;
76
- this.clients?.forEach(client => this.disconnect(client));
77
- this.clients = new Set;
78
- }
79
- if (cache) this.manager.cache().clear();
80
- }
81
-
82
- /**
83
- * Get or set the database connector.
84
- * @param {Connector} [db] The database connector to use.
85
- * @returns {Connector} The current database connector.
86
- */
87
- databaseConnector(db) {
88
- return this.manager.connector(db);
89
- }
90
-
91
- /**
92
- * Get or set the logger.
93
- * @param {Logger} [logger] The logger to use.
94
- * @returns {Logger} The current logger
95
- */
96
- logger(logger) {
97
- if (arguments.length) {
98
- this._logger = logger || voidLogger();
99
- this.manager.logger(this._logger);
100
- }
101
- return this._logger;
102
- }
103
-
104
- // -- Query Management ----
105
-
106
- /**
107
- * Cancel previosuly submitted query requests. These queries will be
108
- * canceled if they are queued but have not yet been submitted.
109
- * @param {QueryResult[]} requests An array
110
- * of query result objects, such as those returned by the `query` method.
111
- */
112
- cancel(requests) {
113
- this.manager.cancel(requests);
114
- }
115
-
116
- /**
117
- * Issue a query for which no result (return value) is needed.
118
- * @param {QueryType[] | QueryType} query The query or an array of queries.
119
- * Each query should be either a Query builder object or a SQL string.
120
- * @param {object} [options] An options object.
121
- * @param {number} [options.priority] The query priority, defaults to
122
- * `Priority.Normal`.
123
- * @returns {QueryResult} A query result promise.
124
- */
125
- exec(query, { priority = Priority.Normal } = {}) {
126
- query = Array.isArray(query) ? query.filter(x => x).join(';\n') : query;
127
- return this.manager.request({ type: 'exec', query }, priority);
128
- }
129
-
130
- /**
131
- * Issue a query to the backing database. The submitted query may be
132
- * consolidate with other queries and its results may be cached.
133
- * @param {QueryType} query The query as either a Query builder objec
134
- * or a SQL string.
135
- * @param {object} [options] An options object.
136
- * @param {'arrow' | 'json'} [options.type] The query result format type.
137
- * @param {boolean} [options.cache=true] If true, cache the query result
138
- * client-side within the QueryManager.
139
- * @param {boolean} [options.persist] If true, request the database
140
- * server to persist a cached query server-side.
141
- * @param {number} [options.priority] The query priority, defaults to
142
- * `Priority.Normal`.
143
- * @returns {QueryResult} A query result promise.
144
- */
145
- query(query, {
146
- type = 'arrow',
147
- cache = true,
148
- priority = Priority.Normal,
149
- ...options
150
- } = {}) {
151
- return this.manager.request({ type, query, cache, options }, priority);
152
- }
153
-
154
- /**
155
- * Issue a query to prefetch data for later use. The query result is cached
156
- * for efficient future access.
157
- * @param {QueryType} query The query as either a Query builder object
158
- * or a SQL string.
159
- * @param {object} [options] An options object.
160
- * @param {'arrow' | 'json'} [options.type] The query result format type.
161
- * @returns {QueryResult} A query result promise.
162
- */
163
- prefetch(query, options = {}) {
164
- return this.query(query, { ...options, cache: true, priority: Priority.Low });
165
- }
166
-
167
- // -- Client Management ----
168
-
169
- /**
170
- * Update client data by submitting the given query and returning the
171
- * data (or error) to the client.
172
- * @param {MosaicClient} client A Mosaic client.
173
- * @param {QueryType} query The data query.
174
- * @param {number} [priority] The query priority.
175
- * @returns {Promise} A Promise that resolves upon completion of the update.
176
- */
177
- updateClient(client, query, priority = Priority.Normal) {
178
- client.queryPending();
179
- return client._pending = this.query(query, { priority })
180
- .then(
181
- data => client.queryResult(data).update(),
182
- err => { this._logger.error(err); client.queryError(err); }
183
- )
184
- .catch(err => this._logger.error(err));
185
- }
186
-
187
- /**
188
- * Issue a query request for a client. If the query is null or undefined,
189
- * the client is simply updated. Otherwise `updateClient` is called. As a
190
- * side effect, this method clears the current preaggregator state.
191
- * @param {MosaicClient} client The client to update.
192
- * @param {QueryType | null} [query] The query to issue.
193
- */
194
- requestQuery(client, query) {
195
- this.preaggregator.clear();
196
- return query
197
- ? this.updateClient(client, query)
198
- : Promise.resolve(client.update());
199
- }
200
-
201
- /**
202
- * Connect a client to the coordinator.
203
- * @param {MosaicClient} client The Mosaic client to connect.
204
- */
205
- connect(client) {
206
- const { clients } = this;
207
-
208
- if (clients.has(client)) {
209
- throw new Error('Client already connected.');
210
- }
211
-
212
- // add client to client set
213
- clients.add(client);
214
-
215
- // register coordinator on client instance
216
- client.coordinator = this;
217
-
218
- // initialize client lifecycle
219
- client.initialize();
220
-
221
- // connect filter selection
222
- connectSelection(this, client.filterBy, client);
223
- }
224
-
225
- /**
226
- * Disconnect a client from the coordinator.
227
- * @param {MosaicClient} client The Mosaic client to disconnect.
228
- */
229
- disconnect(client) {
230
- const { clients, filterGroups } = this;
231
- if (!clients.has(client)) return;
232
- clients.delete(client);
233
- client.coordinator = null;
234
-
235
- const group = filterGroups.get(client.filterBy);
236
- if (group) {
237
- group.clients.delete(client);
238
- }
239
- }
240
- }
241
-
242
- /**
243
- * Connect a selection-client pair to the coordinator to process updates.
244
- * @param {Coordinator} mc The Mosaic coordinator.
245
- * @param {Selection} selection A selection.
246
- * @param {MosaicClient} client A Mosiac client that is filtered by the
247
- * given selection.
248
- */
249
- function connectSelection(mc, selection, client) {
250
- if (!selection) return;
251
- let entry = mc.filterGroups.get(selection);
252
- if (!entry) {
253
- const activate = clause => activateSelection(mc, selection, clause);
254
- const value = () => updateSelection(mc, selection);
255
-
256
- selection.addEventListener('activate', activate);
257
- selection.addEventListener('value', value);
258
-
259
- entry = {
260
- selection,
261
- clients: new Set,
262
- disconnect() {
263
- selection.removeEventListener('activate', activate);
264
- selection.removeEventListener('value', value);
265
- }
266
- };
267
- mc.filterGroups.set(selection, entry);
268
- }
269
- entry.clients.add(client);
270
- }
271
-
272
- /**
273
- * Activate a selection, providing a clause indicative of potential
274
- * next updates. Activation provides a preview of likely next events,
275
- * enabling potential precomputation to optimize updates.
276
- * @param {Coordinator} mc The Mosaic coordinator.
277
- * @param {Selection} selection A selection.
278
- * @param {SelectionClause} clause A selection clause for the activation.
279
- */
280
- function activateSelection(mc, selection, clause) {
281
- const { preaggregator, filterGroups } = mc;
282
- const { clients } = filterGroups.get(selection);
283
- for (const client of clients) {
284
- if (client.enabled) {
285
- preaggregator.request(client, selection, clause);
286
- }
287
- }
288
- }
289
-
290
- /**
291
- * Process an updated selection value, querying filtered data for any
292
- * associated clients.
293
- * @param {Coordinator} mc The Mosaic coordinator.
294
- * @param {Selection} selection A selection.
295
- * @returns {Promise} A Promise that resolves when the update completes.
296
- */
297
- function updateSelection(mc, selection) {
298
- const { preaggregator, filterGroups } = mc;
299
- const { clients } = filterGroups.get(selection);
300
- const { active } = selection;
301
- return Promise.allSettled(Array.from(clients, client => {
302
- if (!client.enabled) return client.requestQuery();
303
- const info = preaggregator.request(client, selection, active);
304
- const filter = info ? null : selection.predicate(client);
305
-
306
- // skip due to cross-filtering
307
- if (info?.skip || (!info && !filter)) return;
308
-
309
- // @ts-ignore
310
- const query = info?.query(active.predicate) ?? client.query(filter);
311
- return mc.updateClient(client, query);
312
- }));
313
- }