Skip to content

Commit f0ff014

Browse files
committed
Allow setting socketURL in magpie.config.js without connecting socket in experiment
1 parent 7e66472 commit f0ff014

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/Magpie.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import EventEmitter from 'events';
22
import Vue from 'vue';
3-
import Socket from './Socket';
3+
import Socket, { states } from './Socket';
44
import * as validators from '../src/validators';
55
import merge from 'lodash/merge';
66
import map from 'lodash/map';
@@ -306,7 +306,10 @@ export default class Magpie extends EventEmitter {
306306
this.currentSlideIndex = 0;
307307
this.measurements = {};
308308
this.currentVarsData = {};
309-
if (this.socketUrl) {
309+
if (
310+
this.socket.state === states.CONNECTED ||
311+
this.socket.state === states.READY
312+
) {
310313
this.socket.setCurrentScreen(this.currentScreenIndex);
311314
}
312315
// Start new trial data and restart response timer
@@ -380,8 +383,14 @@ export default class Magpie extends EventEmitter {
380383
...this.expData,
381384
experiment_end_time: Date.now(),
382385
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+
}),
385394
trials: addEmptyColumns(
386395
flatten(Object.values(this.trialData)).map((o) =>
387396
Object.assign(
@@ -428,13 +437,16 @@ export default class Magpie extends EventEmitter {
428437
}
429438

430439
async submitResults(submissionURL, data, intermediate) {
431-
if (this.socketUrl) {
440+
if (
441+
this.socket.state === states.CONNECTED ||
442+
this.socket.state === states.READY
443+
) {
432444
try {
433445
const submissionType = intermediate
434446
? 'save_intermediate_results'
435447
: 'submit_results';
436448

437-
return new Promise((resolve, reject) =>
449+
return await new Promise((resolve, reject) =>
438450
this.socket.participantChannel
439451
.push(submissionType, {
440452
results: data

0 commit comments

Comments
 (0)