jonbo / node-steam-webapi

Steam WebAPI wrapper for node.js
MIT License
48 stars 20 forks source link

Allow passing cached JSON with methods #9

Open seishun opened 9 years ago

seishun commented 9 years ago

Currently, you have to put all code that might need WebAPI into the callback to Steam.ready. This is inconvenient and not technically necessary, since existing methods don't change.

I propose to add an ability to pass prepared JSON from GetSupportedAPIList to the library, thereby avoiding the need to download it dynamically every time. For example, Steam.ready could accept the parsed JSON instead of the callback.

jonbo commented 9 years ago

Not a bad idea. However, there would then be two code-paths to go through; one if they didn't have a cache (like currently) and one if they did (so more work for the user). Also, wouldn't that be violating node's guideline of a method not being both async and sync? Granted, it would work alright if you didn't really mean not keeping everything in the callback to Steam.ready, which would merge back the code-paths. But, I think it would make more sense to just manage file caching entirely inside the library, maybe making the first argument of .ready an optional object with 'cacheFile' (and 'key') field(s). Thoughts?

seishun commented 9 years ago

That's the thing, having everything in the callback makes things more complicated. Consider, for example, that you also use a MongoDB connection. If you need both MongoDB and Web API for anything, you end up with a nested callback.

Actually, some time after posting this issue I decided to start working on a simpler Web API wrapper that doesn't generate methods and thus doesn't require the method descriptions, so this isn't that relevant to me anymore. But I'm glad that you found the suggestion interesting.

jonbo commented 9 years ago

Yeah, that's one of the major gripes about Node. But there are a lot of ways to mainly avoid the nested callback issue from async callbacks, even without libraries. (For example:)

// Some initial state
var steamReady=false, mongoReady=false;

// Initialize stuff (that's not guaranteed to finish in any particular order)
Steam.ready(function() {
  steamReady=true;
  main();
});
// Just psuedo code..
Mongo.ready(function() {
  mongoReady=true;
  main();
});

function main() {
  if ( !steamReady || !mongoReady) return; // Not ready yet 
  // rest of code that can know everything is initialized..
}

Ah, okay. Yeah, it's really just a simple library that can easily be rewritten, any number of ways. Happy coding.