|
1012 | 1012 | }
|
1013 | 1013 |
|
1014 | 1014 | return !!domain;
|
| 1015 | + }, |
| 1016 | + |
| 1017 | + /** |
| 1018 | + * Buffers `input` events (or any `input` calls) |
| 1019 | + * and triggers `output` not more often than once per `minInterval`. |
| 1020 | + * |
| 1021 | + * var buffer = CKEDITOR.tools.eventsBuffer( 200, function() { |
| 1022 | + * console.log( 'foo!' ); |
| 1023 | + * } ); |
| 1024 | + * |
| 1025 | + * buffer.input(); |
| 1026 | + * // 'foo!' logged immediately |
| 1027 | + * buffer.input(); |
| 1028 | + * // nothing logged |
| 1029 | + * buffer.input(); |
| 1030 | + * // nothing logged |
| 1031 | + * // ... after 200ms single 'foo!' will be logged |
| 1032 | + * |
| 1033 | + * Can be easily used with event: |
| 1034 | + * |
| 1035 | + * var buffer = CKEDITOR.tools.eventsBuffer( 200, function() { |
| 1036 | + * console.log( 'foo!' ); |
| 1037 | + * } ); |
| 1038 | + * |
| 1039 | + * editor.on( 'key', buffer.input ); |
| 1040 | + * // Note: there's no need to bind buffer as a context. |
| 1041 | + * |
| 1042 | + * @since 4.2.1 |
| 1043 | + * @param {Number} minInterval Minimum interval between `output` calls in milliseconds. |
| 1044 | + * @param {Function} output Function that will be executed as `output`. |
| 1045 | + * @returns {Object} |
| 1046 | + * @returns {Function} return.input Buffer's input method. |
| 1047 | + * @returns {Function} return.reset Resets buffered events – `output` will not be executed |
| 1048 | + * until next `input` is triggered. |
| 1049 | + */ |
| 1050 | + eventsBuffer: function( minInterval, output ) { |
| 1051 | + var scheduled, |
| 1052 | + lastOutput = 0; |
| 1053 | + |
| 1054 | + function triggerOutput() { |
| 1055 | + lastOutput = ( new Date() ).getTime(); |
| 1056 | + scheduled = false; |
| 1057 | + output(); |
| 1058 | + } |
| 1059 | + |
| 1060 | + return { |
| 1061 | + input: function() { |
| 1062 | + if ( scheduled ) |
| 1063 | + return; |
| 1064 | + |
| 1065 | + var diff = ( new Date() ).getTime() - lastOutput; |
| 1066 | + |
| 1067 | + // If less than minInterval passed after last check, |
| 1068 | + // schedule next for minInterval after previous one. |
| 1069 | + if ( diff < minInterval ) |
| 1070 | + scheduled = setTimeout( triggerOutput, minInterval - diff ); |
| 1071 | + else |
| 1072 | + triggerOutput(); |
| 1073 | + }, |
| 1074 | + |
| 1075 | + reset: function() { |
| 1076 | + if ( scheduled ) |
| 1077 | + clearTimeout( scheduled ); |
| 1078 | + |
| 1079 | + scheduled = lastOutput = 0; |
| 1080 | + } |
| 1081 | + }; |
1015 | 1082 | }
|
1016 | 1083 | };
|
1017 | 1084 | })();
|
|
0 commit comments