MCPcopy Index your code
hub / github.com/nodejs/node / EventWatcher

Function EventWatcher

test/fixtures/wpt/resources/testharness.js:956–1071  ·  view source on GitHub ↗

* Allow DOM events to be handled using Promises. * * This can make it a lot easier to test a very specific series of events, * including ensuring that unexpected events are not fired at any point. * * `EventWatcher` will assert if an event occurs while there is no `wait_for`

(test, watchedNode, eventTypes, timeoutPromise)

Source from the content-addressed store, hash-verified

954 *
955 */
956 function EventWatcher(test, watchedNode, eventTypes, timeoutPromise)
957 {
958 if (typeof eventTypes === 'string') {
959 eventTypes = [eventTypes];
960 }
961
962 var waitingFor = null;
963
964 // This is null unless we are recording all events, in which case it
965 // will be an Array object.
966 var recordedEvents = null;
967
968 var eventHandler = test.step_func(function(evt) {
969 assert_true(!!waitingFor,
970 'Not expecting event, but got ' + evt.type + ' event');
971 assert_equals(evt.type, waitingFor.types[0],
972 'Expected ' + waitingFor.types[0] + ' event, but got ' +
973 evt.type + ' event instead');
974
975 if (Array.isArray(recordedEvents)) {
976 recordedEvents.push(evt);
977 }
978
979 if (waitingFor.types.length > 1) {
980 // Pop first event from array
981 waitingFor.types.shift();
982 return;
983 }
984 // We need to null out waitingFor before calling the resolve function
985 // since the Promise's resolve handlers may call wait_for() which will
986 // need to set waitingFor.
987 var resolveFunc = waitingFor.resolve;
988 waitingFor = null;
989 // Likewise, we should reset the state of recordedEvents.
990 var result = recordedEvents || evt;
991 recordedEvents = null;
992 resolveFunc(result);
993 });
994
995 for (var i = 0; i < eventTypes.length; i++) {
996 watchedNode.addEventListener(eventTypes[i], eventHandler, false);
997 }
998
999 /**
1000 * Returns a Promise that will resolve after the specified event or
1001 * series of events has occurred.
1002 *
1003 * @param {Object} options An optional options object. If the 'record' property
1004 * on this object has the value 'all', when the Promise
1005 * returned by this function is resolved, *all* Event
1006 * objects that were waited for will be returned as an
1007 * array.
1008 *
1009 * @example
1010 * const watcher = new EventWatcher(t, div, [ 'animationstart',
1011 * 'animationiteration',
1012 * 'animationend' ]);
1013 * return watcher.wait_for([ 'animationstart', 'animationend' ],

Callers

nothing calls this directly

Calls 10

rejectMethod · 0.80
assert_trueFunction · 0.70
assert_equalsFunction · 0.70
addEventListenerMethod · 0.65
removeEventListenerMethod · 0.65
timeoutPromiseFunction · 0.50
pushMethod · 0.45
shiftMethod · 0.45
joinMethod · 0.45
thenMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…