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