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