Skip to content

Commit

Permalink
Merge pull request #3352 from vzhang03/v8
Browse files Browse the repository at this point in the history
Added extension version and type to ExtensionManager.ts
  • Loading branch information
jodeleeuw authored Jul 18, 2024
2 parents 0082626 + 054ba1b commit 9ea0592
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-lies-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"jspsych": patch
---

ExtensionManager correctly uses extension instantiation data to produce version and type, and warns users if fields are missing.
7 changes: 7 additions & 0 deletions .changeset/gentle-icons-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@jspsych/extension-mouse-tracking": minor
"@jspsych/extension-record-video": minor
"@jspsych/extension-webgazer": minor
---

Extensions now return an extension_type and extension_version when returning data (metadata purposes).
2 changes: 0 additions & 2 deletions packages/extension-mouse-tracking/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,6 @@ class MouseTrackingExtension implements JsPsychExtension {
}

return {
extension_type: "mouse-tracking",
extension_version: version,
mouse_tracking_data: this.currentTrialData,
mouse_tracking_targets: Object.fromEntries(this.currentTrialTargets.entries()),
};
Expand Down
2 changes: 0 additions & 2 deletions packages/extension-webgazer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,6 @@ class WebGazerExtension implements JsPsychExtension {

// send back the gazeData
return {
extension_type: "webgazer",
extension_version: version,
webgazer_data: this.currentTrialData,
webgazer_targets: this.currentTrialTargets,
};
Expand Down
4 changes: 1 addition & 3 deletions packages/jspsych/src/ExtensionManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ describe("ExtensionManager", () => {

const onFinishCallback = jest.mocked(manager.extensions.test.on_finish);
onFinishCallback.mockReturnValue({
extension_type: "test",
extension_version: "1.0",
extension: "result",
});

Expand All @@ -115,7 +113,7 @@ describe("ExtensionManager", () => {
expect(onFinishCallback).toHaveBeenCalledWith({ my: "option" });
expect(results).toEqual({
extension_type: ["test"],
extension_version: ["1.0"],
extension_version: ["0.0.1"],
extension: "result",
});
});
Expand Down
33 changes: 26 additions & 7 deletions packages/jspsych/src/ExtensionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,28 @@ export class ExtensionManager {

public async initializeExtensions() {
await Promise.all(
this.extensionsConfiguration.map(({ type, params = {} }) =>
this.getExtensionInstanceByClass(type).initialize(params)
)
this.extensionsConfiguration.map(({ type, params = {} }) => {
this.getExtensionInstanceByClass(type).initialize(params);

const extensionInfo = type["info"] as JsPsychExtensionInfo;

if (!("version" in extensionInfo) && !("data" in extensionInfo)) {
console.warn(
extensionInfo["name"],
"is missing the 'version' and 'data' fields. Please update extension as 'version' and 'data' will be required in v9. See https://www.jspsych.org/latest/developers/extension-development/ for more details."
);
} else if (!("version" in extensionInfo)) {
console.warn(
extensionInfo["name"],
"is missing the 'version' field. Please update extension as 'version' will be required in v9. See https://www.jspsych.org/latest/developers/extension-development/ for more details."
);
} else if (!("data" in extensionInfo)) {
console.warn(
extensionInfo["name"],
"is missing the 'data' field. Please update extension as 'data' will be required in v9. See https://www.jspsych.org/latest/developers/extension-development/ for more details."
);
}
})
);
}

Expand All @@ -67,14 +86,14 @@ export class ExtensionManager {
)
);

const extensionInfo = trialExtensionsConfiguration.length
const extensionInfos = trialExtensionsConfiguration.length
? {
extension_type: results.map((result) => result.extension_type),
extension_version: results.map((result) => result.extension_version),
extension_type: trialExtensionsConfiguration.map(({ type }) => type["info"].name),
extension_version: trialExtensionsConfiguration.map(({ type }) => type["info"].version),
}
: {};

results.push(extensionInfo);
results.unshift(extensionInfos);

return Object.assign({}, ...results);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/jspsych/tests/extensions/test-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { JsPsych, JsPsychExtension } from "../../src";
export class TestExtension implements JsPsychExtension {
static info = {
name: "test",
version: "0.0.1",
data: {},
};

constructor(private jsPsych: JsPsych) {}
Expand Down

0 comments on commit 9ea0592

Please sign in to comment.