In QML, I'm using a C++ library that returns a QObject that does a process and emits a signal when is done. In javascript, I use the connect method of the signal being emitted (success) to attach an anonymous function that should handle the signal as the following piece of code shows:
var requestResponse = apiClient.execute(reqInp);
requestResponse.success.connect(function success(response)
{
var requestResponseJSON = JSON.parse(response.responseAsJsonString());
this.append(response.responseAsJsonString());
});
My problem is that sometimes the QML item that contains this method is destroyed before the C++ code being able to complete, so when the signal is emitted, the anonymous function causes an error, because it calls methods that are undefined (in my example, the method append). I have some nasty crashes in iOS and I suspect that this is what might be causing it.
Is there a way of force disconnection of the signal when the object that created the function is destroyed?
var requestResponse = apiClient.execute(reqInp);
function myFunction(response)
{
var requestResponseJSON = JSON.parse(response.responseAsJsonString());
this.append(response.responseAsJsonString());
}
requestResponse.success.connect(myFunction);
requestResponse.destroyed.disconnect(myFunction)
Related
In Javascript, if a function is referenced that doesn't exist, I can catch the resulting error using:
window.onerror = function(msg, src) {
// insert logic here
return true;
}
By parsing the content of msg, I can determine what function the developer was trying to call, and then by creating a Proxy object, I can prepare the app to ingest the arguments passed to the non-existent function that was referenced should the same function be called again.
However, it'd be ideal if I could handle the reference the first time it occurs. I'm interested in knowing if there's a way to either capture the arguments in the error handler, or if it's possible to declare a "wildcard" function that could respond when an undefined function is referenced.
Does such a thing or methodology exist?
Thank you in advance for your consideration and time.
I'm trying to use the NodeJS module "pcsc-lite" to communicate with a card reader. If you want to take a look at the module : https://github.com/santigimeno/node-pcsclite.
I'm looking for a way to send a sequence of data to my reader using my own method. Because, the module is event-based. So I have to declare two listeners (one in the other) to be able to call the send method.
For example :
module.on("reader", function(reader){
//...
reader.on("status", function(status){
//...
reader.connect({ share_mode : this.SCARD_SHARE_SHARED },function(err, protocol) {
//This is the method I want to be able to call "when I need it"
reader.transmit(...);
});
});
});
I would like to call the transmit method like this for example :
function send(...){
reader.transmit(...);
}
I think there is a way to do it, but I seem to be a little bit hooked to my C/Java programming habits.
Thanks in advance.
If your reader will be a singleton, you can declare it outside the callback, and then assign the variable when you're ready. Without knowing more, here's a simple example:
let reader; // we prepare a variable that's outside of scope of it all.
// your `send` function
function send(params) {
let stuff = doStuffWithParams(params);
reader.transmit(stuff, callback);
}
// we take out init stuff too
function initialize() {
// we know reader variable is already initialized.
reader.on('status', function() {
reader.connect({
share_mode : this.SCARD_SHARE_SHARED
},function(err, protocol) {
// send.
send();
// or even better, emit some event or call some callback from here, to let somebody outside this module know you're ready, then they can call your `send` method.
});
});
}
// now your module init section
let pcsc = require('pcsclite')();
pcsc.on('reader', function(r) {
// assign it to our global reader
reader = r;
initialize();
});
Note: don't call your variables module, it's refering to the file being currently executed and you can get unexpected behavior.
I am having problems getting a reference to a javascript object implemented with the
prototype pattern within a callback. The callback is from a 3rd party component
I utilize within my object. The 3rd party object connects to a message bus.
The following pseudo code shows how I started (The real code for this is working)
var mb = require('MsgBus')
TestClass = function() {
this.messagebus = new mb.MsgBus();
this.messagebus.connect(function(err) {
if(err)
console.log("Error connecting");
else
console.log("Connected");
});
}
But then I wanted to have it automatically retry connecting if the callback reports
an error. I cannot just put another line if the if(err) block that
says "this.messagebus.connection" because I would have to add another anonymous
method for that connect callback and it would just go on and on. So, I want to
split out the callback logic to a named function like this
var mb = require('MsgBus')
TestClass = function() {
this.messagebus = new mb.MsgBus();
this.messagebus.connect(msgBusConnectCallback);
}
function msgBusConnectCallback(err) {
if(err)
this???.messagebus.connect(msgBusConnectCallback);
else
console.log("Connected");
});
}
The callback function gets called, but I cannot figure out how to get a reference
to the object to call connect again. I've also tried to make the callback a
prototype function of the object, still no reference. I cannot create a variable
in the global scope to maintain "this" because the user of this class may
create multiple instances of the class. I am fairly new to JavaScript so I don't
know if I'm just missing something or if I need to take a different approach
altogether. I would appreciate any help and/or direction.
this.messagebus.connect.apply(this, [msgBusConnectCallback]);
I finally figured out the answer, the correct syntax is
this.messagebus.connect(msgBusConnectCallback.bind(this));
In the following code example :
var oldConstructor = Error.constructor;
Error.constructor = function() {
console.log('Constructor');
oldConstructor.apply(null, arguments);
};
var a = new Error('ok');
Why isn't 'Constructor' printed ?
How can I run a function every time a native Error object's constructor is called ?
The goal I'm trying to achieve is that rather than bubbling Errors up the callback chain of the different modules used in my code base (mongoose, express.js, ...etc), I just want every error to emit an event or call a method (maybe with an Observer pattern).
I'm trying to solve the problem this way rather than modifying every line of code creating a new Error object.
Thanks in advance !
Error.constructor is a reference to the Function function, because Error is a function and functions are constructed by Function.
You could have done:
var oldError = Error;
Error = function( arg ) {
console.log('Constructor');
return new oldError( arg );
};
But this is not guaranteed to work at all as modules could have stored a local reference to the Error constructor if they run before your code.
You could instead use the uncaughtexception event
I am taking jQuery.Atmosphere.js as an example, in this it has public function such as onMessage, onError etc. And when implementing this api i have done the following
var socket = $.atmosphere;
var request = new $.atmosphere.AtmosphereRequest();
request.onMessage = function(response) {
// do what i want to do
}
Here the onMessage will be trigger whenever the server pushes data to browser. I don't understand how request.onMessage(response) get notified which is outside the atmosphere api? I have looked in to the jQuery.Atmosphere.js and couldn't connect the dots how this works. I am not talking about websocket or server push or anything about atmosphere framework. I just want understand how javascript function callbacks work. Can anyone point me an example how function callbacks work or send me a link so i can dig in?
Your syntax is incorrect, it should be:
request.onMessage = function(response) {
// do what I want to do
};
As you can see, the onMessage property must be set to a function. When the Message event occurs on this object, the function will be called. The jQuery.Atmosphere.js code contains:
f.onMessage(response);
where f is its internal variable representing the AtmosphereRequest object. This function is called from invokeFunction():
function _invokeFunction(response) {
_f(response, _request);
// Global
_f(response, jQuery.atmosphere);
}
_request is a local variable in the AtmosphereRequest constructor, which contains all the state of this request object. This is part of Javascript object oriented programming; all uses of this AtmosphereRequest object have access to these internal state variables.