UNPKG

@lookit/lookit-initjspsych

Version:

This package overloads jsPsych's init function.

3 lines (2 loc) 3.9 kB
var chsInitJsPsych=function(D,y,N){"use strict";function U(e){var r=Object.create(null);return e&&Object.keys(e).forEach(function(t){if(t!=="default"){var n=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(r,t,n.get?n:{enumerable:!0,get:function(){return e[t]}})}}),r.default=e,Object.freeze(r)}var J=U(D);class I extends Error{constructor(r){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(r)}.`)}}class v extends Error{constructor(r){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(r)}`)}}class b extends Error{constructor(){super("No jsPsych instance available for on_data_update."),this.name="NoJsPsychInstanceError"}}var g=(e,r,t)=>new Promise((n,c)=>{var u=i=>{try{s(t.next(i))}catch(l){c(l)}},a=i=>{try{s(t.throw(i))}catch(l){c(l)}},s=i=>i.done?n(i.value):Promise.resolve(i.value).then(u,a);s((t=t.apply(e,r)).next())});const R=(e,r,t)=>function(n){return g(this,null,function*(){if(!e||!e.data)throw new b;yield y.updateResponse(r,{exp_data:e.data.get().values()}),yield y.finish(),typeof t=="function"&&t(n)})},S=(e,r,t)=>function(n){return g(this,null,function*(){if(!e||!e.getDisplayElement)throw new b;e.getDisplayElement().innerHTML=N.loadingAnimation();const c=n.values(),{exit_url:u}=window.chs.study.attributes;typeof t=="function"&&t(n);try{if(yield y.updateResponse(r,{exp_data:c,completed:!0}),yield y.finish(),window.chs.pendingUploads&&(yield Promise.allSettled(window.chs.pendingUploads.map(a=>a.promise))),u){let a;try{a=new URL(u)}catch(i){try{a=new URL(`https://${u}`)}catch(l){a=new URL(window.location.origin)}}const s=window.chs.response.attributes.hash_child_id;s&&a.searchParams.set("child",s),a.searchParams.set("response",window.chs.response.id),window.location.replace(a.toString())}}catch(a){console.error("Error while finishing the experiment and saving data/video: ",a)}})};var T=Object.defineProperty,L=Object.defineProperties,M=Object.getOwnPropertyDescriptors,f=Object.getOwnPropertySymbols,_=Object.prototype.hasOwnProperty,P=Object.prototype.propertyIsEnumerable,O=(e,r,t)=>r in e?T(e,r,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[r]=t,m=(e,r)=>{for(var t in r||(r={}))_.call(r,t)&&O(e,t,r[t]);if(f)for(var t of f(r))P.call(r,t)&&O(e,t,r[t]);return e},j=(e,r)=>L(e,M(r)),$=(e,r)=>{var t={};for(var n in e)_.call(e,n)&&r.indexOf(n)<0&&(t[n]=e[n]);if(e!=null&&f)for(var n of f(e))r.indexOf(n)<0&&P.call(e,n)&&(t[n]=e[n]);return t},C=(e,r,t)=>new Promise((n,c)=>{var u=i=>{try{s(t.next(i))}catch(l){c(l)}},a=i=>{try{s(t.throw(i))}catch(l){c(l)}},s=i=>i.done?n(i.value):Promise.resolve(i.value).then(u,a);s((t=t.apply(e,r)).next())});const x=e=>(Boolean(e.timeline)||Array.isArray(e))&&!e.type,q=e=>typeof e=="object"&&!x(e);return e=>function(r){const t=r||{},{on_data_update:n,on_finish:c}=t,u=$(t,["on_data_update","on_finish"]);let a=null;const s=(...h)=>R(a,e,n)(...h),i=(...h)=>S(a,e,c)(...h),l=J.initJsPsych(j(m({},u),{on_data_update:s,on_finish:i}));a=l;const z=l.run,E=l;return E.run=function(h){return C(this,null,function*(){const w=(p,d)=>p.map(o=>{if(x(o)){if(Array.isArray(o))return w(o,d);if("timeline"in o&&Array.isArray(o.timeline))return j(m({},o),{timeline:w(o.timeline,d)});throw new v(o)}else if(q(o))if(o!==null&&"type"in o&&o.type!==null&&o.type!==void 0){const A=o;return d(A),A}else throw new I(o);else throw new v(o)}),B=w(h,p=>{var d;"type"in p&&(d=p.type)!=null&&d.chsData&&(p.data=m(m({},p.data),p.type.chsData()))});return yield z(B)})},E}}(jsPsychModule,chsData,chsTemplates); //# sourceMappingURL=https://unpkg.com/@lookit/lookit-initjspsych@3.1.0/dist/index.browser.min.js.map