UNPKG

@lookit/lookit-initjspsych

Version:

This package overloads jsPsych's init function.

3 lines (2 loc) 5.3 kB
var chsInitJsPsych=function(T,h,I){"use strict";function L(e){var t=Object.create(null);return e&&Object.keys(e).forEach(function(r){if(r!=="default"){var i=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,i.get?i:{enumerable:!0,get:function(){return e[r]}})}}),t.default=e,Object.freeze(t)}var M=L(T);class S extends Error{constructor(t){super(`A trial object in the timeline has an undefined type. Maybe the type name is misspelled, or the plugin you want to use is not supported. Object: ${JSON.stringify(t)}.`)}}class P extends Error{constructor(t){super(`An element in the timeline is not structured correctly or is missing required information. It may be a timeline node with a timeline array that is the wrong type or missing/undefined, or a trial object with a missing type. Element: ${JSON.stringify(t)}`)}}class O extends Error{constructor(){super("No jsPsych instance available for on_data_update."),this.name="NoJsPsychInstanceError"}}var J=Math.pow,v=(e,t,r)=>new Promise((i,d)=>{var c=n=>{try{s(r.next(n))}catch(l){d(l)}},o=n=>{try{s(r.throw(n))}catch(l){d(l)}},s=n=>n.done?i(n.value):Promise.resolve(n.value).then(c,o);s((r=r.apply(e,t)).next())});const $=()=>{const e=window.chs.sessionRecorder;return e?e.getSessionTrialRecordingData():null},B=(e,t)=>{t&&!e.chs_recording&&(e.chs_recording=t)},q=(e,...t)=>v(void 0,[e,...t],function*(r,{retries:i=3,baseDelayMs:d=1e3}={}){for(let c=0;;c++)try{return yield r()}catch(o){if(c>=i)throw o;yield new Promise(s=>setTimeout(s,d*J(2,c)))}}),z=(e,t,r)=>function(i){return v(this,null,function*(){if(!e||!e.data)throw new O;yield h.updateResponse(t,{exp_data:e.data.get().values()}),yield h.finish(),typeof r=="function"&&r(i)})},C=(e,t,r)=>function(i){return v(this,null,function*(){if(!e||!e.getDisplayElement)throw new O;e.getDisplayElement().innerHTML=I.loadingAnimation();const d=i.values(),{exit_url:c}=window.chs.study.attributes;typeof r=="function"&&r(i);try{yield q(()=>h.updateResponse(t,{exp_data:d,completed:!0})),yield h.finish()}catch(o){console.error("Error while saving final response data after retries: ",o)}try{if(window.chs.pendingUploads){const o=window.chs.pendingUploads;(yield Promise.allSettled(o.map(n=>n.promise))).forEach((n,l)=>{n.status==="rejected"&&console.error(`Pending upload failed for "${o[l].file}": `,n.reason)});let s=!1;if(o.forEach(n=>{const l=d.filter(p=>p.chs_recording!==void 0&&p.chs_recording.filename===n.file);l.length===0&&console.warn(`No trial data found for uploaded recording "${n.file}"; its upload status was not recorded in the data.`),l.forEach(p=>{p.chs_recording.upload_status=n.status,n.error_message&&(p.chs_recording.upload_error=n.error_message),s=!0})}),s)try{yield h.updateResponse(t,{exp_data:d,completed:!0}),yield h.finish()}catch(n){console.error("Error while saving recording upload statuses: ",n)}}}catch(o){console.error("Error while waiting for pending uploads: ",o)}if(c){let o;try{o=new URL(c)}catch(n){try{o=new URL(`https://${c}`)}catch(l){o=new URL(window.location.origin)}}const s=window.chs.response.attributes.hash_child_id;s&&o.searchParams.set("child",s),o.searchParams.set("response",window.chs.response.id),window.location.replace(o.toString())}})};var F=Object.defineProperty,H=Object.defineProperties,K=Object.getOwnPropertyDescriptors,m=Object.getOwnPropertySymbols,E=Object.prototype.hasOwnProperty,j=Object.prototype.propertyIsEnumerable,x=(e,t,r)=>t in e?F(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,w=(e,t)=>{for(var r in t||(t={}))E.call(t,r)&&x(e,r,t[r]);if(m)for(var r of m(t))j.call(t,r)&&x(e,r,t[r]);return e},D=(e,t)=>H(e,K(t)),W=(e,t)=>{var r={};for(var i in e)E.call(e,i)&&t.indexOf(i)<0&&(r[i]=e[i]);if(e!=null&&m)for(var i of m(e))t.indexOf(i)<0&&j.call(e,i)&&(r[i]=e[i]);return r},G=(e,t,r)=>new Promise((i,d)=>{var c=n=>{try{s(r.next(n))}catch(l){d(l)}},o=n=>{try{s(r.throw(n))}catch(l){d(l)}},s=n=>n.done?i(n.value):Promise.resolve(n.value).then(c,o);s((r=r.apply(e,t)).next())});const A=e=>(Boolean(e.timeline)||Array.isArray(e))&&!e.type,Q=e=>typeof e=="object"&&!A(e);return e=>function(t){const r=t||{},{on_data_update:i,on_finish:d,on_trial_start:c,on_trial_finish:o}=r,s=W(r,["on_data_update","on_finish","on_trial_start","on_trial_finish"]);let n=null,l=null;const p=u=>{l=$(),c==null||c(u)},V=u=>{B(u,l),l=null,o==null||o(u)},X=(...u)=>z(n,e,i)(...u),Y=(...u)=>C(n,e,d)(...u),g=M.initJsPsych(D(w({},s),{on_data_update:X,on_finish:Y,on_trial_start:p,on_trial_finish:V}));n=g;const Z=g.run,N=g;return N.run=function(u){return G(this,null,function*(){const b=(f,y)=>f.map(a=>{if(A(a)){if(Array.isArray(a))return b(a,y);if("timeline"in a&&Array.isArray(a.timeline))return D(w({},a),{timeline:b(a.timeline,y)});throw new P(a)}else if(Q(a))if(a!==null&&"type"in a&&a.type!==null&&a.type!==void 0){const _=a;return y(_),_}else throw new S(a);else throw new P(a)}),k=b(u,f=>{var y,a,_;if("type"in f&&((y=f.type)!=null&&y.chsData&&(f.data=w(w({},f.data),f.type.chsData())),((_=(a=f.type)==null?void 0:a.info)==null?void 0:_.name)==="assent-video")){const R=f.on_finish;f.on_finish=U=>{R==null||R(U),U.response===!1&&g.abortExperiment()}}});return yield Z(k)})},N}}(jsPsychModule,chsData,chsTemplates); //# sourceMappingURL=https://unpkg.com/@lookit/lookit-initjspsych@4.0.0/dist/index.browser.min.js.map