Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added extension version and type to ExtensionManager.ts #3352

Merged
merged 7 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading