Skip to content
Snippets Groups Projects
Commit 72df3d1f authored by Martin Mechtel's avatar Martin Mechtel
Browse files

try to make unit data saving safer

parent 425c9b5d
No related branches found
No related tags found
No related merge requests found
...@@ -439,3 +439,7 @@ export class UnitDef { ...@@ -439,3 +439,7 @@ export class UnitDef {
} }
} }
export interface UnitMsgData {
unitName: string;
msg: string;
}
import { Authorisation } from './../../logindata.service'; import { Authorisation } from './../../logindata.service';
import { debounceTime, bufferTime, switchMap } from 'rxjs/operators'; import { debounceTime, bufferTime, switchMap } from 'rxjs/operators';
import { UnitDef, TestControllerService } from './../test-controller.service'; import { UnitDef, TestControllerService, UnitMsgData } from './../test-controller.service';
import { Subscriber, Subscription, BehaviorSubject, Observable, of } from 'rxjs'; import { Subscriber, Subscription, BehaviorSubject, Observable, of } from 'rxjs';
import { BackendService } from './../backend.service'; import { BackendService } from './../backend.service';
import { ServerError } from './../../backend.service'; import { ServerError } from './../../backend.service';
...@@ -38,9 +38,9 @@ export class UnithostComponent implements OnInit, OnDestroy { ...@@ -38,9 +38,9 @@ export class UnithostComponent implements OnInit, OnDestroy {
private pendingRestorePoint$ = new BehaviorSubject<string>(''); private pendingRestorePoint$ = new BehaviorSubject<string>('');
// changed by itemplayer via postMessage, observed here to save (see below) // changed by itemplayer via postMessage, observed here to save (see below)
public restorePoint$ = new BehaviorSubject<string>(''); public restorePoint$ = new BehaviorSubject<UnitMsgData>(null);
public response$ = new BehaviorSubject<string>(''); public response$ = new BehaviorSubject<UnitMsgData>(null);
public log$ = new BehaviorSubject<string>(''); public log$ = new BehaviorSubject<UnitMsgData>(null);
// buffering restorePoints // buffering restorePoints
private lastBookletState = ''; private lastBookletState = '';
...@@ -73,13 +73,13 @@ export class UnithostComponent implements OnInit, OnDestroy { ...@@ -73,13 +73,13 @@ export class UnithostComponent implements OnInit, OnDestroy {
} }
if (hasData) { if (hasData) {
this.log$.next('ready');
const pendingRespp = this.pendingRestorePoint$.getValue(); const pendingRespp = this.pendingRestorePoint$.getValue();
if (pendingRespp.length > 0) { if (pendingRespp.length > 0) {
this.pendingRestorePoint$.next(''); this.pendingRestorePoint$.next('');
} }
this.itemplayerSessionId = Math.floor(Math.random() * 20000000 + 10000000).toString(); this.itemplayerSessionId = Math.floor(Math.random() * 20000000 + 10000000).toString();
this.log$.next({'unitName': this.myUnitName, 'msg': 'start'});
this.postMessageTarget = m.source; this.postMessageTarget = m.source;
this.postMessageTarget.postMessage({ this.postMessageTarget.postMessage({
type: 'OpenCBA.ToItemPlayer.DataTransfer', type: 'OpenCBA.ToItemPlayer.DataTransfer',
...@@ -127,14 +127,13 @@ export class UnithostComponent implements OnInit, OnDestroy { ...@@ -127,14 +127,13 @@ export class UnithostComponent implements OnInit, OnDestroy {
this.tcs.itemplayerCurrentPage$.next(currentPageChanged); this.tcs.itemplayerCurrentPage$.next(currentPageChanged);
} }
const restorePoint = msgData['restorePoint']; const restorePoint = msgData['restorePoint'] as string;
if (restorePoint !== undefined) { if (restorePoint !== undefined) {
this.restorePoint$.next(restorePoint); this.restorePoint$.next({'unitName': this.myUnitName, 'msg': restorePoint});
} }
const response = msgData['response']; const response = msgData['response'] as string;
if (response !== undefined) { if (response !== undefined) {
console.log('got resp ' + response); this.response$.next({'unitName': this.myUnitName, 'msg': response});
this.response$.next(response);
} }
const canLeaveChanged = msgData['canLeave']; const canLeaveChanged = msgData['canLeave'];
if (canLeaveChanged !== undefined) { if (canLeaveChanged !== undefined) {
...@@ -162,12 +161,12 @@ export class UnithostComponent implements OnInit, OnDestroy { ...@@ -162,12 +161,12 @@ export class UnithostComponent implements OnInit, OnDestroy {
if (b !== null) { if (b !== null) {
const u = b.getUnitAt(this.myUnitNumber); const u = b.getUnitAt(this.myUnitNumber);
if (u !== null) { if (u !== null) {
u.restorePoint = data; u.restorePoint = data.msg;
} }
} }
this.restorePoints[this.myUnitName] = data; this.restorePoints[data.unitName] = data.msg;
this.bs.setUnitRestorePoint(this.lds.authorisation$.getValue(), this.myUnitName, data) this.bs.setUnitRestorePoint(this.lds.authorisation$.getValue(), data.unitName, data.msg)
.subscribe(d => { .subscribe(d => {
if (d === false) { if (d === false) {
console.log('setUnitRestorePoint: false'); console.log('setUnitRestorePoint: false');
...@@ -179,7 +178,7 @@ export class UnithostComponent implements OnInit, OnDestroy { ...@@ -179,7 +178,7 @@ export class UnithostComponent implements OnInit, OnDestroy {
this.response$.pipe( this.response$.pipe(
debounceTime(300) debounceTime(300)
).subscribe(data => this.bs.setUnitResponses(this.lds.authorisation$.getValue(), this.myUnitName, data) ).subscribe(data => this.bs.setUnitResponses(this.lds.authorisation$.getValue(), data.unitName, data.msg)
.subscribe(d => { .subscribe(d => {
if (d === false) { if (d === false) {
console.log('setUnitResponses: false'); console.log('setUnitResponses: false');
...@@ -190,21 +189,20 @@ export class UnithostComponent implements OnInit, OnDestroy { ...@@ -190,21 +189,20 @@ export class UnithostComponent implements OnInit, OnDestroy {
this.log$.pipe( this.log$.pipe(
bufferTime(500) bufferTime(500)
).subscribe((data: string[]) => { ).subscribe((data: UnitMsgData[]) => {
const myLogs = []; const myLogs = {};
data.forEach(lg => { data.forEach(lg => {
if (lg.length > 0) { if (lg.msg.length > 0) {
myLogs.push(JSON.stringify(lg)); if (typeof myLogs[lg.unitName] === 'undefined') {
myLogs[lg.unitName] = [];
}
myLogs[lg.unitName].push(JSON.stringify(lg.msg));
} }
}); });
if (myLogs.length > 0) { for (const unitName in myLogs) {
this.bs.setUnitLog(this.lds.authorisation$.getValue(), this.myUnitName, myLogs).subscribe(d => { if (myLogs[unitName].length > 0) {
if (d === false) { this.bs.setUnitLog(this.lds.authorisation$.getValue(), unitName, myLogs[unitName]).subscribe();
console.log('setUnitLog: false'); }
} else if (d instanceof ServerError) {
console.log('setUnitLog: ServerError');
}
});
} }
}); });
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
export const environment = { export const environment = {
production: false, production: false,
testcenterUrl: 'https://itemdb2.iqb.hu-berlin.de/', testcenterUrl: 'https://www.iqb-testcenter.de/',
appName: 'IQB-Testcenter', appName: 'IQB-Testcenter',
appPublisher: 'IQB - Institut zur Qualitätsentwicklung im Bildungswesen', appPublisher: 'IQB - Institut zur Qualitätsentwicklung im Bildungswesen',
appVersion: '0 (dev)' appVersion: '0 (dev)'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment