Skip to content

Commit

Permalink
Fix-chart-scrolling (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
godind authored Mar 21, 2024
1 parent 3c08552 commit a9a4e0f
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions src/app/core/services/data-set.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@angular/core';
import { Subscription, Observable, sampleTime,ReplaySubject } from 'rxjs';
import { Subscription, Observable, ReplaySubject, MonoTypeOperatorFunction, interval, withLatestFrom } from 'rxjs';
import { AppSettingsService } from './app-settings.service';
import { SignalKService, pathRegistrationValue } from './signalk.service';
import { UUID } from'../../utils/uuid'
Expand Down Expand Up @@ -112,14 +112,13 @@ private setupServiceRegistry(uuid: string): void {
* Starts the recording process of a Data Source. It firsts reads the _historicalDataset configuration,
* then starts building the _historicalDataset values, and pushes them to the Subject.
*
* This method handles the process that takes SK data and feed the Subject. _historicalDataset "clients",
* ie. widgets, will use the getDatasetObservable() method to receive data from the Subject.
* This method handles the process that takes SK data and feeds the Subject. Clients/Observers,
* (widgets mostly), will use the getDatasetObservable() method to receive data from the Subject.
*
* Concept: SK_path_values -> datasource -> (ReplaySubject) <- Widget observers
*
* Once a datasource is started, ReplaySubject subscribers
* (widgets) will receive _historicalDataset data updates.
* .
* Once a datasource is started, subscribers will receive historical data (equal to the
* length of the dataset)pushed to the Subject, as as future data.
*
* @private
* @param {string} uuid The UUID of the DataSource to start
Expand All @@ -135,27 +134,34 @@ private setupServiceRegistry(uuid: string): void {

// Get _historicalDataset data setup
this.setDatasetConfigurationOptions(configuration);
let dataSource: IDatasetServiceDataSource = null;

// Cleanup existing _historicalDataset if present.
const dsIndex = this._svcDataSource. findIndex(dataSub => dataSub.uuid == uuid);
// Check if dataSource is already present
const dsIndex = this._svcDataSource.findIndex(dataSub => dataSub.uuid == uuid);
if (dsIndex >= 0) {
this.stop(uuid);
dataSource = this._svcDataSource[dsIndex];
} else {
// Add a new DataSource
dataSource = this._svcDataSource[
this._svcDataSource.push({
uuid: uuid,
_pathObserverSubscription: null,
_historicalDataset: []
}) - 1
];
}

// Add a fresh _historicalDataset
const dataSource: IDatasetServiceDataSource = this._svcDataSource[
this._svcDataSource.push({
uuid: uuid,
_pathObserverSubscription: null,
_historicalDataset: []
}) - 1
];

console.log(`[Dataset Service] Starting Dataset recording process: ${configuration.uuid}`);
console.log(`[Dataset Service] Path: ${configuration.path}, Scale: ${configuration.timeScaleFormat}, Datapoints: ${configuration.maxDataPoints}, Period: ${configuration.period}`);

// Emit at a regular interval using the last value. We use this and not sampleTime() to make sure that if there is no new data, we still send the last know value. This is to prevent dataset blanks that look ugly on the chart
function sampleInterval<pathRegistrationValue>(period: number): MonoTypeOperatorFunction<pathRegistrationValue> {
return (source) => interval(period).pipe(withLatestFrom(source, (_, value) => value));
};


// Subscribe to path data and update _historicalDataset upon reception
dataSource._pathObserverSubscription = this.signalk.subscribePath(configuration.uuid, configuration.path, configuration.pathSource).pipe(sampleTime(configuration.sampleTime)).subscribe(
dataSource._pathObserverSubscription = this.signalk.subscribePath(configuration.uuid, configuration.path, configuration.pathSource).pipe(sampleInterval(configuration.sampleTime)).subscribe(
(newValue: pathRegistrationValue) => {
if (newValue.value === null) return; // we don't need null values

Expand Down Expand Up @@ -186,6 +192,7 @@ private setupServiceRegistry(uuid: string): void {
const dataSource = this._svcDataSource.find(d => d.uuid == uuid);
console.log(`[Dataset Service] Stopping Dataset ${uuid} data capture`);
dataSource._pathObserverSubscription.unsubscribe();
dataSource._historicalDataset = [];
}

/**
Expand Down

0 comments on commit a9a4e0f

Please sign in to comment.