Is there a way to set an event on all class/object functions where a listener will receive function name and...












1















Is there a way to set an event on all class/object functions where a listener will receive function name and parameters?



I want to be able to run some functionality anytime a function is called that is part of that regular object or class.



The purpose of this, and I may be going down a complicated path, is to assign object level caching for a function. So say I have a service function that calls a nodejs sequelize method, I'd like to cache it for x minutes, for example. The listener above would therefore write to redis with a ttl and check redis anytime the function is called.



I did this with Scala with no boilerplate code using annotations to denote ttl for any method and macros for that boring set and get to redis. Not sure how to accomplish that using javascript / nodejs.



Sorry in advance if this is an inappropriate stack overflow question, but any general direction would help.










share|improve this question























  • Calling a function doesn't trigger an event. You can monkey-patch the function to do what you want.

    – Barmar
    Nov 20 '18 at 1:12
















1















Is there a way to set an event on all class/object functions where a listener will receive function name and parameters?



I want to be able to run some functionality anytime a function is called that is part of that regular object or class.



The purpose of this, and I may be going down a complicated path, is to assign object level caching for a function. So say I have a service function that calls a nodejs sequelize method, I'd like to cache it for x minutes, for example. The listener above would therefore write to redis with a ttl and check redis anytime the function is called.



I did this with Scala with no boilerplate code using annotations to denote ttl for any method and macros for that boring set and get to redis. Not sure how to accomplish that using javascript / nodejs.



Sorry in advance if this is an inappropriate stack overflow question, but any general direction would help.










share|improve this question























  • Calling a function doesn't trigger an event. You can monkey-patch the function to do what you want.

    – Barmar
    Nov 20 '18 at 1:12














1












1








1








Is there a way to set an event on all class/object functions where a listener will receive function name and parameters?



I want to be able to run some functionality anytime a function is called that is part of that regular object or class.



The purpose of this, and I may be going down a complicated path, is to assign object level caching for a function. So say I have a service function that calls a nodejs sequelize method, I'd like to cache it for x minutes, for example. The listener above would therefore write to redis with a ttl and check redis anytime the function is called.



I did this with Scala with no boilerplate code using annotations to denote ttl for any method and macros for that boring set and get to redis. Not sure how to accomplish that using javascript / nodejs.



Sorry in advance if this is an inappropriate stack overflow question, but any general direction would help.










share|improve this question














Is there a way to set an event on all class/object functions where a listener will receive function name and parameters?



I want to be able to run some functionality anytime a function is called that is part of that regular object or class.



The purpose of this, and I may be going down a complicated path, is to assign object level caching for a function. So say I have a service function that calls a nodejs sequelize method, I'd like to cache it for x minutes, for example. The listener above would therefore write to redis with a ttl and check redis anytime the function is called.



I did this with Scala with no boilerplate code using annotations to denote ttl for any method and macros for that boring set and get to redis. Not sure how to accomplish that using javascript / nodejs.



Sorry in advance if this is an inappropriate stack overflow question, but any general direction would help.







javascript node.js javascript-events event-handling






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 1:09









NiceOneMoneyNiceOneMoney

657




657













  • Calling a function doesn't trigger an event. You can monkey-patch the function to do what you want.

    – Barmar
    Nov 20 '18 at 1:12



















  • Calling a function doesn't trigger an event. You can monkey-patch the function to do what you want.

    – Barmar
    Nov 20 '18 at 1:12

















Calling a function doesn't trigger an event. You can monkey-patch the function to do what you want.

– Barmar
Nov 20 '18 at 1:12





Calling a function doesn't trigger an event. You can monkey-patch the function to do what you want.

– Barmar
Nov 20 '18 at 1:12












2 Answers
2






active

oldest

votes


















0














The modern approach to doing this with JavaScript is through a Proxy Object. Here is the description of them from the docs:




The Proxy object is used to define custom behavior for fundamental
operations
(e.g. property lookup, assignment, enumeration, function
invocation
, etc).




Here:



https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy






share|improve this answer































    0














    You are basically looking for a decorator (which are currently Stage 2 Draft, so not yet part of the language).



    However - using a Proxy you can create something like that:






    class MyClass {
    func1() {
    console.log('run func1');
    }
    func2() {
    console.log('run func2');
    }
    }

    m = new MyClass();

    var p = new Proxy(m, {
    get: function(target, name, receiver) {
    if (typeof target[name] == 'function') {
    console.log('wrapper on', name);
    return target[name]
    }
    }
    });

    p.func1();
    p.func2();








    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384831%2fis-there-a-way-to-set-an-event-on-all-class-object-functions-where-a-listener-wi%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      The modern approach to doing this with JavaScript is through a Proxy Object. Here is the description of them from the docs:




      The Proxy object is used to define custom behavior for fundamental
      operations
      (e.g. property lookup, assignment, enumeration, function
      invocation
      , etc).




      Here:



      https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy






      share|improve this answer




























        0














        The modern approach to doing this with JavaScript is through a Proxy Object. Here is the description of them from the docs:




        The Proxy object is used to define custom behavior for fundamental
        operations
        (e.g. property lookup, assignment, enumeration, function
        invocation
        , etc).




        Here:



        https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy






        share|improve this answer


























          0












          0








          0







          The modern approach to doing this with JavaScript is through a Proxy Object. Here is the description of them from the docs:




          The Proxy object is used to define custom behavior for fundamental
          operations
          (e.g. property lookup, assignment, enumeration, function
          invocation
          , etc).




          Here:



          https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy






          share|improve this answer













          The modern approach to doing this with JavaScript is through a Proxy Object. Here is the description of them from the docs:




          The Proxy object is used to define custom behavior for fundamental
          operations
          (e.g. property lookup, assignment, enumeration, function
          invocation
          , etc).




          Here:



          https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 1:18









          Randy CasburnRandy Casburn

          4,9151318




          4,9151318

























              0














              You are basically looking for a decorator (which are currently Stage 2 Draft, so not yet part of the language).



              However - using a Proxy you can create something like that:






              class MyClass {
              func1() {
              console.log('run func1');
              }
              func2() {
              console.log('run func2');
              }
              }

              m = new MyClass();

              var p = new Proxy(m, {
              get: function(target, name, receiver) {
              if (typeof target[name] == 'function') {
              console.log('wrapper on', name);
              return target[name]
              }
              }
              });

              p.func1();
              p.func2();








              share|improve this answer




























                0














                You are basically looking for a decorator (which are currently Stage 2 Draft, so not yet part of the language).



                However - using a Proxy you can create something like that:






                class MyClass {
                func1() {
                console.log('run func1');
                }
                func2() {
                console.log('run func2');
                }
                }

                m = new MyClass();

                var p = new Proxy(m, {
                get: function(target, name, receiver) {
                if (typeof target[name] == 'function') {
                console.log('wrapper on', name);
                return target[name]
                }
                }
                });

                p.func1();
                p.func2();








                share|improve this answer


























                  0












                  0








                  0







                  You are basically looking for a decorator (which are currently Stage 2 Draft, so not yet part of the language).



                  However - using a Proxy you can create something like that:






                  class MyClass {
                  func1() {
                  console.log('run func1');
                  }
                  func2() {
                  console.log('run func2');
                  }
                  }

                  m = new MyClass();

                  var p = new Proxy(m, {
                  get: function(target, name, receiver) {
                  if (typeof target[name] == 'function') {
                  console.log('wrapper on', name);
                  return target[name]
                  }
                  }
                  });

                  p.func1();
                  p.func2();








                  share|improve this answer













                  You are basically looking for a decorator (which are currently Stage 2 Draft, so not yet part of the language).



                  However - using a Proxy you can create something like that:






                  class MyClass {
                  func1() {
                  console.log('run func1');
                  }
                  func2() {
                  console.log('run func2');
                  }
                  }

                  m = new MyClass();

                  var p = new Proxy(m, {
                  get: function(target, name, receiver) {
                  if (typeof target[name] == 'function') {
                  console.log('wrapper on', name);
                  return target[name]
                  }
                  }
                  });

                  p.func1();
                  p.func2();








                  class MyClass {
                  func1() {
                  console.log('run func1');
                  }
                  func2() {
                  console.log('run func2');
                  }
                  }

                  m = new MyClass();

                  var p = new Proxy(m, {
                  get: function(target, name, receiver) {
                  if (typeof target[name] == 'function') {
                  console.log('wrapper on', name);
                  return target[name]
                  }
                  }
                  });

                  p.func1();
                  p.func2();





                  class MyClass {
                  func1() {
                  console.log('run func1');
                  }
                  func2() {
                  console.log('run func2');
                  }
                  }

                  m = new MyClass();

                  var p = new Proxy(m, {
                  get: function(target, name, receiver) {
                  if (typeof target[name] == 'function') {
                  console.log('wrapper on', name);
                  return target[name]
                  }
                  }
                  });

                  p.func1();
                  p.func2();






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 '18 at 1:25









                  DekelDekel

                  42.9k54667




                  42.9k54667






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384831%2fis-there-a-way-to-set-an-event-on-all-class-object-functions-where-a-listener-wi%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      How to send String Array data to Server using php in android

                      Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                      Is anime1.com a legal site for watching anime?