funtionsHandler.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. module.exports = class ClassName {
  2. constructor() {
  3. this.fun = {
  4. defaultHandler: function defaultHandler() {
  5. },
  6. query: {},
  7. game:{}
  8. };
  9. }
  10. registerDefault(fun){
  11. if(typeof(fun) !== "function"){
  12. throw new TypeError("First argument must be a function");
  13. }
  14. this.fun.defaultHandler = fun;
  15. }
  16. registerQuery(query, fun){
  17. if(typeof(query) !== "string"){
  18. throw new TypeError("First argument must be a string");
  19. }
  20. if(typeof(fun) !== "function"){
  21. throw new TypeError("Second argument must be a function");
  22. }
  23. this.fun.query[query] = fun;
  24. }
  25. registerGame(game, fun){
  26. if(typeof(game) !== "string"){
  27. throw new TypeError("First argument must be a string");
  28. }
  29. if(typeof(fun) !== "function"){
  30. throw new TypeError("Second argument must be a function");
  31. }
  32. this.fun.query[game] = fun;
  33. }
  34. getHandler(game, query){
  35. if(this.fun.game[game]){
  36. return this.fun.game[game];
  37. }else if(this.fun.query[query]){
  38. return this.fun.query[query];
  39. }else{
  40. return this.fun.defaultHandler;
  41. }
  42. }
  43. };