@lookit/record 4.0.0 → 4.1.0

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.
package/src/trial.ts CHANGED
@@ -1,16 +1,55 @@
1
+ import chsTemplates from "@lookit/templates";
1
2
  import autoBind from "auto-bind";
2
- import { JsPsych, JsPsychExtension, JsPsychExtensionInfo } from "jspsych";
3
+ import {
4
+ JsPsych,
5
+ JsPsychExtension,
6
+ JsPsychExtensionInfo,
7
+ PluginInfo,
8
+ TrialType,
9
+ } from "jspsych";
10
+ import { version } from "../package.json";
3
11
  import Recorder from "./recorder";
4
12
  import { jsPsychPluginWithInfo } from "./types";
5
13
 
6
- /** This extension will allow reasearchers to record trials. */
14
+ // JsPsychExtensionInfo does not allow parameters, so we define them as interfaces and use these to type the arguments passed to extension initialize and on_start functions.
15
+ interface Parameters {
16
+ /**
17
+ * Content that should be displayed while the recording is uploading. If null
18
+ * (the default), then the default 'uploading video, please wait...' (or
19
+ * appropriate translation based on 'locale') will be displayed. Use a blank
20
+ * string for no message/content. Otherwise this parameter can be set to a
21
+ * custom string and can contain HTML markup. If you want to embed
22
+ * images/video/audio in this HTML string, be sure to preload the media files
23
+ * with the `preload` plugin and manual preloading. Use a blank string (`""`)
24
+ * for no message/content.
25
+ *
26
+ * @default null
27
+ */
28
+ wait_for_upload_message?: null | string;
29
+ /**
30
+ * Locale code used for translating the default 'uploading video, please
31
+ * wait...' message. This code must be present in the translation files. If
32
+ * the code is not found then English will be used. If the
33
+ * 'wait_for_upload_message' parameter is specified then this value
34
+ * isignored.
35
+ *
36
+ * @default "en-us"
37
+ */
38
+ locale?: string;
39
+ }
40
+
41
+ /** This extension allows researchers to record webcam audio/video during trials. */
7
42
  export default class TrialRecordExtension implements JsPsychExtension {
8
43
  public static readonly info: JsPsychExtensionInfo = {
9
44
  name: "chs-trial-record-extension",
45
+ version,
46
+ data: {},
10
47
  };
11
48
 
12
49
  private recorder?: Recorder;
13
50
  private pluginName: string | undefined;
51
+ private uploadMsg: null | string = null;
52
+ private locale: string = "en-us";
14
53
 
15
54
  /**
16
55
  * Video recording extension.
@@ -22,29 +61,76 @@ export default class TrialRecordExtension implements JsPsychExtension {
22
61
  }
23
62
 
24
63
  /**
25
- * Ran on the initialize step for extensions, called when an instance of
64
+ * Runs on the initialize step for extensions, called when an instance of
26
65
  * jsPsych is first initialized through initJsPsych().
66
+ *
67
+ * @param params - Parameters object
68
+ * @param params.wait_for_upload_message - Message to display while waiting
69
+ * for upload. String or null (default)
70
+ * @param params.locale - Message to display while waiting for upload. String
71
+ * or null (default).
27
72
  */
28
- public async initialize() {}
73
+ public async initialize(params?: Parameters) {
74
+ await new Promise<void>((resolve) => {
75
+ this.uploadMsg = params?.wait_for_upload_message
76
+ ? params.wait_for_upload_message
77
+ : null;
78
+ this.locale = params?.locale ? params.locale : "en-us";
79
+ console.log(this.uploadMsg);
80
+ console.log(this.locale);
81
+ resolve();
82
+ });
83
+ }
29
84
 
30
- /** Ran at the start of a trial. */
31
- public on_start() {
85
+ /**
86
+ * Runs at the start of a trial.
87
+ *
88
+ * @param startParams - Parameters object
89
+ * @param startParams.wait_for_upload_message - Message to display while
90
+ * waiting for upload. String or null (default). If given, this will
91
+ * overwrite the value used during initialization.
92
+ */
93
+ public on_start(startParams?: Parameters) {
94
+ if (startParams?.wait_for_upload_message) {
95
+ this.uploadMsg = startParams.wait_for_upload_message;
96
+ }
97
+ if (startParams?.locale) {
98
+ this.locale = startParams.locale;
99
+ }
100
+ console.log(this.uploadMsg);
101
+ console.log(this.locale);
32
102
  this.recorder = new Recorder(this.jsPsych);
33
103
  }
34
104
 
35
- /** Ran when the trial has loaded. */
105
+ /** Runs when the trial has loaded. */
36
106
  public on_load() {
37
107
  this.pluginName = this.getCurrentPluginName();
38
108
  this.recorder?.start(false, `${this.pluginName}`);
39
109
  }
40
110
 
41
111
  /**
42
- * Ran when trial has finished.
112
+ * Runs when trial has finished.
43
113
  *
44
- * @returns Trial data.
114
+ * @returns Any data from the trial extension that should be added to the rest
115
+ * of the trial data.
45
116
  */
46
- public on_finish() {
47
- this.recorder?.stop();
117
+ public async on_finish() {
118
+ const displayEl = this.jsPsych.getDisplayElement();
119
+ if (this.uploadMsg == null) {
120
+ displayEl.innerHTML = chsTemplates.uploadingVideo({
121
+ type: this.jsPsych.getCurrentTrial().type,
122
+ locale: this.locale,
123
+ } as TrialType<PluginInfo>);
124
+ } else {
125
+ displayEl.innerHTML = this.uploadMsg;
126
+ }
127
+ try {
128
+ await this.recorder?.stop();
129
+ displayEl.innerHTML = "";
130
+ } catch (err) {
131
+ console.error("TrialRecordExtension: recorder stop/upload failed.", err);
132
+ // TO DO: display translated error msg and/or researcher contact info
133
+ }
48
134
  return {};
49
135
  }
50
136