UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

77 lines (76 loc) 3.1 kB
function _class_call_check(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _create_class(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } import { searchForFormulaSolution } from './util'; import { loadMathjs } from '../common/util'; var GenerateSolutionsService = /*#__PURE__*/ function() { "use strict"; function GenerateSolutionsService(param) { var onStart = param.onStart, onSuccess = param.onSuccess, onFailure = param.onFailure, onCancel = param.onCancel; _class_call_check(this, GenerateSolutionsService); this._onStart = onStart; this._onSuccess = onSuccess; this._onFailure = onFailure; this._onCancel = onCancel; this._promise = Promise.resolve(); } _create_class(GenerateSolutionsService, [ { key: "start", value: function start(numSolutions, variables, formula, precision) { var _this = this; var scientificNotation = arguments.length > 4 && arguments[4] !== void 0 ? 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; } } ]); return GenerateSolutionsService; }(); export { GenerateSolutionsService as default };