how can i lock a class while other method running? (with timeout) - node.js

I want to wait while a class other method running, current solution:
async waitForUnlock(ms: number) {
if (!this.locked) return
const start = new Date().getTime();
let time = 0;
while (time < ms) {
if (!this.locked) {
return;
}
const end = new Date().getTime();
time = end - start;
}
throw new Error(`timeout ${ms} ms. The queue is locked.`)
}
example usage:
async undoCommand(numberOfCommands: number = 1, timeout: number = 1000): Promise<void> {
await this.waitForUnlock(timeout)
// ....
it's oke? is there a better solution?

Related

Cannon.js loop (requestAnimationFrame) dosent work on node.js?

I'm using this solution: https://github.com/nodejs/help/issues/2483
to calculate frames. Here is my code.
//cannon
const CANNON = require('cannon');
const world = new CANNON.World(),
fixedTimeStep = 1.0/60.0,
maxSubSteps = 7;
//animation
var lastTime = undefined;
const fps = 40,
funcs = [],
skip = Symbol('skip'),
start = Date.now();
let time = start;
const animFrame = () => {
const fns = funcs.slice();
funcs.length = 0;
const t = Date.now();
const dt = t - start;
const t1 = 1e3 / fps;
for(const f of fns)
if(f !== skip) f(dt);
while(time <= t + t1 / 4) time += t1;
setTimeout(animFrame, time - t);
};
const requestAnimationFrame = func => {
funcs.push(func);
return funcs.length - 1;
};
const cancelAnimationFrame = id => {
funcs[id] = skip;
};
class World{
#world;
#shape;
constructor(){
this.#world = new CANNON.World()
this.#shape = new CANNON.Body({mass: 5, shape: new CANNON.Sphere(1)});
this.#shape.position.set(0,0,0);
this.#world.add(this.#shape);
this.setPhysics();
this.updatePhysics();
}
get world(){
return this.#world;
}
setPhysics(){
world.gravity.set(0, 0, -10); // Gravity pulls things down
world.broadphase = new CANNON.NaiveBroadphase();
world.solver.iterations = 40;
}
updatePhysics(time){
requestAnimationFrame(this.updatePhysics);
if(lastTime !== undefined){
var dt = (time - lastTime) / 1000;
this.#world.step(fixedTimeStep, dt, maxSubSteps);
}
lastTime = time;
};
}
animFrame();
var world1 = new World();
var world2 = new World();
When i try to run it with node i get this error
C:\testy_game\cannon (2).js:63
requestAnimationFrame(this.updatePhysics);
^
TypeError: Cannot read properties of undefined (reading
'updatePhysics')
at updatePhysics (C:\testy_game\cannon (2).js:63:32)
at Timeout.animFrame [as _onTimeout] (C:\testy_game\cannon (2).js:25:20) ←[90m at listOnTimeout
(node:internal/timers:557:17)←[39m ←[90m at processTimers
(node:internal/timers:500:7)←[39m
Please help solve the problem

In firebase cloud function the child is self-deleting

When I add a transaction in the function below, the child of the "guncelLig" is automatically deleted. When I remove the second transaction there is no problem, it works very well.
this code work correct :
for (let i = 0; i < users.length; i++) {
const userID = users[i].id;
database.ref().child("users").child("profilData").child(userID).child("guncelLig").transaction(function (value) {
if (value > 1) {
database.ref().child("users").child("profilData").child(userID).update({
"lastUpdate": toSwiftDate(),
});
return value - 1;
}
return value;
});
}
but when I add the new part to the code below, "guncelLig" is completely deleted from the firebase realtime databse:
for (let i = 0; i < users.length; i++) {
const userID = users[i].id;
database.ref().child("users").child("profilData").child(userID).child("guncelLig").transaction(function (value) {
if (value > 1) {
database.ref().child("users").child("profilData").child(userID).update({
"lastUpdate": toSwiftDate(),
});
/// NEW CODE ERROR SOURCHE
database.ref().child("users").child("profilData").child(userID).child("EnYukselLig").transaction(function (EnYukselLig) {
if (EnYukselLig > value - 1) {
return value - 1;
}
return EnYukselLig;
});
return value - 1; // arttır
}
return value; /// ALWAYS RETURN NULL
});
}
Where am I doing wrong? please help me
You should write your code as async/await. With await you can wait for the nested transaction to finish.
for (let i = 0; i < users.length; i++) {
const userID = users[i].id;
database
.ref()
.child("users")
.child("profilData")
.child(userID)
.child("guncelLig")
.transaction(async (value) => {
if (value > 1) {
await database
.ref()
.child("users")
.child("profilData")
.child(userID)
.update({
lastUpdate: toSwiftDate(),
});
/// NEW CODE ERROR SOURCHE
await database
.ref()
.child("users")
.child("profilData")
.child(userID)
.child("EnYukselLig")
.transaction(function (EnYukselLig) {
if (EnYukselLig > value - 1) {
return value - 1;
}
return EnYukselLig;
});
return value - 1; // arttır
}
// No handler if the value is NULL
return value; /// ALWAYS RETURN NULL
});
}
You also don't handle the case if value is already null.
It is not recommmended to nest transactions like that. Transacions by nature can run multiple times and there is no guaranty that they finish as expected depending on the amount of updates that happen on the same location.

How do i do a Command Cooldown?

i was wondering how to make a Command Cool-down for a single command and not all the other commands? it would be much appreciated if someone helps me out on this, Thank you in advanced.
You are going to have to create a Collection() that will contain the users that have executed a command and then have a client.setTimeout() function to remove the users after a set time from the Collection() so that they can use commands again.
Here is an example from this guide:
const cooldowns = new Discord.Collection();
if (!cooldowns.has(command.name)) {
cooldowns.set(command.name, new Discord.Collection());
}
const now = Date.now();
const timestamps = cooldowns.get(command.name);
const cooldownAmount = (command.cooldown || 3) * 1000;
if (timestamps.has(message.author.id)) {
// ...
}
if (timestamps.has(message.author.id)) {
const expirationTime = timestamps.get(message.author.id) + cooldownAmount;
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
return message.reply(`please wait ${timeLeft.toFixed(1)} more second(s) before reusing the \`${command.name}\` command.`);
}
}

node.js setInterval does not trigger a this. object

When the page loads the console log part of the function works showing me the current seconds + 3, but it does not repeat and the (innerText =....) does not work at all. I only added the console log part to the code to try to troubleshoot, the inner text change is the important part.
class Main {
constructor() {
// Initiate variables
this.TimerTexts = [];
this.infoTexts = [];
this.training = -1; // -1 when no class is being trained
this.videoPlaying = false;
this.currentTime2 = new Date(Date.now());
this.currentTime = new Date(Date.now());
this.remTime = this.currentTime.getSeconds() + 3;
this.looper = window.setInterval(this.intervalfunc(), 1000);
}
// ...
intervalfunc() {
this.TimerTexts.innerText = `Time: ${this.remTime} `;
console.log(this.remTime);
}
// ...
}
The problem is you're calling intervalfunc, not passing in as the function for setInterval.
In addition, you'll need to bind the function to your instance.
this.looper = window.setInterval(this.intervalfunc.bind(this), 1000);
You can use arrow function, inside arrow function you can call intervalfunc.
class Main {
constructor() {
// Initiate variables
this.TimerTexts = [];
this.infoTexts = [];
this.training = -1; // -1 when no class is being trained
this.videoPlaying = false;
this.currentTime2 = new Date(Date.now());
this.currentTime = new Date(Date.now());
this.remTime = this.currentTime.getSeconds() + 3;
this.looper = window.setInterval(()=>this.intervalfunc(), 1000);
}
intervalfunc() {
this.TimerTexts.innerText = `Time: ${this.remTime} `;
console.log(this.remTime);
this.remTime += 3;
}
}
new Main()
OR you can do
class Main {
constructor() {
// Initiate variables
this.TimerTexts = [];
this.infoTexts = [];
this.training = -1; // -1 when no class is being trained
this.videoPlaying = false;
this.currentTime2 = new Date(Date.now());
this.currentTime = new Date(Date.now());
this.remTime = this.currentTime.getSeconds() + 3;
var self = this;
this.looper = window.setInterval(this.intervalfunc.bind(self), 1000);
}
intervalfunc() {
this.TimerTexts.innerText = `Time: ${this.remTime} `;
console.log(this.remTime);
this.remTime += 3;
}
}
new Main()

Async/Await Node-Postgres Queries Within ForEach Loops

EDIT: I'm using node v8.0.0
I just started learning how to access SQL databases with node-postgres, and I'm having a little bit of trouble accessing multiple databases to collect the data in a work able format, particularly with executing multiple queries within forEach loops. After a few tries, I'm trying async/await, but I get the following error:
await client.connect()
^^^^^^
SyntaxError: Unexpected identifier
When I tried using a pool or calling .query sequentially, I would get something along the lines of
1
[]
could not connect to postgres Error: Connection terminated
Here is an abbreviated version of my code:
const { Client } = require('pg');
const moment = require('moment');
const _ = require('lodash');
const turf = require('#turf/turf');
const connString = // connection string
var collected = []
const CID = 300
const snaptimes = // array of times
var counter=0;
const client = new Client(connString);
function createArray(i,j) {
// return array of i arrays of length j
}
await client.connect()
snaptimes.forEach(function(snaptime){
var info = {}; // an object of objects
// get information at given snaptime from database 1
const query1 = // parametrized query selecting two columns from database 1
const result1 = await client.query(query1, [CID,snaptime]);
var x = result1.rows;
for (var i = 0; i < x.length; i++) {
// store data from database 1 into info
// each row is an object with two fields
}
// line up subjects on the hole
const query2 = // parametrized query grabbing JSON string from database 2
const result2 = await client.query(query2, [CID,snaptime]);
const raw = result2.rows[0].JSON_col;
const line = createArray(19,0); // an array of 19 empty arrays
for (var i = 0; i < raw.length; i++) {
// parse JSON object and record data into line
}
// begin to collect data
var n = 0;
var g = 0;
// walk down the line
for (var i = 18; i > 0; i--) {
// if no subjects are found at spot i, do nothing, except maybe update g
if ((line[i] === undefined || line[i].length == 0) && g == 0){
g = i;
} else if (line[i] !== undefined && line[i].length != 0) {
// collect data for each subject if subjects are found
line[i].forEach(function(subject){
const query 3 = // parametrized query grabbing data for each subject
const result3 = await client.query(query3,[CID,subject,snaptime]);
x = result3.rows;
const y = moment(x[0].end_time).diff(moment(snaptime),'minutes');
var yhat = 0;
// the summation over info depends on g
if (g===0){
for (var j = i; j <= 18; j++){
yhat = moment.duration(info[j].field1).add(yhat,'m').asMinutes();
}
} else {
for (var j = i; j <= 18; j++){
if (i<j && j<g+1) {
yhat = moment.duration(info[j].field2).add(yhat,'m').asMinutes();
} else {
yhat = moment.duration(info[j].field1).add(yhat,'m').asMinutes();
}
}
}
collected.push([y,yhat,n,i]);
});
}
n+=line[i].length;
g=0;
}
// really rough work-around I once used for printing results after a forEach of queries
counter++;
if (counter===snaptimes.length){
console.log(counter);
console.log(collected);
client.end();
}
});
The problem is caused by your forEach callback not being async:
snaptimes.forEach(function(snaptime){
should be:
snaptimes.forEach(async function (snaptime) {
for the await to be recognizable at all.
Keep in mind that an async function returns immediately and it returns a promise that gets eventually resolved by return statements of the async function (or rejected with uncaught exceptions raised inside the async function).
But also make sure your Node version supports async/await:
Since Node 7.6 it can be used with no --harmony flag.
In Node 7.x before 7.6 you have to use the --harmony flag.
It was not available in Node before 7.0.
See: http://node.green/#ES2017-features-async-functions
Also note that you can use await only inside of functions declared with the async keyword. If you want to use it in the top level of your script or module then you need to wrap it in an immediately invoked function expression:
// cannot use await here
(async () => {
// can use await here
})();
// cannot use await here
Example:
const f = () => new Promise(r => setTimeout(() => r('x'), 500));
let x = await f();
console.log(x);
prints:
$ node t1.js
/home/rsp/node/test/prom-async/t1.js:3
let x = await f();
^
SyntaxError: Unexpected identifier
but this:
const f = () => new Promise(r => setTimeout(() => r('x'), 500));
(async () => {
let x = await f();
console.log(x);
})();
prints:
$ node t2.js
x
after 0.5s delay, as expected.
On versions of Node that don't support async/await the first (incorrect) example will print:
$ ~/opt/node-v6.7.0/bin/node t1.js
/home/rsp/node/test/prom-async/t1.js:3
let x = await f();
^
SyntaxError: Unexpected identifier
and the second (correct) example will print a different error:
$ ~/opt/node-v6.7.0/bin/node t2.js
/home/rsp/node/test/prom-async/t2.js:3
(async () => {
^
SyntaxError: Unexpected token (
It's useful to know because Node versions that don't support async/await will not give you a meaningful error like "async/await not supported" or something like that, unfortunately.
Make sure that you should use async block outside like:
async function() {
return await Promise.resolve('')
}
And it is default supported after node 7.6.0. Before 7.6.0, you should use --harmony option to work for it.
node -v first to check your version.
First of all, you don't know enough about async-await just yet. don't worry, it's actually quite easy; but you need to read the documentation to be able to use that stuff.
More to the point, the problem with your code is that you can only await inside async functions; you're doing that outside of any function.
First of all, here's the solution that is closest to the code you wrote:
const { Client } = require('pg');
const moment = require('moment');
const _ = require('lodash');
const turf = require('#turf/turf');
const connString = // connection string
var collected = []
const CID = 300
const snaptimes = // array of times
var counter=0;
const client = new Client(connString);
function createArray(i,j) {
// return array of i arrays of length j
}
async function processSnaptime (snaptime) {
var info = {}; // an object of objects
// get information at given snaptime from database 1
const query1 = // parametrized query selecting two columns from database 1
const result1 = await client.query(query1, [CID,snaptime]);
var x = result1.rows;
for (var i = 0; i < x.length; i++) {
// store data from database 1 into info
// each row is an object with two fields
}
// line up subjects on the hole
const query2 = // parametrized query grabbing JSON string from database 2
const result2 = await client.query(query2, [CID,snaptime]);
const raw = result2.rows[0].JSON_col;
const line = createArray(19,0); // an array of 19 empty arrays
for (var i = 0; i < raw.length; i++) {
// parse JSON object and record data into line
}
// begin to collect data
var n = 0;
var g = 0;
// walk down the line
for (var i = 18; i > 0; i--) {
// if no subjects are found at spot i, do nothing, except maybe update g
if ((line[i] === undefined || line[i].length == 0) && g == 0){
g = i;
} else if (line[i] !== undefined && line[i].length != 0) {
// collect data for each subject if subjects are found
line[i].forEach(function(subject){
const query 3 = // parametrized query grabbing data for each subject
const result3 = await client.query(query3,[CID,subject,snaptime]);
x = result3.rows;
const y = moment(x[0].end_time).diff(moment(snaptime),'minutes');
var yhat = 0;
// the summation over info depends on g
if (g===0){
for (var j = i; j <= 18; j++){
yhat = moment.duration(info[j].field1).add(yhat,'m').asMinutes();
}
} else {
for (var j = i; j <= 18; j++){
if (i<j && j<g+1) {
yhat = moment.duration(info[j].field2).add(yhat,'m').asMinutes();
} else {
yhat = moment.duration(info[j].field1).add(yhat,'m').asMinutes();
}
}
}
collected.push([y,yhat,n,i]);
});
}
n+=line[i].length;
g=0;
}
// really rough work-around I once used for printing results after a forEach of queries
counter++;
if (counter===snaptimes.length){
console.log(counter);
console.log(collected);
}
}
async function run () {
for (let snaptime of snaptimes) {
await processSnaptime(snaptime);
}
}
/* to run all of them concurrently:
function run () {
let procs = [];
for (let snaptime of snaptimes) {
procs.push(processSnaptime(snaptime));
}
return Promise.all(procs);
}
*/
client.connect().then(run).then(() => client.end());
client.connect returns a promise and I use then to call run once it's resolved. When that part is over, client.end() can be called safely.
run is an async function, therefore it can use await to make the code more readable. The same goes for processSnaptime.
Of course I can't actually run your code, so I can only hope I didn't make any mistakes.

Resources