Merge pull request #3352 from vzhang03/v8

Added extension version and type to ExtensionManager.ts
This commit is contained in:
Josh de Leeuw 2024-07-18 14:17:43 -04:00 committed by GitHub
commit 9ea0592bfc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 41 additions and 14 deletions

View File

@ -0,0 +1,5 @@
---
"jspsych": patch
---
ExtensionManager correctly uses extension instantiation data to produce version and type, and warns users if fields are missing.

View File

@ -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).

View File

@ -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()),
};

View File

@ -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,
};

View File

@ -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",
});
@ -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",
});
});

View File

@ -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."
);
}
})
);
}
@ -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);
}

View File

@ -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) {}