1
0
mirror of https://github.com/psychopy/psychojs.git synced 2025-05-10 10:40:54 +00:00
psychojs/docs/core_ServerManager.js.html

1537 lines
85 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>core/ServerManager.js - PsychoJS API</title>
<meta name="keywords" content="psychopy, psychojs, api" />
<meta name="keyword" content="psychopy, psychojs, api" />
<script src="scripts/prettify/prettify.js"></script>
<script src="scripts/prettify/lang-css.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
<script src="scripts/nav.js" defer></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
<div class="navicon"></div>
</label>
<label for="nav-trigger" class="overlay"></label>
<nav >
<input type="text" id="nav-search" placeholder="Search" />
<h2><a href="index.html">Home</a></h2><h2><a href="https://psychopy.org" target="_blank" class="menu-item" id="website_link" >Project Website</a></h2><h2><a href="https://discourse.psychopy.org/" target="_blank" class="menu-item" id="forum_link" >Forum</a></h2><h2><a href="https://github.com/psychopy/psychojs" target="_blank" class="menu-item" id="github_link" >Github</a></h2><h3>Modules</h3><ul><li><a href="module-core.html">core</a></li><li><a href="module-data.html">data</a></li><li><a href="module-sound.html">sound</a></li><li><a href="module-util.html">util</a><ul class='methods'><li data-type='method'><a href="module-util.html#~_match">_match</a></li></ul></li><li></li><li><a href="module-visual.html">visual</a></li></ul><h3>Classes</h3><ul><li><a href="AudioClip.html">AudioClip</a><ul class='methods'><li data-type='method'><a href="AudioClip.html#_base64ArrayBuffer">_base64ArrayBuffer</a></li><li data-type='method'><a href="AudioClip.html#_decodeAudio">_decodeAudio</a></li><li data-type='method'><a href="AudioClip.html#_GoogleTranscribe">_GoogleTranscribe</a></li><li data-type='method'><a href="AudioClip.html#download">download</a></li><li data-type='method'><a href="AudioClip.html#getDuration">getDuration</a></li><li data-type='method'><a href="AudioClip.html#setVolume">setVolume</a></li><li data-type='method'><a href="AudioClip.html#startPlayback">startPlayback</a></li><li data-type='method'><a href="AudioClip.html#stopPlayback">stopPlayback</a></li><li data-type='method'><a href="AudioClip.html#transcribe">transcribe</a></li><li data-type='method'><a href="AudioClip.html#upload">upload</a></li></ul></li><li><a href="AudioClipPlayer.html">AudioClipPlayer</a><ul class='methods'><li data-type='method'><a href="AudioClipPlayer.html#getDuration">getDuration</a></li><li data-type='method'><a href="AudioClipPlayer.html#play">play</a></li><li data-type='method'><a href="AudioClipPlayer.html#setDuration">setDuration</a></li><li data-type='method'><a href="AudioClipPlayer.html#setLoops">setLoops</a></li><li data-type='method'><a href="AudioClipPlayer.html#setVolume">setVolume</a></li><li data-type='method'><a href="AudioClipPlayer.html#stop">stop</a></li></ul></li><li><a href="BuilderKeyResponse.html">BuilderKeyResponse</a></li><li><a href="ButtonStim.html">ButtonStim</a><ul class='methods'><li data-type='method'><a href="ButtonStim.html#_addEventListeners">_addEventListeners</a></li><li data-type='method'><a href="ButtonStim.html#_estimateBoundingBox">_estimateBoundingBox</a></li><li data-type='method'><a href="ButtonStim.html#_getAnchor">_getAnchor</a></li><li data-type='method'><a href="ButtonStim.html#_getDefaultLetterHeight">_getDefaultLetterHeight</a></li><li data-type='method'><a href="ButtonStim.html#_getTextInputOptions">_getTextInputOptions</a></li><li data-type='method'><a href="ButtonStim.html#_updateIfNeeded">_updateIfNeeded</a></li><li data-type='method'><a href="ButtonStim.html#clear">clear</a></li><li data-type='method'><a href="ButtonStim.html#getText">getText</a></li><li data-type='method'><a href="ButtonStim.html#reset">reset</a></li><li data-type='method'><a href="ButtonStim.html#setAlignment">setAlignment</a></li><li data-type='method'><a href="ButtonStim.html#setAnchor">setAnchor</a></li><li data-type='method'><a href="ButtonStim.html#setBorderColor">setBorderColor</a></li><li data-type='method'><a href="ButtonStim.html#setColor">setColor</a></li><li data-type='method'><a href="ButtonStim.html#setFillColor">setFillColor</a></li><li data-type='method'><a href="ButtonStim.html#setFitToContent">setFitToContent</a></li><li data-type='method'><a href="ButtonStim.html#setFont">setFont</a></li><li data-type='method'><a href="ButtonStim.html#setLanguageStyle">setLanguageStyle</a></li><li data-type='method'><a href="ButtonStim.html#setLetterHeight">setLetterHeight</a></li><li data-type='method'><a href="ButtonStim.html#setSize">setSize</a></li><li data-type='method'><a href="ButtonStim.html#setText">setText</a></li></ul></li><li><a href="Clock.html">Clock</a><ul class='methods'><li data-type='method'><a href="Clock.html#add">add</a></li><li data-type='method'><a href="Clock.html#getLastResetTime">getLastResetTime</a></li><li data-type='method'><a href="Clock.html#getTime">getTime</a></li><li data-type='method'><a href="Clock.html#reset">reset</a></li></ul></li><li><a href="Color.html">Color</a><ul class='methods'><li data-type='method'><a href="Color.html#toString">toString</a></li></ul></li><li><a href="CountdownTimer.html">CountdownTimer</a><ul class='methods'><li data-type='method'><a href="CountdownTimer.html#add">add</a></li><li data-type='method'><a href="CountdownTimer.html#getLastResetTime">getLastResetTime</a></li><li data-type='method'><a href="CountdownTimer.html#getTime">getTime</a></li><li data-type='method'><a href="CountdownTimer.html#reset">reset</a></li></ul></li><li><a href="EventEmitter.html">EventEmitter</a><ul class='methods'><li data-type='method'><a href="EventEmitter.html#emit">emit</a></li><li data-type='method'><a href="EventEmitter.html#off">off</a></li><li data-type='method'><a href="EventEmitter.html#on">on</a></li><li data-type='method'><a href="EventEmitter.html#once">once</a></li></ul></li><li><a href="EventManager.html">EventManager</a><ul class='methods'><li data-type='method'><a href="EventManager.html#_addKeyListeners">_addKeyListeners</a></li><li data-type='method'><a href="EventManager.html#addMouseListeners">addMouseListeners</a></li><li data-type='method'><a href="EventManager.html#clearEvents">clearEvents</a></li><li data-type='method'><a href="EventManager.html#clearKeys">clearKeys</a></li><li data-type='method'><a href="EventManager.html#getKeys">getKeys</a></li><li data-type='method'><a href="EventManager.html#getMouseInfo">getMouseInfo</a></li><li data-type='method'><a href="EventManager.html#resetMoveClock">resetMoveClock</a></li><li data-type='method'><a href="EventManager.html#startMoveClock">startMoveClock</a></li><li data-type='method'><a href="EventManager.html#stopMoveClock">stopMoveClock</a></li></ul></li><li><a href="ExperimentHandler.html">ExperimentHandler</a><ul class='methods'><li data-type='method'><a href="ExperimentHandler.html#addData">addData</a></li><li data-type='method'><a href="ExperimentHandler.html#addLoop">addLoop</a></li><li data-type='method'><a href="ExperimentHandler.html#isEntryEmpty">isEntryEmpty</a></li><li data-type='method'><a href="ExperimentHandler.html#nextEntry">nextEntry</a></li><li data-type='method'><a href="ExperimentHandler.html#removeLoop">removeLoop</a></li><li data-type='method'><a href="ExperimentHandler.html#save">save</a></li></ul></li><li><a href="FaceDetector.html">FaceDetector</a><ul class='methods'><li data-type='method'><a href="FaceDetector.html#_estimateBoundingBox">_estimateBoundingBox</a></li><li data-type='method'><a href="FaceDetector.html#_initFaceApi">_initFaceApi</a></li><li data-type='method'><a href="FaceDetector.html#_updateIfNeeded">_updateIfNeeded</a></li><li data-type='method'><a href="FaceDetector.html#isReady">isReady</a></li><li data-type='method'><a href="FaceDetector.html#setInput">setInput</a></li><li data-type='method'><a href="FaceDetector.html#start">start</a></li><li data-type='method'><a href="FaceDetector.html#stop">stop</a></li></ul></li><li><a href="Form.html">Form</a><ul class='methods'><li data-type='method'><a href="Form.html#_estimateBoundingBox">_estimateBoundingBox</a></li><li data-type='method'><a href="Form.html#_getBoundingBox_px">_getBoundingBox_px</a></li><li data-type='method'><a href="Form.html#_importItems">_importItems</a></li><li data-type='method'><a href="Form.html#_onChange">_onChange</a></li><li data-type='method'><a href="Form.html#_processItems">_processItems</a></li><li data-type='method'><a href="Form.html#_sanitizeItems">_sanitizeItems</a></li><li data-type='method'><a href="Form.html#_setupStimuli">_setupStimuli</a></li><li data-type='method'><a href="Form.html#_updateDecorations">_updateDecorations</a></li><li data-type='method'><a href="Form.html#_updateIfNeeded">_updateIfNeeded</a></li><li data-type='method'><a href="Form.html#_updateVisibleStimuli">_updateVisibleStimuli</a></li><li data-type='method'><a href="Form.html#addDataToExp">addDataToExp</a></li><li data-type='method'><a href="Form.html#contains">contains</a></li><li data-type='method'><a href="Form.html#draw">draw</a></li><li data-type='method'><a href="Form.html#formComplete">formComplete</a></li><li data-type='method'><a href="Form.html#getData">getData</a></li><li data-type='method'><a href="Form.html#hide">hide</a></li><li data-type='method'><a href="Form.html#refresh">refresh</a></li><li data-type='method'><a href="Form.html#release">release</a></li><li data-type='method'><a href="Form.html#reset">reset</a></li><li data-type='method'><a href="Form.html#setAutoDraw">setAutoDraw</a></li><li data-type='method'><a href="Form.html#setDepth">setDepth</a></li><li data-type='method'><a href="Form.html#setOri">setOri</a></li><li data-type='method'><a href="Form.html#setPos">setPos</a></li><li data-type='method'><a href="Form.html#setSize">setSize</a></li></ul></li><li><a href="GratingStim.html">GratingStim</a><ul class='methods'><li data-type='method'><a href="GratingStim.html#_estimateBoundingBox">_estimateBoundingBox</a></li><li data-type='method'><a href="GratingStim.html#_getDisplaySize">_getDisplaySize</a></li><li data-type='method'><a href="GratingStim.html#_getPixiMeshFromPredefinedShaders">_getPixiMeshFromPredefinedShaders</a></li><li data-type='method'><a href="GratingStim.html#_updateIfNeeded">_updateIfNeeded</a></li><li data-type='method'><a href="GratingStim.html#setBlendmode">setBlendmode</a></li><li data-type='method'><a href="GratingStim.html#setColor">setColor</a></li><li data-type='method'><a href="GratingStim.html#setColorSpace">setColorSpace</a></li><li data-type='method'><a href="GratingStim.html#setInterpolate">setInterpolate</a></li><li data-type='method'><a href="GratingStim.html#setMask">setMask</a></li><li data-type='method'><a href="GratingStim.html#setOpacity">setOpacity</a></li><li data-type='method'><a href="GratingStim.html#setPhase">setPhase</a></li><li data-type='method'><a href="GratingStim.html#setSF">setSF</a></li><li data-type='method'><a href="GratingStim.html#setTex">setTex</a></li></ul></li><li><a href="GUI.html">GUI</a><ul class='methods'><li data-type='method'><a href="GUI.html#_onCancelExperiment">_onCancelExperiment</a></li><li data-type='method'><a href="GUI.html#_onResourceEvents">_onResourceEvents</a></li><li data-type='method'><a href="GUI.html#_onStartExperiment">_onStartExperiment</a></li><li data-type='method'><a href="GUI.html#_setProgressMessage">_setProgressMessage</a></li><li data-type='method'><a href="GUI.html#_updateDialog">_updateDialog</a></li><li data-type='method'><a href="GUI.html#_updateProgressBar">_updateProgressBar</a></li><li data-type='method'><a href="GUI.html#_userFriendlyError">_userFriendlyError</a></li><li data-type='method'><a href="GUI.html#closeDialog">closeDialog</a></li><li data-type='method'><a href="GUI.html#dialog">dialog</a></li><li data-type='method'><a href="GUI.html#DlgFromDict">DlgFromDict</a></li><li data-type='method'><a href="GUI.html#finishDialog">finishDialog</a></li></ul></li><li><a href="ImageStim.html">ImageStim</a><ul class='methods'><li data-type='method'><a href="ImageStim.html#_estimateBoundingBox">_estimateBoundingBox</a></li><li data-type='method'><a href="ImageStim.html#_getDisplaySize">_getDisplaySize</a></li><li data-type='method'><a href="ImageStim.html#_updateIfNeeded">_updateIfNeeded</a></li><li data-type='method'><a href="ImageStim.html#setImage">setImage</a></li><li data-type='method'><a href="ImageStim.html#setInterpolate">setInterpolate</a></li><li data-type='method'><a href="ImageStim.html#setMask">setMask</a></li></ul></li><li><a href="Keyboard.html">Keyboard</a><ul class='methods'><li data-type='method'><a href="Keyboard.html#_addKeyListeners">_addKeyListeners</a></li><li data-type='method'><a href="Keyboard.html#clearEvents">clearEvents</a></li><li data-type='method'><a href="Keyboard.html#getEvents">getEvents</a></li><li data-type='method'><a href="Keyboard.html#getKeys">getKeys</a></li><li data-type='method'><a href="Keyboard.html#start">start</a></li><li data-type='method'><a href="Keyboard.html#stop">stop</a></li></ul></li><li><a href="KeyPress.html">KeyPress</a></li><li><a href="Logger.html">Logger</a><ul class='methods'><li data-type='method'><a href="Logger.html#_customConsoleLayout">_customConsoleLayout</a></li><li data-type='method'><a href="Logger.html#_getValue">_getValue</a></li><li data-type='method'><a href="Logger.html#_throttle">_throttle</a></li><li data-type='method'><a href="Logger.html#data">data</a></li><li data-type='method'><a href="Logger.html#exp">exp</a></li><li data-type='method'><a href="Logger.html#flush">flush</a></li><li data-type='method'><a href="Logger.html#log">log</a></li><li data-type='method'><a href="Logger.html#setLevel">setLevel</a></li></ul></li><li><a href="Microphone.html">Microphone</a><ul class='methods'><li data-type='method'><a href="Microphone.html#_onChange">_onChange</a></li><li data-type='method'><a href="Microphone.html#_prepareRecording">_prepareRecording</a></li><li data-type='method'><a href="Microphone.html#download">download</a></li><li data-type='method'><a href="Microphone.html#flush">flush</a></li><li data-type='method'><a href="Microphone.html#getRecording">getRecording</a></li><li data-type='method'><a href="Microphone.html#pause">pause</a></li><li data-type='method'><a href="Microphone.html#resume">resume</a></li><li data-type='method'><a href="Microphone.html#start">start</a></li><li data-type='method'><a href="Microphone.html#stop">stop</a></li><li data-type='method'><a href="Microphone.html#upload">upload</a></li></ul></li><li><a href="MinimalStim.html">MinimalStim</a><ul class='methods'><li data-type='method'><a href="MinimalStim.html#_updateIfNeeded">_updateIfNeeded</a></li><li data-type='method'><a href="MinimalStim.html#contains">contains</a></li><li data-type='method'><a href="MinimalStim.html#draw">draw</a></li><li data-type='method'><a href="MinimalStim.html#hide">hide</a></li><li data-type='method'><a href="MinimalStim.html#release">release</a></li><li data-type='method'><a href="MinimalStim.html#setAutoDraw">setAutoDraw</a></li></ul></li><li><a href="module-core.PsychoJS.html">PsychoJS</a><ul class='methods'><li data-type='method'><a href="module-core.PsychoJS.html#_captureErrors">_captureErrors</a></li><li data-type='method'><a href="module-core.PsychoJS.html#_configure">_configure</a></li><li data-type='method'><a href="module-core.PsychoJS.html#_getParticipantIPInfo">_getParticipantIPInfo</a></li><li data-type='method'><a href="module-core.PsychoJS.html#_makeStatusTopLevel">_makeStatusTopLevel</a></li><li data-type='method'><a href="module-core.PsychoJS.html#getEnvironment">getEnvironment</a></li><li data-type='method'><a href="module-core.PsychoJS.html#importAttributes">importAttributes</a></li><li data-type='method'><a href="module-core.PsychoJS.html#openWindow">openWindow</a></li><li data-type='method'><a href="module-core.PsychoJS.html#quit">quit</a></li><li data-type='method'><a href="module-core.PsychoJS.html#schedule">schedule</a></li><li data-type='method'><a href="module-core.PsychoJS.html#scheduleCondition">scheduleCondition</a></li><li data-type='method'><a href="module-core.PsychoJS.html#setRedirectUrls">setRedirectUrls</a></li><li data-type='method'><a href="module-core.PsychoJS.html#start">start</a></li><li data-type='method'><a href="module-core.PsychoJS.html#waitForResources">waitForResources</a></li></ul></li><li><a href="module-data.TrialHandler.html">TrialHandler</a><ul class='methods'><li data-type='method'><a href="module-data.TrialHandler.html#_prepareSequence">_prepareSequence</a></li><li data-type='method'><a href="module-data.TrialHandler.html#_prepareTrialList">_prepareTrialList</a></li><li data-type='method'><a href="module-data.TrialHandler.html#addData">addData</a></li><li data-type='method'><a href="module-data.TrialHandler.html#forEach">forEach</a></li><li data-type='method'><a href="module-data.TrialHandler.html#getAttributes">getAttributes</a></li><li data-type='method'><a href="module-data.TrialHandler.html#getCurrentTrial">getCurrentTrial</a></li><li data-type='method'><a href="module-data.TrialHandler.html#getEarlierTrial">getEarlierTrial</a></li><li data-type='method'><a href="module-data.TrialHandler.html#getFutureTrial">getFutureTrial</a></li><li data-type='method'><a href="module-data.TrialHandler.html#getSnapshot">getSnapshot</a></li><li data-type='method'><a href="module-data.TrialHandler.html#getTrial">getTrial</a></li><li data-type='method'><a href="module-data.TrialHandler.html#getTrialIndex">getTrialIndex</a></li><li data-type='method'><a href="module-data.TrialHandler.html#next">next</a></li><li data-type='method'><a href="module-data.TrialHandler.html#setSeed">setSeed</a></li><li data-type='method'><a href="module-data.TrialHandler.html#setTrialIndex">setTrialIndex</a></li><li data-type='method'><a href="module-data.TrialHandler.html#Symbol.iterator">Symbol.iterator</a></li></ul></li><li><a href="module-hardware.Camera.html">Camera</a><ul class='methods'><li data-type='method'><a href="module-hardware.Camera.html#_onChange">_onChange</a></li><li data-type='method'><a href="module-hardware.Camera.html#_prepareRecording">_prepareRecording</a></li><li data-type='method'><a href="module-hardware.Camera.html#_upload">_upload</a></li><li data-type='method'><a href="module-hardware.Camera.html#authorize">authorize</a></li><li data-type='method'><a href="module-hardware.Camera.html#close">close</a></li><li data-type='method'><a href="module-hardware.Camera.html#flush">flush</a></li><li data-type='method'><a href="module-hardware.Camera.html#getRecording">getRecording</a></li><li data-type='method'><a href="module-hardware.Camera.html#getStream">getStream</a></li><li data-type='method'><a href="module-hardware.Camera.html#getVideo">getVideo</a></li><li data-type='method'><a href="module-hardware.Camera.html#isReady">isReady</a></li><li data-type='method'><a href="module-hardware.Camera.html#open">open</a></li><li data-type='method'><a href="module-hardware.Camera.html#pause">pause</a></li><li data-type='method'><a href="module-hardware.Camera.html#record">record</a></li><li data-type='method'><a href="module-hardware.Camera.html#resume">resume</a></li><li data-type='method'><a href="module-hardware.Camera.html#stop">stop</a></li></ul></li><li><a href="module-sound.Sound.html">Sound</a><ul class='methods'><li data-type='method'><a href="module-sound.Sound.html#_getPlayer">_getPlayer</a></li><li data-type='method'><a href="module-sound.Sound.html#getDuration">getDuration</a></li><li data-type='method'><a href="module-sound.Sound.html#play">play</a></li><li data-type='method'><a href="module-sound.Sound.html#setLoops">setLoops</a></li><li data-type='method'><a href="module-sound.Sound.html#setSecs">setSecs</a></li><li data-type='method'><a href="module-sound.Sound.html#setSound">setSound</a></li><li data-type='method'><a href="module-sound.Sound.html#setVolume">setVolume</a></li><li data-type='method'><a href="module-sound.Sound.html#stop">stop</a></li></ul></li><li><a href="module-util.PsychObject.html">PsychObject</a><ul class='methods'><li data-type='method'><a href="module-util.PsychObject.html#_addAttribute">_addAttribute</a></li><li data-type='method'><a href="module-util.PsychObject.html#_setAttribute">_setAttribute</a></li><li data-type='method'><a href="module-util.PsychObject.html#emit">emit</a></li><li data-type='method'><a href="module-util.PsychObject.html#off">off</a></li><li data-type='method'><a href="module-util.PsychObject.html#on">on</a></li><li data-type='method'><a href="module-util.PsychObject.html#once">once</a></li><li data-type='method'><a href="module-util.PsychObject.html#toString">toString</a></li></ul></li><li><a href="module-visual.VisualStim.html">VisualStim</a><ul class='methods'><li data-type='method'><a href="module-visual.VisualStim.html#_estimateBoundingBox">_estimateBoundingBox</a></li><li data-type='method'><a href="module-visual.VisualStim.html#_getBoundingBox_px">_getBoundingBox_px</a></li><li data-type='method'><a href="module-visual.VisualStim.html#_onChange">_onChange</a></li><li data-type='method'><a href="module-visual.VisualStim.html#_updateIfNeeded">_updateIfNeeded</a></li><li data-type='method'><a href="module-visual.VisualStim.html#contains">contains</a></li><li data-type='method'><a href="module-visual.VisualStim.html#draw">draw</a></li><li data-type='method'><a href="module-visual.VisualStim.html#hide">hide</a></li><li data-type='method'><a href="module-visual.VisualStim.html#refresh">refresh</a></li><li data-type='method'><a href="module-visual.VisualStim.html#release">release</a></li><li data-type='method'><a href="module-visual.VisualStim.html#setAutoDraw">setAutoDraw</a></li><li data-type='method'><a href="module-visual.VisualStim.html#setDepth">setDepth</a></li><li data-type='method'><a href="module-visual.VisualStim.html#setOri">setOri</a></li><li data-type='method'><a href="module-visual.VisualStim.html#setPos">setPos</a></li><li data-type='method'><a href="module-visual.VisualStim.html#setSize">setSize</a></li></ul></li><li><a href="MonotonicClock.html">MonotonicClock</a><ul class='methods'><li data-type='method'><a href="MonotonicClock.html#getLastResetTime">getLastResetTime</a></li><li data-type='method'><a href="MonotonicClock.html#getTime">getTime</a></li></ul></li><li><a href="Mouse.html">Mouse</a><ul class='methods'><li data-type='method'><a href="Mouse.html#clickReset">clickReset</a></li><li data-type='method'><a href="Mouse.html#getPos">getPos</a></li><li data-type='method'><a href="Mouse.html#getPressed">getPressed</a></li><li data-type='method'><a href="Mouse.html#getRel">getRel</a></li><li data-type='method'><a href="Mouse.html#getWheelRel">getWheelRel</a></li><li data-type='method'><a href="Mouse.html#isPressedIn">isPressedIn</a></li><li data-type='method'><a href="Mouse.html#mouseMoved">mouseMoved</a></li><li data-type='method'><a href="Mouse.html#mouseMoveTime">mouseMoveTime</a></li></ul></li><li><a href="MovieStim.html">MovieStim</a><ul class='methods'><li data-type='method'><a href="MovieStim.html#_estimateBoundingBox">_estimateBoundingBox</a></li><li data-type='method'><a href="MovieStim.html#_getDisplaySize">_getDisplaySize</a></li><li data-type='method'><a href="MovieStim.html#_updateIfNeeded">_updateIfNeeded</a></li><li data-type='method'><a href="MovieStim.html#pause">pause</a></li><li data-type='method'><a href="MovieStim.html#play">play</a></li><li data-type='method'><a href="MovieStim.html#reset">reset</a></li><li data-type='method'><a href="MovieStim.html#seek">seek</a></li><li data-type='method'><a href="MovieStim.html#setMovie">setMovie</a></li><li data-type='method'><a href="MovieStim.html#stop">stop</a></li></ul></li><li><a href="MultiStairHandler.html">MultiStairHandler</a><ul class='methods'><li data-type='method'><a href="MultiStairHandler.html#_nextTrial">_nextTrial</a></li><li data-type='method'><a href="MultiStairHandler.html#_prepareStaircases">_prepareStaircases</a></li><li data-type='method'><a href="MultiStairHandler.html#_validateConditions">_validateConditions</a></li><li data-type='method'><a href="MultiStairHandler.html#addResponse">addResponse</a></li></ul></li><li><a href="Polygon.html">Polygon</a><ul class='methods'><li data-type='method'><a href="Polygon.html#_estimateBoundingBox">_estimateBoundingBox</a></li><li data-type='method'><a href="Polygon.html#_getPixiPolygon">_getPixiPolygon</a></li><li data-type='method'><a href="Polygon.html#_getVertices_px">_getVertices_px</a></li><li data-type='method'><a href="Polygon.html#_updateIfNeeded">_updateIfNeeded</a></li><li data-type='method'><a href="Polygon.html#contains">contains</a></li><li data-type='method'><a href="Polygon.html#setEdges">setEdges</a></li><li data-type='method'><a href="Polygon.html#setRadius">setRadius</a></li><li data-type='method'><a href="Polygon.html#setVertices">setVertices</a></li></ul></li><li><a href="QuestHandler.html">QuestHandler</a><ul class='methods'><li data-type='method'><a href="QuestHandler.html#_estimateQuestValue">_estimateQuestValue</a></li><li data-type='method'><a href="QuestHandler.html#_setupJsQuest">_setupJsQuest</a></li><li data-type='method'><a href="QuestHandler.html#addResponse">addResponse</a></li><li data-type='method'><a href="QuestHandler.html#confInterval">confInterval</a></li><li data-type='method'><a href="QuestHandler.html#getQuestValue">getQuestValue</a></li><li data-type='method'><a href="QuestHandler.html#mean">mean</a></li><li data-type='method'><a href="QuestHandler.html#mode">mode</a></li><li data-type='method'><a href="QuestHandler.html#quantile">quantile</a></li><li data-type='method'><a href="QuestHandler.html#sd">sd</a></li><li data-type='method'><a href="QuestHandler.html#setMethod">setMethod</a></li><li data-type='method'><a href="QuestHandler.html#simulate">simulate</a></li></ul></li><li><a href="Rect.html">Rect</a><ul class='methods'><li data-type='method'><a href="Rect.html#_estimateBoundingBox">_estimateBoundingBox</a></li><li data-type='method'><a href="Rect.html#_getPixiPolygon">_getPixiPolygon</a></li><li data-type='method'><a href="Rect.html#_getVertices_px">_getVertices_px</a></li><li data-type='method'><a href="Rect.html#_updateIfNeeded">_updateIfNeeded</a></li><li data-type='method'><a href="Rect.html#_updateVertices">_updateVertices</a></li><li data-type='method'><a href="Rect.html#contains">contains</a></li><li data-type='method'><a href="Rect.html#setHeight">setHeight</a></li><li data-type='method'><a href="Rect.html#setVertices">setVertices</a></li><li data-type='method'><a href="Rect.html#setWidth">setWidth</a></li></ul></li><li><a href="Scheduler.html">Scheduler</a><ul class='methods'><li data-type='method'><a href="Scheduler.html#add">add</a></li><li data-type='method'><a href="Scheduler.html#addConditional">addConditional</a></li><li data-type='method'><a href="Scheduler.html#start">start</a></li><li data-type='method'><a href="Scheduler.html#stop">stop</a></li></ul></li><li><a href="ServerManager.html">ServerManager</a><ul class='methods'><li data-type='method'><a href="ServerManager.html#_downloadResources">_downloadResources</a></li><li data-type='method'><a href="ServerManager.html#_listResources">_listResources</a></li><li data-type='method'><a href="ServerManager.html#_queryServerAPI">_queryServerAPI</a></li><li data-type='method'><a href="ServerManager.html#_setupPreloadQueue">_setupPreloadQueue</a></li><li data-type='method'><a href="ServerManager.html#closeSession">closeSession</a></li><li data-type='method'><a href="ServerManager.html#getConfiguration">getConfiguration</a></li><li data-type='method'><a href="ServerManager.html#getResource">getResource</a></li><li data-type='method'><a href="ServerManager.html#getResourceStatus">getResourceStatus</a></li><li data-type='method'><a href="ServerManager.html#openSession">openSession</a></li><li data-type='method'><a href="ServerManager.html#prepareResources">prepareResources</a></li><li data-type='method'><a href="ServerManager.html#resetStatus">resetStatus</a></li><li data-type='method'><a href="ServerManager.html#setStatus">setStatus</a></li><li data-type='method'><a href="ServerManager.html#uploadAudioVideo">uploadAudioVideo</a></li><li data-type='method'><a href="ServerManager.html#uploadData">uploadData</a></li><li data-type='method'><a href="ServerManager.html#uploadLog">uploadLog</a></li><li data-type='method'><a href="ServerManager.html#waitForResources">waitForResources</a></li></ul></li><li><a href="ShapeStim.html">ShapeStim</a><ul class='methods'><li data-type='method'><a href="ShapeStim.html#_estimateBoundingBox">_estimateBoundingBox</a></li><li data-type='method'><a href="ShapeStim.html#_getPixiPolygon">_getPixiPolygon</a></li><li data-type='method'><a href="ShapeStim.html#_getVertices_px">_getVertices_px</a></li><li data-type='method'><a href="ShapeStim.html#_updateIfNeeded">_updateIfNeeded</a></li><li data-type='method'><a href="ShapeStim.html#contains">contains</a></li><li data-type='method'><a href="ShapeStim.html#setVertices">setVertices</a></li></ul></li><li><a href="Shelf.html">Shelf</a><ul class='methods'><li data-type='method'><a href="Shelf.html#_checkAvailability">_checkAvailability</a></li><li data-type='method'><a href="Shelf.html#_checkKey">_checkKey</a></li><li data-type='method'><a href="Shelf.html#_getValue">_getValue</a></li><li data-type='method'><a href="Shelf.html#_updateValue">_updateValue</a></li><li data-type='method'><a href="Shelf.html#addIntegerValue">addIntegerValue</a></li><li data-type='method'><a href="Shelf.html#appendListValue">appendListValue</a></li><li data-type='method'><a href="Shelf.html#clearListValue">clearListValue</a></li><li data-type='method'><a href="Shelf.html#counterBalanceSelect">counterBalanceSelect</a></li><li data-type='method'><a href="Shelf.html#flipBooleanValue">flipBooleanValue</a></li><li data-type='method'><a href="Shelf.html#getBooleanValue">getBooleanValue</a></li><li data-type='method'><a href="Shelf.html#getDictionaryFieldNames">getDictionaryFieldNames</a></li><li data-type='method'><a href="Shelf.html#getDictionaryFieldValue">getDictionaryFieldValue</a></li><li data-type='method'><a href="Shelf.html#getDictionaryValue">getDictionaryValue</a></li><li data-type='method'><a href="Shelf.html#getIntegerValue">getIntegerValue</a></li><li data-type='method'><a href="Shelf.html#getListValue">getListValue</a></li><li data-type='method'><a href="Shelf.html#getTextValue">getTextValue</a></li><li data-type='method'><a href="Shelf.html#incrementComponent">incrementComponent</a></li><li data-type='method'><a href="Shelf.html#popListValue">popListValue</a></li><li data-type='method'><a href="Shelf.html#setBooleanValue">setBooleanValue</a></li><li data-type='method'><a href="Shelf.html#setDictionaryFieldValue">setDictionaryFieldValue</a></li><li data-type='method'><a href="Shelf.html#setDictionaryValue">setDictionaryValue</a></li><li data-type='method'><a href="Shelf.html#setIntegerValue">setIntegerValue</a></li><li data-type='method'><a href="Shelf.html#setListValue">setListValue</a></li><li data-type='method'><a href="Shelf.html#setTextValue">setTextValue</a></li><li data-type='method'><a href="Shelf.html#shuffleListValue">shuffleListValue</a></li></ul></li><li><a href="Slider.html">Slider</a><ul class='methods'><li data-type='method'><a href="Slider.html#_addEventListeners">_addEventListeners</a></li><li data-type='method'><a href="Slider.html#_estimateBoundingBox">_estimateBoundingBox</a></li><li data-type='method'><a href="Slider.html#_getBoundingBox_px">_getBoundingBox_px</a></li><li data-type='method'><a href="Slider.html#_getPosition_px">_getPosition_px</a></li><li data-type='method'><a href="Slider.html#_getTextStyle">_getTextStyle</a></li><li data-type='method'><a href="Slider.html#_granularise">_granularise</a></li><li data-type='method'><a href="Slider.html#_handlePointerDown">_handlePointerDown</a></li><li data-type='method'><a href="Slider.html#_handlePointerMove">_handlePointerMove</a></li><li data-type='method'><a href="Slider.html#_handlePointerUp">_handlePointerUp</a></li><li data-type='method'><a href="Slider.html#_isHorizontal">_isHorizontal</a></li><li data-type='method'><a href="Slider.html#_onChange">_onChange</a></li><li data-type='method'><a href="Slider.html#_posToRating">_posToRating</a></li><li data-type='method'><a href="Slider.html#_ratingToPos">_ratingToPos</a></li><li data-type='method'><a href="Slider.html#_removeEventListeners">_removeEventListeners</a></li><li data-type='method'><a href="Slider.html#_sanitizeAttributes">_sanitizeAttributes</a></li><li data-type='method'><a href="Slider.html#_setupBar">_setupBar</a></li><li data-type='method'><a href="Slider.html#_setupLabels">_setupLabels</a></li><li data-type='method'><a href="Slider.html#_setupMarker">_setupMarker</a></li><li data-type='method'><a href="Slider.html#_setupSlider">_setupSlider</a></li><li data-type='method'><a href="Slider.html#_setupStyle">_setupStyle</a></li><li data-type='method'><a href="Slider.html#_setupTicks">_setupTicks</a></li><li data-type='method'><a href="Slider.html#_updateIfNeeded">_updateIfNeeded</a></li><li data-type='method'><a href="Slider.html#contains">contains</a></li><li data-type='method'><a href="Slider.html#draw">draw</a></li><li data-type='method'><a href="Slider.html#getRating">getRating</a></li><li data-type='method'><a href="Slider.html#getRT">getRT</a></li><li data-type='method'><a href="Slider.html#hide">hide</a></li><li data-type='method'><a href="Slider.html#isMarkerDragging">isMarkerDragging</a></li><li data-type='method'><a href="Slider.html#recordRating">recordRating</a></li><li data-type='method'><a href="Slider.html#refresh">refresh</a></li><li data-type='method'><a href="Slider.html#release">release</a></li><li data-type='method'><a href="Slider.html#reset">reset</a></li><li data-type='method'><a href="Slider.html#setAutoDraw">setAutoDraw</a></li><li data-type='method'><a href="Slider.html#setDepth">setDepth</a></li><li data-type='method'><a href="Slider.html#setMarkerPos">setMarkerPos</a></li><li data-type='method'><a href="Slider.html#setOri">setOri</a></li><li data-type='method'><a href="Slider.html#setPos">setPos</a></li><li data-type='method'><a href="Slider.html#setRating">setRating</a></li><li data-type='method'><a href="Slider.html#setReadOnly">setReadOnly</a></li><li data-type='method'><a href="Slider.html#setSize">setSize</a></li></ul></li><li><a href="SpeechRecognition.html">SpeechRecognition</a><ul class='methods'><li data-type='method'><a href="SpeechRecognition.html#_onChange">_onChange</a></li><li data-type='method'><a href="SpeechRecognition.html#_prepareRecognition">_prepareRecognition</a></li><li data-type='method'><a href="SpeechRecognition.html#clearTranscripts">clearTranscripts</a></li><li data-type='method'><a href="SpeechRecognition.html#getTranscripts">getTranscripts</a></li><li data-type='method'><a href="SpeechRecognition.html#start">start</a></li><li data-type='method'><a href="SpeechRecognition.html#stop">stop</a></li></ul></li><li><a href="TextBox.html">TextBox</a><ul class='methods'><li data-type='method'><a href="TextBox.html#_addEventListeners">_addEventListeners</a></li><li data-type='method'><a href="TextBox.html#_estimateBoundingBox">_estimateBoundingBox</a></li><li data-type='method'><a href="TextBox.html#_getAnchor">_getAnchor</a></li><li data-type='method'><a href="TextBox.html#_getDefaultLetterHeight">_getDefaultLetterHeight</a></li><li data-type='method'><a href="TextBox.html#_getTextInputOptions">_getTextInputOptions</a></li><li data-type='method'><a href="TextBox.html#_updateIfNeeded">_updateIfNeeded</a></li><li data-type='method'><a href="TextBox.html#clear">clear</a></li><li data-type='method'><a href="TextBox.html#getText">getText</a></li><li data-type='method'><a href="TextBox.html#reset">reset</a></li><li data-type='method'><a href="TextBox.html#setAlignment">setAlignment</a></li><li data-type='method'><a href="TextBox.html#setAnchor">setAnchor</a></li><li data-type='method'><a href="TextBox.html#setBorderColor">setBorderColor</a></li><li data-type='method'><a href="TextBox.html#setColor">setColor</a></li><li data-type='method'><a href="TextBox.html#setFillColor">setFillColor</a></li><li data-type='method'><a href="TextBox.html#setFitToContent">setFitToContent</a></li><li data-type='method'><a href="TextBox.html#setFont">setFont</a></li><li data-type='method'><a href="TextBox.html#setLanguageStyle">setLanguageStyle</a></li><li data-type='method'><a href="TextBox.html#setLetterHeight">setLetterHeight</a></li><li data-type='method'><a href="TextBox.html#setSize">setSize</a></li><li data-type='method'><a href="TextBox.html#setText">setText</a></li></ul></li><li><a href="TextStim.html">TextStim</a><ul class='methods'><li data-type='method'><a href="TextStim.html#_estimateBoundingBox">_estimateBoundingBox</a></li><li data-type='method'><a href="TextStim.html#_getAnchor">_getAnchor</a></li><li data-type='method'><a href="TextStim.html#_getDefaultLetterHeight">_getDefaultLetterHeight</a></li><li data-type='method'><a href="TextStim.html#_getDefaultWrapWidth">_getDefaultWrapWidth</a></li><li data-type='method'><a href="TextStim.html#_getTextStyle">_getTextStyle</a></li><li data-type='method'><a href="TextStim.html#_updateIfNeeded">_updateIfNeeded</a></li><li data-type='method'><a href="TextStim.html#getBoundingBox">getBoundingBox</a></li><li data-type='method'><a href="TextStim.html#getTextMetrics">getTextMetrics</a></li><li data-type='method'><a href="TextStim.html#setColor">setColor</a></li></ul></li><li><a href="TonePlayer.html">TonePlayer</a><ul class='methods'><li data-type='method'><a href="TonePlayer.html#_initSoundLibrary">_initSoundLibrary</a></li><li data-type='method'><a href="TonePlayer.html#getDuration">getDuration</a></li><li data-type='method'><a href="TonePlayer.html#play">play</a></li><li data-type='method'><a href="TonePlayer.html#setDuration">setDuration</a></li><li data-type='method'><a href="TonePlayer.html#setLoops">setLoops</a></li><li data-type='method'><a href="TonePlayer.html#setVolume">setVolume</a></li><li data-type='method'><a href="TonePlayer.html#stop">stop</a></li></ul></li><li><a href="TrackPlayer.html">TrackPlayer</a><ul class='methods'><li data-type='method'><a href="TrackPlayer.html#getDuration">getDuration</a></li><li data-type='method'><a href="TrackPlayer.html#play">play</a></li><li data-type='method'><a href="TrackPlayer.html#setDuration">setDuration</a></li><li data-type='method'><a href="TrackPlayer.html#setLoops">setLoops</a></li><li data-type='method'><a href="TrackPlayer.html#setVolume">setVolume</a></li><li data-type='method'><a href="TrackPlayer.html#stop">stop</a></li></ul></li><li><a href="Transcript.html">Transcript</a></li><li><a href="Window.html">Window</a><ul class='methods'><li data-type='method'><a href="Window.html#_fullRefresh">_fullRefresh</a></li><li data-type='method'><a href="Window.html#_refresh">_refresh</a></li><li data-type='method'><a href="Window.html#_setupPixi">_setupPixi</a></li><li data-type='method'><a href="Window.html#_updateIfNeeded">_updateIfNeeded</a></li><li data-type='method'><a href="Window.html#_writeLogOnFlip">_writeLogOnFlip</a></li><li data-type='method'><a href="Window.html#addPixiObject">addPixiObject</a></li><li data-type='method'><a href="Window.html#adjustScreenSize">adjustScreenSize</a></li><li data-type='method'><a href="Window.html#callOnFlip">callOnFlip</a></li><li data-type='method'><a href="Window.html#close">close</a></li><li data-type='method'><a href="Window.html#closeFullScreen">closeFullScreen</a></li><li data-type='method'><a href="Window.html#getActualFrameRate">getActualFrameRate</a></li><li data-type='method'><a href="Window.html#logOnFlip">logOnFlip</a></li><li data-type='method'><a href="Window.html#removePixiObject">removePixiObject</a></li><li data-type='method'><a href="Window.html#render">render</a></li></ul></li></ul><h3>Interfaces</h3><ul><li><a href="SoundPlayer.html">SoundPlayer</a><ul class='methods'><li data-type='method'><a href="SoundPlayer.html#getDuration">getDuration</a></li><li data-type='method'><a href="SoundPlayer.html#play">play</a></li><li data-type='method'><a href="SoundPlayer.html#setDuration">setDuration</a></li><li data-type='method'><a href="SoundPlayer.html#setLoops">setLoops</a></li><li data-type='method'><a href="SoundPlayer.html#setVolume">setVolume</a></li><li data-type='method'><a href="SoundPlayer.html#stop">stop</a></li></ul></li></ul><h3>Mixins</h3><ul><li><a href="module-core.WindowMixin.html">WindowMixin</a><ul class='methods'><li data-type='method'><a href="module-core.WindowMixin.html#_getHorLengthPix">_getHorLengthPix</a></li><li data-type='method'><a href="module-core.WindowMixin.html#_getLengthPix">_getLengthPix</a></li><li data-type='method'><a href="module-core.WindowMixin.html#_getLengthUnits">_getLengthUnits</a></li><li data-type='method'><a href="module-core.WindowMixin.html#_getVerLengthPix">_getVerLengthPix</a></li></ul></li><li><a href="module-util.ColorMixin.html">ColorMixin</a><ul class='methods'><li data-type='method'><a href="module-util.ColorMixin.html#getContrastedColor">getContrastedColor</a></li><li data-type='method'><a href="module-util.ColorMixin.html#setColor">setColor</a></li><li data-type='method'><a href="module-util.ColorMixin.html#setContrast">setContrast</a></li></ul></li></ul>
</nav>
<div id="main">
<h1 class="page-title">core/ServerManager.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* Manager responsible for the communication between the experiment running in the participant's browser and the pavlovia.org server.
*
* @author Alain Pitiot
* @version 2022.2.3
* @copyright (c) 2017-2020 Ilixa Ltd. (http://ilixa.com) (c) 2020-2022 Open Science Tools Ltd. (https://opensciencetools.org)
* @license Distributed under the terms of the MIT License
*/
import { Howl } from "howler";
import { ExperimentHandler } from "../data/ExperimentHandler.js";
import { Clock, MonotonicClock } from "../util/Clock.js";
import { PsychObject } from "../util/PsychObject.js";
import * as util from "../util/Util.js";
import { Scheduler } from "../util/Scheduler.js";
import { PsychoJS } from "./PsychoJS.js";
/**
* &lt;p>This manager handles all communications between the experiment running in the participant's browser and the [pavlovia.org]{@link http://pavlovia.org} server, &lt;em>in an asynchronous manner&lt;/em>.&lt;/p>
* &lt;p>It is responsible for reading the configuration file of an experiment, for opening and closing a session, for listing and downloading resources, and for uploading results, logs, and audio recordings.&lt;/p>
*
* @extends PsychObject
*/
export class ServerManager extends PsychObject
{
/**
* Used to indicate to the ServerManager that all resources must be registered (and
* subsequently downloaded)
*
* @type {Symbol}
* @readonly
* @public
*/
static ALL_RESOURCES = Symbol.for("ALL_RESOURCES");
/**
* @memberof module:core
* @param {Object} options
* @param {module:core.PsychoJS} options.psychoJS - the PsychoJS instance
* @param {boolean} [options.autoLog= false] - whether or not to log
*/
constructor({
psychoJS,
autoLog = false,
} = {})
{
super(psychoJS);
// session:
this._session = {};
// resources is a map of &lt;name: string, { path: string, status: ResourceStatus, data: any }>
this._resources = new Map();
this._nbLoadedResources = 0;
this._setupPreloadQueue();
this._addAttribute("autoLog", autoLog);
this._addAttribute("status", ServerManager.Status.READY);
}
/**
* @typedef ServerManager.GetConfigurationPromise
* @property {string} origin the calling method
* @property {string} context the context
* @property {Object.&lt;string, *>} [config] the configuration
* @property {Object.&lt;string, *>} [error] an error message if we could not read the configuration file
*/
/**
* Read the configuration file for the experiment.
*
* @param {string} configURL - the URL of the configuration file
* @returns {Promise&lt;ServerManager.GetConfigurationPromise>} the response
*/
getConfiguration(configURL)
{
const response = {
origin: "ServerManager.getConfiguration",
context: "when reading the configuration file: " + configURL,
};
this._psychoJS.logger.debug("reading the configuration file: " + configURL);
const self = this;
return new Promise(async (resolve, reject) =>
{
try
{
const getResponse = await fetch(configURL, {
method: "GET",
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
redirect: "follow",
referrerPolicy: "no-referrer"
});
if (getResponse.status === 404)
{
throw "the configuration file could not be found";
}
else if (getResponse.status !== 200)
{
throw `unable to read the configuration file: status= ${getResponse.status}`;
}
// the configuration file should be valid json:
const config = await getResponse.json();
resolve(Object.assign(response, { config }));
}
catch (error)
{
self.setStatus(ServerManager.Status.ERROR);
console.error("error:", error);
reject(Object.assign(response, { error }));
}
});
}
/**
* @typedef ServerManager.OpenSessionPromise
* @property {string} origin the calling method
* @property {string} context the context
* @property {string} [token] the session token
* @property {Object.&lt;string, *>} [error] an error message if we could not open the session
*/
/**
* Open a session for this experiment on the remote PsychoJS manager.
*
* @returns {Promise&lt;ServerManager.OpenSessionPromise>} the response
*/
openSession()
{
const response = {
origin: "ServerManager.openSession",
context: "when opening a session for experiment: " + this._psychoJS.config.experiment.fullpath,
};
this._psychoJS.logger.debug("opening a session for experiment: " + this._psychoJS.config.experiment.fullpath);
this.setStatus(ServerManager.Status.BUSY);
// prepare a POST query:
let data = {};
if (this._psychoJS._serverMsg.has("__pilotToken"))
{
data.pilotToken = this._psychoJS._serverMsg.get("__pilotToken");
}
// query the server:
const self = this;
return new Promise(async (resolve, reject) =>
{
try
{
const postResponse = await this._queryServerAPI(
"POST",
`experiments/${this._psychoJS.config.gitlab.projectId}/sessions`,
data,
"FORM"
);
const openSessionResponse = await postResponse.json();
if (postResponse.status !== 200)
{
throw ('error' in openSessionResponse) ? openSessionResponse.error : openSessionResponse;
}
if (!("token" in openSessionResponse))
{
self.setStatus(ServerManager.Status.ERROR);
throw "unexpected answer from the server: no token";
}
if (!("experiment" in openSessionResponse))
{
self.setStatus(ServerManager.Status.ERROR);
throw "unexpected answer from server: no experiment";
}
self._psychoJS.config.session = {
token: openSessionResponse.token,
status: "OPEN",
};
const experiment = openSessionResponse.experiment;
self._psychoJS.config.experiment.status = experiment.status2;
self._psychoJS.config.experiment.saveFormat = Symbol.for(experiment.saveFormat);
self._psychoJS.config.experiment.saveIncompleteResults = experiment.saveIncompleteResults;
self._psychoJS.config.experiment.license = experiment.license;
self._psychoJS.config.experiment.runMode = experiment.runMode;
// secret keys for various services, e.g. Google Speech API
if ("keys" in experiment)
{
self._psychoJS.config.experiment.keys = experiment.keys;
}
else
{
self._psychoJS.config.experiment.keys = [];
}
self.setStatus(ServerManager.Status.READY);
resolve({...response, token: openSessionResponse.token, status: openSessionResponse.status });
}
catch (error)
{
console.error(error);
self.setStatus(ServerManager.Status.ERROR);
reject({...response, error});
}
});
}
/**
* @typedef ServerManager.CloseSessionPromise
* @property {string} origin the calling method
* @property {string} context the context
* @property {Object.&lt;string, *>} [error] an error message if we could not close the session (e.g. if it has not previously been opened)
*/
/**
* Close the session for this experiment on the remote PsychoJS manager.
*
* @param {boolean} [isCompleted= false] - whether or not the experiment was completed
* @param {boolean} [sync= false] - whether or not to communicate with the server in a synchronous manner
* @returns {Promise&lt;ServerManager.CloseSessionPromise> | void} the response
*/
async closeSession(isCompleted = false, sync = false)
{
const response = {
origin: "ServerManager.closeSession",
context: "when closing the session for experiment: " + this._psychoJS.config.experiment.fullpath,
};
this._psychoJS.logger.debug("closing the session for experiment: " + this._psychoJS.config.experiment.name);
this.setStatus(ServerManager.Status.BUSY);
// synchronously query the pavlovia server:
if (sync)
{
const url = this._psychoJS.config.pavlovia.URL
+ "/api/v2/experiments/" + this._psychoJS.config.gitlab.projectId
+ "/sessions/" + this._psychoJS.config.session.token + "/delete";
const formData = new FormData();
formData.append("isCompleted", isCompleted);
navigator.sendBeacon(url, formData);
this._psychoJS.config.session.status = "CLOSED";
}
// asynchronously query the pavlovia server:
else
{
const self = this;
return new Promise(async (resolve, reject) =>
{
try
{
const deleteResponse = await this._queryServerAPI(
"DELETE",
`experiments/${this._psychoJS.config.gitlab.projectId}/sessions/${this._psychoJS.config.session.token}`,
{ isCompleted },
"FORM"
);
const closeSessionResponse = await deleteResponse.json();
if (deleteResponse.status !== 200)
{
throw ('error' in closeSessionResponse) ? closeSessionResponse.error : closeSessionResponse;
}
self.setStatus(ServerManager.Status.READY);
self._psychoJS.config.session.status = "CLOSED";
resolve({ ...response, ...closeSessionResponse });
}
catch (error)
{
console.error(error);
self.setStatus(ServerManager.Status.ERROR);
reject({...response, error});
}
});
}
}
/**
* Get the value of a resource.
*
* @param {string} name - name of the requested resource
* @param {boolean} [errorIfNotDownloaded = false] whether or not to throw an exception if the
* resource status is not DOWNLOADED
* @return {Object} value of the resource, or undefined if the resource has been registered
* but not downloaded yet.
* @throws {Object.&lt;string, *>} exception if no resource with that name has previously been registered
*/
getResource(name, errorIfNotDownloaded = false)
{
const response = {
origin: "ServerManager.getResource",
context: "when getting the value of resource: " + name,
};
const pathStatusData = this._resources.get(name);
if (typeof pathStatusData === "undefined")
{
// throw { ...response, error: 'unknown resource' };
throw Object.assign(response, { error: "unknown resource" });
}
if (errorIfNotDownloaded &amp;&amp; pathStatusData.status !== ServerManager.ResourceStatus.DOWNLOADED)
{
throw Object.assign(response, {
error: name + " is not available for use (yet), its current status is: "
+ util.toString(pathStatusData.status),
});
}
return pathStatusData.data;
}
/**
* Get the status of a single resource or the reduced status of an array of resources.
*
* &lt;p>If an array of resources is given, getResourceStatus returns a single, reduced status
* that is the status furthest away from DOWNLOADED, with the status ordered as follow:
* ERROR (furthest from DOWNLOADED), REGISTERED, DOWNLOADING, and DOWNLOADED&lt;/p>
* &lt;p>For example, given three resources:
* &lt;ul>
* &lt;li>if at least one of the resource status is ERROR, the reduced status is ERROR&lt;/li>
* &lt;li>if at least one of the resource status is DOWNLOADING, the reduced status is DOWNLOADING&lt;/li>
* &lt;li>if the status of all three resources is REGISTERED, the reduced status is REGISTERED&lt;/li>
* &lt;li>if the status of all three resources is DOWNLOADED, the reduced status is DOWNLOADED&lt;/li>
* &lt;/ul>
* &lt;/p>
*
* @param {string | string[]} names names of the resources whose statuses are requested
* @return {module:core.ServerManager.ResourceStatus} status of the resource if there is only one, or reduced status otherwise
* @throws {Object.&lt;string, *>} if at least one of the names is not that of a previously
* registered resource
*/
getResourceStatus(names)
{
const response = {
origin: "ServerManager.getResourceStatus",
context: `when getting the status of resource(s): ${JSON.stringify(names)}`,
};
// sanity checks:
if (typeof names === 'string')
{
names = [names];
}
if (!Array.isArray(names))
{
throw Object.assign(response, { error: "names should be either a string or an array of strings" });
}
const statusOrder = new Map([
[Symbol.keyFor(ServerManager.ResourceStatus.ERROR), 0],
[Symbol.keyFor(ServerManager.ResourceStatus.REGISTERED), 1],
[Symbol.keyFor(ServerManager.ResourceStatus.DOWNLOADING), 2],
[Symbol.keyFor(ServerManager.ResourceStatus.DOWNLOADED), 3]
]);
let reducedStatus = ServerManager.ResourceStatus.DOWNLOADED;
for (const name of names)
{
const pathStatusData = this._resources.get(name);
if (typeof pathStatusData === "undefined")
{
// throw { ...response, error: 'unknown resource' };
throw Object.assign(response, {
error: `unable to find a previously registered resource with name: ${name}`
});
}
// update the reduced status according to the order given by statusOrder:
if (statusOrder.get(Symbol.keyFor(pathStatusData.status)) &lt;
statusOrder.get(Symbol.keyFor(reducedStatus)))
{
reducedStatus = pathStatusData.status;
}
}
return reducedStatus;
}
/**
* Set the resource manager status.
*/
setStatus(status)
{
const response = {
origin: "ServerManager.setStatus",
context: "when changing the status of the server manager to: " + util.toString(status),
};
// check status:
const statusKey = (typeof status === "symbol") ? Symbol.keyFor(status) : null;
if (!statusKey)
{ // throw { ...response, error: 'status must be a symbol' };
throw Object.assign(response, { error: "status must be a symbol" });
}
if (!ServerManager.Status.hasOwnProperty(statusKey))
{ // throw { ...response, error: 'unknown status' };
throw Object.assign(response, { error: "unknown status" });
}
this._status = status;
// inform status listeners:
this.emit(ServerManager.Event.STATUS, this._status);
return this._status;
}
/**
* Reset the resource manager status to ServerManager.Status.READY.
*
* @return {ServerManager.Status.READY} the new status
*/
resetStatus()
{
return this.setStatus(ServerManager.Status.READY);
}
/**
* Prepare resources for the experiment: register them with the server manager and possibly
* start downloading them right away.
*
* &lt;ul>
* &lt;li>For an experiment running locally: the root directory for the specified resources is that of index.html
* unless they are prepended with a protocol, such as http:// or https://.&lt;/li>
* &lt;li>For an experiment running on the server: if no resources are specified, all files in the resources directory
* of the experiment are downloaded, otherwise we only download the specified resources. All resources are assumed
* local to index.html unless they are prepended with a protocol.&lt;/li>
* &lt;li>If resources is null, then we do not download any resources&lt;/li>
* &lt;/ul>
*
* @param {String | Array.&lt;{name: string, path: string, download: boolean} | String | Symbol>} [resources=[]] - the list of resources or a single resource
*/
async prepareResources(resources = [])
{
const response = {
origin: "ServerManager.prepareResources",
context: "when preparing resources for experiment: " + this._psychoJS.config.experiment.name,
};
this._psychoJS.logger.debug("preparing resources for experiment: " + this._psychoJS.config.experiment.name);
try
{
const resourcesToDownload = new Set();
// register the resources:
if (resources !== null)
{
if (typeof resources === "string")
{
resources = [resources];
}
if (!Array.isArray(resources))
{
throw "resources should be either (a) a string or (b) an array of string or objects";
}
// whether all resources have been requested:
const allResources = (resources.length === 1 &amp;&amp;
resources[0] === ServerManager.ALL_RESOURCES);
// if the experiment is hosted on the pavlovia.org server and
// resources is [ServerManager.ALL_RESOURCES], then we register all the resources
// in the "resources" sub-directory
if (this._psychoJS.config.environment === ExperimentHandler.Environment.SERVER &amp;&amp;
allResources)
{
// list the resources from the resources directory of the experiment on the server:
const serverResponse = await this._listResources();
// register and mark for download those resources that have not been
// registered already:
for (const name of serverResponse.resources)
{
if (!this._resources.has(name))
{
const path = serverResponse.resourceDirectory + "/" + name;
this._resources.set(name, {
status: ServerManager.ResourceStatus.REGISTERED,
path,
data: undefined,
});
this._psychoJS.logger.debug(`registered resource: name= ${name}, path= ${path}`);
resourcesToDownload.add(name);
}
}
}
// if the experiment is hosted locally (localhost) or if specific resources were given
// then we register those specific resources, if they have not been registered already
else
{
// we cannot ask for all resources to be registered locally, since we cannot list
// them:
if (this._psychoJS.config.environment === ExperimentHandler.Environment.LOCAL &amp;&amp;
allResources)
{
throw "resources must be manually specified when the experiment is running locally: ALL_RESOURCES cannot be used";
}
// convert those resources that are only a string to an object with name and path:
for (let r = 0; r &lt; resources.length; ++r)
{
const resource = resources[r];
if (typeof resource === "string")
{
resources[r] = {
name: resource,
path: resource,
download: true
}
}
}
for (let { name, path, download } of resources)
{
if (!this._resources.has(name))
{
// to deal with potential CORS issues, we use the pavlovia.org proxy for resources
// not hosted on pavlovia.org:
if ( (path.toLowerCase().indexOf("www.") === 0 ||
path.toLowerCase().indexOf("http:") === 0 ||
path.toLowerCase().indexOf("https:") === 0) &amp;&amp;
(path.indexOf("pavlovia.org") === -1) )
{
path = "https://pavlovia.org/api/v2/proxy/" + path;
}
this._resources.set(name, {
status: ServerManager.ResourceStatus.REGISTERED,
path,
data: undefined,
});
this._psychoJS.logger.debug(`registered resource: name= ${name}, path= ${path}`);
// download resources by default:
if (typeof download === "undefined" || download)
{
resourcesToDownload.add(name);
}
}
}
}
}
// download those registered resources for which download = true
// note: we return a Promise that will be resolved when all the resources are downloaded
if (resourcesToDownload.size === 0)
{
this.emit(ServerManager.Event.RESOURCE, {
message: ServerManager.Event.DOWNLOAD_COMPLETED,
});
return Promise.resolve();
}
else
{
return new Promise((resolve, reject) =>
{
const uuid = this.on(ServerManager.Event.RESOURCE, (signal) =>
{
if (signal.message === ServerManager.Event.DOWNLOAD_COMPLETED)
{
this.off(ServerManager.Event.RESOURCE, uuid);
resolve();
}
});
this._downloadResources(resourcesToDownload);
});
}
}
catch (error)
{
console.error("error", error);
throw Object.assign(response, { error });
// throw { ...response, error: error };
}
}
/**
* Block the experiment until the specified resources have been downloaded.
*
* @param {Array.&lt;{name: string, path: string}>} [resources=[]] - the list of resources
*/
waitForResources(resources = [])
{
// prepare a PsychoJS component:
this._waitForDownloadComponent = {
status: PsychoJS.Status.NOT_STARTED,
clock: new Clock(),
resources: new Set(),
};
const self = this;
return async () =>
{
const t = self._waitForDownloadComponent.clock.getTime();
// start the component:
if (t >= 0.0 &amp;&amp; self._waitForDownloadComponent.status === PsychoJS.Status.NOT_STARTED)
{
self._waitForDownloadComponent.tStart = t;
self._waitForDownloadComponent.status = PsychoJS.Status.STARTED;
// if resources is an empty array, we consider all registered resources:
if (resources.length === 0)
{
for (const [name, { status, path, data }] of this._resources)
{
resources.push({ name, path });
}
}
// only download those resources not already downloaded and not downloading:
const resourcesToDownload = new Set();
for (let { name, path } of resources)
{
// to deal with potential CORS issues, we use the pavlovia.org proxy for resources
// not hosted on pavlovia.org:
if (
(path.toLowerCase().indexOf("www.") === 0
|| path.toLowerCase().indexOf("http:") === 0
|| path.toLowerCase().indexOf("https:") === 0)
&amp;&amp; (path.indexOf("pavlovia.org") === -1)
)
{
path = "https://devlovia.org/api/v2/proxy/" + path;
}
const pathStatusData = this._resources.get(name);
// the resource has not been registered yet:
if (typeof pathStatusData === "undefined")
{
self._resources.set(name, {
status: ServerManager.ResourceStatus.REGISTERED,
path,
data: undefined,
});
self._waitForDownloadComponent.resources.add(name);
resourcesToDownload.add(name);
self._psychoJS.logger.debug("registered resource:", name, path);
}
// the resource has been registered but is not downloaded yet:
else if (typeof pathStatusData.status !== ServerManager.ResourceStatus.DOWNLOADED)
{ // else if (typeof pathStatusData.data === 'undefined')
self._waitForDownloadComponent.resources.add(name);
}
}
self._waitForDownloadComponent.status = PsychoJS.Status.STARTED;
// start the download:
self._downloadResources(resourcesToDownload);
}
if (self._waitForDownloadComponent.status === PsychoJS.Status.STARTED)
{
// check whether all resources have been downloaded:
for (const name of self._waitForDownloadComponent.resources)
{
const pathStatusData = this._resources.get(name);
// the resource has not been downloaded yet: loop this component
if (pathStatusData.status !== ServerManager.ResourceStatus.DOWNLOADED)
{ // if (typeof pathStatusData.data === 'undefined')
return Scheduler.Event.FLIP_REPEAT;
}
}
// all resources have been downloaded: move to the next component:
self._waitForDownloadComponent.status = PsychoJS.Status.FINISHED;
return Scheduler.Event.NEXT;
}
};
}
/**
* @typedef ServerManager.UploadDataPromise
* @property {string} origin the calling method
* @property {string} context the context
* @property {Object.&lt;string, *>} [error] an error message if we could not upload the data
*/
/**
* Asynchronously upload experiment data to the pavlovia server.
*
* @param {string} key - the data key (e.g. the name of .csv file)
* @param {string} value - the data value (e.g. a string containing the .csv header and records)
* @param {boolean} [sync= false] - whether or not to communicate with the server in a synchronous manner
* @returns {Promise&lt;ServerManager.UploadDataPromise>} the response
*/
uploadData(key, value, sync = false)
{
const response = {
origin: "ServerManager.uploadData",
context: "when uploading participant's results for experiment: " + this._psychoJS.config.experiment.fullpath,
};
this._psychoJS.logger.debug("uploading data for experiment: " + this._psychoJS.config.experiment.fullpath);
this.setStatus(ServerManager.Status.BUSY);
const path = `experiments/${this._psychoJS.config.gitlab.projectId}/sessions/${this._psychoJS.config.session.token}/results`;
// synchronously query the pavlovia server:
if (sync)
{
const formData = new FormData();
formData.append("key", key);
formData.append("value", value);
navigator.sendBeacon(`${this._psychoJS.config.pavlovia.URL}/api/v2/${path}`, formData);
}
// asynchronously query the pavlovia server:
else
{
const self = this;
return new Promise(async (resolve, reject) =>
{
try
{
const postResponse = await this._queryServerAPI(
"POST",
`experiments/${this._psychoJS.config.gitlab.projectId}/sessions/${this._psychoJS.config.session.token}/results`,
{ key, value },
"FORM"
);
const uploadDataResponse = await postResponse.json();
if (postResponse.status !== 200)
{
throw ('error' in uploadDataResponse) ? uploadDataResponse.error : uploadDataResponse;
}
self.setStatus(ServerManager.Status.READY);
resolve({ ...response, ...uploadDataResponse });
}
catch (error)
{
console.error(error);
self.setStatus(ServerManager.Status.ERROR);
reject({...response, error});
}
});
}
}
/**
* Asynchronously upload experiment logs to the pavlovia server.
*
* @param {string} logs - the base64 encoded, compressed, formatted logs
* @param {boolean} [compressed=false] - whether or not the logs are compressed
* @returns {Promise&lt;ServerManager.UploadDataPromise>} the response
*/
uploadLog(logs, compressed = false)
{
const response = {
origin: "ServerManager.uploadLog",
context: "when uploading participant's log for experiment: " + this._psychoJS.config.experiment.fullpath,
};
this._psychoJS.logger.debug("uploading server log for experiment: " + this._psychoJS.config.experiment.fullpath);
this.setStatus(ServerManager.Status.BUSY);
// prepare a POST query:
const info = this.psychoJS.experiment.extraInfo;
const filenameWithoutPath = this.psychoJS.experiment.dataFileName.split(/[\\/]/).pop();
const filename = `${filenameWithoutPath}.log`;
const data = {
filename,
logs,
compressed
};
// query the pavlovia server:
const self = this;
return new Promise(async (resolve, reject) =>
{
try
{
const postResponse = await this._queryServerAPI(
"POST",
`experiments/${this._psychoJS.config.gitlab.projectId}/sessions/${self._psychoJS.config.session.token}/logs`,
data,
"FORM"
);
const uploadLogsResponse = await postResponse.json();
if (postResponse.status !== 200)
{
throw ('error' in uploadLogsResponse) ? uploadLogsResponse.error : uploadLogsResponse;
}
self.setStatus(ServerManager.Status.READY);
resolve({...response, ...uploadLogsResponse });
}
catch (error)
{
console.error(error);
self.setStatus(ServerManager.Status.ERROR);
reject({...response, error});
}
});
}
/**
* Synchronously or asynchronously upload audio data to the pavlovia server.
*
* @param @param {Object} options
* @param {Blob} options.mediaBlob - the audio or video blob to be uploaded
* @param {string} options.tag - additional tag
* @param {boolean} [options.waitForCompletion=false] - whether or not to wait for completion
* before returning
* @param {boolean} [options.showDialog=false] - whether or not to open a dialog box to inform the participant to wait for the data to be uploaded to the server
* @param {string} [options.dialogMsg="Please wait a few moments while the data is uploading to the server"] - default message informing the participant to wait for the data to be uploaded to the server
* @returns {Promise&lt;ServerManager.UploadDataPromise>} the response
*/
async uploadAudioVideo({mediaBlob, tag, waitForCompletion = false, showDialog = false, dialogMsg = "Please wait a few moments while the data is uploading to the server"})
{
const response = {
origin: "ServerManager.uploadAudio",
context: "when uploading media data for experiment: " + this._psychoJS.config.experiment.fullpath,
};
try
{
if (this._psychoJS.getEnvironment() !== ExperimentHandler.Environment.SERVER
|| this._psychoJS.config.experiment.status !== "RUNNING"
|| this._psychoJS._serverMsg.has("__pilotToken"))
{
throw "media recordings can only be uploaded to the server for experiments running on the server";
}
this._psychoJS.logger.debug(`uploading media data for experiment: ${this._psychoJS.config.experiment.fullpath}`);
this.setStatus(ServerManager.Status.BUSY);
// open pop-up dialog:
if (showDialog)
{
this.psychoJS.gui.dialog({
warning: dialogMsg,
showOK: false,
});
}
// prepare the request:
const info = this.psychoJS.experiment.extraInfo;
const participant = ((typeof info.participant === "string" &amp;&amp; info.participant.length > 0) ? info.participant : "PARTICIPANT");
const experimentName = (typeof info.expName !== "undefined") ? info.expName : this.psychoJS.config.experiment.name;
const datetime = ((typeof info.date !== "undefined") ? info.date : MonotonicClock.getDateStr());
const filename = participant + "_" + experimentName + "_" + datetime + "_" + tag;
const formData = new FormData();
formData.append("media", mediaBlob, filename);
let url = this._psychoJS.config.pavlovia.URL
+ "/api/v2/experiments/" + this._psychoJS.config.gitlab.projectId
+ "/sessions/" + this._psychoJS.config.session.token
+ "/media";
// query the server:
let response = await fetch(url, {
method: "POST",
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
redirect: "follow",
referrerPolicy: "no-referrer",
body: formData,
});
const postMediaResponse = await response.json();
this._psychoJS.logger.debug(`post media response: ${JSON.stringify(postMediaResponse)}`);
// deal with server errors:
if (!response.ok)
{
throw postMediaResponse;
}
// wait until the upload has completed:
if (waitForCompletion)
{
if (!("uploadToken" in postMediaResponse))
{
throw "incorrect server response: missing uploadToken";
}
const uploadToken = postMediaResponse['uploadToken'];
while (true)
{
// wait a bit:
await new Promise(r =>
{
setTimeout(r, 1000);
});
// check the status of the upload:
url = this._psychoJS.config.pavlovia.URL
+ "/api/v2/experiments/" + this._psychoJS.config.gitlab.projectId
+ "/sessions/" + this._psychoJS.config.session.token
+ "/media/" + uploadToken + "/status";
response = await fetch(url, {
method: "GET",
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
redirect: "follow",
referrerPolicy: "no-referrer"
});
const checkStatusResponse = await response.json();
this._psychoJS.logger.debug(`check upload status response: ${JSON.stringify(checkStatusResponse)}`);
if (("status" in checkStatusResponse) &amp;&amp; checkStatusResponse["status"] === "COMPLETED")
{
break;
}
}
}
if (showDialog)
{
this.psychoJS.gui.closeDialog();
}
this.setStatus(ServerManager.Status.READY);
return postMediaResponse;
}
catch (error)
{
this.setStatus(ServerManager.Status.ERROR);
console.error(error);
throw { ...response, error };
}
}
/**
* List the resources available to the experiment.
*
* @protected
*/
_listResources()
{
const response = {
origin: "ServerManager._listResourcesSession",
context: "when listing the resources for experiment: " + this._psychoJS.config.experiment.fullpath,
};
this._psychoJS.logger.debug(`listing the resources for experiment: ${this._psychoJS.config.experiment.fullpath}`);
this.setStatus(ServerManager.Status.BUSY);
// prepare a GET query:
const data = {
"token": this._psychoJS.config.session.token,
};
// query the server:
const self = this;
return new Promise(async (resolve, reject) =>
{
try
{
const getResponse = await this._queryServerAPI(
"GET",
`experiments/${this._psychoJS.config.gitlab.projectId}/resources`,
data
);
const getResourcesResponse = await getResponse.json();
if (!("resources" in getResourcesResponse))
{
self.setStatus(ServerManager.Status.ERROR);
throw "unexpected answer from server: no resources";
}
if (!("resourceDirectory" in getResourcesResponse))
{
self.setStatus(ServerManager.Status.ERROR);
throw "unexpected answer from server: no resourceDirectory";
}
self.setStatus(ServerManager.Status.READY);
resolve({ ...response, resources: data.resources, resourceDirectory: data.resourceDirectory });
}
catch (error)
{
console.error(error);
self.setStatus(ServerManager.Status.ERROR);
reject({...response, error});
}
});
}
/**
* Download the specified resources.
*
* &lt;p>Note: we use the [preloadjs library]{@link https://www.createjs.com/preloadjs}.&lt;/p>
*
* @protected
* @param {Set} resources - a set of names of previously registered resources
*/
async _downloadResources(resources)
{
const response = {
origin: "ServerManager._downloadResources",
context: "when downloading resources for experiment: " + this._psychoJS.config.experiment.name,
};
this._psychoJS.logger.debug("downloading resources for experiment: " + this._psychoJS.config.experiment.name);
this.setStatus(ServerManager.Status.BUSY);
this.emit(ServerManager.Event.RESOURCE, {
message: ServerManager.Event.DOWNLOADING_RESOURCES,
count: resources.size,
});
// based on the resource extension either (a) add it to the preload manifest, (b) mark it for
// download by howler, or (c) add it to the document fonts
const preloadManifest = [];
const soundResources = new Set();
const fontResources = [];
for (const name of resources)
{
const nameParts = name.toLowerCase().split(".");
const extension = (nameParts.length > 1) ? nameParts.pop() : undefined;
// warn the user if the resource does not have any extension:
if (typeof extension === "undefined")
{
this.psychoJS.logger.warn(`"${name}" does not appear to have an extension, which may negatively impact its loading. We highly recommend you add an extension.`);
}
const pathStatusData = this._resources.get(name);
if (typeof pathStatusData === "undefined")
{
throw Object.assign(response, { error: name + " has not been previously registered" });
}
if (pathStatusData.status !== ServerManager.ResourceStatus.REGISTERED)
{
throw Object.assign(response, { error: name + " is already downloaded or is currently already downloading" });
}
const pathParts = pathStatusData.path.toLowerCase().split(".");
const pathExtension = (pathParts.length > 1) ? pathParts.pop() : undefined;
// preload.js with forced binary:
if (["csv", "odp", "xls", "xlsx", "json"].indexOf(extension) > -1)
{
preloadManifest.push(/*new createjs.LoadItem().set(*/ {
id: name,
src: pathStatusData.path,
type: createjs.Types.BINARY,
crossOrigin: "Anonymous",
} /*)*/);
}
/* note: ascii .csv are adequately handled in binary format, no need to treat them separately
// forced text for .csv:
else if (['csv'].indexOf(resourceExtension) > -1)
manifest.push({ id: resourceName, src: resourceName, type: createjs.Types.TEXT });
*/
// sound files:
else if (["mp3", "mpeg", "opus", "ogg", "oga", "wav", "aac", "caf", "m4a", "weba", "dolby", "flac"].indexOf(extension) > -1)
{
soundResources.add(name);
if (extension === "wav")
{
this.psychoJS.logger.warn(`wav files are not supported by all browsers. We recommend you convert "${name}" to another format, e.g. mp3`);
}
}
// font files
else if (["ttf", "otf", "woff", "woff2"].indexOf(pathExtension) > -1)
{
fontResources.push(name);
}
// all other extensions handled by preload.js (download type decided by preload.js):
else
{
preloadManifest.push(/*new createjs.LoadItem().set(*/ {
id: name,
src: pathStatusData.path,
crossOrigin: "Anonymous",
} /*)*/);
}
}
// start loading resources marked for preload.js:
if (preloadManifest.length > 0)
{
this._preloadQueue.loadManifest(preloadManifest);
}
else
{
if (this._nbLoadedResources === resources.size)
{
this.setStatus(ServerManager.Status.READY);
this.emit(ServerManager.Event.RESOURCE, {
message: ServerManager.Event.DOWNLOAD_COMPLETED,
});
}
}
// start loading fonts:
for (const name of fontResources)
{
const pathStatusData = this._resources.get(name);
pathStatusData.status = ServerManager.ResourceStatus.DOWNLOADING;
this.emit(ServerManager.Event.RESOURCE, {
message: ServerManager.Event.DOWNLOADING_RESOURCE,
resource: name,
});
const pathExtension = pathStatusData.path.toLowerCase().split(".").pop();
try
{
const newFont = await new FontFace(name, `url('${pathStatusData.path}') format('${pathExtension}')`).load();
document.fonts.add(newFont);
++this._nbLoadedResources;
pathStatusData.status = ServerManager.ResourceStatus.DOWNLOADED;
this.emit(ServerManager.Event.RESOURCE, {
message: ServerManager.Event.RESOURCE_DOWNLOADED,
resource: name,
});
if (this._nbLoadedResources === resources.size)
{
this.setStatus(ServerManager.Status.READY);
this.emit(ServerManager.Event.RESOURCE, {
message: ServerManager.Event.DOWNLOAD_COMPLETED,
});
}
}
catch (error)
{
console.error(error);
this.setStatus(ServerManager.Status.ERROR);
pathStatusData.status = ServerManager.ResourceStatus.ERROR;
throw Object.assign(response, {
error: `unable to download resource: ${name}: ${error}`
});
}
}
// start loading resources marked for howler.js:
const self = this;
for (const name of soundResources)
{
const pathStatusData = this._resources.get(name);
pathStatusData.status = ServerManager.ResourceStatus.DOWNLOADING;
this.emit(ServerManager.Event.RESOURCE, {
message: ServerManager.Event.DOWNLOADING_RESOURCE,
resource: name,
});
const howl = new Howl({
src: pathStatusData.path,
preload: false,
autoplay: false,
});
howl.on("load", (event) =>
{
++self._nbLoadedResources;
pathStatusData.data = howl;
pathStatusData.status = ServerManager.ResourceStatus.DOWNLOADED;
self.emit(ServerManager.Event.RESOURCE, {
message: ServerManager.Event.RESOURCE_DOWNLOADED,
resource: name,
});
if (self._nbLoadedResources === resources.size)
{
self.setStatus(ServerManager.Status.READY);
self.emit(ServerManager.Event.RESOURCE, {
message: ServerManager.Event.DOWNLOAD_COMPLETED,
});
}
});
howl.on("loaderror", (id, error) =>
{
// throw { ...response, error: 'unable to download resource: ' + name + ' (' + util.toString(error) + ')' };
throw Object.assign(response, { error: "unable to download resource: " + name + " (" + util.toString(error) + ")" });
});
howl.load();
}
}
/**
* Setup the preload.js queue, and the associated callbacks.
*
* @protected
*/
_setupPreloadQueue()
{
const response = {
origin: "ServerManager._setupPreloadQueue",
context: "when setting up a preload queue"
};
this._preloadQueue = new createjs.LoadQueue(true, "", true);
const self = this;
// the loading of a specific resource has started:
this._preloadQueue.addEventListener("filestart", (event) =>
{
const pathStatusData = self._resources.get(event.item.id);
pathStatusData.status = ServerManager.ResourceStatus.DOWNLOADING;
self.emit(ServerManager.Event.RESOURCE, {
message: ServerManager.Event.DOWNLOADING_RESOURCE,
resource: event.item.id,
});
});
// the loading of a specific resource has completed:
this._preloadQueue.addEventListener("fileload", (event) =>
{
const pathStatusData = self._resources.get(event.item.id);
pathStatusData.data = event.result;
pathStatusData.status = ServerManager.ResourceStatus.DOWNLOADED;
++self._nbLoadedResources;
self.emit(ServerManager.Event.RESOURCE, {
message: ServerManager.Event.RESOURCE_DOWNLOADED,
resource: event.item.id,
});
});
// the loading of all given resources completed:
this._preloadQueue.addEventListener("complete", (event) =>
{
self._preloadQueue.close();
if (self._nbLoadedResources === self._resources.size)
{
self.setStatus(ServerManager.Status.READY);
self.emit(ServerManager.Event.RESOURCE, {
message: ServerManager.Event.DOWNLOAD_COMPLETED,
});
}
});
// error: we throw an exception
this._preloadQueue.addEventListener("error", (event) =>
{
self.setStatus(ServerManager.Status.ERROR);
if (typeof event.item !== "undefined")
{
const pathStatusData = self._resources.get(event.item.id);
pathStatusData.status = ServerManager.ResourceStatus.ERROR;
throw Object.assign(response, {
error: "unable to download resource: " + event.item.id + " (" + event.title + ")",
});
}
else
{
console.error(event);
if (event.title === "FILE_LOAD_ERROR" &amp;&amp; typeof event.data !== "undefined")
{
const id = event.data.id;
const title = event.data.src;
throw Object.assign(response, {
error: "unable to download resource: " + id + " (" + title + ")",
});
}
else
{
throw Object.assign(response, {
error: "unspecified download error",
});
}
}
});
}
/**
* Query the pavlovia server API.
*
* @protected
* @param method the HTTP method, i.e. GET, PUT, POST, or DELETE
* @param path the resource path, without the server address
* @param data the data to be sent
* @param {string} [contentType="JSON"] the content type, either JSON or FORM
*/
_queryServerAPI(method, path, data, contentType = "JSON")
{
const fullPath = `${this._psychoJS.config.pavlovia.URL}/api/v2/${path}`;
if (method === "PUT" || method === "POST" || method === "DELETE")
{
if (contentType === "JSON")
{
return fetch(fullPath, {
method,
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
redirect: 'follow',
referrerPolicy: 'no-referrer',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
}
else
{
const formData = new FormData();
for (const attribute in data)
{
formData.append(attribute, data[attribute]);
}
return fetch(fullPath, {
method,
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
redirect: 'follow',
referrerPolicy: 'no-referrer',
body: formData
});
}
}
if (method === "GET")
{
let url = new URL(fullPath);
url.search = new URLSearchParams(data).toString();
return fetch(url, {
method: "GET",
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
redirect: "follow",
referrerPolicy: "no-referrer"
});
}
throw {
origin: "ServerManager._queryServer",
context: "when querying the server",
error: "the method should be GET, PUT, POST, or DELETE"
};
}
}
/**
* Server event
*
* &lt;p>A server event is emitted by the manager to inform its listeners of either a change of status, or of a resource related event (e.g. download started, download is completed).&lt;/p>
*
* @enum {Symbol}
* @readonly
*/
ServerManager.Event = {
/**
* Event type: resource event
*/
RESOURCE: Symbol.for("RESOURCE"),
/**
* Event: resources have started to download
*/
DOWNLOADING_RESOURCES: Symbol.for("DOWNLOADING_RESOURCES"),
/**
* Event: a specific resource download has started
*/
DOWNLOADING_RESOURCE: Symbol.for("DOWNLOADING_RESOURCE"),
/**
* Event: a specific resource has been downloaded
*/
RESOURCE_DOWNLOADED: Symbol.for("RESOURCE_DOWNLOADED"),
/**
* Event: resources have all downloaded
*/
DOWNLOAD_COMPLETED: Symbol.for("DOWNLOAD_COMPLETED"),
/**
* Event type: status event
*/
STATUS: Symbol.for("STATUS"),
};
/**
* Server status
*
* @enum {Symbol}
* @readonly
*/
ServerManager.Status = {
/**
* The manager is ready.
*/
READY: Symbol.for("READY"),
/**
* The manager is busy, e.g. it is downloaded resources.
*/
BUSY: Symbol.for("BUSY"),
/**
* The manager has encountered an error, e.g. it was unable to download a resource.
*/
ERROR: Symbol.for("ERROR"),
};
/**
* Resource status
*
* @enum {Symbol}
* @readonly
*/
ServerManager.ResourceStatus = {
/**
* There was an error during downloading, or the resource is in an unknown state.
*/
ERROR: Symbol.for("ERROR"),
/**
* The resource has been registered.
*/
REGISTERED: Symbol.for("REGISTERED"),
/**
* The resource is currently downloading.
*/
DOWNLOADING: Symbol.for("DOWNLOADING"),
/**
* The resource has been downloaded.
*/
DOWNLOADED: Symbol.for("DOWNLOADED"),
};
</code></pre>
</article>
</section>
</div>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.7</a> on Mon Aug 01 2022 09:56:49 GMT+0200 (Central European Summer Time) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
<script src="scripts/polyfill.js"></script>
<script src="scripts/linenumber.js"></script>
<script src="scripts/search.js" defer></script>
</body>
</html>