From a98115c647147098d06c9e2084e13cbfb23700a9 Mon Sep 17 00:00:00 2001 From: Daniel Asher Resnick Date: Sat, 4 Feb 2023 13:53:39 -0600 Subject: [PATCH] Use Fetch API and thenify BurningData --- src/public/js/burning-service.js | 116 +++++++++++++++---------------- 1 file changed, 55 insertions(+), 61 deletions(-) diff --git a/src/public/js/burning-service.js b/src/public/js/burning-service.js index 242aaab..fe7761b 100644 --- a/src/public/js/burning-service.js +++ b/src/public/js/burning-service.js @@ -1,4 +1,4 @@ - +var DEBUG = true; /**** Class Settings (Angular Service) ****/ function Settings() { this.enforceLifepathReqts = true; @@ -218,13 +218,14 @@ function CharacterStorageService($http) { /* Load character names from server */ this.loadCharacterNames = function(){ - $http.get("/list_chars/user1", {'timeout': 3000} ). - success(function(data,status,headers,config){ + fetch("/list_chars/user1") + .then((response) => response.json()) + .then((data) => { myself.characterIdAndNames = data; console.log("Loaded saved character names"); - }). - error(function(data,status,headers,config){ - console.log("Error: Loading saved character names from server failed: HTTP code " + status + ": " + data); + }) + .catch((error) => { + console.log("Error: Loading saved character names from server failed: "+error); }); } @@ -305,24 +306,25 @@ function BurningDataService($http) { // Log events this.registerEvent("stocksLoaded", function() { console.log("Stocks fetched from server: ", Object.keys(myself.stocks)) - // DEBUG: - // console.log("Stocks fetched from server: ", myself.stocks) + if(DEBUG) + console.log("Stocks fetched from server: ", myself.stocks) }); this.registerEvent("skillsLoaded", function() { console.log("Loaded " + Object.keys(myself.skills).length + " skills."); - // DEBUG: - // console.log("Loaded skills: ", myself.skills); + if(DEBUG) + console.log("Loaded skills: ", myself.skills); }); this.registerEvent("traitsLoaded", function() { console.log("Loaded " + Object.keys(myself.traits).length + " traits."); - // DEBUG: - // console.log("Loaded traits: ", myself.traits); + if(DEBUG) + console.log("Loaded traits: ", myself.traits); }); - $http.get("/stocks", {'timeout': 3000}). - success(function(data,status,headers,config){ - // DEBUG: - // console.log(data); + fetch("/stocks") + .then((response) => response.json()) + .then((data) => { + if(DEBUG) + console.log("Loaded stock data: "+data); myself.stocks = data; for (var stock of Object.keys(data)) { myself.startingStatPts[stock] = new StartingStatPoints(myself.stocks[stock].starting_stats); @@ -330,68 +332,60 @@ function BurningDataService($http) { myself.defineStockEvent(stock, "resourcesLoaded"); } myself.triggerEvent("stocksLoaded"); - }). - error(function(data,status,headers,config){ - console.log("Error: Getting stocks from server failed: HTTP code " + status + ": " + data); + }) + .catch((error) => { + console.log("Error: Getting stocks from server failed: "+error); + }); + + /* Load skills from server */ + fetch("/skills") + .then((response) => response.json()) + .then((data) => { + myself.skills = data; + myself.triggerEvent("skillsLoaded"); + }) + .catch((error) => { + console.log("Error: Getting skills from server failed: "+error); + }); + + /* Load traits from server */ + fetch("/traits") + .then((response) => response.json()) + .then((data) => { + myself.traits = data; + myself.triggerEvent("traitsLoaded"); + }) + .catch((error) => { + console.log("Error: Getting traits from server failed: "+error); }); /* Load lifepaths from server */ this.loadLifepathsForStock = function(stock){ - $http.get("/lifepaths/" + stock, {'timeout': 3000} ). - success(function(data,status,headers,config){ + fetch("/lifepaths/" + stock) + .then((response) => response.json()) + .then((data) => { myself.lifepaths[stock] = data; myself.triggerStockEvent(stock, "lifepathsLoaded"); console.log("Loaded "+stock+" lifepaths. " + Object.keys(myself.lifepaths[stock]).length + " settings"); - }). - error(function(data,status,headers,config){ - console.log("Error: Getting "+stock+" lifepaths from server failed: HTTP code " + status + ": " + data); + }) + .catch((error) => { + console.log("Error: Getting "+stock+" lifepaths from server failed: "+error); }); } /* Load resources from server */ this.loadResourcesForStock = function(stock){ - $http.get("/resources/" + stock, {'timeout': 3000} ). - success(function(data,status,headers,config){ + fetch("/resources/" + stock) + .then((response) => response.json()) + .then((data) => { myself.resources[stock] = data; myself.triggerStockEvent(stock, "resourcesLoaded"); console.log("Loaded "+stock+" resources. "); - }). - error(function(data,status,headers,config){ - console.log("Error: Getting "+stock+" stat points from server failed: HTTP code " + status + ": " + data); + }) + .catch((error) => { + console.log("Error: Getting "+stock+" stat points from server failed: "+error); }); } - - - /* Load skills from server */ - $http.get("/skills", {'timeout': 3000} ). - success(function(data,status,headers,config){ - myself.skills = data; - myself.triggerEvent("skillsLoaded"); - }). - error(function(data,status,headers,config){ - console.log("Error: Getting skills from server failed: HTTP code " + status + ": " + data); - }); - - /* Load traits from server */ - $http.get("/traits", {'timeout': 3000} ). - success(function(data,status,headers,config){ - myself.traits = data; - myself.triggerEvent("traitsLoaded"); - }). - error(function(data,status,headers,config){ - console.log("Error: Getting traits from server failed: HTTP code " + status + ": " + data); - }); - // DEBUG: - // setTimeout(() => testStockLoading(myself), (2 * 1000)); -} - -function testStockLoading(dataService) { - console.log(dataService); - for (var stock in dataService.stocks) { - console.log(stock); - dataService.loadLifepathsForStock(stock); - dataService.loadResourcesForStock(stock); - } } /**** End BurningDataService ****/