Skip to content

Commit

Permalink
Fixing issue with commit callback not being called for scheduled commits
Browse files Browse the repository at this point in the history
  • Loading branch information
jcputney committed Feb 20, 2021
1 parent 98ee351 commit e52bf85
Show file tree
Hide file tree
Showing 11 changed files with 6,283 additions and 19,992 deletions.
30 changes: 21 additions & 9 deletions dist/scorm-again.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/scorm-again.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/scorm-again.min.js

Large diffs are not rendered by default.

19,969 changes: 0 additions & 19,969 deletions package-lock.json

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"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",
"main": "dist/scorm-again.min.js",
"directories": {
Expand Down
16 changes: 11 additions & 5 deletions src/BaseAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,15 @@ export default class BaseAPI {
* Sets the value of the CMIElement.
*
* @param {string} callbackName
* @param {string} commitCallback
* @param {boolean} checkTerminated
* @param {string} CMIElement
* @param {*} value
* @return {string}
*/
setValue(
callbackName: String,
commitCallback: String,
checkTerminated: boolean,
CMIElement,
value) {
Expand Down Expand Up @@ -244,7 +246,7 @@ export default class BaseAPI {
// schedule a commit, if autocommit is turned on
if (String(this.lastErrorCode) === '0') {
if (this.settings.autocommit && !this.#timeout) {
this.scheduleCommit(this.settings.autocommitSeconds * 1000);
this.scheduleCommit(this.settings.autocommitSeconds * 1000, commitCallback);
}
}

Expand Down Expand Up @@ -1178,9 +1180,10 @@ export default class BaseAPI {
* Throws a SCORM error
*
* @param {number} when - the number of milliseconds to wait before committing
* @param {string} callback - the name of the commit event callback
*/
scheduleCommit(when: number) {
this.#timeout = new ScheduledCommit(this, when);
scheduleCommit(when: number, callback: string) {
this.#timeout = new ScheduledCommit(this, when, callback);
this.apiLog('scheduleCommit', '', 'scheduled',
global_constants.LOG_LEVEL_DEBUG);
}
Expand All @@ -1205,15 +1208,18 @@ class ScheduledCommit {
#API;
#cancelled = false;
#timeout;
#callback;

/**
* Constructor for ScheduledCommit
* @param {BaseAPI} API
* @param {number} when
* @param {string} callback
*/
constructor(API: any, when: number) {
constructor(API: any, when: number, callback: string) {
this.#API = API;
this.#timeout = setTimeout(this.wrapper.bind(this), when);
this.#callback = callback;
}

/**
Expand All @@ -1231,7 +1237,7 @@ class ScheduledCommit {
*/
wrapper() {
if (!this.#cancelled) {
this.#API.commit();
this.#API.commit(this.#callback);
}
}
}
2 changes: 1 addition & 1 deletion src/Scorm12API.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export default class Scorm12API extends BaseAPI {
* @return {string}
*/
lmsSetValue(CMIElement, value) {
return this.setValue('LMSSetValue', false, CMIElement, value);
return this.setValue('LMSSetValue', 'LMSCommit', false, CMIElement, value);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Scorm2004API.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export default class Scorm2004API extends BaseAPI {
* @return {string}
*/
lmsSetValue(CMIElement, value) {
return this.setValue('SetValue', true, CMIElement, value);
return this.setValue('SetValue', 'Commit', true, CMIElement, value);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion test/Scorm12API.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,15 +486,18 @@ describe('SCORM 1.2 API Tests', () => {
const callback = sinon.spy(() => 1);
const callback2 = sinon.spy(() => 2);
const callback3 = sinon.spy(() => 3);
const callback4 = sinon.spy(() => 4);
scorm12API.on('CommitSuccess', callback);
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');
clock.tick(2000);
expect(callback.calledOnce).to.be.true;
expect(callback2.calledOnce).to.be.true;
expect(callback3.calledOnce).to.be.true;
expect(callback4.calledOnce).to.be.true;

scorm12API.off('CommitSuccess', callback);

Expand All @@ -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(callback2.calledTwice).to.be.true;
expect(callback3.calledTwice).to.be.true;
expect(callback4.calledTwice).to.be.true;
});
it('Should handle CommitError event',
() => {
Expand Down
6 changes: 5 additions & 1 deletion test/Scorm2004API.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,15 +687,18 @@ describe('SCORM 2004 API Tests', () => {
const callback = sinon.spy(() => 1);
const callback2 = sinon.spy(() => 2);
const callback3 = sinon.spy(() => 3);
const callback4 = sinon.spy(() => 4);
scorm2004API.on('CommitSuccess', callback);
scorm2004API.on('CommitSuccess', callback2);
scorm2004API.on('SetValue', callback3);
scorm2004API.on('Commit', callback3);
scorm2004API.on('SetValue', callback4);

scorm2004API.lmsSetValue('cmi.session_time', 'PT1M0S');
clock.tick(2000);
expect(callback.calledOnce).to.be.true;
expect(callback2.calledOnce).to.be.true;
expect(callback3.calledOnce).to.be.true;
expect(callback4.calledOnce).to.be.true;

scorm2004API.off('CommitSuccess', callback);

Expand All @@ -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(callback2.calledTwice).to.be.true;
expect(callback3.calledTwice).to.be.true;
expect(callback4.calledTwice).to.be.true;
});
it('Should handle CommitError event',
() => {
Expand Down
Loading

0 comments on commit e52bf85

Please sign in to comment.