refactor: Extact spell related code to helium

This commit is contained in:
1ambda 2017-02-07 11:07:28 +09:00
parent 3aa6c54735
commit 115191ee7e
4 changed files with 85 additions and 64 deletions

View file

@ -249,31 +249,28 @@ function ParagraphCtrl($scope, $rootScope, $route, $window, $routeParams, $locat
// remove leading spaces
const textWithoutMagic = splited[1].replace(/^\s+/g, '');
const spell = heliumService.getSpellByMagic(magic);
const spellResult = spell.interpret(textWithoutMagic);
const parsed = spellResult.getAllParsedDataWithTypes(
heliumService.getAllSpells(), magic, textWithoutMagic);
// handle actual result message in promise
parsed.then(resultsMsg => {
const status = 'FINISHED';
$scope.paragraph.status = status;
$scope.paragraph.results.code = status;
$scope.paragraph.results.msg = resultsMsg;
$scope.paragraph.config.tableHide = false;
if (digestRequired) { $scope.$digest(); }
heliumService.executeSpell(magic, textWithoutMagic)
.then(resultsMsg => {
const status = 'FINISHED';
$scope.paragraph.status = status;
$scope.paragraph.results.code = status;
$scope.paragraph.results.msg = resultsMsg;
$scope.paragraph.config.tableHide = false;
if (digestRequired) { $scope.$digest(); }
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,
digestRequired, propagated);
});
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,
digestRequired, propagated);
});
} catch (error) {
$scope.handleSpellError(paragraphText, error,
digestRequired, propagated);
@ -311,9 +308,8 @@ function ParagraphCtrl($scope, $rootScope, $route, $window, $routeParams, $locat
}
const magic = SpellResult.extractMagic(paragraphText);
const spell = heliumService.getSpellByMagic(magic);
if (spell) {
if (heliumService.getSpellByMagic(magic)) {
$scope.runParagraphUsingSpell(paragraphText, magic, digestRequired, propagated);
} else {
$scope.runParagraphUsingBackendInterpreter(paragraphText);

View file

@ -294,12 +294,7 @@ function ResultCtrl($scope, $rootScope, $route, $window, $routeParams, $location
renderApp(`p${appState.id}`, appState);
} else {
if (!DefaultDisplayType[type]) {
const spell = heliumService.getSpellByMagic(type);
if (!spell) {
console.error(`Can't execute spell due to unknown display type: ${type}`);
return;
}
$scope.renderCustomDisplay(type, data, spell);
$scope.renderCustomDisplay(type, data);
} else {
const targetElemId = $scope.createDisplayDOMId(`p${$scope.id}`, type);
$scope.renderDefaultDisplay(targetElemId, type, data, refresh);
@ -314,38 +309,40 @@ function ResultCtrl($scope, $rootScope, $route, $window, $routeParams, $location
/**
* Render multiple sub results for custom display
*/
$scope.renderCustomDisplay = function(type, data, spell) {
$scope.renderCustomDisplay = function(type, data) {
// get result from intp
const spellResult = spell.interpret(data.trim());
const parsed = spellResult.getAllParsedDataWithTypes(
heliumService.getAllSpells());
if (!heliumService.getSpellByMagic(type)) {
console.error(`Can't execute spell due to unknown display type: ${type}`);
return;
}
// custom display result can include multiple subset results
parsed.then(dataWithTypes => {
const containerDOMId = `p${$scope.id}_custom`;
const afterLoaded = () => {
const containerDOM = angular.element(`#${containerDOMId}`);
// Spell.interpret() can create multiple outputs
for(let i = 0; i < dataWithTypes.length; i++) {
const dt = dataWithTypes[i];
const data = dt.data;
const type = dt.type;
heliumService.executeSpellAsDisplaySystem(type, data)
.then(dataWithTypes => {
const containerDOMId = `p${$scope.id}_custom`;
const afterLoaded = () => {
const containerDOM = angular.element(`#${containerDOMId}`);
// Spell.interpret() can create multiple outputs
for(let i = 0; i < dataWithTypes.length; i++) {
const dt = dataWithTypes[i];
const data = dt.data;
const type = dt.type;
// prepare each DOM to be filled
const subResultDOMId = $scope.createDisplayDOMId(`p${$scope.id}_custom_${i}`, type);
const subResultDOM = document.createElement('div');
containerDOM.append(subResultDOM);
subResultDOM.setAttribute('id', subResultDOMId);
// prepare each DOM to be filled
const subResultDOMId = $scope.createDisplayDOMId(`p${$scope.id}_custom_${i}`, type);
const subResultDOM = document.createElement('div');
containerDOM.append(subResultDOM);
subResultDOM.setAttribute('id', subResultDOMId);
$scope.renderDefaultDisplay(subResultDOMId, type, data, true);
}
};
$scope.renderDefaultDisplay(subResultDOMId, type, data, true);
}
};
retryUntilElemIsLoaded(containerDOMId, afterLoaded);
}).catch(error => {
console.error(`Failed to render custom display: ${$scope.type}\n` + error);
});
retryUntilElemIsLoaded(containerDOMId, afterLoaded);
})
.catch(error => {
console.error(`Failed to render custom display: ${$scope.type}\n` + error);
});
};
/**

View file

@ -48,12 +48,28 @@ export function createDefaultPackages(pkgSearchResults, sce) {
return defaultPackages;
}
export function findPackageByMagic(pkgSearchResults, magic) {
return pkgSearchResults.find(psr => {
psr.pkg.type === HeliumType.SPELL && psr.pkg.spell.magic === magic;
});
/**
* @param defaultPackages {name, pkgSearchResult}
* @param magic
* @returns {pkgSearchResult}
*/
export function findPackageByMagic(defaultPackages, magic) {
for (let name in defaultPackages) {
const pkgSearchResult = defaultPackages[name];
if (pkgSearchResult.pkg.type === HeliumType.SPELL &&
pkgSearchResult.pkg.spell.magic === magic) {
return pkgSearchResult;
}
}
return undefined;
}
/**
* @param singlePkgSearchResults list of PkgSearchResult for a single package
* @param version
* @returns {T} found PkgSearchResult otherwise returns `undefined`
*/
export function findPackageByVersion(singlePkgSearchResults, version) {
return singlePkgSearchResults.find(psr => {
return psr.pkg.version === version;

View file

@ -69,10 +69,22 @@ export default function heliumService($http, $sce, baseUrlSrv) {
};
/**
* @returns {Object} map for `{ magic : spell }`
*/
this.getAllSpells = function() {
return spellPerMagic;
this.executeSpell = function(magic, textWithoutMagic) {
const spell = this.getSpellByMagic(magic);
const spellResult = spell.interpret(textWithoutMagic);
const parsed = spellResult.getAllParsedDataWithTypes(
spellPerMagic, magic, textWithoutMagic);
return parsed;
};
this.executeSpellAsDisplaySystem = function(type, data) {
const spell = this.getSpellByMagic(type);
const spellResult = spell.interpret(data.trim());
const parsed = spellResult.getAllParsedDataWithTypes(spellPerMagic);
return parsed;
};
this.getVisualizationBundles = function() {