@patch-adams/core 1.4.26 → 1.4.28

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/dist/index.cjs CHANGED
@@ -4090,6 +4090,19 @@ function generateLrsBridgeCode(options) {
4090
4090
  // Setup connection
4091
4091
  var bridgeReady = setupBridge();
4092
4092
 
4093
+ // Track async readiness: both actor and doc metadata must resolve before sending statements
4094
+ var actorReady = false;
4095
+ var docReady = false;
4096
+ var launchEventsSent = false;
4097
+
4098
+ function tryLaunchEvents() {
4099
+ if (launchEventsSent) return;
4100
+ if (!actorReady || !docReady) return;
4101
+ launchEventsSent = true;
4102
+ log('Both actor and doc metadata ready, sending launch events');
4103
+ setTimeout(sendLaunchEvents, 50);
4104
+ }
4105
+
4093
4106
  if (bridgeReady) {
4094
4107
  LRS.initialized = true;
4095
4108
 
@@ -4100,14 +4113,23 @@ function generateLrsBridgeCode(options) {
4100
4113
  log('Actor:', LRS.actor);
4101
4114
  log('Course:', LRS.courseInfo);
4102
4115
 
4103
- // Then try async actor extraction from Bravais session
4104
- // This will update the actor with real user info if available
4116
+ // Then try async actor extraction (employee lookup, Bravais session)
4117
+ // Launch events will NOT be sent until this completes
4105
4118
  extractActorAsync(function(actor) {
4106
4119
  log('Actor updated after async fetch:', actor);
4120
+ actorReady = true;
4121
+ // Log final actor state for diagnostics
4122
+ if (window.console && window.console.info) {
4123
+ console.info('[PA-LRS] Actor resolved: name=' + (actor ? actor.name : 'none') +
4124
+ ', mbox=' + (actor ? actor.mbox : 'none') +
4125
+ ', account=' + (actor && actor.account ? actor.account.name : 'none'));
4126
+ }
4127
+ tryLaunchEvents();
4107
4128
  });
4108
4129
  } else {
4109
4130
  warn('Bridge setup failed - operating in offline mode');
4110
4131
  LRS.mode = 'offline';
4132
+ actorReady = true; // No actor to wait for in offline mode
4111
4133
  }
4112
4134
 
4113
4135
  // Always-visible bridge summary (not gated by DEBUG)
@@ -4152,13 +4174,17 @@ function generateLrsBridgeCode(options) {
4152
4174
  }
4153
4175
  }
4154
4176
 
4155
- function fetchDocDataAndSend(docId) {
4177
+ function onDocReady() {
4178
+ docReady = true;
4179
+ tryLaunchEvents();
4180
+ }
4181
+
4182
+ function fetchDocDataAndReady(docId) {
4156
4183
  fetchDocumentMetadata(docId, function(docData) {
4157
4184
  if (docData) {
4158
4185
  updateCourseInfoFromApi(docData);
4159
4186
  }
4160
- // Send launch events after API fetch (success or failure)
4161
- setTimeout(sendLaunchEvents, 100);
4187
+ onDocReady();
4162
4188
  });
4163
4189
  }
4164
4190
 
@@ -4178,20 +4204,20 @@ function generateLrsBridgeCode(options) {
4178
4204
  } else {
4179
4205
  warn('Could not fetch document data from shared API - statements may fail aggregation');
4180
4206
  }
4181
- setTimeout(sendLaunchEvents, 100);
4207
+ onDocReady();
4182
4208
  });
4183
4209
  } else if (documentId) {
4184
4210
  // No shared link token, try with extracted document ID (requires auth)
4185
4211
  log('No shared link token, fetching document data with ID:', documentId);
4186
- fetchDocDataAndSend(documentId);
4212
+ fetchDocDataAndReady(documentId);
4187
4213
  } else {
4188
4214
  // No identifiers available
4189
4215
  warn('No shared link token or document ID - statements may fail aggregation');
4190
- setTimeout(sendLaunchEvents, 500);
4216
+ onDocReady();
4191
4217
  }
4192
4218
  } else {
4193
- // Already have GUID - send after short delay
4194
- setTimeout(sendLaunchEvents, 500);
4219
+ // Already have GUID
4220
+ onDocReady();
4195
4221
  }
4196
4222
 
4197
4223
  // Setup beforeunload to send terminated
@@ -4221,6 +4247,32 @@ function generateLrsBridgeCode(options) {
4221
4247
  }, 1000);
4222
4248
  }
4223
4249
 
4250
+ // If actor is still Unknown Learner, poll for SCORM API availability
4251
+ // The SCORM API may not be available during <head> init but appears later
4252
+ // (e.g., scorm-calls.js finds and initializes it after DOM ready)
4253
+ if (!LRS.actor || LRS.actor.name === 'Unknown Learner' || !LRS.actor.account || LRS.actor.account.name === 'unknown') {
4254
+ var actorRetryCount = 0;
4255
+ var actorMaxRetries = 15;
4256
+ var actorRetryInterval = setInterval(function() {
4257
+ actorRetryCount++;
4258
+ // Check if SCORM API has become available (injected by scorm-calls.js or LMS)
4259
+ var hasScormApi = LRS.scormApi ||
4260
+ (typeof window.API !== 'undefined' && window.API) ||
4261
+ (typeof window.API_1484_11 !== 'undefined' && window.API_1484_11);
4262
+
4263
+ if (hasScormApi) {
4264
+ log('SCORM API now available (attempt ' + actorRetryCount + '), refreshing actor...');
4265
+ clearInterval(actorRetryInterval);
4266
+ LRS.refreshActor(function(actor) {
4267
+ log('Actor refreshed after SCORM API detected:', actor ? actor.name : 'none');
4268
+ });
4269
+ } else if (actorRetryCount >= actorMaxRetries) {
4270
+ log('SCORM API not found after ' + actorMaxRetries + ' attempts, actor stays as-is');
4271
+ clearInterval(actorRetryInterval);
4272
+ }
4273
+ }, 1000);
4274
+ }
4275
+
4224
4276
  log('LRS bridge setup complete');
4225
4277
  }
4226
4278