@tallyui/database 3.17.0 → 3.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +211 -2
- package/dist/index.js +416 -14
- package/package.json +2 -2
- package/src/index.ts +21 -0
- package/src/replication.ts +7 -1
- package/src/request-metrics-store.ts +122 -0
- package/src/request-metrics.ts +180 -0
- package/src/sync-cadence-runner.ts +93 -0
- package/src/sync-cadence-store.ts +91 -0
- package/src/sync-cadence.ts +79 -0
package/dist/index.d.ts
CHANGED
|
@@ -208,6 +208,11 @@ interface StartReplicationOptions<RxDocType, CheckpointType = any> {
|
|
|
208
208
|
* pull asks the store again and a held push goes. `resume()` keeps the latch, and the reset keeps a sign-in hold.
|
|
209
209
|
*/
|
|
210
210
|
connectivity?: ConnectivityTracker;
|
|
211
|
+
/**
|
|
212
|
+
* The pull page size, overriding adapter.pull.batchSize (for example the sync cadence profile's pullBatchSize).
|
|
213
|
+
* RxDB fixes it when the replication starts.
|
|
214
|
+
*/
|
|
215
|
+
pullBatchSize?: number;
|
|
211
216
|
}
|
|
212
217
|
/** RxDB's replication state plus the pull notice and a way to restart after a till error. */
|
|
213
218
|
type TallyReplicationState<RxDocType, CheckpointType = any> = RxReplicationState<RxDocType, CheckpointType> & {
|
|
@@ -222,7 +227,7 @@ type TallyReplicationState<RxDocType, CheckpointType = any> = RxReplicationState
|
|
|
222
227
|
* Returns the RxReplicationState which exposes observables for monitoring
|
|
223
228
|
* and methods like cancel(), awaitInSync(), reSync().
|
|
224
229
|
*/
|
|
225
|
-
declare function startReplication<RxDocType, CheckpointType = any>({ collection, adapter, context, live, retryTime, autoStart, maxRetryTimeMs, connectivity, }: StartReplicationOptions<RxDocType, CheckpointType>): TallyReplicationState<RxDocType, CheckpointType>;
|
|
230
|
+
declare function startReplication<RxDocType, CheckpointType = any>({ collection, adapter, context, live, retryTime, autoStart, maxRetryTimeMs, connectivity, pullBatchSize, }: StartReplicationOptions<RxDocType, CheckpointType>): TallyReplicationState<RxDocType, CheckpointType>;
|
|
226
231
|
|
|
227
232
|
/**
|
|
228
233
|
* ADR-061: exactly one live tab per store, and a second tab never opens
|
|
@@ -563,4 +568,208 @@ declare function startFingerprintReconcile<Doc>({ adapter, reSync, startDelayMs,
|
|
|
563
568
|
state$: Observable<FingerprintReconcileState>;
|
|
564
569
|
};
|
|
565
570
|
|
|
566
|
-
|
|
571
|
+
/** How often the till checks the store for changes, and how many records each check's page may carry. */
|
|
572
|
+
interface SyncCadence {
|
|
573
|
+
checkIntervalMs: number;
|
|
574
|
+
pullBatchSize: number;
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* The presets, macros over the two dials.
|
|
578
|
+
*
|
|
579
|
+
* Two independent budget lines: `checkIntervalMs` prices how often we ask, `pullBatchSize` how heavy each request
|
|
580
|
+
* is. Page weight is what hurts shared hosting, and the check interval only multiplies it, so each tier moves both
|
|
581
|
+
* lines, and no preset ships the 100-record maximum: that stays reachable only through Custom.
|
|
582
|
+
*
|
|
583
|
+
* Eco 5 min · 25 — 12 checks/hour, lightest page
|
|
584
|
+
* Balanced 60 s · 50 — 60 checks/hour
|
|
585
|
+
* Realtime 10 s · 75 — 360 checks/hour, fast but not max-weight pages
|
|
586
|
+
*
|
|
587
|
+
* The numbers are in one table so they are easy to re-tune.
|
|
588
|
+
*/
|
|
589
|
+
declare const SYNC_CADENCE_PRESETS: {
|
|
590
|
+
readonly eco: {
|
|
591
|
+
readonly checkIntervalMs: 300000;
|
|
592
|
+
readonly pullBatchSize: 25;
|
|
593
|
+
};
|
|
594
|
+
readonly balanced: {
|
|
595
|
+
readonly checkIntervalMs: 60000;
|
|
596
|
+
readonly pullBatchSize: 50;
|
|
597
|
+
};
|
|
598
|
+
readonly realtime: {
|
|
599
|
+
readonly checkIntervalMs: 10000;
|
|
600
|
+
readonly pullBatchSize: 75;
|
|
601
|
+
};
|
|
602
|
+
};
|
|
603
|
+
type SyncCadencePreset = keyof typeof SYNC_CADENCE_PRESETS;
|
|
604
|
+
type SyncCadenceChoice = SyncCadencePreset | 'custom';
|
|
605
|
+
/** The shipped default: what a fresh till gets, and what "Reset" restores. */
|
|
606
|
+
declare const DEFAULT_SYNC_CADENCE_PRESET: SyncCadencePreset;
|
|
607
|
+
/** v2 cadence-controller.ts reconfigure's clamp bounds. */
|
|
608
|
+
declare const SYNC_CADENCE_LIMITS: {
|
|
609
|
+
readonly checkIntervalMs: {
|
|
610
|
+
readonly min: 5000;
|
|
611
|
+
readonly max: 300000;
|
|
612
|
+
};
|
|
613
|
+
readonly pullBatchSize: {
|
|
614
|
+
readonly min: 10;
|
|
615
|
+
readonly max: 100;
|
|
616
|
+
};
|
|
617
|
+
};
|
|
618
|
+
declare function isSyncCadencePreset(value: unknown): value is SyncCadencePreset;
|
|
619
|
+
/**
|
|
620
|
+
* Math.trunc, then clamp to SYNC_CADENCE_LIMITS. A missing field takes Balanced's number. Throws TypeError on a
|
|
621
|
+
* non-finite number, as v2's reconfigure does.
|
|
622
|
+
*/
|
|
623
|
+
declare function clampSyncCadence(value: Partial<SyncCadence>): SyncCadence;
|
|
624
|
+
/** v2's presetFor: an exact match on both numbers gives the preset's name, anything else gives 'custom'. */
|
|
625
|
+
declare function syncCadencePresetFor(value: SyncCadence): SyncCadenceChoice;
|
|
626
|
+
/** Both budget lines in the preset's own numbers, derived so copy can never drift from the table. */
|
|
627
|
+
declare function syncCadencePresetBudget(name: SyncCadencePreset): {
|
|
628
|
+
intervalSeconds: number;
|
|
629
|
+
records: number;
|
|
630
|
+
};
|
|
631
|
+
/** Checks per hour at this interval, rounded to a whole number. */
|
|
632
|
+
declare function requestsPerHour(checkIntervalMs: number): number;
|
|
633
|
+
|
|
634
|
+
/** The chosen sync cadence of this device. The host adds the collection as `sync_cadence`, as with `printer_profiles`. */
|
|
635
|
+
declare const SYNC_CADENCE_COLLECTION = "sync_cadence";
|
|
636
|
+
/** One document per database: a TallyUI database belongs to one store session. */
|
|
637
|
+
declare const SYNC_CADENCE_DOC_ID = "default";
|
|
638
|
+
interface SyncCadenceDoc {
|
|
639
|
+
id: string;
|
|
640
|
+
/** A named preset is stored by name; its numbers always come from SYNC_CADENCE_PRESETS. */
|
|
641
|
+
preset: SyncCadenceChoice;
|
|
642
|
+
/** Only when preset is 'custom'. */
|
|
643
|
+
checkIntervalMs?: number;
|
|
644
|
+
pullBatchSize?: number;
|
|
645
|
+
/** ISO 8601 instant of the last set. */
|
|
646
|
+
updatedAt: string;
|
|
647
|
+
}
|
|
648
|
+
declare const syncCadenceSchema: RxJsonSchema<SyncCadenceDoc>;
|
|
649
|
+
declare function syncCadenceCollection(): {
|
|
650
|
+
readonly schema: RxJsonSchema<SyncCadenceDoc>;
|
|
651
|
+
};
|
|
652
|
+
type SyncCadenceCollection = RxCollection<SyncCadenceDoc>;
|
|
653
|
+
declare function addSyncCadenceCollection(db: RxDatabase): Promise<SyncCadenceCollection>;
|
|
654
|
+
interface SyncCadenceProfile extends SyncCadence {
|
|
655
|
+
preset: SyncCadenceChoice;
|
|
656
|
+
}
|
|
657
|
+
declare function readSyncCadence(collection: SyncCadenceCollection): Promise<SyncCadenceProfile>;
|
|
658
|
+
declare function syncCadence$(collection: SyncCadenceCollection): Observable<SyncCadenceProfile>;
|
|
659
|
+
declare function setSyncCadence(collection: SyncCadenceCollection, value: SyncCadencePreset | SyncCadence, options?: {
|
|
660
|
+
now?: () => number;
|
|
661
|
+
}): Promise<SyncCadenceProfile>;
|
|
662
|
+
declare function resetSyncCadence(collection: SyncCadenceCollection, options?: {
|
|
663
|
+
now?: () => number;
|
|
664
|
+
}): Promise<SyncCadenceProfile>;
|
|
665
|
+
|
|
666
|
+
/** What a tick asks to check again. RxDB's replication state (and TallyReplicationState) fits as is. */
|
|
667
|
+
interface SyncCadenceTarget {
|
|
668
|
+
reSync(): void;
|
|
669
|
+
isPaused?(): boolean;
|
|
670
|
+
}
|
|
671
|
+
interface StartSyncCadenceOptions {
|
|
672
|
+
targets: readonly SyncCadenceTarget[];
|
|
673
|
+
/** Usually syncCadence$(collection). No timer runs until its first value. */
|
|
674
|
+
cadence$: Observable<SyncCadence>;
|
|
675
|
+
connectivity?: ConnectivityTracker;
|
|
676
|
+
/** Injectable for tests; default Math.random. */
|
|
677
|
+
random?: () => number;
|
|
678
|
+
/** Injectable for tests; default Date.now. Only used for nextDueAtMs$. */
|
|
679
|
+
now?: () => number;
|
|
680
|
+
}
|
|
681
|
+
interface SyncCadenceRunner {
|
|
682
|
+
/** When the next tick is due (the `now()` clock), or undefined before the first cadence value and after stop. */
|
|
683
|
+
readonly nextDueAtMs$: Observable<number | undefined>;
|
|
684
|
+
/** Tick now ("Check now"), then re-arm from now. A no-op after stop. */
|
|
685
|
+
tickNow(): void;
|
|
686
|
+
/** Clears the timer, unsubscribes from cadence$, and emits undefined then completes nextDueAtMs$. Idempotent. */
|
|
687
|
+
stop(): void;
|
|
688
|
+
}
|
|
689
|
+
/** v2's ±20 % jitter (sync-engine change-signal/tick-cadence.ts). Exported for tests and for a "next check in" line. */
|
|
690
|
+
declare function jitteredIntervalMs(intervalMs: number, random: () => number): number;
|
|
691
|
+
/**
|
|
692
|
+
* Opt-in polling beside startReplication: on each jittered tick, `reSync()` on every target that is not paused. A new
|
|
693
|
+
* cadence value re-arms the timer from now, as v2's reconfigure does. A tick is skipped while connectivity's
|
|
694
|
+
* update-required latch is set, never for offline or degraded: the replication's own backoff and holds decide those.
|
|
695
|
+
*/
|
|
696
|
+
declare function startSyncCadence({ targets, cadence$, connectivity, random, now, }: StartSyncCadenceOptions): SyncCadenceRunner;
|
|
697
|
+
|
|
698
|
+
declare const REQUEST_METRICS_HOUR_MS = 3600000;
|
|
699
|
+
/** Hourly buckets kept: the newest 48 that exist, by count not age (an idle hour makes no bucket). v2 metrics.ts:4. */
|
|
700
|
+
declare const REQUEST_METRICS_MAX_BUCKETS = 48;
|
|
701
|
+
interface RequestMetricsBucket {
|
|
702
|
+
hourStartMs: number;
|
|
703
|
+
requests: number;
|
|
704
|
+
bytes: number;
|
|
705
|
+
durationTotalMs: number;
|
|
706
|
+
durationCount: number;
|
|
707
|
+
errors: number;
|
|
708
|
+
loadLast?: number;
|
|
709
|
+
loadMax?: number;
|
|
710
|
+
}
|
|
711
|
+
/**
|
|
712
|
+
* One completed request attempt.
|
|
713
|
+
*
|
|
714
|
+
* `errors` counts failed request ATTEMPTS, not records: one queued record can contribute several and then leave the
|
|
715
|
+
* queue acknowledged, so reconciling it against the sync backlog is a category error. `failed` is the settled
|
|
716
|
+
* verdict, not the raw HTTP status, and a request we cancelled is not a failure.
|
|
717
|
+
*/
|
|
718
|
+
interface RequestAttempt {
|
|
719
|
+
atMs: number;
|
|
720
|
+
durationMs: number;
|
|
721
|
+
bytes: number;
|
|
722
|
+
failed: boolean;
|
|
723
|
+
}
|
|
724
|
+
interface RequestMetrics {
|
|
725
|
+
record(attempt: RequestAttempt): void;
|
|
726
|
+
/** Ignored unless finite. Lands in the bucket of `atMs` (default now()). */
|
|
727
|
+
recordServerLoad(load1: number, atMs?: number): void;
|
|
728
|
+
/** Sorted by hour, ascending; copies. */
|
|
729
|
+
buckets(): RequestMetricsBucket[];
|
|
730
|
+
/** Emits buckets() now and after every change. */
|
|
731
|
+
readonly buckets$: Observable<RequestMetricsBucket[]>;
|
|
732
|
+
/** Folds persisted buckets into the live ones (v2 hydrateMetricsBuckets); invalid entries skipped. */
|
|
733
|
+
hydrate(persisted: unknown): void;
|
|
734
|
+
}
|
|
735
|
+
declare function isRequestMetricsBucket(value: unknown): value is RequestMetricsBucket;
|
|
736
|
+
declare function createRequestMetrics(options?: {
|
|
737
|
+
now?: () => number;
|
|
738
|
+
}): RequestMetrics;
|
|
739
|
+
interface MeteredFetchOptions {
|
|
740
|
+
/** Default: !response.ok && response.status !== 304. */
|
|
741
|
+
isFailure?: (response: Response) => boolean;
|
|
742
|
+
/** Read this header as a server-load reading (WCPOS: 'X-Server-Load'). Default: not read. */
|
|
743
|
+
serverLoadHeader?: string;
|
|
744
|
+
now?: () => number;
|
|
745
|
+
}
|
|
746
|
+
/** Wraps `fetch` so each request lands in `metrics`. The response is returned as is, unread. */
|
|
747
|
+
declare function meteredFetch(fetch: typeof globalThis.fetch, metrics: RequestMetrics, options?: MeteredFetchOptions): typeof globalThis.fetch;
|
|
748
|
+
|
|
749
|
+
/** The request metrics of this device. The host adds the collection as `request_metrics`, as with `sync_cadence`. */
|
|
750
|
+
declare const REQUEST_METRICS_COLLECTION = "request_metrics";
|
|
751
|
+
/** v2 METRICS_PERSIST_INTERVAL_MS. */
|
|
752
|
+
declare const REQUEST_METRICS_PERSIST_INTERVAL_MS = 300000;
|
|
753
|
+
interface RequestMetricsDoc extends RequestMetricsBucket {
|
|
754
|
+
/** String(hourStartMs): the primary key. */
|
|
755
|
+
hour: string;
|
|
756
|
+
}
|
|
757
|
+
declare const requestMetricsSchema: RxJsonSchema<RequestMetricsDoc>;
|
|
758
|
+
declare function requestMetricsCollection(): {
|
|
759
|
+
readonly schema: RxJsonSchema<RequestMetricsDoc>;
|
|
760
|
+
};
|
|
761
|
+
type RequestMetricsCollection = RxCollection<RequestMetricsDoc>;
|
|
762
|
+
declare function addRequestMetricsCollection(db: RxDatabase): Promise<RequestMetricsCollection>;
|
|
763
|
+
interface RequestMetricsPersistence {
|
|
764
|
+
/** Resolves once the stored rows have been hydrated into the counter. */
|
|
765
|
+
readonly ready: Promise<void>;
|
|
766
|
+
/** Write the changed hours now, and delete rows beyond the newest REQUEST_METRICS_MAX_BUCKETS. */
|
|
767
|
+
flush(): Promise<void>;
|
|
768
|
+
/** Stop the timer, then flush once. Idempotent; later flushes are no-ops. */
|
|
769
|
+
stop(): Promise<void>;
|
|
770
|
+
}
|
|
771
|
+
declare function persistRequestMetrics(metrics: RequestMetrics, collection: RequestMetricsCollection, options?: {
|
|
772
|
+
everyMs?: number;
|
|
773
|
+
}): RequestMetricsPersistence;
|
|
774
|
+
|
|
775
|
+
export { CATALOGUE_OFFLINE_GAP_MS, type CatalogueReconcileEvent, type CatalogueReconcileState, type CatalogueReconcileSummary, type ConnectivityEvent, type ConnectivitySnapshot, type ConnectivityTracker, type CreateDatabaseOptions, DEFAULT_SYNC_CADENCE_PRESET, FAILURE_CONFIRMATION_MS, FAILURE_CONFIRMATION_TOLERANCE_MS, type FingerprintReconcileResult, type FingerprintReconcileState, INITIAL_CONNECTIVITY, type IdReconcileResult, type LiveTabHandle, type LiveTabOptions, type LiveTabState, type MeteredFetchOptions, REQUEST_METRICS_COLLECTION, REQUEST_METRICS_HOUR_MS, REQUEST_METRICS_MAX_BUCKETS, REQUEST_METRICS_PERSIST_INTERVAL_MS, REQUIRED_MULTI_INSTANCE_BY_ENGINE, type RequestAttempt, type RequestMetrics, type RequestMetricsBucket, type RequestMetricsCollection, type RequestMetricsDoc, type RequestMetricsPersistence, STOCK_LEVELS_COLLECTION, STORAGE_READ_WATCHDOG_MS, STORAGE_WRITE_STALL_MS, SYNC_CADENCE_COLLECTION, SYNC_CADENCE_DOC_ID, SYNC_CADENCE_LIMITS, SYNC_CADENCE_PRESETS, type StartCatalogueReconcileOptions, type StartFingerprintReconcileOptions, type StartIdReconcileOptions, type StartReplicationOptions, type StartStockReconcileOptions, type StartSyncCadenceOptions, type StockLevelRow, type StockReconcileResult, type StockReconcileState, type StorageHealth, type StorageWatchdogOptions, type SyncCadence, type SyncCadenceChoice, type SyncCadenceCollection, type SyncCadenceDoc, type SyncCadencePreset, type SyncCadenceProfile, type SyncCadenceRunner, type SyncCadenceTarget, type TallyDatabase, type TallyReplicationState, WEB_STORAGE_ENGINE, addRequestMetricsCollection, addSyncCadenceCollection, clampSyncCadence, connectorCollection, createConnectivityTracker, createRequestMetrics, createTallyDatabase, getStorage, getStorageHealth, isFingerprintResultCurrent, isRequestMetricsBucket, isStorageWorkerFailure, isSyncCadencePreset, jitteredIntervalMs, meteredFetch, persistRequestMetrics, readSyncCadence, reduceConnectivity, requestMetricsCollection, requestMetricsSchema, requestsPerHour, resetSyncCadence, setSyncCadence, shouldReconcileAfterGap, startCatalogueReconcile, startFingerprintReconcile, startIdReconcile, startLiveTab, startReplication, startStockReconcile, startSyncCadence, stockLevelsCollection, stockLevelsSchema, syncCadence$, syncCadenceCollection, syncCadencePresetBudget, syncCadencePresetFor, syncCadenceSchema, withStorageWatchdog };
|