This is the most practical example I found .. is an example where the server sends 2 messages (or more) followed and at the same time, it does not check if there is something running, in case if it should have wait until the end to send the new request.
I thought of creating a finite loop that expects a variable to be false
to proceed, the variable would be the one that says the last request job "is over" but I think it is gambiarra.
// CONFIGURACOES DA DB //
var db = new Dexie('teste');
db.version(1).stores({
sequences: '++id,of'
});
db.open().
catch ();
// CONFIGURACOES DA DB //
var executionOrder = []; // tem como deixar vazio sem ter undefined?
// isso foi o exemplo mais pratico que encontrei.. é um exemplo em que o servidor envia 4 mensagens (ou mais) seguidas e ao "mesmo tempo", ele nao checa se há algo sendo executado, no caso se houvesse ele deveria esperar até o fim para enviar a nova requisiçao
setInterval(function () {
var newOrder = ['NOVO'].reverse();
executionOrder = executionOrder.concat(newOrder);
execute('INIT');
}, 4000);
setInterval(function () {
var newOrder = ['APAGA'].reverse();
executionOrder = executionOrder.concat(newOrder);
execute('INIT');
}, 4000);
setInterval(function () {
var newOrder = ['NOVO'].reverse();
executionOrder = executionOrder.concat(newOrder);
execute('INIT');
}, 4000);
function execute(req) {
if (req == 'INIT') {
callback();
} if (req == 'NOVO') {
db.sequences.add({
of: '0'
});
console.log('ADD 1');
callback();
} if (req == 'APAGA') {
db.sequences.where("of").equalsIgnoreCase('0').each(function (seq) {
db.sequences.delete(seq.id);
}).then(function () {
console.log('DEL ALL');
callback();
});
}
};
function callback() {
if (executionOrder.length) {
var next = executionOrder.pop();
execute(next);
}
}
If you take a setInterval
you will notice that everything happens well .. But with 2 or more it does not wait ..
Example working: link