Changing the async wait time in Polymer - asynchronous

Using Polymer's async function is there a way to change the remaining wait time after it's been called? To contextualise usage, a popup modal is currently setup to disappear after 3 seconds once its opened using async, but it needs to extend wait time if the user hovers over it.
Thanks

No, Polymer has no API to change the timeout of an active timer, but you could stop the timer with cancelAsync() and restart it with another call to async().
// initially...
this._timer = this.async(() => ..., 5000);
// on mouse-hover...
this.cancelAsync(this._timer);
this._timer = this.async(() => ..., 2000);

Related

Is it possible to destroy firestore listeners soon if client is not connected? [duplicate]

Is there any way to pause firestore listener without removing it?
I have multiple firebase listeners, some are dependent on other, that changes or start other listeners on data change. Lets say my first listener starts a second listener its onSnapshot. First listener started on useEffect. For certain condition I may not want to change the second listener, so I need to discard data change update from first listener.
If condition met (button click), I discard data changes on first listener for a few moments. Currently I'm doing this using a boolean with useRef. My react app is working fine, with dependant listeners like this. I could remove the listener but I do not want to remove and recreate the listener.
I was wondering if there is a pausing mechanism or method available for any listener. I think it will save a tiny read cost if there was such a method because I'm not using that data sent onSnapshot.
Code example:
useEffect(() => {
let firstListener, secondListener;
//console.log("useEffect...");
function ListenerFunc(p) {
secondListener = await firestore
.collection("test")
.doc(p)
.onSnapshot((doc) => {
//console.log("Current data: ", doc.data());
//Need to discard unwanted change here.
//Changing it on button click for a 2 seconds then it changes back to : pauser.current = false.
if (pauser.current) {
console.log("paused for a moment.");
//pauser.current = false;
return;
}
else {
//update.
}
})
}
firstListener = firestore
.collection("test")
.doc("tab")
.onSnapshot((doc) => {
//console.log("Current data: ", doc.data());
var p = doc.data().p; //get variable p
ListenerFunc(p);
});
// cleanup.
}
Unfortunately this is not possible. If you need to stop listening for changes, even temporarily, you have to detach your listener and attach a new one when you want to start listening again, there is no pause mechanism for listeners.
You could open a Feature Request in Google's Issue Tracker if you'd like so that the product team can consider this, but given that this has already been proposed in this GitHub Feature Request for the IOS SDK and it was rejected I don't see this changing anytime soon.

Calling same onAuthStateChanged listener in multiple screens creates new listeners?

I am calling onAuthStateChanged in multiple screens of React-Navigation and I would like to know if it creates new listeners or does it reset the older one?
I know I can unsubscribe like below but do I have to just call this listener for ex. in Home screen or do I have to do it for each screen? Remember, in react navigation the class state is not reset when navigating so the listener should remain active even when navigating.
const unsubscribe = firebase.auth().onAuthStateChanged(user_auth => {
if (user_auth) {
this.setState({ current_user_id: user_auth.uid });
if (!this.state.set_as_favorite) this.getUsersFavorites();
unsubscribe();
} else {
//log out
}
});
Each listener is unique, so you need to unsubscribe by calling the function returned when you register the listener.
On the other hand these auth state listeners are client-side only objects. So registering multiple auth-state listeners does not mean that you are connecting to the server multiple times.
In fact, connecting to the server is based on using Firebase Authentication period, and does not depend at all on whether (or how many) auth state listeners there.

Why does Future.timeout not work in dart?

I want to run a computation that might take too long. If it doesn't finish in a specific time, abort the computation and return a different value. For this problem I found Future.timeout, it would almost do the same thing that I want, except it doesn't work for this code.
Future<String> timeoutTest() async
{
return await longComputation().timeout(
Duration(milliseconds: 10),
onTimeout: () => "Took too long"
);
}
Future<String> longComputation() async
{
int startTime = DateTime.now().millisecondsSinceEpoch;
Rational n = Rational.one;
for(int i = 1; i < 2000; i++)
{
n *= Rational.fromInt(i);
}
String result = n.toDecimalString();
print("Time took: ${DateTime.now().millisecondsSinceEpoch - startTime} ms");
return result;
}
When I call print(await timeoutTest()) I either expect a string of digits that took maximum 10ms to calculate OR the "Took too long" string if it took more than 10ms. But I get the string of digits, and the message in the console: "Time took: 877 ms". So the timeout didn't work.
If I fake the computation with Future.delay, the timeout works as expected. It returns a different value, because the longComputation took at least 100ms. (I still get the message in the console: "Time took: 103ms", but this is not the main problem.)
Future<String> longComputation() async
{
int startTime = DateTime.now().millisecondsSinceEpoch;
String result = await Future.delayed(
Duration(milliseconds: 100),
() => "Fake computation result"
);
print("Time took: ${DateTime.now().millisecondsSinceEpoch - startTime} ms");
return result;
}
I'm assuming I messed up something in the longComputation, but what? There were no un-awaited Futures.
This behavior can be confusing but it is important to remember that your Dart code are only executed in a single thread (unless you are using isolates).
The problem is that the logic behind .timeout needs to run in the same single thread and Dart can't just stop the execution of your own code. So if you are running a CPU intensive calculation without any pauses you are stopping the Dart VM from running any other events on the event queue.
What the implementation of .timeout actually does, is creating an internal Timer which are going to be triggered in the future unless you get a result before the timeout value. This Timer event are going on top on the event queue like any other event in the Dart VM.
But in your first example, we are actually never going to execute any other event on the event queue before you are giving the result. So from the Future's point of view, you are returning a result before the deadline.
This is going to look like .timeout is kind of pointless but what it really are for is when you are making some IO operations like API requests where the Dart VM are actually waiting for some answer.
If you are going to use it for heavy calculations, you can either spawn an Isolate so your main isolate instance can wait on the other isolate. Alternative, you can insert some pauses in your calculation which makes space for the Dart VM to execute other events on the event queue. An example could be inserting await Future<void>(() => null); which are going to spawn a new event on top of the event queue. When we wait for all events on the queue to be executed before our own empty calculation.
It would then also make sense to add some logic so your own code can see if the timeout value has been reached so you can stop the calculation if that is the case.
You can't actually cancel Futures. At best you could have your .timeout callback set a flag that longComputation periodically checks to become a no-op as soon as possible.
Also see Future.timeout documention is misleading.

How to add a pause between 2 requests in POSTMAN

I have a collection of requests in POSTMAN. I wanted to add a pause between 2 requests but I couldn't find a way to do so from reading their docs. Any idea?
UPDATE I only wanted to put a pause after one request instead of after every request in the collection.
In case someone is still looking for this - You can add delay after/before 1 of many test in a collection you can use:
setTimeout(function(){}, [number]);
where 'number' is the milliseconds. If it's added at 'Tests' it will be executed after request is send. If it's added at Pre-request Scripts it will be executed before request is send.
Using javascript's busy wait is a good hack but it makes your CPU hot and the app unresponsive. I figured out this solution using postman-echo.
Assuming you want to add a long delay between Request_A and Request_B.
First, in Request_A's test script set up an env var to mark the start.
environment.delayTimerStart = new Date();
Then, create a GET request in the creation (here called 'Delay 10s'). It makes a GET on https://postman-echo.com/delay/10 (It returns after 10s)
In its test script, add
var curDate = new Date();
if (curDate - environment.delayTimerStart < delay_time_in_sec*1000) {
postman.setNextRequest('Delay 10s');
} else {
postman.setNextRequest("Request_B");
}
In this way you can add a delay of any length.
Note: 10 sec is the maxium value that postman-echo accepts. If you just need a short delay, simply GET https://postman-echo.com/delay/[1~10].
Try something like this-
if(jsonBody.responseCode=="SUCCESS"){
setTimeout(function(){
console.log("Sleeping for 3 seconds before next request.");
}, 3000);
postman.setNextRequest("nextAPI");
}
I know two possible ways to do this
Method I
Run your request as a collection. (https://www.getpostman.com/docs/collections)
Use Newman (Postman's collection runner from command line) to run your collection with --delay flag. The delay input value is in milliseconds.
Method II
This is a nice hack which I found here https://github.com/postmanlabs/postman-app-support/issues/1038. You can add a delay function to your test script in Postman.
Just simple example, I'm sure you will be understand.
setTimeout(() => {}, 15000);
15000 it's a value in miliseconds
looking at the current documentation if you are using Postman Runner
Delay
This is the interval (in ms) between each request in your collection run.
https://www.getpostman.com/docs/postman/collection_runs/starting_a_collection_run
and if you are using newman
--delay-request [number] Specify a delay (in ms) between requests [number]
https://www.getpostman.com/docs/postman/collection_runs/command_line_integration_with_newman
If you have the standalone Postman App (supports ES7) and you are intending to test only on Postman, not on newman(which doesn't support ES7), then you can have something like this in the Pre-Request script of the Request you want to delay:
function foo() {
return (new Promise((resolve, reject) => {
setTimeout(() => {
resolve("done!"); // passing argument is optional, can just use resolve()
}, 10000) // specify the delay time in ms here..
}))
}
async function waitForMe() {
await foo().then((val) => {
console.log(val); // not required, you can just do an await without then
})
}
waitForMe();
I prefer to use an online service Postman Echo's delay endpoint (docs are here). Simply create a request that uses this service and call it between the two other requests you wish to add a delay between.
If you want to check the status of something before continuing, you can use postman.setNextRequest() in the Tests of a request to loop until something has been completed. Simply do the following:
1) Create a collection structured as:
Delay For 10 Seconds
Status Check
Continue Processing
2) In the Status Check request Tests:
if(responseBody.has("Success")) //or any other success condition
{
postman.setNextRequest('Continue Processing');
tests["Success found"] = true;
}
else
{
postman.setNextRequest('Delay For 10 Seconds');
tests["No success found"] = true;
}
You can use JavaScript setTimeout method. This will make sure that your method will be executed after given delay.
setTimeout(checkStatusCode, 500);
function checkStatusCode() {
pm.sendRequest('https://postman-echo.com/get', function (err, res) {
tests['status code should be 200']= res.code ===200;
});
}

Meteor How to block a method call before the first one is finished?

I have the following scenario:
Client side has a button clicking it will execute Meteor.call method on the server-side which will call API and fetch products, During this time I wan't to disable this button + block this method from executing again basically nothing stops you from clicking the button 100x times and server will keep on executing same method again and again.
Few ideas I had in my mind: Use sessions to disable button (Problem: can still using the console Meteor.call and abuse it)
I also looked at Meteor.apply in the docs with wait:true didn't seems to stop from method execution. I honestly not sure how this kind of thing is handled with no hacks.
Client-side:
'click .button-products': function(e){
Meteor.call('getActiveProducts', function(error, results){
if (error)
return Alerts.add(error.reason, 'danger', {autoHide: 5000});
if (results.success)
return Alerts.add('Finished Importing Products Successfully', 'success', {autoHide: 5000});
})
}
Server-side
Meteor.methods({
getActiveProducts: function(){
var user = Meteor.user();
var api = api.forUser(user);
importProducts = function(items){
nextPage = items.pagination.next_page;
items.results.forEach(function(product){
var sameproduct = apiProducts.findOne({listing_id: product.listing_id});
if (sameproduct) {
return;
}
var productExtend = _.extend(product, {userId: Meteor.userId()});
apiProducts.insert(productExtend);
});
};
var products = api.ProductsActive('GET', {includes: 'Images', limit: 1});
importProducts(products);
while (nextPage !== null) {
products = api.ProductsActive('GET', {includes: 'Images', page: nextPage, limit: 1});
importProducts(products);
}
return {success: true};
}
});
From the Meteor docs:
On the server, methods from a given client run one at a time. The N+1th invocation from a client won't start until the Nth invocation returns. However, you can change this by calling this.unblock. This will allow the N+1th invocation to start running in a new fiber.
What this means is that subsequent calls to the method won't actually know that they were made while the first call was still running, because the first call will have already finished running. But you could do something like this:
Meteor.methods({
getActiveProducts: function() {
var currentUser = Meteor.users.findOne(this.userId);
if (currentUser && !currentUser.gettingProducts) {
Meteor.users.update(this.userId, {$set: {gettingProducts: true}});
// let the other calls run, but now they won't get past the if block
this.unblock();
// do your actual method stuff here
Meteor.users.update(this.userId, {$set: {gettingProducts: false}});
}
}
});
Now subsequent calls may run while the first is still running, but they won't run anything inside the if block. Theoretically, if the user sends enough calls, the first call could finish before all of the others have started. But this should at least significantly limit the number of etsy calls that can be initiated by a user. You could adapt this technique to be more robust, such as storing the last time a successful call was initiated and making sure X seconds have passed, or storing the number of times the method has been called in the last hour and limiting that number, etc.
A package I wrote a while back might come in handy for you. Essentially it exposes the Session api on the server side (hence the name), meaning you can do something like ServerSession.set('doingSomethingImportant', true) within the call, and then check this session's value in subsequent calls. The session can only be set on the server, and expires upon connection close (so they could spam calls, but only as fast as they can refresh the page).
In the event of error, you can just reset the session. There shouldn't be any issues related to unexpected errors either because the session will just expire upon connection close. Let me know what you think :)

Resources