feat: Make spell execution transactional

This commit is contained in:
1ambda 2017-02-16 18:57:43 +09:00
parent d9e87a8650
commit c9b0145d27
3 changed files with 68 additions and 12 deletions

View file

@ -32,6 +32,12 @@ function ParagraphCtrl($scope, $rootScope, $route, $window, $routeParams, $locat
$scope.originalText = '';
$scope.editor = null;
// transactional info for spell execution
$scope.spellTransaction = {
totalResultCount: 0, renderedResultCount: 0,
propagated: false, resultsMsg: [], paragraphText: '',
};
var editorSetting = {};
// flag that is used to set editor setting on paste percent sign
var pastePercentSign = false;
@ -236,6 +242,45 @@ function ParagraphCtrl($scope, $rootScope, $route, $window, $routeParams, $locat
}
};
$scope.prepareSpellTransaction = function(resultsMsg, propagated, paragraphText) {
$scope.spellTransaction.totalResultCount = resultsMsg.length;
$scope.spellTransaction.renderedResultCount = 0;
$scope.spellTransaction.propagated = propagated;
$scope.spellTransaction.resultsMsg = resultsMsg;
$scope.spellTransaction.paragraphText = paragraphText;
};
/**
* - update spell transaction count and
* - check transaction is finished based on the result count
* @returns {boolean}
*/
$scope.increaseSpellTransactionResultCount = function() {
$scope.spellTransaction.renderedResultCount += 1;
const total = $scope.spellTransaction.totalResultCount;
const current = $scope.spellTransaction.renderedResultCount;
return total === current;
};
$scope.cleanupSpellTransaction = function() {
const status = 'FINISHED';
$scope.paragraph.status = status;
$scope.paragraph.results.code = status;
const propagated = $scope.spellTransaction.propagated;
const resultsMsg = $scope.spellTransaction.resultsMsg;
const paragraphText = $scope.spellTransaction.paragraphText;
if (!propagated) {
const propagable = SpellResult.createPropagable(resultsMsg);
$scope.propagateSpellResult(
$scope.paragraph.id, $scope.paragraph.title,
paragraphText, propagable, status, '',
$scope.paragraph.config, $scope.paragraph.settings.params);
}
};
$scope.runParagraphUsingSpell = function(paragraphText,
magic, digestRequired, propagated) {
$scope.paragraph.results = {};
@ -252,19 +297,11 @@ function ParagraphCtrl($scope, $rootScope, $route, $window, $routeParams, $locat
// handle actual result message in promise
heliumService.executeSpell(magic, textWithoutMagic)
.then(resultsMsg => {
const status = 'FINISHED';
$scope.paragraph.status = status;
$scope.paragraph.results.code = status;
$scope.prepareSpellTransaction(resultsMsg, propagated, paragraphText);
$scope.paragraph.results.msg = resultsMsg;
$scope.paragraph.config.tableHide = false;
if (!propagated) {
const propagable = SpellResult.createPropagable(resultsMsg);
$scope.propagateSpellResult(
$scope.paragraph.id, $scope.paragraph.title,
paragraphText, propagable, status, '',
$scope.paragraph.config, $scope.paragraph.settings.params);
}
})
.catch(error => {
$scope.handleSpellError(paragraphText, error,
@ -1153,6 +1190,8 @@ function ParagraphCtrl($scope, $rootScope, $route, $window, $routeParams, $locat
}
};
/** $scope.$on */
$scope.$on('runParagraphUsingSpell', function(event, data) {
const oldPara = $scope.paragraph;
let newPara = data.paragraph;
@ -1337,4 +1376,17 @@ function ParagraphCtrl($scope, $rootScope, $route, $window, $routeParams, $locat
$scope.$on('closeTable', function(event) {
$scope.closeTable($scope.paragraph);
});
$scope.$on('resultRendered', function(event, paragraphId) {
if ($scope.paragraph.id !== paragraphId) {
return;
}
/** increase spell result count and return if not finished */
if (!$scope.increaseSpellTransactionResultCount()) {
return;
}
$scope.cleanupSpellTransaction();
});
}

View file

@ -279,6 +279,10 @@ function ResultCtrl($scope, $rootScope, $route, $window, $routeParams, $location
} else {
console.error(`Unknown Display Type: ${type}`);
}
// send message to parent that this result is rendered
const paragraphId = $scope.$parent.paragraph.id;
$scope.$emit('resultRendered', paragraphId);
};
const renderResult = function(type, refresh) {

View file

@ -32,13 +32,13 @@ export default function heliumService($http, $sce, baseUrlSrv) {
url = url + '?refresh=true';
}
let visualizationBundles = [];
// name `heliumBundles` should be same as `HelumBundleFactory.HELIUM_BUNDLES_VAR`
var heliumBundles = [];
let heliumBundles = [];
// map for `{ magic: interpreter }`
let spellPerMagic = {};
// map for `{ magic: package-name }`
let pkgNamePerMagic = {}
let visualizationBundles = [];
// load should be promise
this.load = $http.get(url).success(function(response) {