tinybase 3.1.0-beta.4 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/debug/ui-react.js +2 -2
- package/lib/{checkpoints.d.ts → types/checkpoints.d.ts} +13 -13
- package/lib/{debug → types}/indexes.d.ts +10 -16
- package/lib/{debug → types}/metrics.d.ts +49 -50
- package/lib/{debug → types}/persisters.d.ts +26 -30
- package/lib/{cjs-es6 → types}/queries.d.ts +474 -309
- package/lib/{debug → types}/relationships.d.ts +11 -17
- package/lib/{debug → types}/store.d.ts +180 -1038
- package/lib/types/tinybase.d.ts +14 -0
- package/lib/{debug → types}/tools.d.ts +12 -12
- package/lib/{debug → types}/ui-react.d.ts +11 -5
- package/lib/{cjs-es6 → types/with-schemas}/checkpoints.d.ts +93 -7
- package/lib/{cjs → types/with-schemas}/indexes.d.ts +136 -18
- package/lib/types/with-schemas/internal/queries.d.ts +15 -0
- package/lib/types/with-schemas/internal/store.d.ts +101 -0
- package/lib/types/with-schemas/internal/ui-react.d.ts +776 -0
- package/lib/{cjs → types/with-schemas}/metrics.d.ts +115 -50
- package/lib/{cjs → types/with-schemas}/persisters.d.ts +102 -4
- package/lib/{debug → types/with-schemas}/queries.d.ts +865 -346
- package/lib/{cjs → types/with-schemas}/relationships.d.ts +151 -20
- package/lib/{cjs-es6 → types/with-schemas}/store.d.ts +1082 -801
- package/lib/types/with-schemas/tinybase.d.ts +14 -0
- package/lib/{cjs-es6 → types/with-schemas}/tools.d.ts +21 -10
- package/lib/{cjs-es6 → types/with-schemas}/ui-react.d.ts +1871 -1314
- package/package.json +145 -29
- package/readme.md +14 -14
- package/lib/cjs/checkpoints.d.ts +0 -961
- package/lib/cjs/queries.d.ts +0 -3028
- package/lib/cjs/store.d.ts +0 -6143
- package/lib/cjs/tinybase.d.ts +0 -14
- package/lib/cjs/tools.d.ts +0 -536
- package/lib/cjs/ui-react.d.ts +0 -10921
- package/lib/cjs-es6/indexes.d.ts +0 -974
- package/lib/cjs-es6/metrics.d.ts +0 -829
- package/lib/cjs-es6/persisters.d.ts +0 -733
- package/lib/cjs-es6/relationships.d.ts +0 -1209
- package/lib/cjs-es6/tinybase.d.ts +0 -14
- package/lib/common.d.ts +0 -115
- package/lib/debug/checkpoints.d.ts +0 -961
- package/lib/debug/common.d.ts +0 -115
- package/lib/debug/tinybase.d.ts +0 -14
- package/lib/es6/checkpoints.d.ts +0 -961
- package/lib/es6/common.d.ts +0 -115
- package/lib/es6/indexes.d.ts +0 -974
- package/lib/es6/metrics.d.ts +0 -829
- package/lib/es6/persisters.d.ts +0 -733
- package/lib/es6/queries.d.ts +0 -3028
- package/lib/es6/relationships.d.ts +0 -1209
- package/lib/es6/store.d.ts +0 -6143
- package/lib/es6/tinybase.d.ts +0 -14
- package/lib/es6/tools.d.ts +0 -536
- package/lib/es6/ui-react.d.ts +0 -10921
- package/lib/indexes.d.ts +0 -974
- package/lib/metrics.d.ts +0 -829
- package/lib/persisters.d.ts +0 -733
- package/lib/queries.d.ts +0 -3028
- package/lib/relationships.d.ts +0 -1209
- package/lib/store.d.ts +0 -6143
- package/lib/tinybase.d.ts +0 -14
- package/lib/tools.d.ts +0 -536
- package/lib/ui-react.d.ts +0 -10921
- package/lib/umd/checkpoints.d.ts +0 -961
- package/lib/umd/common.d.ts +0 -115
- package/lib/umd/indexes.d.ts +0 -974
- package/lib/umd/metrics.d.ts +0 -829
- package/lib/umd/persisters.d.ts +0 -733
- package/lib/umd/queries.d.ts +0 -3028
- package/lib/umd/relationships.d.ts +0 -1209
- package/lib/umd/store.d.ts +0 -6143
- package/lib/umd/tinybase.d.ts +0 -14
- package/lib/umd/tools.d.ts +0 -536
- package/lib/umd/ui-react.d.ts +0 -10921
- package/lib/umd-es6/checkpoints.d.ts +0 -961
- package/lib/umd-es6/common.d.ts +0 -115
- package/lib/umd-es6/indexes.d.ts +0 -974
- package/lib/umd-es6/metrics.d.ts +0 -829
- package/lib/umd-es6/persisters.d.ts +0 -733
- package/lib/umd-es6/queries.d.ts +0 -3028
- package/lib/umd-es6/relationships.d.ts +0 -1209
- package/lib/umd-es6/store.d.ts +0 -6143
- package/lib/umd-es6/tinybase.d.ts +0 -14
- package/lib/umd-es6/tools.d.ts +0 -536
- package/lib/umd-es6/ui-react.d.ts +0 -10921
- /package/lib/{cjs-es6 → types}/common.d.ts +0 -0
- /package/lib/{cjs → types/with-schemas}/common.d.ts +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The metrics module of the TinyBase project provides the ability to create
|
|
3
|
-
*
|
|
2
|
+
* The metrics module of the TinyBase project provides the ability to create and
|
|
3
|
+
* track metrics and aggregates of the data in Store objects.
|
|
4
4
|
*
|
|
5
5
|
* The main entry point to this module is the createMetrics function, which
|
|
6
6
|
* returns a new Metrics object. From there, you can create new Metric
|
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
* @module metrics
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
-
import {
|
|
14
|
+
import {CellIdFromSchema, TableIdFromSchema} from './internal/store';
|
|
15
|
+
import {GetCell, OptionalSchemas, Store} from './store.d';
|
|
15
16
|
import {Id, IdOrNull, Ids} from './common.d';
|
|
16
17
|
|
|
17
18
|
/**
|
|
@@ -37,8 +38,8 @@ export type Metric = number;
|
|
|
37
38
|
export type MetricCallback = (metricId: Id, metric?: Metric) => void;
|
|
38
39
|
|
|
39
40
|
/**
|
|
40
|
-
* The
|
|
41
|
-
* and returns an aggregate that is used as a Metric.
|
|
41
|
+
* The MetricAggregate type describes a custom function that takes an array of
|
|
42
|
+
* numbers and returns an aggregate that is used as a Metric.
|
|
42
43
|
*
|
|
43
44
|
* There are a number of common predefined aggregators, such as for counting,
|
|
44
45
|
* summing, and averaging values. This type is instead used for when you wish to
|
|
@@ -50,12 +51,12 @@ export type MetricCallback = (metricId: Id, metric?: Metric) => void;
|
|
|
50
51
|
* @returns The value of the Metric.
|
|
51
52
|
* @category Aggregators
|
|
52
53
|
*/
|
|
53
|
-
export type
|
|
54
|
+
export type MetricAggregate = (numbers: number[], length: number) => Metric;
|
|
54
55
|
|
|
55
56
|
/**
|
|
56
|
-
* The
|
|
57
|
-
* custom
|
|
58
|
-
* the input values.
|
|
57
|
+
* The MetricAggregateAdd type describes a function that can be used to optimize
|
|
58
|
+
* a custom MetricAggregate by providing a shortcut for when a single value is
|
|
59
|
+
* added to the input values.
|
|
59
60
|
*
|
|
60
61
|
* Some aggregation functions do not need to recalculate the aggregation of the
|
|
61
62
|
* whole set when one value changes. For example, when adding a new number to a
|
|
@@ -65,10 +66,10 @@ export type Aggregate = (numbers: number[], length: number) => Metric;
|
|
|
65
66
|
* being added, return `undefined` and the Metric will be completely
|
|
66
67
|
* recalculated.
|
|
67
68
|
*
|
|
68
|
-
* Where possible, if you are providing a custom
|
|
69
|
-
* implementation of an
|
|
70
|
-
* cost of growing the input data set. See the setMetricDefinition
|
|
71
|
-
* more examples.
|
|
69
|
+
* Where possible, if you are providing a custom MetricAggregate, seek an
|
|
70
|
+
* implementation of an MetricAggregateAdd function that can reduce the
|
|
71
|
+
* complexity cost of growing the input data set. See the setMetricDefinition
|
|
72
|
+
* method for more examples.
|
|
72
73
|
*
|
|
73
74
|
* @param metric The current value of the Metric.
|
|
74
75
|
* @param add The number being added to the Metric's aggregation.
|
|
@@ -76,16 +77,16 @@ export type Aggregate = (numbers: number[], length: number) => Metric;
|
|
|
76
77
|
* @returns The new value of the Metric.
|
|
77
78
|
* @category Aggregators
|
|
78
79
|
*/
|
|
79
|
-
export type
|
|
80
|
+
export type MetricAggregateAdd = (
|
|
80
81
|
metric: Metric,
|
|
81
82
|
add: number,
|
|
82
83
|
length: number,
|
|
83
84
|
) => Metric | undefined;
|
|
84
85
|
|
|
85
86
|
/**
|
|
86
|
-
* The
|
|
87
|
-
* custom
|
|
88
|
-
* from the input values.
|
|
87
|
+
* The MetricAggregateRemove type describes a function that can be used to
|
|
88
|
+
* optimize a custom MetricAggregate by providing a shortcut for when a single
|
|
89
|
+
* value is removed from the input values.
|
|
89
90
|
*
|
|
90
91
|
* Some aggregation functions do not need to recalculate the aggregation of the
|
|
91
92
|
* whole set when one value changes. For example, when removing a number from a
|
|
@@ -98,10 +99,10 @@ export type AggregateAdd = (
|
|
|
98
99
|
* values, and the previous minimum is being removed. The whole of the rest of
|
|
99
100
|
* the list will need to be re-scanned to find a new minimum.
|
|
100
101
|
*
|
|
101
|
-
* Where possible, if you are providing a custom
|
|
102
|
-
* implementation of an
|
|
103
|
-
* cost of shrinking the input data set. See the setMetricDefinition
|
|
104
|
-
* more examples.
|
|
102
|
+
* Where possible, if you are providing a custom MetricAggregate, seek an
|
|
103
|
+
* implementation of an MetricAggregateRemove function that can reduce the
|
|
104
|
+
* complexity cost of shrinking the input data set. See the setMetricDefinition
|
|
105
|
+
* method for more examples.
|
|
105
106
|
*
|
|
106
107
|
* @param metric The current value of the Metric.
|
|
107
108
|
* @param remove The number being removed from the Metric's aggregation.
|
|
@@ -109,16 +110,16 @@ export type AggregateAdd = (
|
|
|
109
110
|
* @returns The new value of the Metric.
|
|
110
111
|
* @category Aggregators
|
|
111
112
|
*/
|
|
112
|
-
export type
|
|
113
|
+
export type MetricAggregateRemove = (
|
|
113
114
|
metric: Metric,
|
|
114
115
|
remove: number,
|
|
115
116
|
length: number,
|
|
116
117
|
) => Metric | undefined;
|
|
117
118
|
|
|
118
119
|
/**
|
|
119
|
-
* The
|
|
120
|
-
* custom
|
|
121
|
-
* values is replaced with another.
|
|
120
|
+
* The MetricAggregateReplace type describes a function that can be used to
|
|
121
|
+
* optimize a custom MetricAggregate by providing a shortcut for when a single
|
|
122
|
+
* value in the input values is replaced with another.
|
|
122
123
|
*
|
|
123
124
|
* Some aggregation functions do not need to recalculate the aggregation of the
|
|
124
125
|
* whole set when one value changes. For example, when replacing a number in a
|
|
@@ -126,13 +127,12 @@ export type AggregateRemove = (
|
|
|
126
127
|
* minus the old value.
|
|
127
128
|
*
|
|
128
129
|
* 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.
|
|
130
|
+
* changing, return `undefined` and the Metric will be completely recalculated.
|
|
131
131
|
*
|
|
132
|
-
* Where possible, if you are providing a custom
|
|
133
|
-
* implementation of an
|
|
134
|
-
* cost of changing the input data set in place. See the
|
|
135
|
-
* method for more examples.
|
|
132
|
+
* Where possible, if you are providing a custom MetricAggregate, seek an
|
|
133
|
+
* implementation of an MetricAggregateReplace function that can reduce the
|
|
134
|
+
* complexity cost of changing the input data set in place. See the
|
|
135
|
+
* setMetricDefinition method for more examples.
|
|
136
136
|
*
|
|
137
137
|
* @param metric The current value of the Metric.
|
|
138
138
|
* @param add The number being added to the Metric's aggregation.
|
|
@@ -141,7 +141,7 @@ export type AggregateRemove = (
|
|
|
141
141
|
* @returns The new value of the Metric.
|
|
142
142
|
* @category Aggregators
|
|
143
143
|
*/
|
|
144
|
-
export type
|
|
144
|
+
export type MetricAggregateReplace = (
|
|
145
145
|
metric: Metric,
|
|
146
146
|
add: number,
|
|
147
147
|
remove: number,
|
|
@@ -152,6 +152,17 @@ export type AggregateReplace = (
|
|
|
152
152
|
* The MetricListener type describes a function that is used to listen to
|
|
153
153
|
* changes to a Metric.
|
|
154
154
|
*
|
|
155
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
156
|
+
*
|
|
157
|
+
* ```ts override
|
|
158
|
+
* (
|
|
159
|
+
* metrics: Metrics,
|
|
160
|
+
* metricId: Id,
|
|
161
|
+
* newMetric: Metric | undefined,
|
|
162
|
+
* oldMetric: Metric | undefined,
|
|
163
|
+
* ) => void;
|
|
164
|
+
* ```
|
|
165
|
+
*
|
|
155
166
|
* A MetricListener is provided when using the addMetricListener method. See
|
|
156
167
|
* that method for specific examples.
|
|
157
168
|
*
|
|
@@ -168,8 +179,8 @@ export type AggregateReplace = (
|
|
|
168
179
|
* @param oldMetric The old value of the Metric that changed.
|
|
169
180
|
* @category Listener
|
|
170
181
|
*/
|
|
171
|
-
export type MetricListener = (
|
|
172
|
-
metrics: Metrics
|
|
182
|
+
export type MetricListener<Schemas extends OptionalSchemas> = (
|
|
183
|
+
metrics: Metrics<Schemas>,
|
|
173
184
|
metricId: Id,
|
|
174
185
|
newMetric: Metric | undefined,
|
|
175
186
|
oldMetric: Metric | undefined,
|
|
@@ -246,10 +257,24 @@ export type MetricsListenerStats = {
|
|
|
246
257
|
* @see Todo App demos
|
|
247
258
|
* @category Metrics
|
|
248
259
|
*/
|
|
249
|
-
export interface Metrics<Schemas extends OptionalSchemas
|
|
260
|
+
export interface Metrics<in out Schemas extends OptionalSchemas> {
|
|
250
261
|
/**
|
|
251
262
|
* The setMetricDefinition method lets you set the definition of a Metric.
|
|
252
263
|
*
|
|
264
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
265
|
+
*
|
|
266
|
+
* ```ts override
|
|
267
|
+
* setMetricDefinition(
|
|
268
|
+
* metricId: Id,
|
|
269
|
+
* tableId: Id,
|
|
270
|
+
* aggregate?: 'sum' | 'avg' | 'min' | 'max' | MetricAggregate,
|
|
271
|
+
* getNumber?: Id | ((getCell: GetCell, rowId: Id) => number),
|
|
272
|
+
* aggregateAdd?: MetricAggregateAdd,
|
|
273
|
+
* aggregateRemove?: MetricAggregateRemove,
|
|
274
|
+
* aggregateReplace?: MetricAggregateReplace,
|
|
275
|
+
* ): Metrics;
|
|
276
|
+
* ```
|
|
277
|
+
*
|
|
253
278
|
* Every Metric definition is identified by a unique Id, and if you re-use an
|
|
254
279
|
* existing Id with this method, the previous definition is overwritten.
|
|
255
280
|
*
|
|
@@ -287,14 +312,14 @@ export interface Metrics<Schemas extends OptionalSchemas = NoSchemas> {
|
|
|
287
312
|
* that if the `aggregate` and `getNumber` parameters are both omitted, the
|
|
288
313
|
* Metric will simply be a count of the Row objects in the Table).
|
|
289
314
|
* @param aggregateAdd A function that can be used to optimize a custom
|
|
290
|
-
*
|
|
291
|
-
* input values - for example, when a Row is added to the Table.
|
|
315
|
+
* MetricAggregate by providing a shortcut for when a single value is added to
|
|
316
|
+
* the input values - for example, when a Row is added to the Table.
|
|
292
317
|
* @param aggregateRemove A function that can be used to optimize a custom
|
|
293
|
-
*
|
|
294
|
-
* the input values - for example ,when a Row is removed from the Table.
|
|
318
|
+
* MetricAggregate by providing a shortcut for when a single value is removed
|
|
319
|
+
* from the input values - for example ,when a Row is removed from the Table.
|
|
295
320
|
* @param aggregateReplace A function that can be used to optimize a custom
|
|
296
|
-
*
|
|
297
|
-
* values is replaced with another - for example, when a Row is updated.
|
|
321
|
+
* MetricAggregate by providing a shortcut for when a single value in the
|
|
322
|
+
* input values is replaced with another - for example, when a Row is updated.
|
|
298
323
|
* @returns A reference to the Metrics object.
|
|
299
324
|
* @example
|
|
300
325
|
* This example creates a Store, creates a Metrics object, and defines a
|
|
@@ -412,19 +437,27 @@ export interface Metrics<Schemas extends OptionalSchemas = NoSchemas> {
|
|
|
412
437
|
* ```
|
|
413
438
|
* @category Configuration
|
|
414
439
|
*/
|
|
415
|
-
setMetricDefinition(
|
|
440
|
+
setMetricDefinition<TableId extends TableIdFromSchema<Schemas[0]>>(
|
|
416
441
|
metricId: Id,
|
|
417
|
-
tableId:
|
|
418
|
-
aggregate?: 'sum' | 'avg' | 'min' | 'max' |
|
|
419
|
-
getNumber?:
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
442
|
+
tableId: TableId,
|
|
443
|
+
aggregate?: 'sum' | 'avg' | 'min' | 'max' | MetricAggregate,
|
|
444
|
+
getNumber?:
|
|
445
|
+
| CellIdFromSchema<Schemas[0], TableId>
|
|
446
|
+
| ((getCell: GetCell<Schemas[0], TableId>, rowId: Id) => number),
|
|
447
|
+
aggregateAdd?: MetricAggregateAdd,
|
|
448
|
+
aggregateRemove?: MetricAggregateRemove,
|
|
449
|
+
aggregateReplace?: MetricAggregateReplace,
|
|
423
450
|
): Metrics<Schemas>;
|
|
424
451
|
|
|
425
452
|
/**
|
|
426
453
|
* The delMetricDefinition method removes an existing Metric definition.
|
|
427
454
|
*
|
|
455
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
456
|
+
*
|
|
457
|
+
* ```ts override
|
|
458
|
+
* delMetricDefinition(metricId: Id): Metrics;
|
|
459
|
+
* ```
|
|
460
|
+
*
|
|
428
461
|
* @param metricId The Id of the Metric to remove.
|
|
429
462
|
* @returns A reference to the Metrics object.
|
|
430
463
|
* @example
|
|
@@ -455,6 +488,12 @@ export interface Metrics<Schemas extends OptionalSchemas = NoSchemas> {
|
|
|
455
488
|
* The getStore method returns a reference to the underlying Store that is
|
|
456
489
|
* backing this Metrics object.
|
|
457
490
|
*
|
|
491
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
492
|
+
*
|
|
493
|
+
* ```ts override
|
|
494
|
+
* getStore(): Store;
|
|
495
|
+
* ```
|
|
496
|
+
*
|
|
458
497
|
* @returns A reference to the Store.
|
|
459
498
|
* @example
|
|
460
499
|
* This example creates a Metrics object against a newly-created Store and
|
|
@@ -554,6 +593,12 @@ export interface Metrics<Schemas extends OptionalSchemas = NoSchemas> {
|
|
|
554
593
|
* The getTableId method returns the Id of the underlying Table that is
|
|
555
594
|
* backing a Metric.
|
|
556
595
|
*
|
|
596
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
597
|
+
*
|
|
598
|
+
* ```ts override
|
|
599
|
+
* getTableId(metricId: Id): Id | undefined;
|
|
600
|
+
* ```
|
|
601
|
+
*
|
|
557
602
|
* If the Metric Id is invalid, the method returns `undefined`.
|
|
558
603
|
*
|
|
559
604
|
* @param metricId The Id of a Metric.
|
|
@@ -573,7 +618,9 @@ export interface Metrics<Schemas extends OptionalSchemas = NoSchemas> {
|
|
|
573
618
|
* ```
|
|
574
619
|
* @category Getter
|
|
575
620
|
*/
|
|
576
|
-
getTableId
|
|
621
|
+
getTableId<TableId extends TableIdFromSchema<Schemas[0]>>(
|
|
622
|
+
metricId: Id,
|
|
623
|
+
): TableId | undefined;
|
|
577
624
|
|
|
578
625
|
/**
|
|
579
626
|
* The getMetric method gets the current value of a Metric.
|
|
@@ -613,6 +660,12 @@ export interface Metrics<Schemas extends OptionalSchemas = NoSchemas> {
|
|
|
613
660
|
* object that will be called whenever the value of a specified Metric
|
|
614
661
|
* changes.
|
|
615
662
|
*
|
|
663
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
664
|
+
*
|
|
665
|
+
* ```ts override
|
|
666
|
+
* addMetricListener(metricId: IdOrNull, listener: MetricListener): Id;
|
|
667
|
+
* ```
|
|
668
|
+
*
|
|
616
669
|
* You can either listen to a single Metric (by specifying the Metric Id as
|
|
617
670
|
* the method's first parameter), or changes to any Metric (by providing a
|
|
618
671
|
* `null` wildcard).
|
|
@@ -686,12 +739,18 @@ export interface Metrics<Schemas extends OptionalSchemas = NoSchemas> {
|
|
|
686
739
|
* ```
|
|
687
740
|
* @category Listener
|
|
688
741
|
*/
|
|
689
|
-
addMetricListener(metricId: IdOrNull, listener: MetricListener): Id;
|
|
742
|
+
addMetricListener(metricId: IdOrNull, listener: MetricListener<Schemas>): Id;
|
|
690
743
|
|
|
691
744
|
/**
|
|
692
745
|
* The delListener method removes a listener that was previously added to the
|
|
693
746
|
* Metrics object.
|
|
694
747
|
*
|
|
748
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
749
|
+
*
|
|
750
|
+
* ```ts override
|
|
751
|
+
* delListener(listenerId: Id): Metrics;
|
|
752
|
+
* ```
|
|
753
|
+
*
|
|
695
754
|
* Use the Id returned by the addMetricListener method. Note that the Metrics
|
|
696
755
|
* object may re-use this Id for future listeners added to it.
|
|
697
756
|
*
|
|
@@ -796,6 +855,12 @@ export interface Metrics<Schemas extends OptionalSchemas = NoSchemas> {
|
|
|
796
855
|
* The createMetrics function creates a Metrics object, and is the main entry
|
|
797
856
|
* point into the metrics module.
|
|
798
857
|
*
|
|
858
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
859
|
+
*
|
|
860
|
+
* ```ts override
|
|
861
|
+
* createMetrics(store: Store): Metrics;
|
|
862
|
+
* ```
|
|
863
|
+
*
|
|
799
864
|
* A given Store can only have one Metrics object associated with it. If you
|
|
800
865
|
* call this function twice on the same Store, your second call will return a
|
|
801
866
|
* reference to the Metrics object created by the first.
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
* @module persisters
|
|
28
28
|
*/
|
|
29
29
|
|
|
30
|
-
import {
|
|
30
|
+
import {OptionalSchemas, Store, Tables, Values} from './store.d';
|
|
31
31
|
import {Callback} from './common.d';
|
|
32
32
|
|
|
33
33
|
/**
|
|
@@ -145,11 +145,17 @@ export type PersisterStats = {
|
|
|
145
145
|
* ```
|
|
146
146
|
* @category Persister
|
|
147
147
|
*/
|
|
148
|
-
export interface Persister<Schemas extends OptionalSchemas
|
|
148
|
+
export interface Persister<in out Schemas extends OptionalSchemas> {
|
|
149
149
|
/**
|
|
150
150
|
* The load method gets persisted data from storage, and loads it into the
|
|
151
151
|
* Store with which the Persister is associated, once.
|
|
152
152
|
*
|
|
153
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
154
|
+
*
|
|
155
|
+
* ```ts override
|
|
156
|
+
* load(initialTables?: Tables, initialValues?: Values): Promise<Persister>;
|
|
157
|
+
* ```
|
|
158
|
+
*
|
|
153
159
|
* The optional parameter allows you to specify what the initial Tables object
|
|
154
160
|
* for the Store will be if there is nothing currently persisted. Using this
|
|
155
161
|
* instead of the `initialTables` parameter in the regular createStore
|
|
@@ -208,7 +214,7 @@ export interface Persister<Schemas extends OptionalSchemas = NoSchemas> {
|
|
|
208
214
|
*/
|
|
209
215
|
load(
|
|
210
216
|
initialTables?: Tables<Schemas[0], true>,
|
|
211
|
-
initialValues?: Values,
|
|
217
|
+
initialValues?: Values<Schemas[1], true>,
|
|
212
218
|
): Promise<Persister<Schemas>>;
|
|
213
219
|
|
|
214
220
|
/**
|
|
@@ -216,6 +222,15 @@ export interface Persister<Schemas extends OptionalSchemas = NoSchemas> {
|
|
|
216
222
|
* into the Store with which the Persister is associated, once, and then
|
|
217
223
|
* continuously.
|
|
218
224
|
*
|
|
225
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
226
|
+
*
|
|
227
|
+
* ```ts override
|
|
228
|
+
* startAutoLoad(
|
|
229
|
+
* initialTables?: Tables,
|
|
230
|
+
* initialValues?: Values,
|
|
231
|
+
* ): Promise<Persister>;
|
|
232
|
+
* ```
|
|
233
|
+
*
|
|
219
234
|
* The optional parameter allows you to specify what the initial Tables object
|
|
220
235
|
* for the Store will be if there is nothing at first persisted. Using this
|
|
221
236
|
* instead of the `initialTables` parameter in the regular createStore
|
|
@@ -267,13 +282,19 @@ export interface Persister<Schemas extends OptionalSchemas = NoSchemas> {
|
|
|
267
282
|
*/
|
|
268
283
|
startAutoLoad(
|
|
269
284
|
initialTables?: Tables<Schemas[0], true>,
|
|
270
|
-
initialValues?: Values,
|
|
285
|
+
initialValues?: Values<Schemas[1], true>,
|
|
271
286
|
): Promise<Persister<Schemas>>;
|
|
272
287
|
|
|
273
288
|
/**
|
|
274
289
|
* The stopAutoLoad method stops the automatic loading of data from storage
|
|
275
290
|
* previously started with the startAutoLoad method.
|
|
276
291
|
*
|
|
292
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
293
|
+
*
|
|
294
|
+
* ```ts override
|
|
295
|
+
* stopAutoLoad(): Persister;
|
|
296
|
+
* ```
|
|
297
|
+
*
|
|
277
298
|
* If the Persister is not currently set to automatically load, this method
|
|
278
299
|
* has no effect.
|
|
279
300
|
*
|
|
@@ -319,6 +340,12 @@ export interface Persister<Schemas extends OptionalSchemas = NoSchemas> {
|
|
|
319
340
|
* The save method takes data from the Store with which the Persister is
|
|
320
341
|
* associated and persists it into storage, once.
|
|
321
342
|
*
|
|
343
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
344
|
+
*
|
|
345
|
+
* ```ts override
|
|
346
|
+
* save(): Promise<Persister>;
|
|
347
|
+
* ```
|
|
348
|
+
*
|
|
322
349
|
* This method is asynchronous because the persisted data may be on a remote
|
|
323
350
|
* machine or a filesystem. Even for those storage types that are synchronous
|
|
324
351
|
* (like browser storage) it is still recommended that you `await` calls to
|
|
@@ -348,6 +375,12 @@ export interface Persister<Schemas extends OptionalSchemas = NoSchemas> {
|
|
|
348
375
|
* The save method takes data from the Store with which the Persister is
|
|
349
376
|
* associated and persists it into storage, once, and then continuously.
|
|
350
377
|
*
|
|
378
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
379
|
+
*
|
|
380
|
+
* ```ts override
|
|
381
|
+
* startAutoSave(): Promise<Persister>;
|
|
382
|
+
* ```
|
|
383
|
+
*
|
|
351
384
|
* This method first runs a single call to the save method to ensure the data
|
|
352
385
|
* is in sync with the persisted storage. It then continues to watch for
|
|
353
386
|
* changes to the Store, automatically saving the data to storage.
|
|
@@ -386,6 +419,12 @@ export interface Persister<Schemas extends OptionalSchemas = NoSchemas> {
|
|
|
386
419
|
* The stopAutoSave method stops the automatic save of data to storage
|
|
387
420
|
* previously started with the startAutoSave method.
|
|
388
421
|
*
|
|
422
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
423
|
+
*
|
|
424
|
+
* ```ts override
|
|
425
|
+
* stopAutoSave(): Persister;
|
|
426
|
+
* ```
|
|
427
|
+
*
|
|
389
428
|
* If the Persister is not currently set to automatically save, this method
|
|
390
429
|
* has no effect.
|
|
391
430
|
*
|
|
@@ -424,6 +463,12 @@ export interface Persister<Schemas extends OptionalSchemas = NoSchemas> {
|
|
|
424
463
|
* The getStore method returns a reference to the underlying Store that is
|
|
425
464
|
* backing this Persister object.
|
|
426
465
|
*
|
|
466
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
467
|
+
*
|
|
468
|
+
* ```ts override
|
|
469
|
+
* getStore(): Store;
|
|
470
|
+
* ```
|
|
471
|
+
*
|
|
427
472
|
* @returns A reference to the Store.
|
|
428
473
|
* @example
|
|
429
474
|
* This example creates a Persister object against a newly-created Store and
|
|
@@ -448,6 +493,12 @@ export interface Persister<Schemas extends OptionalSchemas = NoSchemas> {
|
|
|
448
493
|
* The destroy method should be called when this Persister object is no longer
|
|
449
494
|
* used.
|
|
450
495
|
*
|
|
496
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
497
|
+
*
|
|
498
|
+
* ```ts override
|
|
499
|
+
* destroy(): Persister;
|
|
500
|
+
* ```
|
|
501
|
+
*
|
|
451
502
|
* This guarantees that all of the listeners that the object registered with
|
|
452
503
|
* the underlying Store and storage are removed and it can be correctly
|
|
453
504
|
* garbage collected. It is equivalent to running the stopAutoLoad method and
|
|
@@ -525,6 +576,15 @@ export interface Persister<Schemas extends OptionalSchemas = NoSchemas> {
|
|
|
525
576
|
* The createSessionPersister function creates a Persister object that can
|
|
526
577
|
* persist the Store to the browser's session storage.
|
|
527
578
|
*
|
|
579
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
580
|
+
*
|
|
581
|
+
* ```ts override
|
|
582
|
+
* createSessionPersister(
|
|
583
|
+
* store: Store,
|
|
584
|
+
* storageName: string,
|
|
585
|
+
* ): Persister;
|
|
586
|
+
* ```
|
|
587
|
+
*
|
|
528
588
|
* As well as providing a reference to the Store to persist, you must provide a
|
|
529
589
|
* `storageName` parameter which is unique to your application. This is the key
|
|
530
590
|
* that the browser uses to identify the storage location.
|
|
@@ -558,6 +618,15 @@ export function createSessionPersister<Schemas extends OptionalSchemas>(
|
|
|
558
618
|
* The createLocalPersister function creates a Persister object that can
|
|
559
619
|
* persist the Store to the browser's local storage.
|
|
560
620
|
*
|
|
621
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
622
|
+
*
|
|
623
|
+
* ```ts override
|
|
624
|
+
* createLocalPersister(
|
|
625
|
+
* store: Store,
|
|
626
|
+
* storageName: string,
|
|
627
|
+
* ): Persister;
|
|
628
|
+
* ```
|
|
629
|
+
*
|
|
561
630
|
* As well as providing a reference to the Store to persist, you must provide a
|
|
562
631
|
* `storageName` parameter which is unique to your application. This is the key
|
|
563
632
|
* that the browser uses to identify the storage location.
|
|
@@ -591,6 +660,17 @@ export function createLocalPersister<Schemas extends OptionalSchemas>(
|
|
|
591
660
|
* The createRemotePersister function creates a Persister object that can
|
|
592
661
|
* persist the Store to a remote server.
|
|
593
662
|
*
|
|
663
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
664
|
+
*
|
|
665
|
+
* ```ts override
|
|
666
|
+
* createRemotePersister(
|
|
667
|
+
* store: Store,
|
|
668
|
+
* loadUrl: string,
|
|
669
|
+
* saveUrl: string,
|
|
670
|
+
* autoLoadIntervalSeconds: number,
|
|
671
|
+
* ): Persister;
|
|
672
|
+
* ```
|
|
673
|
+
*
|
|
594
674
|
* As well as providing a reference to the Store to persist, you must provide
|
|
595
675
|
* `loadUrl` and `saveUrl` parameters. These identify the endpoints of the
|
|
596
676
|
* server that support the `GET` method (to fetch the Store JSON to load) and
|
|
@@ -640,6 +720,12 @@ export function createRemotePersister<Schemas extends OptionalSchemas>(
|
|
|
640
720
|
* The createFilePersister function creates a Persister object that can persist
|
|
641
721
|
* the Store to a local file (in an appropriate environment).
|
|
642
722
|
*
|
|
723
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
724
|
+
*
|
|
725
|
+
* ```ts override
|
|
726
|
+
* createFilePersister(store: Store, filePath: string): Persister;
|
|
727
|
+
* ```
|
|
728
|
+
*
|
|
643
729
|
* As well as providing a reference to the Store to persist, you must provide
|
|
644
730
|
* `filePath` parameter which identifies the file to persist it to.
|
|
645
731
|
*
|
|
@@ -673,6 +759,18 @@ export function createFilePersister<Schemas extends OptionalSchemas>(
|
|
|
673
759
|
* The createCustomPersister function creates a Persister object that you can
|
|
674
760
|
* configure to persist the Store in any way you wish.
|
|
675
761
|
*
|
|
762
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
763
|
+
*
|
|
764
|
+
* ```ts override
|
|
765
|
+
* createCustomPersister(
|
|
766
|
+
* store: Store,
|
|
767
|
+
* getPersisted: () => Promise<string | null | undefined>,
|
|
768
|
+
* setPersisted: (json: string) => Promise<void>,
|
|
769
|
+
* startListeningToPersisted: (didChange: Callback) => void,
|
|
770
|
+
* stopListeningToPersisted: Callback,
|
|
771
|
+
* ): Persister;
|
|
772
|
+
* ```
|
|
773
|
+
*
|
|
676
774
|
* As well as providing a reference to the Store to persist, you must provide
|
|
677
775
|
* functions that handle how to fetch, write, and listen to, the persistence
|
|
678
776
|
* layer.
|