| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
QUnit.module( 've.EventSequencer' ); |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
ve.TestEventSequencer = function VeTestEventSequencer() { |
| 13 |
|
| 14 |
ve.TestEventSequencer.super.apply( this, arguments ); |
| 15 |
|
| 16 |
this.postponedCallbacks = {}; |
| 17 |
this.postponedCallbackId = 1; |
| 18 |
}; |
| 19 |
|
| 20 |
OO.inheritClass( ve.TestEventSequencer, ve.EventSequencer ); |
| 21 |
|
| 22 |
ve.TestEventSequencer.prototype.postpone = function ( callback ) { |
| 23 |
this.postponedCallbacks[ this.postponedCallbackId++ ] = callback; |
| 24 |
}; |
| 25 |
|
| 26 |
ve.TestEventSequencer.prototype.cancelPostponed = function ( timeoutId ) { |
| 27 |
delete this.postponedCallbacks[ timeoutId ]; |
| 28 |
}; |
| 29 |
|
| 30 |
ve.TestEventSequencer.prototype.runPostponed = function () { |
| 31 |
function sortStringIds( a, b ) { |
| 32 |
return parseInt( a ) - parseInt( b ); |
| 33 |
} |
| 34 |
let ids; |
| 35 |
while ( ( ids = Object.keys( this.postponedCallbacks ) ).length > 0 ) { |
| 36 |
ids.sort( sortStringIds ); |
| 37 |
for ( let i = 0, len = ids.length; i < len; i++ ) { |
| 38 |
const callback = this.postponedCallbacks[ ids[ i ] ]; |
| 39 |
delete this.postponedCallbacks[ ids[ i ] ]; |
| 40 |
|
| 41 |
if ( callback ) { |
| 42 |
callback(); |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
}; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
QUnit.test( 'EventSequencer', ( assert ) => { |
| 51 |
const calls = []; |
| 52 |
|
| 53 |
let sequencer = new ve.TestEventSequencer( [ 'event1', 'event2', 'event3' ] ).on( { |
| 54 |
event1: function () { |
| 55 |
calls.push( 'on1' ); |
| 56 |
}, |
| 57 |
event3: function () { |
| 58 |
calls.push( 'on3' ); |
| 59 |
} |
| 60 |
} ).after( { |
| 61 |
event2: function () { |
| 62 |
calls.push( 'after2' ); |
| 63 |
}, |
| 64 |
event3: function () { |
| 65 |
calls.push( 'after3' ); |
| 66 |
} |
| 67 |
} ).onLoop( |
| 68 |
() => { |
| 69 |
calls.push( 'onLoop' ); |
| 70 |
} |
| 71 |
).afterLoop( |
| 72 |
() => { |
| 73 |
calls.push( 'afterLoop' ); |
| 74 |
} |
| 75 |
).afterOne( { |
| 76 |
event1: function () { |
| 77 |
calls.push( 'after1One' ); |
| 78 |
} |
| 79 |
} ); |
| 80 |
|
| 81 |
sequencer.onEvent( 'event1' ); |
| 82 |
sequencer.onEvent( 'event2' ); |
| 83 |
sequencer.onEvent( 'event3' ); |
| 84 |
sequencer.runPostponed(); |
| 85 |
|
| 86 |
assert.deepEqual( |
| 87 |
calls, |
| 88 |
[ 'onLoop', 'on1', 'after1One', 'after2', 'on3', 'after3', 'afterLoop' ], |
| 89 |
'First event loop' |
| 90 |
); |
| 91 |
|
| 92 |
calls.length = 0; |
| 93 |
sequencer.afterLoopOne( () => { |
| 94 |
calls.push( 'afterLoopOne' ); |
| 95 |
} ); |
| 96 |
|
| 97 |
sequencer.onEvent( 'event1' ); |
| 98 |
sequencer.onEvent( 'event2' ); |
| 99 |
sequencer.onEvent( 'event3' ); |
| 100 |
sequencer.runPostponed(); |
| 101 |
|
| 102 |
assert.deepEqual( |
| 103 |
calls, |
| 104 |
[ 'onLoop', 'on1', 'after2', 'on3', 'after3', 'afterLoop', 'afterLoopOne' ], |
| 105 |
'Second event loop' |
| 106 |
); |
| 107 |
|
| 108 |
calls.length = 0; |
| 109 |
|
| 110 |
sequencer = new ve.TestEventSequencer( [ 'keydown', 'keypress' ] ).on( { |
| 111 |
keydown: function () { |
| 112 |
calls.push( 'onkeydown' ); |
| 113 |
}, |
| 114 |
keypress: function () { |
| 115 |
calls.push( 'onkeypress' ); |
| 116 |
} |
| 117 |
} ).after( { |
| 118 |
keydown: function () { |
| 119 |
calls.push( 'afterkeydown' ); |
| 120 |
}, |
| 121 |
keypress: function () { |
| 122 |
calls.push( 'afterkeypress' ); |
| 123 |
} |
| 124 |
} ); |
| 125 |
sequencer.onEvent( 'keydown' ); |
| 126 |
sequencer.onEvent( 'keypress' ); |
| 127 |
sequencer.runPostponed(); |
| 128 |
|
| 129 |
assert.deepEqual( |
| 130 |
calls, |
| 131 |
[ 'onkeydown', 'onkeypress', 'afterkeydown', 'afterkeypress' ], |
| 132 |
'Keydown/keypress special-cased ordering' |
| 133 |
); |
| 134 |
} ); |