can draw be cancelled in preDraw.dt handler?

can draw be cancelled in preDraw.dt handler?

loukinglouking Posts: 286Questions: 56Answers: 0

I'm trying to resolve a race condition between an interval timed draw and some other user actions. Claude AI suggests returning false in the preDraw.dt handler, but for whatever reason I'm not finding that in your documentation. Will this work? (I'm using 1.10, but will also move to 2.x soon)

    // preDraw.dt (camelCase) is the correct DataTables event name; returning false cancels the draw.
    _dt_table.on('preDraw.dt', function() {
        if (scan_mousedown) {
            console.log('preDraw.dt: draw blocked by scan-action-btn guard');
            return false;
        }
    });

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 3,192Questions: 92Answers: 439
    Answer ✓

    This is from the docs:

  • loukinglouking Posts: 286Questions: 56Answers: 0

    thanks, not sure how I missed that

  • allanallan Posts: 65,759Questions: 1Answers: 10,939 Site admin

    Easy done!

    The "Please note" in the documentation there is quite important. By the time preDraw triggers, the sorting and filtering will already have happened, so if you interact with the API after that event, and the draw has been cancelled, the data might not align with what is still shown (i.e. ordering might have changed, etc).

    You need to be really careful if you use this!

    Rather than cancelling a draw, I'd be tempted to put in a flag around the user action which will indicate to the interval timer that it shouldn't update the table until the user action has completed and any subsequent data updates made.

    Regards,
    Allan

  • loukinglouking Posts: 286Questions: 56Answers: 0

    Rather than cancelling a draw, I'd be tempted to put in a flag around the user action which will indicate to the interval timer that it shouldn't update the table until the user action has completed and any subsequent data updates made.

    This is being done, too, but there's a small window where the draw was initiated then the user action occurs before the response is received. Actually for convenience this flag is being checked when the draw interval expires, which opens the window somewhat. See scan_mousedown.

    Actually I'm not sure I'm clear on the case where ordering and filtering may have been done. Maybe this wouldn't happen with a periodic draw? I can see though that if the user is doing filtering or ordering the preDraw event will occur. I imagine if the user clicks on one of the protected buttons right after filtering a draw would be missed, but it would be picked up after the next interval?

            function start_updates() {
                let draw_interval = setInterval(function() {
                    if (scan_mousedown) return;
                    results_cookie_mutex.promise()
                        .then(function(mutex) {
                            mutex.lock();
                            let resturl = window.location.pathname + `/rest?since=${last_draw}`;
                            last_draw = moment().format();
                            refresh_table_data(_dt_table, resturl, 'full-hold');
                            mutex.unlock();
                        })
                        .catch(function(err) {
                            mutex.unlock();
                            throw err;
                        });
                }, CHECK_TABLE_UPDATE);
    
                return draw_interval;
            }
    
    
  • allanallan Posts: 65,759Questions: 1Answers: 10,939 Site admin

    Maybe this wouldn't happen with a periodic draw?

    If you cause the table to draw, it will go through the sort, filter, preDraw and then draw actions. The table draw is a synchronous action unless you are using Ajax or server-side processing (looks like you are using at least Ajax).

    I imagine if the user clicks on one of the protected buttons right after filtering a draw would be missed, but it would be picked up after the next interval?

    Correct. The next draw would bring everything back into sync. That includes if the draw was triggered by the end user.

    Allan

Sign In or Register to comment.