2
That was the most practical example I could find.. is an example where the server sends 2 messages (or more) in a row and at "the same time", it does not check if there is something running, in case if there was it should wait until the end to send the new request.
I thought about creating a finite loop that waits for a variable to stay false
to proceed, the variable would be the one that says that the work of the last request "ended" but I think this 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
will observe that everything goes well.. But with 2 or more it does not wait..
Working example: http://jsfiddle.net/uxnry1m6/
Please contextualize your question. Does this run on the client? Nodejs Server? Requests are coming from multiple clients? Single client? Websocket?
– Vinícius Gobbo A. de Oliveira
@Viníciusgobboa.deOliveira this runs on the client, the setinterval represent messages coming from the Websocket server that is handled by
OnMessage
which is represented by Setinterval. The execute function is who handles incoming messages.– Elaine