Replace BurningData events with Promise handlers

pull/8/head
Daniel Asher Resnick 2 years ago
parent 0782b4465e
commit 30f477a2e0
  1. 72
      src/public/js/burning-service.js
  2. 26
      src/public/js/burning.js

@ -237,6 +237,10 @@ function CharacterStorageService($http) {
/**** Class BurningDataService (Angular Service) ****/ /**** Class BurningDataService (Angular Service) ****/
// This service is used to load the lifepaths, skills, traits, etc. from the server. // This service is used to load the lifepaths, skills, traits, etc. from the server.
function BurningDataService($http) { function BurningDataService($http) {
// Used to reference the object from within functions and callbacks
var myself = this;
/* JSON Data structure representing lifepaths. The structure is: /* JSON Data structure representing lifepaths. The structure is:
stock: stock:
setting_name: setting_name:
@ -264,43 +268,10 @@ function BurningDataService($http) {
// A hash of StartingStatPoints objects keyed by stock. // A hash of StartingStatPoints objects keyed by stock.
this.startingStatPts = {}; this.startingStatPts = {};
this.events = { /* Loading of stocks, skills, and traits begins on initializing the service
stocksLoaded: { triggered : false, callbacks : [] },
traitsLoaded: { triggered : false, callbacks : [] },
skillsLoaded: { triggered : false, callbacks : [] },
}
this.registerEvent = function(eventName, callback){ /* Load stocks from server */
if(this.events[eventName].triggered) this.whenStocksLoaded = fetch("/stocks")
callback();
this.events[eventName].callbacks.push(callback);
}
this.triggerEvent = function(eventName) {
myself.events[eventName].triggered = true;
for(var callback of myself.events[eventName].callbacks) {
callback();
}
}
this.stockEvents = {};
this.defineStockEvent = function(stockName, eventName) {
if(!(stockName in myself.stockEvents)) myself.stockEvents[stockName] = {};
myself.stockEvents[stockName][eventName] = { triggered : false, callbacks : [] } ;
}
this.registerStockEvent = function(stockName, eventName, callback){
if(myself.stockEvents[stockName][eventName].triggered)
callback();
myself.stockEvents[stockName][eventName].callbacks.push(callback);
}
this.triggerStockEvent = function(stockName, eventName) {
myself.stockEvents[stockName][eventName].triggered = true;
for(var callback of myself.stockEvents[stockName][eventName].callbacks) {
callback();
}
}
fetch("/stocks")
.then((response) => response.json()) .then((response) => response.json())
.then((data) => { .then((data) => {
if(DEBUG) if(DEBUG)
@ -308,64 +279,67 @@ function BurningDataService($http) {
myself.stocks = data; myself.stocks = data;
for (var stock of Object.keys(data)) { for (var stock of Object.keys(data)) {
myself.startingStatPts[stock] = new StartingStatPoints(myself.stocks[stock].starting_stats); myself.startingStatPts[stock] = new StartingStatPoints(myself.stocks[stock].starting_stats);
myself.defineStockEvent(stock, "lifepathsLoaded");
myself.defineStockEvent(stock, "resourcesLoaded");
} }
myself.triggerEvent("stocksLoaded");
}) })
.catch((error) => { .catch((error) => {
console.log("Error: Getting stocks from server failed: "+error); console.log("Error: Getting stocks from server failed: "+error);
}); });
/* Load skills from server */ /* Load skills from server */
fetch("/skills") this.whenSkillsLoaded = fetch("/skills")
.then((response) => response.json()) .then((response) => response.json())
.then((data) => { .then((data) => {
myself.skills = data; myself.skills = data;
myself.triggerEvent("skillsLoaded"); if(DEBUG)
console.log("Loaded skill data: "+data);
}) })
.catch((error) => { .catch((error) => {
console.log("Error: Getting skills from server failed: "+error); console.log("Error: Getting skills from server failed: "+error);
}); });
/* Load traits from server */ /* Load traits from server */
fetch("/traits") this.whenTraitsLoaded = fetch("/traits")
.then((response) => response.json()) .then((response) => response.json())
.then((data) => { .then((data) => {
myself.traits = data; myself.traits = data;
myself.triggerEvent("traitsLoaded"); if(DEBUG)
console.log("Loaded trait data: "+data);
}) })
.catch((error) => { .catch((error) => {
console.log("Error: Getting traits from server failed: "+error); console.log("Error: Getting traits from server failed: "+error);
}); });
/* Lifepaths and resources defer until their stock is selected */
this.whenLifePathsLoadedForStock = {};
/* Load lifepaths from server */ /* Load lifepaths from server */
this.loadLifepathsForStock = function(stock){ this.loadLifepathsForStock = function(stock){
fetch("/lifepaths/" + stock) return myself.whenLifePathsLoadedForStock[stock] = fetch("/lifepaths/" + stock)
.then((response) => response.json()) .then((response) => response.json())
.then((data) => { .then((data) => {
myself.lifepaths[stock] = data; myself.lifepaths[stock] = data;
myself.triggerStockEvent(stock, "lifepathsLoaded"); if(DEBUG)
console.log("Loaded "+stock+" lifepaths. " + Object.keys(myself.lifepaths[stock]).length + " settings"); console.log("Loaded "+stock+" lifepaths. " + Object.keys(myself.lifepaths[stock]).length + " settings");
}) })
.catch((error) => { .catch((error) => {
console.log("Error: Getting "+stock+" lifepaths from server failed: "+error); console.log("Error: Getting "+stock+" lifepaths from server failed: "+error);
}); });
} };
/* Load resources from server */ /* Load resources from server */
this.whenResourcesLoadedForStock = {};
this.loadResourcesForStock = function(stock){ this.loadResourcesForStock = function(stock){
fetch("/resources/" + stock) return myself.whenResourcesLoadedForStock[stock] = fetch("/resources/" + stock)
.then((response) => response.json()) .then((response) => response.json())
.then((data) => { .then((data) => {
myself.resources[stock] = data; myself.resources[stock] = data;
myself.triggerStockEvent(stock, "resourcesLoaded"); if(DEBUG)
console.log("Loaded "+stock+" resources. "); console.log("Loaded "+stock+" resources. ");
}) })
.catch((error) => { .catch((error) => {
console.log("Error: Getting "+stock+" stat points from server failed: "+error); console.log("Error: Getting "+stock+" stat points from server failed: "+error);
}); });
} };
} }
/**** End BurningDataService ****/ /**** End BurningDataService ****/

@ -349,7 +349,6 @@ function BurningCtrl($scope, $http, $modal, $timeout, settings, appropriateWeapo
return result; return result;
} }
$scope.onGenderChange = function(){ $scope.onGenderChange = function(){
if ($scope.name.length == 0) { if ($scope.name.length == 0) {
$scope.generateName(); $scope.generateName();
@ -359,19 +358,21 @@ function BurningCtrl($scope, $http, $modal, $timeout, settings, appropriateWeapo
$scope.onStockChange = function(){ $scope.onStockChange = function(){
if(!$scope.stock) return; if(!$scope.stock) return;
if(!$scope.stockSelected) {
if(!$scope.stockSelected) { // Removes 'select a stock' after the first selection
$scope.stocks.shift(); $scope.stocks.shift();
$scope.stockSelected = true; $scope.stockSelected = true;
} }
var oldName = $scope.name;
if(!burningData.lifepaths[$scope.stock]) { let loadPromises = [];
burningData.loadLifepathsForStock($scope.stock);
}
if(!burningData.resources[$scope.stock]) { if(!burningData.resources[$scope.stock]) {
burningData.loadResourcesForStock($scope.stock); loadPromises.push(burningData.loadResourcesForStock($scope.stock));
} }
// TODO: technically a bug — only want this registered once per stock... if(!burningData.lifepaths[$scope.stock]) {
burningData.registerStockEvent($scope.stock, "lifepathsLoaded", function () { loadPromises.push(burningData.loadLifepathsForStock($scope.stock));
}
Promise.all(loadPromises).then(() => {
var oldName = $scope.name;
// Make a blank character sheet // Make a blank character sheet
$scope.initialize($scope.stock); $scope.initialize($scope.stock);
@ -383,6 +384,8 @@ function BurningCtrl($scope, $http, $modal, $timeout, settings, appropriateWeapo
calculateSettingNames($scope, burningData); calculateSettingNames($scope, burningData);
calculateCurrentSettingLifepathNames($scope, burningData); calculateCurrentSettingLifepathNames($scope, burningData);
calculateSpecialTraitsForDisplay($scope, burningData); calculateSpecialTraitsForDisplay($scope, burningData);
calculateGearSelectionLists($scope, burningData);
calculatePropertySelectionLists($scope, burningData);
}); });
} }
@ -409,10 +412,11 @@ function BurningCtrl($scope, $http, $modal, $timeout, settings, appropriateWeapo
calculateUnspentSkillPoints($scope); calculateUnspentSkillPoints($scope);
} }
burningData.registerEvent("stocksLoaded", function() { burningData.whenStocksLoaded.then(() => {
$scope.stocks = [{ name: "Select a stock" }] $scope.stocks = [{ name: "Select a stock" }];
$scope.stocks = $scope.stocks.concat(Object.values(burningData.stocks)); $scope.stocks = $scope.stocks.concat(Object.values(burningData.stocks));
$scope.stockSelected = false; $scope.stockSelected = false;
$scope.$digest();
}); });
$scope.$on('$locationChangeStart', function(event, nextUrl, currentUrl) { $scope.$on('$locationChangeStart', function(event, nextUrl, currentUrl) {

Loading…
Cancel
Save