Skip to content

Commit

Permalink
Add data finish function
Browse files Browse the repository at this point in the history
  • Loading branch information
okaycj committed Apr 10, 2024
1 parent 40adad1 commit eb9e31d
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 102 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ module.exports = {
},
plugins: ["@typescript-eslint"],
ignorePatterns: ["packages/**/dist/*"],
rules: { "require-await": "error" },
};
171 changes: 171 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 23 additions & 18 deletions packages/data/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Child,
PastSession,
Promises,
Response,
ResponseAttrsUpdate,
ResponseUpdate,
Expand All @@ -9,35 +10,39 @@ import {
import { get, getUuids, patch } from "./utils";

const CONFIG = <const>{ ...getUuids() };
const promises: Promises[] = [];

export async function retrieveChild() {
return await get<Child>(`children/${CONFIG.child}/`);
function deposit<T extends Promises>(promise: T) {
promises.push(promise);
return promise;
}

export async function retrievePastSessions(uuid: string) {
return await get<PastSession[]>(`past-sessions/${uuid}/`);
export function finish() {
return Promise.all(promises);
}

export async function retrieveStudy() {
return await get<Study>(`studies/${CONFIG.study}/`);
export function retrieveChild() {
return deposit(get<Child>(`children/${CONFIG.child}/`));
}

export async function retrieveResponse(uuid: string) {
return await get<Response>(`responses/${uuid}/`);
export function retrievePastSessions(uuid: string) {
return deposit(get<PastSession[]>(`past-sessions/${uuid}/`));
}

export async function updateResponse(
uuid: string,
data: ResponseAttrsUpdate,
controller?: AbortController,
) {
return await patch<ResponseUpdate, Response>(
`responses/${uuid}/`,
{
export function retrieveStudy() {
return deposit(get<Study>(`studies/${CONFIG.study}/`));
}

export function retrieveResponse(uuid: string) {
return deposit(get<Response>(`responses/${uuid}/`));
}

export function updateResponse(uuid: string, data: ResponseAttrsUpdate) {
return deposit(
patch<ResponseUpdate, Response>(`responses/${uuid}/`, {
id: uuid,
type: "responses",
attributes: data,
},
controller,
}),
);
}
10 changes: 6 additions & 4 deletions packages/data/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
import {
finish,
retrieveChild,
retrievePastSessions,
retrieveResponse,
retrieveStudy,
updateResponse,
} from "./api";
import { Child, PastSession, Study } from "./types";
import { Child, PastSession, Response, Study } from "./types";

declare global {
interface Window {
chs: {
study: Study;
child: Child;
pastSessions: PastSession[];
response: Response;
};
}
}

async function load(response_uuid: string) {
console.log("Loading data...");
!window.chs &&
Object.assign(window, {
chs: {
study: await retrieveStudy(),
child: await retrieveChild(),
pastSessions: await retrievePastSessions(response_uuid),
response: await retrieveResponse(response_uuid),
},
});
console.log("Data loaded.");
await finish();
}

export default { load, retrieveResponse, updateResponse };
export default { load, retrieveResponse, updateResponse, finish };
3 changes: 3 additions & 0 deletions packages/data/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { DataCollection } from "jspsych/dist/modules/data/DataCollection";

export type Promises = Promise<Data<Attributes> | Data<Attributes>[]>;

export type Relationship = {
links: {
related: string;
Expand Down Expand Up @@ -134,4 +136,5 @@ export interface ResponseUpdate {
export interface ResponseAttrsUpdate {
exp_data?: DataCollection[];
completed?: boolean;
survey_consent?: boolean;
}
6 changes: 3 additions & 3 deletions packages/data/src/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ test("", async () => {
expect(await patch("some url", {})).toEqual(child);
});

test("", async () => {
test("", () => {
const data = {
study: "1647e101-282a-4fde-a32b-4f493d14f57e",
child: "8a2b2f04-63eb-485a-8e55-7b9362368f19",
Expand All @@ -42,7 +42,7 @@ test("", async () => {
expect(getUuids()).toEqual(data);
});

test("", async () => {
test("", () => {
const data = {
study: "1647e101-282a-4fde-a32b-4f493d14f57e",
child: "8a2b2f04-63eb-485a-8e55-7b9362368f19",
Expand All @@ -53,7 +53,7 @@ test("", async () => {
expect(getUuids()).toEqual(data);
});

test("", async () => {
test("", () => {
setLocationHref("https://mit.edu");
expect(() => {
getUuids();
Expand Down
9 changes: 2 additions & 7 deletions packages/data/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function jsonData<T>(request: Request) {
return json.data;
}

export async function get<T>(url: string) {
export function get<T>(url: string) {
/**
* Function for REST get.
*/
Expand All @@ -21,11 +21,7 @@ export async function get<T>(url: string) {
return jsonData<T>(request);
}

export async function patch<T, G>(
url: string,
data: T,
controller?: AbortController,
) {
export function patch<T, G>(url: string, data: T) {
/**
* Function for REST patch.
*/
Expand All @@ -36,7 +32,6 @@ export async function patch<T, G>(
"Content-Type": "application/vnd.api+json",
},
mode: "same-origin", // Do not send CSRF token to another domain.
signal: controller ? controller.signal : undefined,
body: JSON.stringify({ data }),
});

Expand Down
2 changes: 1 addition & 1 deletion packages/lookit-initjspsych/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function lookitInitJsPsych(responseUuid: string) {
});
const origJsPsychRun = jsPsych.run;

jsPsych.run = async function (timeline) {
jsPsych.run = function (timeline) {
// check timeline here...
return origJsPsychRun(timeline);
};
Expand Down
Loading

0 comments on commit eb9e31d

Please sign in to comment.