Fixing issue with commit callback not being called for scheduled commits

This commit is contained in:
Jonathan Putney
2021-02-20 12:07:41 -05:00
parent 98ee351886
commit e52bf85903
11 changed files with 6283 additions and 19992 deletions
+21 -9
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+3 -3
View File
File diff suppressed because one or more lines are too long
-19969
View File
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "scorm-again", "name": "scorm-again",
"version": "1.5.0", "version": "1.5.1",
"description": "A modern SCORM JavaScript run-time library for AICC, SCORM 1.2, and SCORM 2004", "description": "A modern SCORM JavaScript run-time library for AICC, SCORM 1.2, and SCORM 2004",
"main": "dist/scorm-again.min.js", "main": "dist/scorm-again.min.js",
"directories": { "directories": {
+11 -5
View File
@@ -200,6 +200,7 @@ export default class BaseAPI {
* Sets the value of the CMIElement. * Sets the value of the CMIElement.
* *
* @param {string} callbackName * @param {string} callbackName
* @param {string} commitCallback
* @param {boolean} checkTerminated * @param {boolean} checkTerminated
* @param {string} CMIElement * @param {string} CMIElement
* @param {*} value * @param {*} value
@@ -207,6 +208,7 @@ export default class BaseAPI {
*/ */
setValue( setValue(
callbackName: String, callbackName: String,
commitCallback: String,
checkTerminated: boolean, checkTerminated: boolean,
CMIElement, CMIElement,
value) { value) {
@@ -244,7 +246,7 @@ export default class BaseAPI {
// schedule a commit, if autocommit is turned on // schedule a commit, if autocommit is turned on
if (String(this.lastErrorCode) === '0') { if (String(this.lastErrorCode) === '0') {
if (this.settings.autocommit && !this.#timeout) { if (this.settings.autocommit && !this.#timeout) {
this.scheduleCommit(this.settings.autocommitSeconds * 1000); this.scheduleCommit(this.settings.autocommitSeconds * 1000, commitCallback);
} }
} }
@@ -1178,9 +1180,10 @@ export default class BaseAPI {
* Throws a SCORM error * Throws a SCORM error
* *
* @param {number} when - the number of milliseconds to wait before committing * @param {number} when - the number of milliseconds to wait before committing
* @param {string} callback - the name of the commit event callback
*/ */
scheduleCommit(when: number) { scheduleCommit(when: number, callback: string) {
this.#timeout = new ScheduledCommit(this, when); this.#timeout = new ScheduledCommit(this, when, callback);
this.apiLog('scheduleCommit', '', 'scheduled', this.apiLog('scheduleCommit', '', 'scheduled',
global_constants.LOG_LEVEL_DEBUG); global_constants.LOG_LEVEL_DEBUG);
} }
@@ -1205,15 +1208,18 @@ class ScheduledCommit {
#API; #API;
#cancelled = false; #cancelled = false;
#timeout; #timeout;
#callback;
/** /**
* Constructor for ScheduledCommit * Constructor for ScheduledCommit
* @param {BaseAPI} API * @param {BaseAPI} API
* @param {number} when * @param {number} when
* @param {string} callback
*/ */
constructor(API: any, when: number) { constructor(API: any, when: number, callback: string) {
this.#API = API; this.#API = API;
this.#timeout = setTimeout(this.wrapper.bind(this), when); this.#timeout = setTimeout(this.wrapper.bind(this), when);
this.#callback = callback;
} }
/** /**
@@ -1231,7 +1237,7 @@ class ScheduledCommit {
*/ */
wrapper() { wrapper() {
if (!this.#cancelled) { if (!this.#cancelled) {
this.#API.commit(); this.#API.commit(this.#callback);
} }
} }
} }
+1 -1
View File
@@ -98,7 +98,7 @@ export default class Scorm12API extends BaseAPI {
* @return {string} * @return {string}
*/ */
lmsSetValue(CMIElement, value) { lmsSetValue(CMIElement, value) {
return this.setValue('LMSSetValue', false, CMIElement, value); return this.setValue('LMSSetValue', 'LMSCommit', false, CMIElement, value);
} }
/** /**
+1 -1
View File
@@ -124,7 +124,7 @@ export default class Scorm2004API extends BaseAPI {
* @return {string} * @return {string}
*/ */
lmsSetValue(CMIElement, value) { lmsSetValue(CMIElement, value) {
return this.setValue('SetValue', true, CMIElement, value); return this.setValue('SetValue', 'Commit', true, CMIElement, value);
} }
/** /**
+5 -1
View File
@@ -486,15 +486,18 @@ describe('SCORM 1.2 API Tests', () => {
const callback = sinon.spy(() => 1); const callback = sinon.spy(() => 1);
const callback2 = sinon.spy(() => 2); const callback2 = sinon.spy(() => 2);
const callback3 = sinon.spy(() => 3); const callback3 = sinon.spy(() => 3);
const callback4 = sinon.spy(() => 4);
scorm12API.on('CommitSuccess', callback); scorm12API.on('CommitSuccess', callback);
scorm12API.on('CommitSuccess', callback2); scorm12API.on('CommitSuccess', callback2);
scorm12API.on('LMSSetValue', callback3); scorm12API.on('LMSCommit', callback3);
scorm12API.on('LMSSetValue', callback4);
scorm12API.lmsSetValue('cmi.core.session_time', '00:01:00'); scorm12API.lmsSetValue('cmi.core.session_time', '00:01:00');
clock.tick(2000); clock.tick(2000);
expect(callback.calledOnce).to.be.true; expect(callback.calledOnce).to.be.true;
expect(callback2.calledOnce).to.be.true; expect(callback2.calledOnce).to.be.true;
expect(callback3.calledOnce).to.be.true; expect(callback3.calledOnce).to.be.true;
expect(callback4.calledOnce).to.be.true;
scorm12API.off('CommitSuccess', callback); scorm12API.off('CommitSuccess', callback);
@@ -503,6 +506,7 @@ describe('SCORM 1.2 API Tests', () => {
expect(callback.calledTwice).to.be.false; // removed callback should not be called a second time expect(callback.calledTwice).to.be.false; // removed callback should not be called a second time
expect(callback2.calledTwice).to.be.true; expect(callback2.calledTwice).to.be.true;
expect(callback3.calledTwice).to.be.true; expect(callback3.calledTwice).to.be.true;
expect(callback4.calledTwice).to.be.true;
}); });
it('Should handle CommitError event', it('Should handle CommitError event',
() => { () => {
+5 -1
View File
@@ -687,15 +687,18 @@ describe('SCORM 2004 API Tests', () => {
const callback = sinon.spy(() => 1); const callback = sinon.spy(() => 1);
const callback2 = sinon.spy(() => 2); const callback2 = sinon.spy(() => 2);
const callback3 = sinon.spy(() => 3); const callback3 = sinon.spy(() => 3);
const callback4 = sinon.spy(() => 4);
scorm2004API.on('CommitSuccess', callback); scorm2004API.on('CommitSuccess', callback);
scorm2004API.on('CommitSuccess', callback2); scorm2004API.on('CommitSuccess', callback2);
scorm2004API.on('SetValue', callback3); scorm2004API.on('Commit', callback3);
scorm2004API.on('SetValue', callback4);
scorm2004API.lmsSetValue('cmi.session_time', 'PT1M0S'); scorm2004API.lmsSetValue('cmi.session_time', 'PT1M0S');
clock.tick(2000); clock.tick(2000);
expect(callback.calledOnce).to.be.true; expect(callback.calledOnce).to.be.true;
expect(callback2.calledOnce).to.be.true; expect(callback2.calledOnce).to.be.true;
expect(callback3.calledOnce).to.be.true; expect(callback3.calledOnce).to.be.true;
expect(callback4.calledOnce).to.be.true;
scorm2004API.off('CommitSuccess', callback); scorm2004API.off('CommitSuccess', callback);
@@ -704,6 +707,7 @@ describe('SCORM 2004 API Tests', () => {
expect(callback.calledTwice).to.be.false; // removed callback should not be called a second time expect(callback.calledTwice).to.be.false; // removed callback should not be called a second time
expect(callback2.calledTwice).to.be.true; expect(callback2.calledTwice).to.be.true;
expect(callback3.calledTwice).to.be.true; expect(callback3.calledTwice).to.be.true;
expect(callback4.calledTwice).to.be.true;
}); });
it('Should handle CommitError event', it('Should handle CommitError event',
() => { () => {
+6234
View File
File diff suppressed because it is too large Load Diff