Last active
January 1, 2016 21:49
-
-
Save psigen/8206151 to your computer and use it in GitHub Desktop.
Node module that demonstrates a failure to call native function as callback to Object.observe.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var my_module = require('./build/Release/my_module'); | |
console.log(my_module.my_function); // Prints "[Function]", as expected | |
my_module.my_function(); // Prints "Ran my function." | |
// Create a dummy object | |
var foo = {} | |
// Let's hook up an observer to my object | |
Object.observe(foo, my_module.my_function); | |
foo.bar = true; // change object | |
// Nothing happens! my_function isn't called, and no exception is thrown | |
// Ok, let's just wrap the same function in an anonymous function | |
Object.observe(foo, function(changes) { my_module.my_function(changes); } ); | |
foo.baz = true; // change object | |
// Prints "Ran my function." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
'targets': [ | |
{ | |
'target_name': 'my_module', | |
'include_dirs': [ "<!(node -e \"require('nan')\")" ], | |
'sources': [ 'my_module.cc' ], | |
}, | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <node.h> | |
#include "nan.h" | |
using namespace v8; | |
class my_class | |
{ | |
public: | |
static NAN_METHOD(my_function); | |
}; | |
NAN_METHOD(my_class::my_function) | |
{ | |
printf("Ran my function.\n"); | |
fflush(stdout); | |
} | |
void Init(Handle<Object> exports) | |
{ | |
exports->Set(NanSymbol("my_function"), | |
FunctionTemplate::New(my_class::my_function)->GetFunction()); | |
} | |
NODE_MODULE(my_module, Init) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "node_observer_test", | |
"author": { | |
"name": "Pras Velagapudi <psigen@gmail.com>", | |
"email": "psigen@gmail.com" | |
}, | |
"license": "BSD", | |
"scripts": { | |
"start": "node --harmony app.js" | |
}, | |
"dependencies": { | |
"nan": "~0.7.0" | |
}, | |
"engines": { | |
"node": ">=0.11.6" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment