UNPKG

@lookit/lookit-initjspsych

Version:

This package overloads jsPsych's init function.

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