@instructure/quiz-interactions
Version:
A React UI component Library for quiz interaction types.
56 lines • 2.02 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import { searchForFormulaSolution } from './util';
import { loadMathjs } from '../common/util';
var GenerateSolutionsService = /*#__PURE__*/function () {
function GenerateSolutionsService(_ref) {
var onStart = _ref.onStart,
onSuccess = _ref.onSuccess,
onFailure = _ref.onFailure,
onCancel = _ref.onCancel;
_classCallCheck(this, GenerateSolutionsService);
this._onStart = onStart;
this._onSuccess = onSuccess;
this._onFailure = onFailure;
this._onCancel = onCancel;
this._promise = Promise.resolve();
}
return _createClass(GenerateSolutionsService, [{
key: "start",
value: function start(numSolutions, variables, formula, precision) {
var _this = this;
var scientificNotation = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
this._onStart();
var foundSolutions = [];
var promises = [];
for (var i = 0; i < numSolutions; i++) {
promises.push(loadMathjs().then(function () {
var solution = searchForFormulaSolution(variables, formula, foundSolutions, precision, scientificNotation);
if (solution !== null) {
foundSolutions.push(solution);
}
}));
}
var promise = Promise.all(promises).then(function () {
if (!_this.__promiseIsCurrent(promise)) {
return;
}
foundSolutions.length === numSolutions ? _this._onSuccess(foundSolutions) : _this._onFailure(foundSolutions);
});
this._promise = promise;
return promise;
}
}, {
key: "cancel",
value: function cancel() {
this._promise = Promise.resolve();
this._onCancel();
}
}, {
key: "__promiseIsCurrent",
value: function __promiseIsCurrent(promise) {
return promise === this._promise;
}
}]);
}();
export { GenerateSolutionsService as default };