tinybase 3.0.1 → 3.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/lib/cjs/checkpoints.cjs +1 -0
  2. package/lib/cjs/checkpoints.cjs.gz +0 -0
  3. package/lib/cjs/checkpoints.d.ts +959 -0
  4. package/lib/cjs/common.cjs +1 -0
  5. package/lib/cjs/common.cjs.gz +0 -0
  6. package/lib/cjs/common.d.ts +115 -0
  7. package/lib/cjs/indexes.cjs +1 -0
  8. package/lib/cjs/indexes.cjs.gz +0 -0
  9. package/lib/cjs/indexes.d.ts +966 -0
  10. package/lib/cjs/metrics.cjs +1 -0
  11. package/lib/cjs/metrics.cjs.gz +0 -0
  12. package/lib/cjs/metrics.d.ts +827 -0
  13. package/lib/cjs/persisters.cjs +1 -0
  14. package/lib/cjs/persisters.cjs.gz +0 -0
  15. package/lib/cjs/persisters.d.ts +727 -0
  16. package/lib/cjs/queries.cjs +1 -0
  17. package/lib/cjs/queries.cjs.gz +0 -0
  18. package/lib/cjs/queries.d.ts +3024 -0
  19. package/lib/cjs/relationships.cjs +1 -0
  20. package/lib/cjs/relationships.cjs.gz +0 -0
  21. package/lib/cjs/relationships.d.ts +1201 -0
  22. package/lib/cjs/store.cjs +1 -0
  23. package/lib/cjs/store.cjs.gz +0 -0
  24. package/lib/cjs/store.d.ts +5244 -0
  25. package/lib/cjs/tinybase.cjs +1 -0
  26. package/lib/cjs/tinybase.cjs.gz +0 -0
  27. package/lib/cjs/tinybase.d.ts +14 -0
  28. package/lib/cjs/tools.cjs +1 -0
  29. package/lib/cjs/tools.cjs.gz +0 -0
  30. package/lib/cjs/tools.d.ts +512 -0
  31. package/lib/cjs/ui-react.cjs +1 -0
  32. package/lib/cjs/ui-react.cjs.gz +0 -0
  33. package/lib/cjs/ui-react.d.ts +10921 -0
  34. package/lib/cjs-es6/checkpoints.cjs +1 -0
  35. package/lib/cjs-es6/checkpoints.cjs.gz +0 -0
  36. package/lib/cjs-es6/checkpoints.d.ts +959 -0
  37. package/lib/cjs-es6/common.cjs +1 -0
  38. package/lib/cjs-es6/common.cjs.gz +0 -0
  39. package/lib/cjs-es6/common.d.ts +115 -0
  40. package/lib/cjs-es6/indexes.cjs +1 -0
  41. package/lib/cjs-es6/indexes.cjs.gz +0 -0
  42. package/lib/cjs-es6/indexes.d.ts +966 -0
  43. package/lib/cjs-es6/metrics.cjs +1 -0
  44. package/lib/cjs-es6/metrics.cjs.gz +0 -0
  45. package/lib/cjs-es6/metrics.d.ts +827 -0
  46. package/lib/cjs-es6/persisters.cjs +1 -0
  47. package/lib/cjs-es6/persisters.cjs.gz +0 -0
  48. package/lib/cjs-es6/persisters.d.ts +727 -0
  49. package/lib/cjs-es6/queries.cjs +1 -0
  50. package/lib/cjs-es6/queries.cjs.gz +0 -0
  51. package/lib/cjs-es6/queries.d.ts +3024 -0
  52. package/lib/cjs-es6/relationships.cjs +1 -0
  53. package/lib/cjs-es6/relationships.cjs.gz +0 -0
  54. package/lib/cjs-es6/relationships.d.ts +1201 -0
  55. package/lib/cjs-es6/store.cjs +1 -0
  56. package/lib/cjs-es6/store.cjs.gz +0 -0
  57. package/lib/cjs-es6/store.d.ts +5244 -0
  58. package/lib/cjs-es6/tinybase.cjs +1 -0
  59. package/lib/cjs-es6/tinybase.cjs.gz +0 -0
  60. package/lib/cjs-es6/tinybase.d.ts +14 -0
  61. package/lib/cjs-es6/tools.cjs +1 -0
  62. package/lib/cjs-es6/tools.cjs.gz +0 -0
  63. package/lib/cjs-es6/tools.d.ts +512 -0
  64. package/lib/cjs-es6/ui-react.cjs +1 -0
  65. package/lib/cjs-es6/ui-react.cjs.gz +0 -0
  66. package/lib/cjs-es6/ui-react.d.ts +10921 -0
  67. package/package.json +27 -19
  68. package/readme.md +1 -1
@@ -0,0 +1,827 @@
1
+ /**
2
+ * The metrics module of the TinyBase project provides the ability to create
3
+ * and track metrics and aggregates of the data in Store objects.
4
+ *
5
+ * The main entry point to this module is the createMetrics function, which
6
+ * returns a new Metrics object. From there, you can create new Metric
7
+ * definitions, access the values of those Metrics directly, and register
8
+ * listeners for when they change.
9
+ *
10
+ * @packageDocumentation
11
+ * @module metrics
12
+ */
13
+
14
+ import {GetCell, Store} from './store.d';
15
+ import {Id, IdOrNull, Ids} from './common.d';
16
+
17
+ /**
18
+ * The Metric type is simply an alias, but represents a number formed by
19
+ * aggregating multiple other numbers together.
20
+ *
21
+ * @category Metric
22
+ */
23
+ export type Metric = number;
24
+
25
+ /**
26
+ * The MetricCallback type describes a function that takes a Metric's Id and a
27
+ * callback to loop over each Row within it.
28
+ *
29
+ * A MetricCallback is provided when using the forEachMetric method, so that you
30
+ * can do something based on every Metric in the Metrics object. See that method
31
+ * for specific examples.
32
+ *
33
+ * @param metricId The Id of the Metric that the callback can operate on.
34
+ * @param metric The value of the Metric.
35
+ * @category Callback
36
+ */
37
+ export type MetricCallback = (metricId: Id, metric?: Metric) => void;
38
+
39
+ /**
40
+ * The Aggregate type describes a custom function that takes an array of numbers
41
+ * and returns an aggregate that is used as a Metric.
42
+ *
43
+ * There are a number of common predefined aggregators, such as for counting,
44
+ * summing, and averaging values. This type is instead used for when you wish to
45
+ * use a more complex aggregation of your own devising. See the
46
+ * setMetricDefinition method for more examples.
47
+ *
48
+ * @param numbers The array of numbers in the Metric's aggregation.
49
+ * @param length The length of the array of numbers in the Metric's aggregation.
50
+ * @returns The value of the Metric.
51
+ * @category Aggregators
52
+ */
53
+ export type Aggregate = (numbers: number[], length: number) => Metric;
54
+
55
+ /**
56
+ * The AggregateAdd type describes a function that can be used to optimize a
57
+ * custom Aggregate by providing a shortcut for when a single value is added to
58
+ * the input values.
59
+ *
60
+ * Some aggregation functions do not need to recalculate the aggregation of the
61
+ * whole set when one value changes. For example, when adding a new number to a
62
+ * series, the new sum of the series is the new value added to the previous sum.
63
+ *
64
+ * If it is not possible to shortcut the aggregation based on just one value
65
+ * being added, return `undefined` and the Metric will be completely
66
+ * recalculated.
67
+ *
68
+ * Where possible, if you are providing a custom Aggregate, seek an
69
+ * implementation of an AggregateAdd function that can reduce the complexity
70
+ * cost of growing the input data set. See the setMetricDefinition method for
71
+ * more examples.
72
+ *
73
+ * @param metric The current value of the Metric.
74
+ * @param add The number being added to the Metric's aggregation.
75
+ * @param length The length of the array of numbers in the Metric's aggregation.
76
+ * @returns The new value of the Metric.
77
+ * @category Aggregators
78
+ */
79
+ export type AggregateAdd = (
80
+ metric: Metric,
81
+ add: number,
82
+ length: number,
83
+ ) => Metric | undefined;
84
+
85
+ /**
86
+ * The AggregateRemove type describes a function that can be used to optimize a
87
+ * custom Aggregate by providing a shortcut for when a single value is removed
88
+ * from the input values.
89
+ *
90
+ * Some aggregation functions do not need to recalculate the aggregation of the
91
+ * whole set when one value changes. For example, when removing a number from a
92
+ * series, the new sum of the series is the new value subtracted from the
93
+ * previous sum.
94
+ *
95
+ * If it is not possible to shortcut the aggregation based on just one value
96
+ * being removed, return `undefined` and the Metric will be completely
97
+ * recalculated. One example might be if you were taking the minimum of the
98
+ * values, and the previous minimum is being removed. The whole of the rest of
99
+ * the list will need to be re-scanned to find a new minimum.
100
+ *
101
+ * Where possible, if you are providing a custom Aggregate, seek an
102
+ * implementation of an AggregateRemove function that can reduce the complexity
103
+ * cost of shrinking the input data set. See the setMetricDefinition method for
104
+ * more examples.
105
+ *
106
+ * @param metric The current value of the Metric.
107
+ * @param remove The number being removed from the Metric's aggregation.
108
+ * @param length The length of the array of numbers in the Metric's aggregation.
109
+ * @returns The new value of the Metric.
110
+ * @category Aggregators
111
+ */
112
+ export type AggregateRemove = (
113
+ metric: Metric,
114
+ remove: number,
115
+ length: number,
116
+ ) => Metric | undefined;
117
+
118
+ /**
119
+ * The AggregateReplace type describes a function that can be used to optimize a
120
+ * custom Aggregate by providing a shortcut for when a single value in the input
121
+ * values is replaced with another.
122
+ *
123
+ * Some aggregation functions do not need to recalculate the aggregation of the
124
+ * whole set when one value changes. For example, when replacing a number in a
125
+ * series, the new sum of the series is the previous sum, plus the new value,
126
+ * minus the old value.
127
+ *
128
+ * If it is not possible to shortcut the aggregation based on just one value
129
+ * changing, return `undefined` and the Metric will be completely
130
+ * recalculated.
131
+ *
132
+ * Where possible, if you are providing a custom Aggregate, seek an
133
+ * implementation of an AggregateReplace function that can reduce the complexity
134
+ * cost of changing the input data set in place. See the setMetricDefinition
135
+ * method for more examples.
136
+ *
137
+ * @param metric The current value of the Metric.
138
+ * @param add The number being added to the Metric's aggregation.
139
+ * @param remove The number being removed from the Metric's aggregation.
140
+ * @param length The length of the array of numbers in the Metric's aggregation.
141
+ * @returns The new value of the Metric.
142
+ * @category Aggregators
143
+ */
144
+ export type AggregateReplace = (
145
+ metric: Metric,
146
+ add: number,
147
+ remove: number,
148
+ length: number,
149
+ ) => Metric | undefined;
150
+
151
+ /**
152
+ * The MetricListener type describes a function that is used to listen to
153
+ * changes to a Metric.
154
+ *
155
+ * A MetricListener is provided when using the addMetricListener method. See
156
+ * that method for specific examples.
157
+ *
158
+ * When called, a MetricListener is given a reference to the Metrics object, the
159
+ * Id of the Metric that changed, and the new and old values of the Metric.
160
+ *
161
+ * If this is the first time that a Metric has had a value (such as when a table
162
+ * has gained its first row), the old value will be `undefined`. If a Metric now
163
+ * no longer has a value, the new value will be `undefined`.
164
+ *
165
+ * @param metrics A reference to the Metrics object that changed.
166
+ * @param metricId The Id of the Metric that changed.
167
+ * @param newMetric The new value of the Metric that changed.
168
+ * @param oldMetric The old value of the Metric that changed.
169
+ * @category Listener
170
+ */
171
+ export type MetricListener = (
172
+ metrics: Metrics,
173
+ metricId: Id,
174
+ newMetric: Metric | undefined,
175
+ oldMetric: Metric | undefined,
176
+ ) => void;
177
+
178
+ /**
179
+ * The MetricsListenerStats type describes the number of listeners registered
180
+ * with the Metrics object, and can be used for debugging purposes.
181
+ *
182
+ * A MetricsListenerStats object is returned from the getListenerStats method,
183
+ * and is only populated in a debug build.
184
+ *
185
+ * @category Development
186
+ */
187
+ export type MetricsListenerStats = {
188
+ /**
189
+ * The number of MetricListener functions registered with the Metrics object.
190
+ */
191
+ metric?: number;
192
+ };
193
+
194
+ /**
195
+ * A Metrics object lets you define, query, and listen to, aggregations of Cell
196
+ * values within a Table in a Store.
197
+ *
198
+ * This is useful for counting the number of Row objects in a Table, averaging
199
+ * Cell values, or efficiently performing any arbitrary aggregations.
200
+ *
201
+ * Create a Metrics object easily with the createMetrics function. From there,
202
+ * you can add new Metric definitions (with the setMetricDefinition method),
203
+ * query their values (with the getMetric method), and add listeners for when
204
+ * they change (with the addMetricListener method).
205
+ *
206
+ * This module provides a number of predefined and self-explanatory aggregations
207
+ * ('sum', 'avg', 'min', and 'max'), and defaults to counting Row objects when
208
+ * using the setMetricDefinition method. However, far more complex aggregations
209
+ * can be configured with custom functions.
210
+ *
211
+ * @example
212
+ * This example shows a very simple lifecycle of a Metrics object: from
213
+ * creation, to adding a definition, getting a Metric, and then registering and
214
+ * removing a listener for it.
215
+ *
216
+ * ```js
217
+ * const store = createStore().setTable('species', {
218
+ * dog: {price: 5},
219
+ * cat: {price: 4},
220
+ * worm: {price: 1},
221
+ * });
222
+ *
223
+ * const metrics = createMetrics(store);
224
+ * metrics.setMetricDefinition(
225
+ * 'highestPrice', // metricId
226
+ * 'species', // tableId to aggregate
227
+ * 'max', // aggregation
228
+ * 'price', // cellId to aggregate
229
+ * );
230
+ *
231
+ * console.log(metrics.getMetric('highestPrice'));
232
+ * // -> 5
233
+ *
234
+ * const listenerId = metrics.addMetricListener('highestPrice', () => {
235
+ * console.log(metrics.getMetric('highestPrice'));
236
+ * });
237
+ * store.setCell('species', 'horse', 'price', 20);
238
+ * // -> 20
239
+ *
240
+ * metrics.delListener(listenerId);
241
+ * metrics.destroy();
242
+ * ```
243
+ * @see Metrics And Indexes guides
244
+ * @see Rolling Dice demos
245
+ * @see Country demo
246
+ * @see Todo App demos
247
+ * @category Metrics
248
+ */
249
+ export interface Metrics {
250
+ /**
251
+ * The setMetricDefinition method lets you set the definition of a Metric.
252
+ *
253
+ * Every Metric definition is identified by a unique Id, and if you re-use an
254
+ * existing Id with this method, the previous definition is overwritten.
255
+ *
256
+ * A Metric is an aggregation of numeric values produced from each Row within
257
+ * a single Table. Therefore the definition must specify the Table (by its Id)
258
+ * to be aggregated.
259
+ *
260
+ * Without the third `aggregate` parameter, the Metric will simply be a count
261
+ * of the number of Row objects in the Table. But often you will specify a
262
+ * more interesting aggregate - such as the four predefined aggregates, 'sum',
263
+ * 'avg', 'min', and 'max' - or a custom function that produces your own
264
+ * aggregation of an array of numbers.
265
+ *
266
+ * The fourth `getNumber` parameter specifies which Cell in each Row contains
267
+ * the numerical values to be used in the aggregation. Alternatively, a custom
268
+ * function can be provided that produces your own numeric value from the
269
+ * local Row as a whole.
270
+ *
271
+ * The final three parameters, `aggregateAdd`, `aggregateRemove`,
272
+ * `aggregateReplace` need only be provided when you are using your own custom
273
+ * `aggregate` function. These give you the opportunity to reduce your custom
274
+ * function's algorithmic complexity by providing shortcuts that can nudge an
275
+ * aggregation result when a single value is added, removed, or replaced in
276
+ * the input values.
277
+ *
278
+ * @param metricId The Id of the Metric to define.
279
+ * @param tableId The Id of the Table the Metric will be calculated from.
280
+ * @param aggregate Either a string representing one of a set of common
281
+ * aggregation techniques ('sum', 'avg', 'min', or 'max'), or a function that
282
+ * aggregates numeric values from each Row to create the Metric's overall
283
+ * value. Defaults to 'sum'.
284
+ * @param getNumber Either the Id of a Cell containing, or a function that
285
+ * produces, the numeric value that will be aggregated in the way specified by
286
+ * the `aggregate` parameter. Defaults to a function that returns `1` (meaning
287
+ * that if the `aggregate` and `getNumber` parameters are both omitted, the
288
+ * Metric will simply be a count of the Row objects in the Table).
289
+ * @param aggregateAdd A function that can be used to optimize a custom
290
+ * Aggregate by providing a shortcut for when a single value is added to the
291
+ * input values - for example, when a Row is added to the Table.
292
+ * @param aggregateRemove A function that can be used to optimize a custom
293
+ * Aggregate by providing a shortcut for when a single value is removed from
294
+ * the input values - for example ,when a Row is removed from the Table.
295
+ * @param aggregateReplace A function that can be used to optimize a custom
296
+ * Aggregate by providing a shortcut for when a single value in the input
297
+ * values is replaced with another - for example, when a Row is updated.
298
+ * @returns A reference to the Metrics object.
299
+ * @example
300
+ * This example creates a Store, creates a Metrics object, and defines a
301
+ * simple Metric to count the Row objects in the Table.
302
+ *
303
+ * ```js
304
+ * const store = createStore().setTable('species', {
305
+ * dog: {price: 5},
306
+ * cat: {price: 4},
307
+ * worm: {price: 1},
308
+ * });
309
+ *
310
+ * const metrics = createMetrics(store);
311
+ * metrics.setMetricDefinition('speciesCount', 'species');
312
+ *
313
+ * console.log(metrics.getMetric('speciesCount'));
314
+ * // -> 3
315
+ * ```
316
+ * @example
317
+ * This example creates a Store, creates a Metrics object, and defines a
318
+ * standard Metric to get the highest value of each `price` Cell in the Row
319
+ * objects in the Table.
320
+ *
321
+ * ```js
322
+ * const store = createStore().setTable('species', {
323
+ * dog: {price: 5},
324
+ * cat: {price: 4},
325
+ * worm: {price: 1},
326
+ * });
327
+ *
328
+ * const metrics = createMetrics(store);
329
+ * metrics.setMetricDefinition('highestPrice', 'species', 'max', 'price');
330
+ *
331
+ * console.log(metrics.getMetric('highestPrice'));
332
+ * // -> 5
333
+ * ```
334
+ * @example
335
+ * This example creates a Store, creates a Metrics object, and defines a
336
+ * custom Metric to get the lowest value of each `price` Cell, greater than 2.
337
+ *
338
+ * ```js
339
+ * const store = createStore().setTable('species', {
340
+ * dog: {price: 5},
341
+ * cat: {price: 4},
342
+ * worm: {price: 1},
343
+ * });
344
+ *
345
+ * const metrics = createMetrics(store);
346
+ * metrics.setMetricDefinition(
347
+ * 'lowestPriceOver2',
348
+ * 'species',
349
+ * (numbers) => Math.min(...numbers.filter((number) => number > 2)),
350
+ * 'price',
351
+ * );
352
+ *
353
+ * console.log(metrics.getMetric('lowestPriceOver2'));
354
+ * // -> 4
355
+ * ```
356
+ * @example
357
+ * This example also creates a Store, creates a Metrics object, and defines a
358
+ * custom Metric to get the lowest value of each `price` Cell, greater than 2.
359
+ * However, it also reduces algorithmic complexity with two shortcut
360
+ * functions.
361
+ *
362
+ * ```js
363
+ * const store = createStore().setTable('species', {
364
+ * dog: {price: 5},
365
+ * cat: {price: 4},
366
+ * worm: {price: 1},
367
+ * });
368
+ *
369
+ * const metrics = createMetrics(store);
370
+ * metrics.setMetricDefinition(
371
+ * 'lowestPriceOver2',
372
+ * 'species',
373
+ * (numbers) => Math.min(...numbers.filter((number) => number > 2)),
374
+ * 'price',
375
+ * (metric, add) => (add > 2 ? Math.min(metric, add) : metric),
376
+ * (metric, remove) => (remove == metric ? undefined : metric),
377
+ * (metric, add, remove) =>
378
+ * remove == metric
379
+ * ? undefined
380
+ * : add > 2
381
+ * ? Math.min(metric, add)
382
+ * : metric,
383
+ * );
384
+ *
385
+ * console.log(metrics.getMetric('lowestPriceOver2'));
386
+ * // -> 4
387
+ * store.setRow('species', 'fish', {price: 3});
388
+ * console.log(metrics.getMetric('lowestPriceOver2'));
389
+ * // -> 3
390
+ * ```
391
+ * @example
392
+ * This example creates a Store, creates a Metrics object, and defines a
393
+ * custom Metric to get the average value of a discounted price.
394
+ *
395
+ * ```js
396
+ * const store = createStore().setTable('species', {
397
+ * dog: {price: 5, discount: 0.3},
398
+ * cat: {price: 4, discount: 0.2},
399
+ * worm: {price: 1, discount: 0.2},
400
+ * });
401
+ *
402
+ * const metrics = createMetrics(store);
403
+ * metrics.setMetricDefinition(
404
+ * 'averageDiscountedPrice',
405
+ * 'species',
406
+ * 'avg',
407
+ * (getCell) => getCell('price') * (1 - getCell('discount')),
408
+ * );
409
+ *
410
+ * console.log(metrics.getMetric('averageDiscountedPrice'));
411
+ * // -> 2.5
412
+ * ```
413
+ * @category Configuration
414
+ */
415
+ setMetricDefinition(
416
+ metricId: Id,
417
+ tableId: Id,
418
+ aggregate?: 'sum' | 'avg' | 'min' | 'max' | Aggregate,
419
+ getNumber?: Id | ((getCell: GetCell, rowId: Id) => number),
420
+ aggregateAdd?: AggregateAdd,
421
+ aggregateRemove?: AggregateRemove,
422
+ aggregateReplace?: AggregateReplace,
423
+ ): Metrics;
424
+
425
+ /**
426
+ * The delMetricDefinition method removes an existing Metric definition.
427
+ *
428
+ * @param metricId The Id of the Metric to remove.
429
+ * @returns A reference to the Metrics object.
430
+ * @example
431
+ * This example creates a Store, creates a Metrics object, defines a simple
432
+ * Metric, and then removes it.
433
+ *
434
+ * ```js
435
+ * const store = createStore().setTable('species', {
436
+ * dog: {price: 5},
437
+ * cat: {price: 4},
438
+ * worm: {price: 1},
439
+ * });
440
+ *
441
+ * const metrics = createMetrics(store);
442
+ * metrics.setMetricDefinition('speciesCount', 'species');
443
+ * console.log(metrics.getMetricIds());
444
+ * // -> ['speciesCount']
445
+ *
446
+ * metrics.delMetricDefinition('speciesCount');
447
+ * console.log(metrics.getMetricIds());
448
+ * // -> []
449
+ * ```
450
+ * @category Configuration
451
+ */
452
+ delMetricDefinition(metricId: Id): Metrics;
453
+
454
+ /**
455
+ * The getStore method returns a reference to the underlying Store that is
456
+ * backing this Metrics object.
457
+ *
458
+ * @returns A reference to the Store.
459
+ * @example
460
+ * This example creates a Metrics object against a newly-created Store and
461
+ * then gets its reference in order to update its data.
462
+ *
463
+ * ```js
464
+ * const metrics = createMetrics(createStore());
465
+ * metrics.setMetricDefinition('speciesCount', 'species');
466
+ * metrics.getStore().setCell('species', 'dog', 'price', 5);
467
+ * console.log(metrics.getMetric('speciesCount'));
468
+ * // -> 1
469
+ * ```
470
+ * @category Getter
471
+ */
472
+ getStore(): Store;
473
+
474
+ /**
475
+ * The getMetricIds method returns an array of the Metric Ids registered with
476
+ * this Metrics object.
477
+ *
478
+ * @returns An array of Ids.
479
+ * @example
480
+ * This example creates a Metrics object with two definitions, and then gets
481
+ * the Ids of the definitions.
482
+ *
483
+ * ```js
484
+ * const metrics = createMetrics(createStore())
485
+ * .setMetricDefinition('speciesCount', 'species')
486
+ * .setMetricDefinition('petsCount', 'pets');
487
+ *
488
+ * console.log(metrics.getMetricIds());
489
+ * // -> ['speciesCount', 'petsCount']
490
+ * ```
491
+ * @category Getter
492
+ */
493
+ getMetricIds(): Ids;
494
+
495
+ /**
496
+ * The forEachMetric method takes a function that it will then call for each
497
+ * Metric in the Metrics object.
498
+ *
499
+ * This method is useful for iterating over all the Metrics in a functional
500
+ * style. The `metricCallback` parameter is a MetricCallback function that
501
+ * will be called with the Id of each Metric and its value.
502
+ *
503
+ * @param metricCallback The function that should be called for every Metric.
504
+ * @example
505
+ * This example iterates over each Metric in a Metrics object.
506
+ *
507
+ * ```js
508
+ * const store = createStore().setTable('species', {
509
+ * dog: {price: 5},
510
+ * cat: {price: 4},
511
+ * worm: {price: 1},
512
+ * });
513
+ * const metrics = createMetrics(store)
514
+ * .setMetricDefinition('highestPrice', 'species', 'max', 'price')
515
+ * .setMetricDefinition('lowestPrice', 'species', 'min', 'price');
516
+ *
517
+ * metrics.forEachMetric((metricId, metric) => {
518
+ * console.log([metricId, metric]);
519
+ * });
520
+ * // -> ['highestPrice', 5]
521
+ * // -> ['lowestPrice', 1]
522
+ * ```
523
+ * @category Iterator
524
+ */
525
+ forEachMetric(metricCallback: MetricCallback): void;
526
+
527
+ /**
528
+ * The hasMetric method returns a boolean indicating whether a given Metric
529
+ * exists in the Metrics object, and has a value.
530
+ *
531
+ * @param metricId The Id of a possible Metric in the Metrics object.
532
+ * @returns Whether a Metric with that Id exists.
533
+ * @example
534
+ * This example shows two simple Metric existence checks.
535
+ *
536
+ * ```js
537
+ * const store = createStore();
538
+ * const metrics = createMetrics(store);
539
+ * metrics.setMetricDefinition('highestPrice', 'species', 'max', 'price');
540
+ *
541
+ * console.log(metrics.hasMetric('lowestPrice'));
542
+ * // -> false
543
+ * console.log(metrics.hasMetric('highestPrice'));
544
+ * // -> false
545
+ * store.setTable('species', {dog: {price: 5}, cat: {price: 4}});
546
+ * console.log(metrics.hasMetric('highestPrice'));
547
+ * // -> true
548
+ * ```
549
+ * @category Getter
550
+ */
551
+ hasMetric(metricId: Id): boolean;
552
+
553
+ /**
554
+ * The getTableId method returns the Id of the underlying Table that is
555
+ * backing a Metric.
556
+ *
557
+ * If the Metric Id is invalid, the method returns `undefined`.
558
+ *
559
+ * @param metricId The Id of a Metric.
560
+ * @returns The Id of the Table backing the Metric, or `undefined`.
561
+ * @example
562
+ * This example creates a Metrics object, a single Metric definition, and then
563
+ * queries it (and a non-existent definition) to get the underlying Table Id.
564
+ *
565
+ * ```js
566
+ * const metrics = createMetrics(createStore());
567
+ * metrics.setMetricDefinition('speciesCount', 'species');
568
+ *
569
+ * console.log(metrics.getTableId('speciesCount'));
570
+ * // -> 'species'
571
+ * console.log(metrics.getTableId('petsCount'));
572
+ * // -> undefined
573
+ * ```
574
+ * @category Getter
575
+ */
576
+ getTableId(metricId: Id): Id | undefined;
577
+
578
+ /**
579
+ * The getMetric method gets the current value of a Metric.
580
+ *
581
+ * If the identified Metric does not exist (or if the definition references a
582
+ * Table or Cell value that does not exist) then `undefined` is returned.
583
+ *
584
+ * @param metricId The Id of the Metric.
585
+ * @returns The numeric value of the Metric, or `undefined`.
586
+ * @example
587
+ * This example creates a Store, creates a Metrics object, and defines a
588
+ * simple Metric to average the price values in the Table. It then uses
589
+ * getMetric to access its value (and also the value of a Metric that has not
590
+ * been defined).
591
+ *
592
+ * ```js
593
+ * const store = createStore().setTable('species', {
594
+ * dog: {price: 5},
595
+ * cat: {price: 4},
596
+ * worm: {price: 1},
597
+ * });
598
+ *
599
+ * const metrics = createMetrics(store);
600
+ * metrics.setMetricDefinition('highestPrice', 'species', 'max', 'price');
601
+ *
602
+ * console.log(metrics.getMetric('highestPrice'));
603
+ * // -> 5
604
+ * console.log(metrics.getMetric('lowestPrice'));
605
+ * // -> undefined
606
+ * ```
607
+ * @category Getter
608
+ */
609
+ getMetric(metricId: Id): Metric | undefined;
610
+
611
+ /**
612
+ * The addMetricListener method registers a listener function with the Metrics
613
+ * object that will be called whenever the value of a specified Metric
614
+ * changes.
615
+ *
616
+ * You can either listen to a single Metric (by specifying the Metric Id as
617
+ * the method's first parameter), or changes to any Metric (by providing a
618
+ * `null` wildcard).
619
+ *
620
+ * The provided listener is a MetricListener function, and will be called with
621
+ * a reference to the Metrics object, the Id of the Metric that changed, the
622
+ * new Metric value, and the old Metric value.
623
+ *
624
+ * @param metricId The Id of the Metric to listen to, or `null` as a wildcard.
625
+ * @param listener The function that will be called whenever the Metric
626
+ * changes.
627
+ * @returns A unique Id for the listener that can later be used to remove it.
628
+ * @example
629
+ * This example creates a Store, a Metrics object, and then registers a
630
+ * listener that responds to any changes to a specific Metric.
631
+ *
632
+ * ```js
633
+ * const store = createStore().setTable('species', {
634
+ * dog: {price: 5},
635
+ * cat: {price: 4},
636
+ * worm: {price: 1},
637
+ * });
638
+ *
639
+ * const metrics = createMetrics(store);
640
+ * metrics.setMetricDefinition('highestPrice', 'species', 'max', 'price');
641
+ *
642
+ * const listenerId = metrics.addMetricListener(
643
+ * 'highestPrice',
644
+ * (metrics, metricId, newMetric, oldMetric) => {
645
+ * console.log('highestPrice metric changed');
646
+ * console.log([oldMetric, newMetric]);
647
+ * },
648
+ * );
649
+ *
650
+ * store.setCell('species', 'horse', 'price', 20);
651
+ * // -> 'highestPrice metric changed'
652
+ * // -> [5, 20]
653
+ *
654
+ * metrics.delListener(listenerId);
655
+ * ```
656
+ * @example
657
+ * This example creates a Store, a Metrics object, and then registers a
658
+ * listener that responds to any changes to any Metric.
659
+ *
660
+ * ```js
661
+ * const store = createStore().setTable('species', {
662
+ * dog: {price: 5},
663
+ * cat: {price: 4},
664
+ * worm: {price: 1},
665
+ * });
666
+ *
667
+ * const metrics = createMetrics(store)
668
+ * .setMetricDefinition('highestPrice', 'species', 'max', 'price')
669
+ * .setMetricDefinition('speciesCount', 'species');
670
+ *
671
+ * const listenerId = metrics.addMetricListener(
672
+ * null,
673
+ * (metrics, metricId, newMetric, oldMetric) => {
674
+ * console.log(`${metricId} metric changed`);
675
+ * console.log([oldMetric, newMetric]);
676
+ * },
677
+ * );
678
+ *
679
+ * store.setCell('species', 'horse', 'price', 20);
680
+ * // -> 'highestPrice metric changed'
681
+ * // -> [5, 20]
682
+ * // -> 'speciesCount metric changed'
683
+ * // -> [3, 4]
684
+ *
685
+ * metrics.delListener(listenerId);
686
+ * ```
687
+ * @category Listener
688
+ */
689
+ addMetricListener(metricId: IdOrNull, listener: MetricListener): Id;
690
+
691
+ /**
692
+ * The delListener method removes a listener that was previously added to the
693
+ * Metrics object.
694
+ *
695
+ * Use the Id returned by the addMetricListener method. Note that the Metrics
696
+ * object may re-use this Id for future listeners added to it.
697
+ *
698
+ * @param listenerId The Id of the listener to remove.
699
+ * @returns A reference to the Metrics object.
700
+ * @example
701
+ * This example creates a Store, a Metrics object, registers a listener, and
702
+ * then removes it.
703
+ *
704
+ * ```js
705
+ * const store = createStore().setTable('species', {
706
+ * dog: {price: 5},
707
+ * cat: {price: 4},
708
+ * worm: {price: 1},
709
+ * });
710
+ *
711
+ * const metrics = createMetrics(store);
712
+ * metrics.setMetricDefinition('highestPrice', 'species', 'max', 'price');
713
+ *
714
+ * const listenerId = metrics.addMetricListener(
715
+ * 'highestPrice',
716
+ * (metrics, metricId, newMetric, oldMetric) => {
717
+ * console.log('highestPrice metric changed');
718
+ * },
719
+ * );
720
+ *
721
+ * store.setCell('species', 'horse', 'price', 20);
722
+ * // -> 'highestPrice metric changed'
723
+ *
724
+ * metrics.delListener(listenerId);
725
+ *
726
+ * store.setCell('species', 'giraffe', 'price', 50);
727
+ * // -> undefined
728
+ * // The listener is not called.
729
+ * ```
730
+ * @category Listener
731
+ */
732
+ delListener(listenerId: Id): Metrics;
733
+
734
+ /**
735
+ * The destroy method should be called when this Metrics object is no longer
736
+ * used.
737
+ *
738
+ * This guarantees that all of the listeners that the object registered with
739
+ * the underlying Store are removed and it can be correctly garbage collected.
740
+ *
741
+ * @example
742
+ * This example creates a Store, adds a Metrics object with a definition (that
743
+ * registers a RowListener with the underlying Store), and then destroys it
744
+ * again, removing the listener.
745
+ *
746
+ * ```js
747
+ * const store = createStore().setTable('species', {
748
+ * dog: {price: 5},
749
+ * cat: {price: 4},
750
+ * worm: {price: 1},
751
+ * });
752
+ *
753
+ * const metrics = createMetrics(store);
754
+ * metrics.setMetricDefinition('speciesCount', 'species');
755
+ * console.log(store.getListenerStats().row);
756
+ * // -> 1
757
+ *
758
+ * metrics.destroy();
759
+ *
760
+ * console.log(store.getListenerStats().row);
761
+ * // -> 0
762
+ * ```
763
+ * @category Lifecycle
764
+ */
765
+ destroy(): void;
766
+
767
+ /**
768
+ * The getListenerStats method provides a set of statistics about the
769
+ * listeners registered with the Metrics object, and is used for debugging
770
+ * purposes.
771
+ *
772
+ * The statistics are only populated in a debug build: production builds
773
+ * return an empty object. The method is intended to be used during
774
+ * development to ensure your application is not leaking listener
775
+ * registrations, for example.
776
+ *
777
+ * @returns A MetricsListenerStats object containing Metrics listener
778
+ * statistics.
779
+ * @example
780
+ * This example gets the listener statistics of a Metrics object.
781
+ *
782
+ * ```js
783
+ * const store = createStore();
784
+ * const metrics = createMetrics(store);
785
+ * metrics.addMetricListener(null, () => console.log('Metric changed'));
786
+ *
787
+ * console.log(metrics.getListenerStats());
788
+ * // -> {metric: 1}
789
+ * ```
790
+ * @category Development
791
+ */
792
+ getListenerStats(): MetricsListenerStats;
793
+ }
794
+
795
+ /**
796
+ * The createMetrics function creates a Metrics object, and is the main entry
797
+ * point into the metrics module.
798
+ *
799
+ * A given Store can only have one Metrics object associated with it. If you
800
+ * call this function twice on the same Store, your second call will return a
801
+ * reference to the Metrics object created by the first.
802
+ *
803
+ * @param store The Store for which to register Metric definitions.
804
+ * @returns A reference to the new Metrics object.
805
+ * @example
806
+ * This example creates a Metrics object.
807
+ *
808
+ * ```js
809
+ * const store = createStore();
810
+ * const metrics = createMetrics(store);
811
+ * console.log(metrics.getMetricIds());
812
+ * // -> []
813
+ * ```
814
+ * @example
815
+ * This example creates a Metrics object, and calls the method a second time
816
+ * for the same Store to return the same object.
817
+ *
818
+ * ```js
819
+ * const store = createStore();
820
+ * const metrics1 = createMetrics(store);
821
+ * const metrics2 = createMetrics(store);
822
+ * console.log(metrics1 === metrics2);
823
+ * // -> true
824
+ * ```
825
+ * @category Creation
826
+ */
827
+ export function createMetrics(store: Store): Metrics;