jspsych 7.3.2 → 7.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/README.md +9 -3
  2. package/css/jspsych.css +2 -4
  3. package/dist/JsPsych.d.ts +112 -112
  4. package/dist/TimelineNode.d.ts +34 -34
  5. package/dist/index.browser.js +4061 -3117
  6. package/dist/index.browser.js.map +1 -1
  7. package/dist/index.browser.min.js +2 -2
  8. package/dist/index.browser.min.js.map +1 -1
  9. package/dist/index.cjs +4057 -3113
  10. package/dist/index.cjs.map +1 -1
  11. package/dist/index.d.ts +12 -12
  12. package/dist/index.js +4057 -3111
  13. package/dist/index.js.map +1 -1
  14. package/dist/migration.d.ts +3 -3
  15. package/dist/modules/data/DataCollection.d.ts +46 -46
  16. package/dist/modules/data/DataColumn.d.ts +15 -15
  17. package/dist/modules/data/index.d.ts +25 -25
  18. package/dist/modules/data/utils.d.ts +3 -3
  19. package/dist/modules/extensions.d.ts +22 -22
  20. package/dist/modules/plugin-api/HardwareAPI.d.ts +15 -15
  21. package/dist/modules/plugin-api/KeyboardListenerAPI.d.ts +34 -34
  22. package/dist/modules/plugin-api/MediaAPI.d.ts +32 -32
  23. package/dist/modules/plugin-api/SimulationAPI.d.ts +44 -41
  24. package/dist/modules/plugin-api/TimeoutAPI.d.ts +17 -5
  25. package/dist/modules/plugin-api/index.d.ts +8 -8
  26. package/dist/modules/plugins.d.ts +136 -136
  27. package/dist/modules/randomization.d.ts +42 -42
  28. package/dist/modules/turk.d.ts +40 -40
  29. package/dist/modules/utils.d.ts +13 -7
  30. package/package.json +3 -3
  31. package/src/JsPsych.ts +21 -8
  32. package/src/modules/plugin-api/SimulationAPI.ts +9 -4
  33. package/src/modules/plugin-api/TimeoutAPI.ts +15 -3
  34. package/src/modules/plugin-api/index.ts +14 -11
  35. package/src/modules/utils.ts +31 -0
@@ -1,136 +1,136 @@
1
- /**
2
- Flatten the type output to improve type hints shown in editors.
3
- Borrowed from type-fest
4
- */
5
- declare type Simplify<T> = {
6
- [KeyType in keyof T]: T[KeyType];
7
- };
8
- /**
9
- Create a type that makes the given keys required. The remaining keys are kept as is.
10
- Borrowed from type-fest
11
- */
12
- declare type SetRequired<BaseType, Keys extends keyof BaseType> = Simplify<Omit<BaseType, Keys> & Required<Pick<BaseType, Keys>>>;
13
- /**
14
- * Parameter types for plugins
15
- */
16
- export declare enum ParameterType {
17
- BOOL = 0,
18
- STRING = 1,
19
- INT = 2,
20
- FLOAT = 3,
21
- FUNCTION = 4,
22
- KEY = 5,
23
- KEYS = 6,
24
- SELECT = 7,
25
- HTML_STRING = 8,
26
- IMAGE = 9,
27
- AUDIO = 10,
28
- VIDEO = 11,
29
- OBJECT = 12,
30
- COMPLEX = 13,
31
- TIMELINE = 14
32
- }
33
- declare type ParameterTypeMap = {
34
- [ParameterType.BOOL]: boolean;
35
- [ParameterType.STRING]: string;
36
- [ParameterType.INT]: number;
37
- [ParameterType.FLOAT]: number;
38
- [ParameterType.FUNCTION]: (...args: any[]) => any;
39
- [ParameterType.KEY]: string;
40
- [ParameterType.KEYS]: string[] | "ALL_KEYS" | "NO_KEYS";
41
- [ParameterType.SELECT]: any;
42
- [ParameterType.HTML_STRING]: string;
43
- [ParameterType.IMAGE]: string;
44
- [ParameterType.AUDIO]: string;
45
- [ParameterType.VIDEO]: string;
46
- [ParameterType.OBJECT]: object;
47
- [ParameterType.COMPLEX]: any;
48
- [ParameterType.TIMELINE]: any;
49
- };
50
- export interface ParameterInfo {
51
- type: ParameterType;
52
- array?: boolean;
53
- pretty_name?: string;
54
- default?: any;
55
- preload?: boolean;
56
- }
57
- export interface ParameterInfos {
58
- [key: string]: ParameterInfo;
59
- }
60
- declare type InferredParameter<I extends ParameterInfo> = I["array"] extends true ? Array<ParameterTypeMap[I["type"]]> : ParameterTypeMap[I["type"]];
61
- declare type RequiredParameterNames<I extends ParameterInfos> = {
62
- [K in keyof I]: I[K]["default"] extends undefined ? K : never;
63
- }[keyof I];
64
- declare type InferredParameters<I extends ParameterInfos> = SetRequired<{
65
- [Property in keyof I]?: InferredParameter<I[Property]>;
66
- }, RequiredParameterNames<I>>;
67
- export declare const universalPluginParameters: {
68
- /**
69
- * Data to add to this trial (key-value pairs)
70
- */
71
- readonly data: {
72
- readonly type: ParameterType.OBJECT;
73
- readonly pretty_name: "Data";
74
- readonly default: {};
75
- };
76
- /**
77
- * Function to execute when trial begins
78
- */
79
- readonly on_start: {
80
- readonly type: ParameterType.FUNCTION;
81
- readonly pretty_name: "On start";
82
- readonly default: () => void;
83
- };
84
- /**
85
- * Function to execute when trial is finished
86
- */
87
- readonly on_finish: {
88
- readonly type: ParameterType.FUNCTION;
89
- readonly pretty_name: "On finish";
90
- readonly default: () => void;
91
- };
92
- /**
93
- * Function to execute after the trial has loaded
94
- */
95
- readonly on_load: {
96
- readonly type: ParameterType.FUNCTION;
97
- readonly pretty_name: "On load";
98
- readonly default: () => void;
99
- };
100
- /**
101
- * Length of gap between the end of this trial and the start of the next trial
102
- */
103
- readonly post_trial_gap: {
104
- readonly type: ParameterType.INT;
105
- readonly pretty_name: "Post trial gap";
106
- readonly default: any;
107
- };
108
- /**
109
- * A list of CSS classes to add to the jsPsych display element for the duration of this trial
110
- */
111
- readonly css_classes: {
112
- readonly type: ParameterType.STRING;
113
- readonly pretty_name: "Custom CSS classes";
114
- readonly default: any;
115
- };
116
- /**
117
- * Options to control simulation mode for the trial.
118
- */
119
- readonly simulation_options: {
120
- readonly type: ParameterType.COMPLEX;
121
- readonly default: any;
122
- };
123
- };
124
- export declare type UniversalPluginParameters = InferredParameters<typeof universalPluginParameters>;
125
- export interface PluginInfo {
126
- name: string;
127
- parameters: {
128
- [key: string]: ParameterInfo;
129
- };
130
- }
131
- export interface JsPsychPlugin<I extends PluginInfo> {
132
- trial(display_element: HTMLElement, trial: TrialType<I>, on_load?: () => void): void | Promise<any>;
133
- }
134
- export declare type TrialType<I extends PluginInfo> = InferredParameters<I["parameters"]> & UniversalPluginParameters;
135
- export declare type PluginParameters<I extends PluginInfo> = InferredParameters<I["parameters"]>;
136
- export {};
1
+ /**
2
+ Flatten the type output to improve type hints shown in editors.
3
+ Borrowed from type-fest
4
+ */
5
+ type Simplify<T> = {
6
+ [KeyType in keyof T]: T[KeyType];
7
+ };
8
+ /**
9
+ Create a type that makes the given keys required. The remaining keys are kept as is.
10
+ Borrowed from type-fest
11
+ */
12
+ type SetRequired<BaseType, Keys extends keyof BaseType> = Simplify<Omit<BaseType, Keys> & Required<Pick<BaseType, Keys>>>;
13
+ /**
14
+ * Parameter types for plugins
15
+ */
16
+ export declare enum ParameterType {
17
+ BOOL = 0,
18
+ STRING = 1,
19
+ INT = 2,
20
+ FLOAT = 3,
21
+ FUNCTION = 4,
22
+ KEY = 5,
23
+ KEYS = 6,
24
+ SELECT = 7,
25
+ HTML_STRING = 8,
26
+ IMAGE = 9,
27
+ AUDIO = 10,
28
+ VIDEO = 11,
29
+ OBJECT = 12,
30
+ COMPLEX = 13,
31
+ TIMELINE = 14
32
+ }
33
+ type ParameterTypeMap = {
34
+ [ParameterType.BOOL]: boolean;
35
+ [ParameterType.STRING]: string;
36
+ [ParameterType.INT]: number;
37
+ [ParameterType.FLOAT]: number;
38
+ [ParameterType.FUNCTION]: (...args: any[]) => any;
39
+ [ParameterType.KEY]: string;
40
+ [ParameterType.KEYS]: string[] | "ALL_KEYS" | "NO_KEYS";
41
+ [ParameterType.SELECT]: any;
42
+ [ParameterType.HTML_STRING]: string;
43
+ [ParameterType.IMAGE]: string;
44
+ [ParameterType.AUDIO]: string;
45
+ [ParameterType.VIDEO]: string;
46
+ [ParameterType.OBJECT]: object;
47
+ [ParameterType.COMPLEX]: any;
48
+ [ParameterType.TIMELINE]: any;
49
+ };
50
+ export interface ParameterInfo {
51
+ type: ParameterType;
52
+ array?: boolean;
53
+ pretty_name?: string;
54
+ default?: any;
55
+ preload?: boolean;
56
+ }
57
+ export interface ParameterInfos {
58
+ [key: string]: ParameterInfo;
59
+ }
60
+ type InferredParameter<I extends ParameterInfo> = I["array"] extends true ? Array<ParameterTypeMap[I["type"]]> : ParameterTypeMap[I["type"]];
61
+ type RequiredParameterNames<I extends ParameterInfos> = {
62
+ [K in keyof I]: I[K]["default"] extends undefined ? K : never;
63
+ }[keyof I];
64
+ type InferredParameters<I extends ParameterInfos> = SetRequired<{
65
+ [Property in keyof I]?: InferredParameter<I[Property]>;
66
+ }, RequiredParameterNames<I>>;
67
+ export declare const universalPluginParameters: {
68
+ /**
69
+ * Data to add to this trial (key-value pairs)
70
+ */
71
+ readonly data: {
72
+ readonly type: ParameterType.OBJECT;
73
+ readonly pretty_name: "Data";
74
+ readonly default: {};
75
+ };
76
+ /**
77
+ * Function to execute when trial begins
78
+ */
79
+ readonly on_start: {
80
+ readonly type: ParameterType.FUNCTION;
81
+ readonly pretty_name: "On start";
82
+ readonly default: () => void;
83
+ };
84
+ /**
85
+ * Function to execute when trial is finished
86
+ */
87
+ readonly on_finish: {
88
+ readonly type: ParameterType.FUNCTION;
89
+ readonly pretty_name: "On finish";
90
+ readonly default: () => void;
91
+ };
92
+ /**
93
+ * Function to execute after the trial has loaded
94
+ */
95
+ readonly on_load: {
96
+ readonly type: ParameterType.FUNCTION;
97
+ readonly pretty_name: "On load";
98
+ readonly default: () => void;
99
+ };
100
+ /**
101
+ * Length of gap between the end of this trial and the start of the next trial
102
+ */
103
+ readonly post_trial_gap: {
104
+ readonly type: ParameterType.INT;
105
+ readonly pretty_name: "Post trial gap";
106
+ readonly default: any;
107
+ };
108
+ /**
109
+ * A list of CSS classes to add to the jsPsych display element for the duration of this trial
110
+ */
111
+ readonly css_classes: {
112
+ readonly type: ParameterType.STRING;
113
+ readonly pretty_name: "Custom CSS classes";
114
+ readonly default: any;
115
+ };
116
+ /**
117
+ * Options to control simulation mode for the trial.
118
+ */
119
+ readonly simulation_options: {
120
+ readonly type: ParameterType.COMPLEX;
121
+ readonly default: any;
122
+ };
123
+ };
124
+ export type UniversalPluginParameters = InferredParameters<typeof universalPluginParameters>;
125
+ export interface PluginInfo {
126
+ name: string;
127
+ parameters: {
128
+ [key: string]: ParameterInfo;
129
+ };
130
+ }
131
+ export interface JsPsychPlugin<I extends PluginInfo> {
132
+ trial(display_element: HTMLElement, trial: TrialType<I>, on_load?: () => void): void | Promise<any>;
133
+ }
134
+ export type TrialType<I extends PluginInfo> = InferredParameters<I["parameters"]> & UniversalPluginParameters;
135
+ export type PluginParameters<I extends PluginInfo> = InferredParameters<I["parameters"]>;
136
+ export {};
@@ -1,42 +1,42 @@
1
- /**
2
- * Uses the `seedrandom` package to replace Math.random() with a seedable PRNG.
3
- *
4
- * @param seed An optional seed. If none is given, a random seed will be generated.
5
- * @returns The seed value.
6
- */
7
- export declare function setSeed(seed?: string): string;
8
- export declare function repeat(array: any, repetitions: any, unpack?: boolean): any;
9
- export declare function shuffle(array: Array<any>): any[];
10
- export declare function shuffleNoRepeats(arr: Array<any>, equalityTest: (a: any, b: any) => boolean): any[];
11
- export declare function shuffleAlternateGroups(arr_groups: any, random_group_order?: boolean): any[];
12
- export declare function sampleWithoutReplacement(arr: any, size: any): any[];
13
- export declare function sampleWithReplacement(arr: any, size: any, weights?: any): any[];
14
- export declare function factorial(factors: Record<string, any>, repetitions?: number, unpack?: boolean): any;
15
- export declare function randomID(length?: number): string;
16
- /**
17
- * Generate a random integer from `lower` to `upper`, inclusive of both end points.
18
- * @param lower The lowest value it is possible to generate
19
- * @param upper The highest value it is possible to generate
20
- * @returns A random integer
21
- */
22
- export declare function randomInt(lower: number, upper: number): number;
23
- /**
24
- * Generates a random sample from a Bernoulli distribution.
25
- * @param p The probability of sampling 1.
26
- * @returns 0, with probability 1-p, or 1, with probability p.
27
- */
28
- export declare function sampleBernoulli(p: number): 0 | 1;
29
- export declare function sampleNormal(mean: number, standard_deviation: number): number;
30
- export declare function sampleExponential(rate: number): number;
31
- export declare function sampleExGaussian(mean: number, standard_deviation: number, rate: number, positive?: boolean): number;
32
- /**
33
- * Generate one or more random words.
34
- *
35
- * This is a wrapper function for the {@link https://www.npmjs.com/package/random-words `random-words` npm package}.
36
- *
37
- * @param opts An object with optional properties `min`, `max`, `exactly`,
38
- * `join`, `maxLength`, `wordsPerString`, `separator`, and `formatter`.
39
- *
40
- * @returns An array of words or a single string, depending on parameter choices.
41
- */
42
- export declare function randomWords(opts: any): any;
1
+ /**
2
+ * Uses the `seedrandom` package to replace Math.random() with a seedable PRNG.
3
+ *
4
+ * @param seed An optional seed. If none is given, a random seed will be generated.
5
+ * @returns The seed value.
6
+ */
7
+ export declare function setSeed(seed?: string): string;
8
+ export declare function repeat(array: any, repetitions: any, unpack?: boolean): any;
9
+ export declare function shuffle(array: Array<any>): any[];
10
+ export declare function shuffleNoRepeats(arr: Array<any>, equalityTest: (a: any, b: any) => boolean): any[];
11
+ export declare function shuffleAlternateGroups(arr_groups: any, random_group_order?: boolean): any[];
12
+ export declare function sampleWithoutReplacement(arr: any, size: any): any[];
13
+ export declare function sampleWithReplacement(arr: any, size: any, weights?: any): any[];
14
+ export declare function factorial(factors: Record<string, any>, repetitions?: number, unpack?: boolean): any;
15
+ export declare function randomID(length?: number): string;
16
+ /**
17
+ * Generate a random integer from `lower` to `upper`, inclusive of both end points.
18
+ * @param lower The lowest value it is possible to generate
19
+ * @param upper The highest value it is possible to generate
20
+ * @returns A random integer
21
+ */
22
+ export declare function randomInt(lower: number, upper: number): number;
23
+ /**
24
+ * Generates a random sample from a Bernoulli distribution.
25
+ * @param p The probability of sampling 1.
26
+ * @returns 0, with probability 1-p, or 1, with probability p.
27
+ */
28
+ export declare function sampleBernoulli(p: number): 0 | 1;
29
+ export declare function sampleNormal(mean: number, standard_deviation: number): number;
30
+ export declare function sampleExponential(rate: number): number;
31
+ export declare function sampleExGaussian(mean: number, standard_deviation: number, rate: number, positive?: boolean): number;
32
+ /**
33
+ * Generate one or more random words.
34
+ *
35
+ * This is a wrapper function for the {@link https://www.npmjs.com/package/random-words `random-words` npm package}.
36
+ *
37
+ * @param opts An object with optional properties `min`, `max`, `exactly`,
38
+ * `join`, `maxLength`, `wordsPerString`, `separator`, and `formatter`.
39
+ *
40
+ * @returns An array of words or a single string, depending on parameter choices.
41
+ */
42
+ export declare function randomWords(opts: any): string[];
@@ -1,40 +1,40 @@
1
- interface turkInformation {
2
- /**
3
- * Is the experiment being loaded in preview mode on Mechanical Turk?
4
- */
5
- previewMode: boolean;
6
- /**
7
- * Is the experiment being loaded outside of the Mechanical Turk environment?
8
- */
9
- outsideTurk: boolean;
10
- /**
11
- * The HIT ID.
12
- */
13
- hitId: string;
14
- /**
15
- * The Assignment ID.
16
- */
17
- assignmentId: string;
18
- /**
19
- * The worker ID.
20
- */
21
- workerId: string;
22
- /**
23
- * URL for submission of the HIT.
24
- */
25
- turkSubmitTo: string;
26
- }
27
- /**
28
- * Gets information about the Mechanical Turk Environment, HIT, Assignment, and Worker
29
- * by parsing the URL variables that Mechanical Turk generates.
30
- * @returns An object containing information about the Mechanical Turk Environment, HIT, Assignment, and Worker.
31
- */
32
- export declare function turkInfo(): turkInformation;
33
- /**
34
- * Send data to Mechnical Turk for storage.
35
- * @param data An object containing `key:value` pairs to send to Mechanical Turk. Values
36
- * cannot contain nested objects, arrays, or functions.
37
- * @returns Nothing
38
- */
39
- export declare function submitToTurk(data: any): void;
40
- export {};
1
+ interface turkInformation {
2
+ /**
3
+ * Is the experiment being loaded in preview mode on Mechanical Turk?
4
+ */
5
+ previewMode: boolean;
6
+ /**
7
+ * Is the experiment being loaded outside of the Mechanical Turk environment?
8
+ */
9
+ outsideTurk: boolean;
10
+ /**
11
+ * The HIT ID.
12
+ */
13
+ hitId: string;
14
+ /**
15
+ * The Assignment ID.
16
+ */
17
+ assignmentId: string;
18
+ /**
19
+ * The worker ID.
20
+ */
21
+ workerId: string;
22
+ /**
23
+ * URL for submission of the HIT.
24
+ */
25
+ turkSubmitTo: string;
26
+ }
27
+ /**
28
+ * Gets information about the Mechanical Turk Environment, HIT, Assignment, and Worker
29
+ * by parsing the URL variables that Mechanical Turk generates.
30
+ * @returns An object containing information about the Mechanical Turk Environment, HIT, Assignment, and Worker.
31
+ */
32
+ export declare function turkInfo(): turkInformation;
33
+ /**
34
+ * Send data to Mechnical Turk for storage.
35
+ * @param data An object containing `key:value` pairs to send to Mechanical Turk. Values
36
+ * cannot contain nested objects, arrays, or functions.
37
+ * @returns Nothing
38
+ */
39
+ export declare function submitToTurk(data: any): void;
40
+ export {};
@@ -1,7 +1,13 @@
1
- /**
2
- * Finds all of the unique items in an array.
3
- * @param arr The array to extract unique values from
4
- * @returns An array with one copy of each unique item in `arr`
5
- */
6
- export declare function unique(arr: Array<any>): any[];
7
- export declare function deepCopy(obj: any): any;
1
+ /**
2
+ * Finds all of the unique items in an array.
3
+ * @param arr The array to extract unique values from
4
+ * @returns An array with one copy of each unique item in `arr`
5
+ */
6
+ export declare function unique(arr: Array<any>): any[];
7
+ export declare function deepCopy(obj: any): any;
8
+ /**
9
+ * Merges two objects, recursively.
10
+ * @param obj1 Object to merge
11
+ * @param obj2 Object to merge
12
+ */
13
+ export declare function deepMerge(obj1: any, obj2: any): any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jspsych",
3
- "version": "7.3.2",
3
+ "version": "7.3.4",
4
4
  "description": "Behavioral experiments in a browser",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
@@ -47,7 +47,7 @@
47
47
  },
48
48
  "devDependencies": {
49
49
  "@fontsource/open-sans": "4.5.3",
50
- "@jspsych/config": "^1.3.1",
50
+ "@jspsych/config": "^2.0.0",
51
51
  "@types/dom-mediacapture-record": "^1.0.11",
52
52
  "base64-inline-loader": "^2.0.1",
53
53
  "css-loader": "^6.6.0",
@@ -55,7 +55,7 @@
55
55
  "npm-run-all": "^4.1.5",
56
56
  "sass": "^1.43.5",
57
57
  "sass-loader": "^12.4.0",
58
- "webpack": "^5.68.0",
58
+ "webpack": "^5.76.0",
59
59
  "webpack-cli": "^4.9.2",
60
60
  "webpack-remove-empty-scripts": "^0.7.2",
61
61
  "replace-in-file-webpack-plugin": "^1.0.6"
package/src/JsPsych.ts CHANGED
@@ -627,13 +627,14 @@ export class JsPsych {
627
627
  };
628
628
 
629
629
  let trial_complete;
630
+ let trial_sim_opts;
631
+ let trial_sim_opts_merged;
630
632
  if (!this.simulation_mode) {
631
633
  trial_complete = trial.type.trial(this.DOM_target, trial, load_callback);
632
634
  }
633
635
  if (this.simulation_mode) {
634
636
  // check if the trial supports simulation
635
637
  if (trial.type.simulate) {
636
- let trial_sim_opts;
637
638
  if (!trial.simulation_options) {
638
639
  trial_sim_opts = this.simulation_options.default;
639
640
  }
@@ -656,16 +657,23 @@ export class JsPsych {
656
657
  trial_sim_opts = trial.simulation_options;
657
658
  }
658
659
  }
659
- trial_sim_opts = this.utils.deepCopy(trial_sim_opts);
660
- trial_sim_opts = this.replaceFunctionsWithValues(trial_sim_opts, null);
660
+ // merge in default options that aren't overriden by the trial's simulation_options
661
+ // including nested parameters in the simulation_options
662
+ trial_sim_opts_merged = this.utils.deepMerge(
663
+ this.simulation_options.default,
664
+ trial_sim_opts
665
+ );
661
666
 
662
- if (trial_sim_opts?.simulate === false) {
667
+ trial_sim_opts_merged = this.utils.deepCopy(trial_sim_opts_merged);
668
+ trial_sim_opts_merged = this.replaceFunctionsWithValues(trial_sim_opts_merged, null);
669
+
670
+ if (trial_sim_opts_merged?.simulate === false) {
663
671
  trial_complete = trial.type.trial(this.DOM_target, trial, load_callback);
664
672
  } else {
665
673
  trial_complete = trial.type.simulate(
666
674
  trial,
667
- trial_sim_opts?.mode || this.simulation_mode,
668
- trial_sim_opts,
675
+ trial_sim_opts_merged?.mode || this.simulation_mode,
676
+ trial_sim_opts_merged,
669
677
  load_callback
670
678
  );
671
679
  }
@@ -678,8 +686,13 @@ export class JsPsych {
678
686
  // see if trial_complete is a Promise by looking for .then() function
679
687
  const is_promise = trial_complete && typeof trial_complete.then == "function";
680
688
 
681
- // in simulation mode we let the simulate function call the load_callback always.
682
- if (!is_promise && !this.simulation_mode) {
689
+ // in simulation mode we let the simulate function call the load_callback always,
690
+ // so we don't need to call it here. however, if we are in simulation mode but not simulating
691
+ // this particular trial we need to call it.
692
+ if (
693
+ !is_promise &&
694
+ (!this.simulation_mode || (this.simulation_mode && trial_sim_opts_merged?.simulate === false))
695
+ ) {
683
696
  load_callback();
684
697
  }
685
698
 
@@ -1,6 +1,11 @@
1
1
  export class SimulationAPI {
2
+ constructor(
3
+ private getDisplayContainerElement: () => HTMLElement,
4
+ private setJsPsychTimeout: (callback: () => void, delay: number) => number
5
+ ) {}
6
+
2
7
  dispatchEvent(event: Event) {
3
- document.body.dispatchEvent(event);
8
+ this.getDisplayContainerElement().dispatchEvent(event);
4
9
  }
5
10
 
6
11
  /**
@@ -26,7 +31,7 @@ export class SimulationAPI {
26
31
  */
27
32
  pressKey(key: string, delay = 0) {
28
33
  if (delay > 0) {
29
- setTimeout(() => {
34
+ this.setJsPsychTimeout(() => {
30
35
  this.keyDown(key);
31
36
  this.keyUp(key);
32
37
  }, delay);
@@ -43,7 +48,7 @@ export class SimulationAPI {
43
48
  */
44
49
  clickTarget(target: Element, delay = 0) {
45
50
  if (delay > 0) {
46
- setTimeout(() => {
51
+ this.setJsPsychTimeout(() => {
47
52
  target.dispatchEvent(new MouseEvent("mousedown", { bubbles: true }));
48
53
  target.dispatchEvent(new MouseEvent("mouseup", { bubbles: true }));
49
54
  target.dispatchEvent(new MouseEvent("click", { bubbles: true }));
@@ -63,7 +68,7 @@ export class SimulationAPI {
63
68
  */
64
69
  fillTextInput(target: HTMLInputElement, text: string, delay = 0) {
65
70
  if (delay > 0) {
66
- setTimeout(() => {
71
+ this.setJsPsychTimeout(() => {
67
72
  target.value = text;
68
73
  }, delay);
69
74
  } else {
@@ -1,13 +1,25 @@
1
+ /**
2
+ * A class that provides a wrapper around the global setTimeout and clearTimeout functions.
3
+ */
1
4
  export class TimeoutAPI {
2
- private timeout_handlers = [];
5
+ private timeout_handlers: number[] = [];
3
6
 
4
- setTimeout(callback, delay) {
7
+ /**
8
+ * Calls a function after a specified delay, in milliseconds.
9
+ * @param callback The function to call after the delay.
10
+ * @param delay The number of milliseconds to wait before calling the function.
11
+ * @returns A handle that can be used to clear the timeout with clearTimeout.
12
+ */
13
+ setTimeout(callback: () => void, delay: number): number {
5
14
  const handle = window.setTimeout(callback, delay);
6
15
  this.timeout_handlers.push(handle);
7
16
  return handle;
8
17
  }
9
18
 
10
- clearAllTimeouts() {
19
+ /**
20
+ * Clears all timeouts that have been created with setTimeout.
21
+ */
22
+ clearAllTimeouts(): void {
11
23
  for (const handler of this.timeout_handlers) {
12
24
  clearTimeout(handler);
13
25
  }
@@ -9,19 +9,22 @@ import { TimeoutAPI } from "./TimeoutAPI";
9
9
 
10
10
  export function createJointPluginAPIObject(jsPsych: JsPsych) {
11
11
  const settings = jsPsych.getInitSettings();
12
+ const keyboardListenerAPI = autoBind(
13
+ new KeyboardListenerAPI(
14
+ jsPsych.getDisplayContainerElement,
15
+ settings.case_sensitive_responses,
16
+ settings.minimum_valid_rt
17
+ )
18
+ );
19
+ const timeoutAPI = autoBind(new TimeoutAPI());
20
+ const mediaAPI = autoBind(new MediaAPI(settings.use_webaudio, jsPsych.webaudio_context));
21
+ const hardwareAPI = autoBind(new HardwareAPI());
22
+ const simulationAPI = autoBind(
23
+ new SimulationAPI(jsPsych.getDisplayContainerElement, timeoutAPI.setTimeout)
24
+ );
12
25
  return Object.assign(
13
26
  {},
14
- ...[
15
- new KeyboardListenerAPI(
16
- jsPsych.getDisplayContainerElement,
17
- settings.case_sensitive_responses,
18
- settings.minimum_valid_rt
19
- ),
20
- new TimeoutAPI(),
21
- new MediaAPI(settings.use_webaudio, jsPsych.webaudio_context),
22
- new HardwareAPI(),
23
- new SimulationAPI(),
24
- ].map((object) => autoBind(object))
27
+ ...[keyboardListenerAPI, timeoutAPI, mediaAPI, hardwareAPI, simulationAPI]
25
28
  ) as KeyboardListenerAPI & TimeoutAPI & MediaAPI & HardwareAPI & SimulationAPI;
26
29
  }
27
30