Is there a way to set an event on all class/object functions where a listener will receive function name and...
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
add a comment |
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
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
add a comment |
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
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
javascript node.js javascript-events event-handling
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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
add a comment |
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();add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Nov 20 '18 at 1:18
Randy CasburnRandy Casburn
4,9151318
4,9151318
add a comment |
add a comment |
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();add a comment |
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();add a comment |
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();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();answered Nov 20 '18 at 1:25
DekelDekel
42.9k54667
42.9k54667
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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