Skip to content

Commit

Permalink
Merge pull request #26 from xob/fix_reset
Browse files Browse the repository at this point in the history
Change the way of bringing the API back to its original state
  • Loading branch information
xob authored Apr 12, 2020
2 parents 42ccaf2 + a90fb1b commit 3ec9b7f
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 71 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## 2.0.0

### Added

- This CHANGELOG file to document changes to the project
- The `reset` method should now be used instead of the previous
`replaceWithAnotherScormAPI` method. It resets the API to its original state
cleanly, instead of having to create a whole new API.

### Removed

- The `replaceWithAnotherScormAPI` method was removed. It caused issues with
SCOs that keep a pointer to the original API after replacing it a few times.
36 changes: 10 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,24 +213,16 @@ window.API.apiLogLevel = 5; // No logging

## Resetting

There may come a time when you need to reset the SCORM 1.2 API. Two methods are available.

You can create a new API and replace the one available on `window.API`:

```javascript
window.API = new window.simplifyScorm.ScormAPI();
```

You can also create a new API and ask the existing one to use the new API instead.
There may come a time when you need to reset the SCORM 1.2 API.
This is useful when a SCORM object allows retrying after a failure, and you want to track each attempt in a separate CMI object.
SCORM objects often keep a reference to the original API, which is why this manipulation is needed:
SCORM objects often keep a reference to the original API, which is why we recommend resetting the API instead of creating a new one:

```javascript
var newAPI = new window.simplifyScorm.ScormAPI();
window.API.replaceWithAnotherScormAPI(newAPI);
window.API = newAPI;
window.API.reset();
```

Resetting the API brings it back to its original state, including a brand new, untouched CMI object.

# SCORM 2004

Simplify Scorm will create a `window.API_1484_11` object, required by SCORM 2004, and will handle all the scorm interactions. Everything will be recorded on the object at `window.API_1484_11.cmi`.
Expand Down Expand Up @@ -423,20 +415,12 @@ window.API_1484_11.apiLogLevel = 5; // No logging

## Resetting

There may come a time when you need to reset the SCORM 2004 API. Two methods are available.

You can create a new API and replace the one available on `window.API_1484_11`:

```javascript
window.API_1484_11 = new window.simplifyScorm.ScormAPI2004();
```

You can also create a new API and ask the existing one to use the new API instead.
There may come a time when you need to reset the SCORM 2004 API.
This is useful when a SCORM object allows retrying after a failure, and you want to track each attempt in a separate CMI object.
SCORM objects often keep a reference to the original API, which is why this manipulation is needed:
SCORM objects often keep a reference to the original API, which is why we recommend resetting the API instead of creating a new one:

```javascript
var newAPI = new window.simplifyScorm.ScormAPI2004();
window.API_1484_11.replaceWithAnotherScormAPI(newAPI);
window.API_1484_11 = newAPI;
window.API_1484_11.reset();
```

Resetting the API brings it back to its original state, including a brand new, untouched CMI object.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simplify-scorm",
"version": "1.1.1",
"version": "2.0.0",
"description": "",
"license": "MIT",
"private": true,
Expand Down
14 changes: 14 additions & 0 deletions src/baseAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
_self.processListeners = processListeners;
_self.throwSCORMError = throwSCORMError;
}
BaseAPI.reset = reset;

/**
* Logging for all SCORM actions
Expand Down Expand Up @@ -178,6 +179,19 @@
}
}

/**
* Reset the API to its initial state
*/
function reset() {
// Internal State
this.currentState = constants.STATE_NOT_INITIALIZED;
this.lastErrorCode = 0;

// Utility Functions
this.apiLogLevel = constants.LOG_LEVEL_ERROR;
this.listenerArray = [];
}

/**
* Throws a SCORM error
*
Expand Down
27 changes: 5 additions & 22 deletions src/scormAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
_self.checkState = checkState;
_self.getLmsErrorMessageDetails = getLmsErrorMessageDetails;
_self.loadFromJSON = loadFromJSON;
_self.replaceWithAnotherScormAPI = replaceWithAnotherScormAPI;
_self.reset = reset;

/**
* @returns {string} bool
Expand Down Expand Up @@ -412,30 +412,13 @@
}

/**
* Replace the whole API with another
* Reset the API to its initial state
*/
function replaceWithAnotherScormAPI(newAPI) {
// API Signature
_self.LMSInitialize = newAPI.LMSInitialize;
_self.LMSFinish = newAPI.LMSFinish;
_self.LMSGetValue = newAPI.LMSGetValue;
_self.LMSSetValue = newAPI.LMSSetValue;
_self.LMSCommit = newAPI.LMSCommit;
_self.LMSGetLastError = newAPI.LMSGetLastError;
_self.LMSGetErrorString = newAPI.LMSGetErrorString;
_self.LMSGetDiagnostic = newAPI.LMSGetDiagnostic;
function reset() {
BaseAPI.reset.call(_self);

// Data Model
_self.cmi = newAPI.cmi;

// Utility Functions
_self.checkState = newAPI.checkState;
_self.getLmsErrorMessageDetails = newAPI.getLmsErrorMessageDetails;
_self.loadFromJSON = newAPI.loadFromJSON;
_self.replaceWithAnotherScormAPI = newAPI.replaceWithAnotherScormAPI;

// API itself
_self = newAPI; // eslint-disable-line consistent-this
_self.cmi = new CMI(_self);
}

return _self;
Expand Down
28 changes: 6 additions & 22 deletions src/scormAPI2004.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
// Utility Functions
_self.getLmsErrorMessageDetails = getLmsErrorMessageDetails;
_self.loadFromJSON = loadFromJSON;
_self.replaceWithAnotherScormAPI = replaceWithAnotherScormAPI;
_self.reset = reset;

/**
* @param Empty String
Expand Down Expand Up @@ -511,30 +511,14 @@
}

/**
* Replace the whole API with another
* Reset the API to its initial state
*/
function replaceWithAnotherScormAPI(newAPI) {
// API Signature
_self.Initialize = newAPI.Initialize;
_self.Terminate = newAPI.Terminate;
_self.GetValue = newAPI.GetValue;
_self.SetValue = newAPI.SetValue;
_self.Commit = newAPI.Commit;
_self.GetLastError = newAPI.GetLastError;
_self.GetErrorString = newAPI.GetErrorString;
_self.GetDiagnostic = newAPI.GetDiagnostic;
function reset() {
BaseAPI.reset.call(_self);

// Data Model
_self.cmi = newAPI.cmi;
_self.adl = newAPI.adl;

// Utility Functions
_self.getLmsErrorMessageDetails = newAPI.getLmsErrorMessageDetails;
_self.loadFromJSON = newAPI.loadFromJSON;
_self.replaceWithAnotherScormAPI = newAPI.replaceWithAnotherScormAPI;

// API itself
_self = newAPI; // eslint-disable-line consistent-this
_self.cmi = new CMI(_self);
_self.adl = new ADL(_self);
}

return _self;
Expand Down

0 comments on commit 3ec9b7f

Please sign in to comment.