Ticket #15481: list-table.dev.js-x7815.diff

File list-table.dev.js-x7815.diff, 1.9 KB (added by edward mindreantre, 2 years ago)

1 second keypress, now with fixed race condition

Line 
1Index: wp-admin/js/list-table.dev.js
2===================================================================
3--- wp-admin/js/list-table.dev.js       (revision 16472)
4+++ wp-admin/js/list-table.dev.js       (working copy)
5@@ -214,17 +214,23 @@
6 
7                return false;
8        });
9+       
10+       // Keep track of how many times setTimeout has been called.
11+       var refresh_results_counter = 0;
12 
13-       // searchbox
14-       function change_search(ev) {
15-               if ( 'keypress' == ev.type && 13 != ev.keyCode )
16+       function refresh_results(search_text)
17+       {
18+               // Decrease the timer only if it is above 0. Otherwise race condition: type 4 characters quickly then press enter...
19+               if (refresh_results_counter > 0)
20+                       refresh_results_counter --;
21+
22+               // If setTimeout has been called more than once, just ignore the first ones.
23+               if (refresh_results_counter > 0)
24                        return;
25+               
26+               // NOW we can search
27+               var data = $(search_text).parent('.search-box').find(':input').serializeObject();
28 
29-               ev.preventDefault();
30-               ev.stopImmediatePropagation();
31-
32-               var data = $(this).parent('.search-box').find(':input').serializeObject();
33-
34                listTable.update_rows(data, true, function() {
35                        $('h2 .subtitle').remove();
36 
37@@ -234,6 +240,34 @@
38                                ));
39                });
40        }
41+
42+       // searchbox
43+       function change_search(ev) {
44+               if ( 'keypress' != ev.type )
45+                       return;
46+                       
47+               if ( 13 == ev.keyCode )
48+               {
49+                       ev.preventDefault();
50+                       ev.stopImmediatePropagation();
51+                       // Get the results immediately.
52+                       refresh_results_counter = 1;
53+                       refresh_results(this);
54+               }
55+               else
56+               {
57+                       // Set a timeout for one second before fetching the new results.
58+                       // Increase the counter so that the search always waits one second after the LAST keypress.
59+                       refresh_results_counter++;
60+                       // This is a "closure".
61+                       setTimeout(function(that) {
62+                               return function() {
63+                                       refresh_results(that);
64+                               };
65+                       }(this), 1000);
66+               }
67+
68+       }
69        $('.search-box :submit').click(change_search);
70        $('.search-box :text').keypress(change_search);
71