|
1 | 1 | import EventEmitter from 'events'; |
2 | 2 | import Vue from 'vue'; |
3 | | -import Socket from './Socket'; |
| 3 | +import Socket, { states } from './Socket'; |
4 | 4 | import * as validators from '../src/validators'; |
5 | 5 | import merge from 'lodash/merge'; |
6 | 6 | import map from 'lodash/map'; |
@@ -306,7 +306,10 @@ export default class Magpie extends EventEmitter { |
306 | 306 | this.currentSlideIndex = 0; |
307 | 307 | this.measurements = {}; |
308 | 308 | this.currentVarsData = {}; |
309 | | - if (this.socketUrl) { |
| 309 | + if ( |
| 310 | + this.socket.state === states.CONNECTED || |
| 311 | + this.socket.state === states.READY |
| 312 | + ) { |
310 | 313 | this.socket.setCurrentScreen(this.currentScreenIndex); |
311 | 314 | } |
312 | 315 | // Start new trial data and restart response timer |
@@ -380,8 +383,14 @@ export default class Magpie extends EventEmitter { |
380 | 383 | ...this.expData, |
381 | 384 | experiment_end_time: Date.now(), |
382 | 385 | experiment_duration: Date.now() - this.expData.experiment_start_time, |
383 | | - ...(this.socketUrl && { participantId: this.socket.participantId }), |
384 | | - ...(this.socketUrl && { groupLabel: this.socket.groupLabel }), |
| 386 | + ...((this.socket.state === states.CONNECTED || |
| 387 | + this.socket.state === states.READY) && { |
| 388 | + participantId: this.socket.participantId |
| 389 | + }), |
| 390 | + ...((this.socket.state === states.CONNECTED || |
| 391 | + this.socket.state === states.READY) && { |
| 392 | + groupLabel: this.socket.groupLabel |
| 393 | + }), |
385 | 394 | trials: addEmptyColumns( |
386 | 395 | flatten(Object.values(this.trialData)).map((o) => |
387 | 396 | Object.assign( |
@@ -428,13 +437,16 @@ export default class Magpie extends EventEmitter { |
428 | 437 | } |
429 | 438 |
|
430 | 439 | async submitResults(submissionURL, data, intermediate) { |
431 | | - if (this.socketUrl) { |
| 440 | + if ( |
| 441 | + this.socket.state === states.CONNECTED || |
| 442 | + this.socket.state === states.READY |
| 443 | + ) { |
432 | 444 | try { |
433 | 445 | const submissionType = intermediate |
434 | 446 | ? 'save_intermediate_results' |
435 | 447 | : 'submit_results'; |
436 | 448 |
|
437 | | - return new Promise((resolve, reject) => |
| 449 | + return await new Promise((resolve, reject) => |
438 | 450 | this.socket.participantChannel |
439 | 451 | .push(submissionType, { |
440 | 452 | results: data |
|
0 commit comments