@openstax/ts-utils 1.6.2 → 1.7.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.
@@ -117,7 +117,18 @@ exports.mapFind = mapFind;
117
117
  const once = (fn) => {
118
118
  const initialValue = {};
119
119
  let result = initialValue;
120
- return ((...args) => result === initialValue ? (result = fn(...args)) : result);
120
+ return ((...args) => {
121
+ if (result !== initialValue) {
122
+ return result;
123
+ }
124
+ result = fn(...args);
125
+ if (typeof result === 'object' && result instanceof Promise) {
126
+ // Clear the result when possible but do not return a Promise that resolves to the initialValue
127
+ result.catch(() => result = initialValue);
128
+ }
129
+ // If this is a rejected Promise, it should be returned before catch() actually overwrites it
130
+ return result;
131
+ });
121
132
  };
122
133
  exports.once = once;
123
134
  /**
@@ -9,7 +9,7 @@ export interface Grade {
9
9
  comment?: string;
10
10
  activityProgress: 'Initialized' | 'Started' | 'inProgress' | 'Submitted' | 'Completed';
11
11
  gradingProgress: 'FullyGraded' | 'Pending' | 'PendingManual' | 'Failed' | 'NotReady';
12
- userId: string;
12
+ userId?: string;
13
13
  }
14
14
  export declare const getRegistrationAttemptInfo: (lrs: LrsGateway, registration: string, options?: {
15
15
  activity?: string | undefined;
@@ -25,7 +25,7 @@ export declare const getScoreGrade: (score: {
25
25
  raw?: number;
26
26
  min?: number;
27
27
  max?: number;
28
- }, completed: boolean, userId: string, maxScore?: number | undefined) => Grade;
28
+ }, completed: boolean, userId?: string | undefined, maxScore?: number | undefined) => Grade;
29
29
  export declare type Progress = {
30
30
  scaled: number;
31
31
  max?: number;
@@ -34,6 +34,7 @@ export declare type Progress = {
34
34
  export declare type GradeAndProgress = {
35
35
  grade: Grade;
36
36
  progress: Progress;
37
+ name?: string;
37
38
  };
38
39
  export declare const getCurrentGrade: (services: {
39
40
  lrs: LrsGateway;
@@ -41,6 +42,7 @@ export declare const getCurrentGrade: (services: {
41
42
  }, registration: string, options?: {
42
43
  currentPreference?: "latest" | "oldest" | undefined;
43
44
  incompleteAttemptCallback?: ((info: ActivityState) => Promise<GradeAndProgress>) | undefined;
45
+ name?: string | undefined;
44
46
  scoreMaximum?: number | undefined;
45
47
  userId?: string | undefined;
46
48
  } | undefined) => Promise<GradeAndProgress | null>;
@@ -48,17 +48,20 @@ const getScoreGrade = (score, completed, userId, maxScore) => {
48
48
  };
49
49
  };
50
50
  exports.getScoreGrade = getScoreGrade;
51
- // These methods assigns 0's to incomplete activities
52
- const getCompletedActivityStateGradeAndProgress = (state, userId, maxScore) => {
51
+ // These methods assign 0's to incomplete activities
52
+ const getCompletedActivityStateGradeAndProgress = ({ name, scoreMaximum, state, userId }) => {
53
53
  var _a, _b;
54
54
  return ({
55
- grade: (0, exports.getScoreGrade)(((_b = (_a = state.currentAttemptCompleted) === null || _a === void 0 ? void 0 : _a.result) === null || _b === void 0 ? void 0 : _b.score) || {}, !!state.currentAttemptCompleted, userId, maxScore),
55
+ grade: (0, exports.getScoreGrade)(((_b = (_a = state.currentAttemptCompleted) === null || _a === void 0 ? void 0 : _a.result) === null || _b === void 0 ? void 0 : _b.score) || {}, !!state.currentAttemptCompleted, userId, scoreMaximum),
56
56
  progress: {
57
57
  scaled: state.currentAttemptCompleted ? 1 : 0,
58
58
  },
59
+ name,
59
60
  });
60
61
  };
61
- const getCompletedUserInfosGradeAndProgress = (infos, scoreMaximum) => infos.map(({ data, fullName, platformUserId }) => getCompletedActivityStateGradeAndProgress(data, platformUserId !== null && platformUserId !== void 0 ? platformUserId : fullName, scoreMaximum));
62
+ const getCompletedUserInfosGradeAndProgress = (infos, scoreMaximum) => infos.map(({ data, fullName, platformUserId }) => getCompletedActivityStateGradeAndProgress({
63
+ name: fullName, scoreMaximum, userId: platformUserId, state: data
64
+ }));
62
65
  const getCurrentGrade = async (services, registration, options) => {
63
66
  var _a;
64
67
  const user = await services.ltiAuthProvider.getUser();
@@ -66,14 +69,14 @@ const getCurrentGrade = async (services, registration, options) => {
66
69
  return null;
67
70
  }
68
71
  const userId = (_a = options === null || options === void 0 ? void 0 : options.userId) !== null && _a !== void 0 ? _a : user.uuid;
69
- const { currentPreference, incompleteAttemptCallback, scoreMaximum } = options !== null && options !== void 0 ? options : {};
72
+ const { currentPreference, incompleteAttemptCallback, name, scoreMaximum } = options !== null && options !== void 0 ? options : {};
70
73
  const infoPerUser = await (0, exports.getRegistrationAttemptInfo)(services.lrs, registration, { currentPreference });
71
74
  const userInfo = infoPerUser[user.uuid];
72
75
  if (!userInfo) {
73
- return getCompletedActivityStateGradeAndProgress((0, attempt_utils_1.resolveAttemptInfo)([]), userId, scoreMaximum);
76
+ return getCompletedActivityStateGradeAndProgress({ name, scoreMaximum, state: (0, attempt_utils_1.resolveAttemptInfo)([]), userId });
74
77
  }
75
78
  if (userInfo.currentAttemptCompleted || !incompleteAttemptCallback) {
76
- return getCompletedActivityStateGradeAndProgress(userInfo, userId, scoreMaximum);
79
+ return getCompletedActivityStateGradeAndProgress({ name, scoreMaximum, state: userInfo, userId });
77
80
  }
78
81
  return incompleteAttemptCallback(userInfo);
79
82
  };