Is it possible to monitor the JS function and identify when it's completed? Example: I have a JS method that has a long process time, I would like to monitor the process or at least know when it's done.
There are a few ways to know when a function has finished running.
Synchronous Functions
If the function in question runs completely synchronously, then the code which called the function will wait until it is done before executing. For example:
function a() {
// do something
}
function b() {
a();
console.log("a is done"); // will only run once a is finished
}
Asynchronous Functions
Promises in JavaScript are another way to do this task, and they work even when you have asynchronous calls - e.g. requests to a server, etc. To get started, you make the first function return a promise:
function a() {
return new Promise((resolve, reject)=>{
// do something
resolve(); // once we're done, resolve the promise
});
}
The promise's constructor takes a function which takes two arguments, resolve and reject, which are given to it by JavaScript. You can call the resolve() function when you are done with processing, and the reject() function if you encounter some error which means you are unable to resolve.
To use this, you can do something like so:
function b() {
a().then(()=>{
console.log("a is done"); // will only run once a is finished
});
}
The nice thing about running a promise is that all other code can continue running - nothing is blocked behind the promise except the anonymous function which you pass into the then method. This is definitely the approach I would take for something like this.
Monitoring progress / logging
With regards to monitoring the progress of a function, it really depends on how the function works. Your best bet is probably to make calls to some sort of logging function whenever the long function reaches certain parts. For example:
function a() {
// do something
console.log("did something");
// do something else
console.log("done");
}
If you can't change function a() for whatever reason, there's no good way to do this (that I'm aware of), unless you know it changes some global variable and you can check that for changes, but an approach like that would be quite dodgy and definitely not a good way to do things.
Conclusion
There's not too much more that I can really say without knowing what kind of function / process it is that you want to monitor - there are special ways to check on file upload progress, for example, that I have not covered here.
Likewise, if the process is happening on a different server or device, these methods won't work at all - you'd likely need to check out WebSockets or (more likely) Server Sent Events to get progress updates for those tasks.
System.out.println("Check1");
expect(mockobject.function(parameters)).andReturn("hello";
System.out.println("check2***************************");
replay(mockobject);
While executing the test, the mockobject.function(parameters) executes and call goes to the method.Debugging using the system out, it checks inside the function is also displayed in console.
Why is it so? The expect() doesn't allow the call to be made to the desired function?
Assuming mockobject is a mock created with mock() or createMock() and not partialMockBuilder(), this can't happen unless function is final.
I've been looking at this "example" in the GAS documentation, but there is no explanation accompanying it from the googlers documenting it:
https://developers.google.com/apps-script/guides/html-service-communication#user_objects
My question pertains to the successhandler and the passing of parameters to the updateButton function. When called in the onclick, the successhandler doesn't include the variables (within the parentheses) in it's call to updateButton. Following the successhandler call is the user object and getEmail but they seem to be out of order (the function updateButton has email before button).
So, how/why does this code work? I've tested it, and even rearranged the variables and what not but the only way it works is in the documentation's writeup. I am unable to understand how the updateButton function knows the parameter values when they are not included in the call to updateButton and they seem to be out of order in the example. Thoughts?
If I understood your question the return of the getEmail function at your GAS script will be redirect to your sucess Handler. You can try to use Logger.log(Session.getActiveUser().getEmail()) and check the return . The parameters order are first the values returned by the gas function and finally the element which triggered the action.
On javascript Object i am invoking
obj.requestFileSystem(LOCAL,5*1024*1024) . This method is implemented in PlainNAPI Plugin , now i have to pass one success callback to this function as follows......
obj.requestFileSystem(LOCAL,5*1024*1024,initFS); // initFS is a function in javascript that is an argument to the success callback .
In NPAPI it is an object when the requestFileSystem is completed then initFS function in javascript should be called. How to return from NPAPI plugin to javascript and to execute initFS function.
function initFS(fs) {
alert('Inside the initFS');
alert(fs.root.getFullPath);
}
Please don't tell me what you're actually doing with this plugin, since it sounds like it's something akin to giving people access to things from a web browser that could easily be abused by someone else.
Basically a function is just an NPObject when it gets to your function inside NPRuntime; to call the function you just do a NPN_InvokeDefault on that NPObject.
Note that you must be on the main thread in order to call nearly all NPN_ functions.
EDIT: So if you have to do the callback from a different thread then your easiest solution is to use NPN_PluginThreadAsyncCall; basically you create an object to hold the data you need and call PluginThreadAsyncCall with that pointer as the void* parameter and it will get passed to the function you specify.
Make sure that A) the pointer will still be valid, and B) your function is able to free that memory after it runs. From the callback function you can call NPN_InvokeDefault safely.
Not that NPN_PluginThreadAsyncCall doesn't seem to work on Safari 5.1 anymore. If you need that support or if what I've explained doesn't make sense you might want to consider using FireBreath to build your plugin; it does all of this for you.
I'm a programming noob and didn't quite understand the concept behind callback methods. Tried reading about it in wiki and it went over my head. Can somebody please explain this in simple terms?
The callback is something that you pass to a function, which tells it what it should call at some point in its operation. The code in the function decides when to call the function (and what arguments to pass). Typically, the way you do this is to pass the function itself as the 'callback', in languages where functions are objects. In other languages, you might have to pass some kind of special thing called a "function pointer" (or similar); or you might have to pass the name of the function (which then gets looked up at runtime).
A trivial example, in Python:
void call_something_three_times(what_to_call, what_to_pass_it):
for i in xrange(3): what_to_call(what_to_pass_it)
# Write "Hi mom" three times, using a callback.
call_something_three_times(sys.stdout.write, "Hi mom\n")
This example let us separate the task of repeating a function call, from the task of actually calling the function. That's not very useful, but it demonstrates the concept.
In the real world, callbacks are used a lot for things like threading libraries, where you call some thread-creation function with a callback that describes the work that the thread will do. The thread-creation function does the necessary work to set up a thread, and then arranges for the callback function to be called by the new thread.
Wiki says:
In computer programming, a callback is
a reference to executable code, or a
piece of executable code, that is
passed as an argument to other code.
This allows a lower-level software
layer to call a subroutine (or
function) defined in a higher-level
layer.
In common terms it is the mechanism to notify a piece of code i.e. a method, which piece of code to execute, i.e. another method, when it is needed.
Callback is related to the fact that the client of the calling function specifies a function that belongs to the client code's responsibility to the calling function to execute and this is passed as an argument.
An example is in GUIs. You pass as argument the function to be called once an event occurs (e.g. button pressed) and once the event
occurs this function is called.
This function is usually implemented by the object that originally registered for the event
Callback function is a function that is called through a function pointer. If you pass the pointer (address) of a function as an argument to another, when that pointer is used to call the function it points to it is said that a call back is made.
Why Should You Use Callback Functions?
A callback can be used for notifications. For instance, you need to set a timer in your application. Each time the timer expires, your application must be notified. But, the implementer of the time'rs mechanism doesn't know anything about your application. It only wants a pointer to a function with a given prototype, and in using that pointer it makes a callback, notifying your application about the event that has occurred. Indeed, the SetTimer() WinAPI uses a callback function to notify that the timer has expired (and, in case there is no callback function provided, it posts a message to the application's queue).
In general you supply a function as a parameter which gets called when something occurs.
In C code you will pass something that looks like this:
int (callback *)(void *, long );
meaning a function that takes two parameters, void * and long and returns an int.
With object-orientated languages the syntax is sometimes simpler. For example you might be able to construct a callback mechanism that allows the user to pass in an object that looks like a function or has an abstract method (thus wrapping a function) and context data too.
Modern languages use the term "delegate" to refer to a function "pattern". These can be used as callbacks. Some languages also use the term lambda which is essentially a function with no name, often created "on the fly" in a block of code and passed as a callback.
C++11 has introduced these into its standard.
The advantage of using a callback is that you can separate out, i.e. reduce / decouple an API from what is calling it, and to some extent vice versa, i.e. although in one place you know you are calling into the API, at the point of the "handler" it does not need to know from where it was called.
For example, you can have an API that generates objects and then "calls-back" as they get generated.
Call back means that you pass the code as a parameter. For example, imagine a button, that much show a dialog when pressed:
Button testBtn;
testBtn.setOnClickListener(new OnClickListener() {
#Override
public void onCLick() {
JOptionPane.showDialog(testBtn, "Test button pressed");
}
}
Here we tell the button what to execute, when it will be click. So, the framework will execute the passed code, when it detecs the click. Inside the framework there are some code like:
void processEvent(Event e) {
if (e.type == Event.CLICK) {
e.getComponent().getOnClickListener().onClick();
}
}
So, some basic code calls back the listener when the appropriate event happens.
PS: Pseudocode here, just do describe the idea.
A callback method is a method that gets called when an event occurs
In simple word, actually, a Callback is a reference to a function or method, which you can pass as a parameter to another function for calling it back later.
From the above figure, B_reference() is the callback method.
Source code sample:
>>> def A(A_msg,B_reference):
... # After printing message, method B is called.
... print(A_msg)
... B_reference()
...
>>> def B():
... print("Message from B")
...
>>>
>>> A("Message from A",B)
Message from A
Message from B
>>>
If you still don't understand what it is, you can check this video: