2018-07-04 17:51:31 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
import { OpenSlidesComponent } from 'app/openslides.component';
|
|
|
|
import { WebsocketService } from './websocket.service';
|
2018-08-24 13:05:03 +02:00
|
|
|
|
|
|
|
import { CollectionStringModelMapperService } from './collectionStringModelMapper.service';
|
2018-09-13 14:40:04 +02:00
|
|
|
import { DataStoreService } from './data-store.service';
|
2018-07-04 17:51:31 +02:00
|
|
|
|
2018-10-14 08:26:51 +02:00
|
|
|
interface AutoupdateFormat {
|
|
|
|
/**
|
|
|
|
* All changed (and created) items as their full/restricted data grouped by their collection.
|
|
|
|
*/
|
|
|
|
changed: {
|
|
|
|
[collectionString: string]: object[];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* All deleted items (by id) grouped by their collection.
|
|
|
|
*/
|
|
|
|
deleted: {
|
|
|
|
[collectionString: string]: number[];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The current change id for this autoupdate
|
|
|
|
*/
|
|
|
|
change_id: number;
|
|
|
|
}
|
|
|
|
|
2018-07-04 17:51:31 +02:00
|
|
|
/**
|
2018-07-12 14:11:31 +02:00
|
|
|
* Handles the initial update and automatic updates using the {@link WebsocketService}
|
|
|
|
* Incoming objects, usually BaseModels, will be saved in the dataStore (`this.DS`)
|
|
|
|
* This service usually creates all models
|
|
|
|
*
|
|
|
|
* The dataStore will injected over the parent class: {@link OpenSlidesComponent}.
|
2018-07-04 17:51:31 +02:00
|
|
|
*/
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
2018-07-12 14:11:31 +02:00
|
|
|
export class AutoupdateService extends OpenSlidesComponent {
|
|
|
|
/**
|
|
|
|
* Constructor to create the AutoupdateService. Calls the constructor of the parent class.
|
|
|
|
* @param websocketService
|
|
|
|
*/
|
2018-09-20 13:03:51 +02:00
|
|
|
public constructor(
|
|
|
|
websocketService: WebsocketService,
|
|
|
|
private DS: DataStoreService,
|
|
|
|
private modelMapper: CollectionStringModelMapperService
|
|
|
|
) {
|
2018-07-04 17:51:31 +02:00
|
|
|
super();
|
2018-10-14 08:26:51 +02:00
|
|
|
websocketService.getOberservable<AutoupdateFormat>('autoupdate').subscribe(response => {
|
2018-07-04 17:51:31 +02:00
|
|
|
this.storeResponse(response);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
|
|
|
* Handle the answer of incoming data via {@link WebsocketService}.
|
|
|
|
*
|
2018-08-24 13:05:03 +02:00
|
|
|
* Bundles the data per action and collection. THis speeds up the caching in the DataStore.
|
|
|
|
*
|
2018-07-12 14:11:31 +02:00
|
|
|
* Detects the Class of an incomming model, creates a new empty object and assigns
|
|
|
|
* the data to it using the deserialize function.
|
|
|
|
*
|
|
|
|
* Saves models in DataStore.
|
|
|
|
*/
|
2018-10-14 08:26:51 +02:00
|
|
|
public storeResponse(autoupdate: AutoupdateFormat): void {
|
2018-08-24 13:05:03 +02:00
|
|
|
// Delete the removed objects from the DataStore
|
|
|
|
Object.keys(autoupdate.deleted).forEach(collection => {
|
2018-10-14 08:26:51 +02:00
|
|
|
this.DS.remove(collection, autoupdate.deleted[collection], autoupdate.change_id);
|
2018-08-24 13:05:03 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Add the objects to the DataStore.
|
|
|
|
Object.keys(autoupdate.changed).forEach(collection => {
|
2018-09-20 13:03:51 +02:00
|
|
|
const targetClass = this.modelMapper.getModelConstructor(collection);
|
2018-08-24 13:05:03 +02:00
|
|
|
if (!targetClass) {
|
2018-10-14 08:26:51 +02:00
|
|
|
throw new Error(`Unregistered resource ${collection}`);
|
2018-08-24 13:05:03 +02:00
|
|
|
}
|
2018-10-14 08:26:51 +02:00
|
|
|
this.DS.add(autoupdate.changed[collection].map(model => new targetClass(model)), autoupdate.change_id);
|
2018-08-24 13:05:03 +02:00
|
|
|
});
|
2018-07-12 14:11:31 +02:00
|
|
|
}
|
2018-08-28 11:07:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a WebSocket request to the Server with the maxChangeId of the DataStore.
|
|
|
|
* The server should return an autoupdate with all new data.
|
|
|
|
*
|
|
|
|
* TODO: Wait for changeIds to be implemented on the server.
|
|
|
|
*/
|
2018-09-07 13:12:59 +02:00
|
|
|
public requestChanges(): void {
|
2018-08-28 11:07:10 +02:00
|
|
|
console.log('requesting changed objects');
|
|
|
|
// this.websocketService.send('changeIdRequest', this.DS.maxChangeId);
|
|
|
|
}
|
2018-07-04 17:51:31 +02:00
|
|
|
}
|